Menu ▾ ▴

example_convolution

Gonen Raveh
#include <GL/glui.h>
#include <GL/megalui.h>

// Globals
UiImage *gImage0 = NULL;
UiImage *gImage1 = NULL;
int     gMainWindow = 0;
int     gMousePos[2];
int     gKernelSize=0;
int     gImageSize = 320;

void onEvent_Mouse(int button, int button_state, int x, int y) {if (button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN) {}}
void onEvent_Motion(int x, int y) {}
void onEvent_PassiveMotion(int x, int y) { gMousePos[0]=x; gMousePos[1]=y; }
void onEvent_Reshape(int x, int y) { uiReshape(x, y);}
void onEvent_Keyboard(unsigned char Key, int x, int y) {}
/** Here I do my cleaning */
void onEvent_WindowCloseEvent(int) {
    uiReleaseImage(&gImage0);
    uiReleaseImage(&gImage1);
    uiReleaseWindow(gMainWindow);
    exit(0);
}
void onEvent_Idle(void *userdata) {
    gKernelSize = -1;
    if (gImage1) {
        int x = gMousePos[0], y = gMousePos[1], w = gImage1->width;     
        if (x>=50 && x<50+gImageSize && y>=100 && y<100+gImageSize) {
            int ks = (y-100)/25;
            gKernelSize = ks;
            float ks2 = (float)((ks*2+1)*(ks*2+1));
            float sr=0;
            for (int i=ks; i<gImageSize-ks; ++i) for (int j=ks; j<gImageSize-ks; ++j) {
                sr=0;
                for (int r=-ks; r<=ks; ++r) for (int c=-ks; c<=ks; ++c) sr += (float)gImage0->imageData[3*(i*w+r*w+j+c)+0];
                sr /= ks2;
                gImage1->imageData[3*(i*w+j)+0] = (unsigned char)sr;
                gImage1->imageData[3*(i*w+j)+1] = (unsigned char)sr;
                gImage1->imageData[3*(i*w+j)+2] = (unsigned char)sr;
            }
            uiUpdateImage(gImage1, gImage1->imageData);
        }
    }
    uiRedraw(); 
}
/** Here I do my 2D/3D Graphics rendering */
void onEvent_RenderGraphics() {
    // Do Once
    if (!gImage0) {
        gImage0 = uiNamedImage("chessboard", gImageSize);
        gImage1 = uiNamedImage("chessboard", gImageSize);
    }
    uiCanvasClear(uiColorb(0,0,0,255));
    uiDrawImage(gImage0, 50, 100, UI_DRAWIMAGE_BORDER | UI_BORDER_RED, 1.0, 1.0, 0.0f, 1.0f); 
    uiDrawImage(gImage1, (float)(50+gImageSize+50), (float)100, UI_DRAWIMAGE_BORDER | UI_BORDER_RED, 1.0, 1.0, 0.0f, 1.0f); 
    uiDrawTextPrintf(uiColorb(255,255,255,192), 10, 10, 10.0f, 0.0f, "Megalui Image Convolution Demo (%04d,%04d)", gMousePos[0], gMousePos[1]);
    if (gKernelSize>=0) {
        uiDrawRect(uiColorb(0,0,255,192), (float)(gMousePos[0]-5), (float)(gMousePos[1]-5), (float)(gMousePos[0]+120), 
            (float)(gMousePos[1]+20), true, 1.0f, 0.0f, 0);
        uiDrawTextPrintf(uiColorb(255,255,0,255), (float)gMousePos[0], (float)gMousePos[1], 12.0f, 0.0f, "Kernel(%02dx%02d)", 1+2*gKernelSize, 1+2*gKernelSize);
    }
    uiDrawText(uiColorb(255,255,0,192), "Before Convolution", 50.f, 80.f, 10.0f, 0.0f);
    uiDrawText(uiColorb(255,255,0,192), "After Convolution", (float)(50+gImageSize+50), 80.f, 10.0f, 0.0f);
    uiRefresh();
}

int main(int argc, char* argv[]) {
  glutInit(&argc, argv);
  glutInitDisplayMode   (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowPosition(50, 50);
  glutInitWindowSize    (800, 600);

  gMainWindow = uiCreateWindow( "Megalui Example 1", 
                UI_CREATEWINDOW_CONTROL_2D | UI_CREATEWINDOW_CONTROL_3D, 
                onEvent_WindowCloseEvent);

  glutMotionFunc                  (onEvent_Motion);
  glutPassiveMotionFunc           (onEvent_PassiveMotion);   
  GLUI_Master_Object::instance()->set_glutDisplayFunc (onEvent_RenderGraphics);
  GLUI_Master_Object::instance()->set_glutIdleFunc    (onEvent_Idle);
  GLUI_Master_Object::instance()->set_glutReshapeFunc (onEvent_Reshape);  
  GLUI_Master_Object::instance()->set_glutKeyboardFunc(onEvent_Keyboard);
  GLUI_Master_Object::instance()->set_glutMouseFunc   (onEvent_Mouse);

  uiInit(argc, argv); 

  // Start GLUT Main Loop
  glutMainLoop();
  return 0;  
}