PDA

View Full Version : Flipping an image



Mukund
11-27-2010, 06:06 PM
Hello,

i needed a suggestion regarding the method im using to flip an image:

i am getting live feed from a webcam. (im continuosly obtaining frames from the webcam using OpenCV)

Now, im drawing these frames onto a glut window. The frame im obtaining from OpenCV is flipped, so i wrote this:

image_data[] has the raw pixel information. This is later used in glDrawPixels() to draw the frame.



/* 640x480 image */
for (unsigned int i = 0, j = (640 * 480 * 3) - 1; i <= j; i++, j--) {
unsigned char temp = image_data[i];
image_data[i] = image_data[j];
image_data[j] = temp;
}

Now, this happens many times in a second. Is this the right way to do it? i mean isn't it very expensive in terms of computation?

Any suggestions would be really helpful.

Thanks.

V-man
11-27-2010, 06:53 PM
I suggest to you use a texture instead and just apply the texture to a quad.
1st, allocate the texture
glGenTextures(1, &amp;textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);

notice the NULL for glTexImage2D. It will just allocate memory for the texture in VRAM.

2nd
update the texture with glTexSubImage2D.

Also, since you will be using a non power of 2 texture, make sure GL 2.0 is supported.

Mukund
11-27-2010, 06:59 PM
Thanks a lot V-man, i will try the method suggested by you.

ugluk
11-27-2010, 10:06 PM
Don't forget, if you flip the quad's coordinates, the texture will get flipped also. So you can either flip by providing 'flipped' texture coordinates to your vertices, or you can flip the vertex coordinates themselves.

Mukund
11-28-2010, 12:39 PM
Thanks ugluk :).