PDA

View Full Version : Counting pixels of a given color



11-10-2002, 05:29 PM
I would like to count the pixels that are on the screen of a given color lets say represented by variables r g and b

I think the way I should do this is something like the following, but I can't seem to be able to finish

buff=(unsigned char *) calloc(1,w*h*3)
glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,buff)

is that right?
what do I do now with buff to get each pixel information out of it?

Thanks
Joan

mikael_aronsson
11-11-2002, 06:10 AM
Hi !

In the buffer you get 3 bytes for each pixel pos=((y*width+x)*3) the first byte (pos) is the red color component in the range 0-255 the next (pos+1) is green and the last one (pos+2) is blue.

Mikael

Gavin
11-12-2002, 04:02 AM
To make your life a little easier make your buffer an array of structs of...
typedef struct RGB
{
GLubyte r, g, b;
}SRGB;

and malloc your array
(SRGB*) malloc(sizeof(SRGB) * width*height);

ok..... so then you access pixels

myarray[i + (j*width)].r