Loading BMP`s. Red turn Blue, Blue turns Red! Help!

My functions load the bitmap well, the problem is when I make it a texture, the Red pixels turn Blue and the Blue red!

Maybe is this sentence:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, BitmapInfo->bmiHeader.biWidth, BitmapInfo->bmiHeader.biHeight,

Is there a constant, in stead of GL_RGB, that is GL_BGR or something like that? Help please!


Yeah, you are on the right track. Don’t know much about this problem, since I never bumped into it myself.
The problem in the pixelformat. Think GL_BGR (in some form) is available as an extension. Take a look at your <GL/gl.h> file, and look for GL_BGR in there, and use it.

When you say ‘My function…’, I suppose you have written it yourself. If so, why not loop through each pixel in the bitmap, and swap red and blue component before returning…

There is a GL_BGR extension you can use but when I got a similar problem once I just swapped the red and blue components in the array just a bob said. This way I don’t have to remember to keep switching from BGR to RGB and so forth. Much easier.

Yes, I notices my error. The constant must be GL_BGR_EXT (for the “format” parameter). This way it works fine. I will see if I can change the R and B component before passing the bits…THX for the help!

Mhhhh…tell me something…how did you do to switch the Red and Blue values? I tried but no success…

If you could show a the snipplet where you read the colors, we could show you how to invert the colors.

Or you could load like do and then if for example you use unsigned char for you array then B is 1 char, G is one char and R is one char

unsigned char bluetemp;

for ( int i =0; i < width*height; i+=3)
bluetemp = buffer[i];
buffer[i] = buffer[i+2]; //switch R and blue
buffer[i+2] = bluetemp;

Ok. the last parameter the LoadBitmap function returns is GLubyte type. Can I make the switch between blue and red in this variable? I think I cant. The function that reads the bits in the BMP is:
if (fread(mybits, 1, bmpsize, fp) < bmpsize)
return (NULL);
My problem is that I dont know WHERE to do the changes…any idea?

This is how I did it:

for(unsigned int k=0; k<width; ++k)
tempColor =imageJpg[jwidth3+3k+0]; // temp = red
width3+3k+0] =imageJpg[jwidth3+3k+2]; // red = blue
width3+3k+2] = tempColor; // blue =temp = red

What this basically does is looks that the huge array in memory that holds all the texture info. First it looks at the first 3 values and switches the first and 3rd. Then it looks at the 2nd 3 values and switches the 1st and 3rd values again. I continue this until I reach the end of the data for the texture.

You do the changes after you read the data from the bitmap, and before returning.

if(fread(mybits, 1, bmpsize, fp) < bmpsize)

// you do the changes here, like Gorg said
unsigned char blue;

for(int i=0; i<bmpsize; i+=3)
blue = mybits[i];
mybits[i] = mybits[i+2];
mybits[i+2] = blue;

// now you can return

I found the way! I was doing right but I was using incorrect types to switch the Red and Rlue components! thanks everybody for your help!