View Full Version : Loading BMP`s. Red turn Blue, Blue turns Red! Help!

Anonymous Coward
08-12-2000, 08:47 AM
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!


08-12-2000, 08:58 AM
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...

08-12-2000, 09:46 AM
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.

Anonymous Coward
08-12-2000, 10:24 AM
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!

Anonymous Coward
08-12-2000, 11:15 AM
Mhhhh...tell me something...how did you do to switch the Red and Blue values? I tried but no success... http://www.opengl.org/discussion_boards/ubb/smile.gif

08-12-2000, 11:30 AM
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;

Anonymous Coward
08-12-2000, 12:24 PM
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?

08-12-2000, 04:00 PM
This is how I did it:

for(unsigned int k=0; k<width; ++k)
tempColor =imageJpg[j*width*3+3*k+0]; // temp = red
imageJpg[j*width*3+3*k+0] =imageJpg[j*width*3+3*k+2]; // red = blue
imageJpg[j*width*3+3*k+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.

08-13-2000, 12:33 AM
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

Anonymous Coward
08-13-2000, 01:11 AM
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! http://www.opengl.org/discussion_boards/ubb/tongue.gif