How to get RGB value from 24 bit bitmap file

How to get RGB value from 24 bit bitmap file ?

but i am getting wrong value. the function is

void mouseclick(int but,int state,int x,int y)
{
int c=0;
int r,g,b;
if(but==GLUT_LEFT_BUTTON)
{
c=(3*(BitmapInfo)->bmiHeader.biWidthy )+(x3);
r=BitmapBits[c];
g= BitmapBits[c+1];
b= BitmapBits[c+2];
printf("%d %d %d ",r,g,b);
glFlush();
}
}

thank you

Originally posted by karthickumar M:
[b]How to get RGB value from 24 bit bitmap file ?

but i am getting wrong value. the function is

void mouseclick(int but,int state,int x,int y)
{
int c=0;
int r,g,b;
if(but==GLUT_LEFT_BUTTON)
{
c=(3*(BitmapInfo)->bmiHeader.biWidthy )+(x3);
r=BitmapBits[c];
g= BitmapBits[c+1];
b= BitmapBits[c+2];
printf("%d %d %d ",r,g,b);
glFlush();
}
}

thank you[/b]
I am thinking it is that the origin in OpenGL is 0,0 at lower left whereas many windowing systems have 0,0 at top so you might need to change your line from

c=(3*(BitmapInfo)->bmiHeader.biWidthy )+(x3);

to

c=(3*(BitmapInfo)->bmiHeader.biWidth*(height-y) )+(x*3);

or something like that.

If not it, please be more specific about the problem you are having - I can think of several other possibilities depending on the symptoms of the problem.

Originally posted by karthickumar M:
[b]How to get RGB value from 24 bit bitmap file ?

but i am getting wrong value. the function is

void mouseclick(int but,int state,int x,int y)
{
int c=0;
int r,g,b;
if(but==GLUT_LEFT_BUTTON)
{
c=(3*(BitmapInfo)->bmiHeader.biWidthy )+(x3);
r=BitmapBits[c];
g= BitmapBits[c+1];
b= BitmapBits[c+2];
printf("%d %d %d ",r,g,b);
glFlush();
}
}

thank you[/b]
Could be the origin of OpenGL and your windowing system are not the saem (i.e. origin in OpenGL is 0,0 at lower left - some windowing systems have origin at upper left).

Change
c=(3*(BitmapInfo)->bmiHeader.biWidthy )+(x3);
to
c=(3*(BitmapInfo)->bmiHeader.biWidth*(winheight-y) )+(x*3);
where winheight is the height of the viewport or window client area.

Also, verify your type for BitmapBits is char or change the r,g, b vars to type char or coerce the r, g, b values to receive char. Is each RGB component stored as an int or is the RGB value stored in a single int? Or is the data contiguous with each component stored packed together? What kind of values are you getting for each RGB component? 0-255? or 0-4294967296?

Hi shinpaughp

we are using 0 - 255
we tried u r code its working fine in newly created bmp files. if converted images its showing wrong value.
for example if image is JPEG converted to BMP file and run the program ,its print wrong RGB value.

if the image is Fresh BMP its print correct value

Thank you
KarthicKumar

This looks to be your converter that make some messes… I don’t know about BMP, but as for many formats, the converter might use some compression, or another way to make them (windows bmp…).

You should have a deeper look about this format.

One thing you should look at is the height of the bitmap (from the bitinfoheader or equivalent) - if it is negative then it is top-down, if positive it is bottom-up so you need to use both your original method and the method I provided based on this info.

One place to look for this info is http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1rw2.asp which should be useful even for non-Windows platforms.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.