Advertisement

TGA drives me nuts!

Started by April 26, 2001 05:28 PM
2 comments, last by panic 23 years, 7 months ago
I'm working on a little something, and are having some toubles with exporting a TGA image. I'm reading the image data from a Black and White .lnd file, the data I'm reading is a 256x256 RGBA texture, I can read it, and then create a texture from it and map something within openGL. And it shows up alright. But now I would like to export it to a TGA, so I downloaded some TGA format specs and started writing a simple exporter. I CAN export the texture to TGA now, with alpha channel and everything, but the only problem is that it saves it as BGRA instead of RGBA. I don't understand why, since when I map something in openGL with the texture I create a texture using GL_RGBA in glTexImage2D(), and it shows correct. But when I open the exported TGA in photoshop it's Red and Blue channel are swapped, causing the image to have an incorrect shade. I tried to solve this by simply going thru the imagebuffer and swap the red channel with the blue, atleast I think that's what I did here's the code for that:
    
for(i = 0; i < 256*256*4; i += 4) {
buffer2[i+2] = buffer1[i];       
buffer2[i+1] = buffer1[i+1];
buffer2[i] = buffer1[i+2];
buffer2[i+3] = buffer1[i+3];
}
    
But it doesn't work, all it does is kind'a smearing the image every 4'th column.. I'm out of ideas, someone please help Thanks return 1; Edited by - panic on April 26, 2001 6:32:16 PM
This is directly from the TGA file loading tutorial on nehe.gamedev.net It may help

If the data was loaded properly, things are going good All we have to do now is swap the Red and Blue bytes. In OpenGL we use RGB (red, green, blue). The data in a TGA file is stored BGR (blue, green, red). If we didn''t swap the red and blue bytes, anything in the picture that should be red would be blue and anything that should be blue would be red.

The first thing we do is create a loop (i) that goes from 0 to imageSize. By doing this, we can loop through all of the image data. Our loop will increase by steps of 3 (0, 3, 6, 9, etc) if the TGA file is 24 bit, and 4 (0, 4, 8, 12, etc) if the image is 32 bit. The reason we increase by steps is so that the value at i is always going to be the first byte (lue byte) in our group of 3 or 4 bytes.

Inside the loop, we store the lue byte in our temp variable. We then grab the red byte which is stored at texture->imageData[i+2] (Remember that TGAs store the colors as BGR<A href='http://. B is i+0, G is i+1 and R is i+2) and store it where the lue byte used to be. <br><br>Lastly we move the lue byte that we stored in the temp variable to the location where the [r]ed byte used to be (i+2), and we close the file with fclose(file). <br><br>If everything went ok, the TGA should now be stored in memory as usable OpenGL texture data! <br> <br><br> for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) // Loop Through The Image Data<br> { // Swaps The 1st And 3rd Bytes (''R''ed and ''B''lue)<br> temp=texture->imageData<i>; // Temporarily Store The Value At Image Data ''i''<br> texture->imageData = texture->imageData; // Set The 1st Byte To The Value Of The 3rd Byte<br> texture->imageData = temp; // Set The 3rd Byte To The Value In ''temp'' (1st Byte Value)<br> }<br><br> fclose (file); // Close The File<br><br> </b> </i> ' Target=_Blank>Link</a>
Advertisement
Sorry I don''t know how to work the message board so the code is a little messed up looking
Thanks, I got it working now



return 1;

This topic is closed to new replies.

Advertisement