PDA

View Full Version : Simple question about convolution on GL_RED



newbiecow
12-22-2012, 08:47 AM
I wrote the following program:



#include <stdio.h>
#include <stdlib.h>

#include <GL\glew.h>

#define GLUT_DISABLE_ATEXIT_HACK
#include <glut.h>

#define WIDTH 16
#define HEIGHT 16

GLubyte pixels[WIDTH*HEIGHT]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00
};


GLfloat horizontal[3][3] = {
{ 0, -1, 0 },
{ 0, 1, 0 },
{ 0, 0, 0 }
};

GLfloat vertical[3][3] = {
{ 0, 0, 0 },
{ -1, 1, 0 },
{ 0, 0, 0 }
};

GLfloat laplacian[3][3] = {
{ -0.125, -0.125, -0.125 },
{ -0.125, 1.0 , -0.125 },
{ -0.125, -0.125, -0.125 },
};


void init(void)
{
glewInit();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor(0.0, 0.0, 0.0, 0.0);

printf("Using the horizontal filter\n");
glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE,
3, 3, GL_RED, GL_FLOAT, horizontal);
glEnable(GL_CONVOLUTION_2D);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i( 1, 1);
glDrawPixels(WIDTH, HEIGHT, GL_RED, GL_UNSIGNED_BYTE, pixels);
glFlush();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'h' :
printf("Using a horizontal filter\n");
glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3,
GL_RED, GL_FLOAT, horizontal);
break;

case 'v' :
printf("Using the vertical filter\n");
glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3,
GL_RED, GL_FLOAT, vertical);
break;

case 'l' :
printf("Using the laplacian filter\n");
glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3,
GL_RED, GL_FLOAT, laplacian);
break;

case 27:
exit(0);
}
glutPostRedisplay();
}

/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(WIDTH,HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}


What I meant to do is convolute on an image only with a red color on each pixel and reduce the memory footprint by only store the red pixel value. But it just doesn't work. Anyone call tell where the bug is?
Thanks in advance.