Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


TGA drives me nuts!

This topic is 6292 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
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 [b]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 temp=texture->imageData; // Temporarily Store The Value At Image Data ''i''
texture->imageData[i] = texture->imageData[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
texture->imageData[i + 2] = temp; // Set The 3rd Byte To The Value In ''temp'' (1st Byte Value)

fclose (file); // Close The File

' Target=_Blank>Link

Share this post

Link to post
Share on other sites
Sorry I don''t know how to work the message board so the code is a little messed up looking

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!