Sign in to follow this  

TGA files

This topic is 3871 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

Here's some C++ code to output a sample TGA with transparency, using the information on Paul Bourke's website:


#include <fstream>
using std::ofstream;
using std::endl;

#include <ios>
using std::ios_base;

int main(void)
{
// Assumes char is 1 byte, short is 2 bytes.
char temp_c = 0;
short temp_s = 0;
ofstream out("test.tga", ios_base::binary);

temp_c = 0;
out.write(&temp_c, sizeof(temp_c)); // idlength
out.write(&temp_c, sizeof(temp_c)); // colourmaptype

temp_c = 2;
out.write(&temp_c, sizeof(temp_c)); // datatypecode (2 == uncompressed RGB(A) data)

temp_s = 0;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // colourmaporigin
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // colourmaplength

temp_c = 0;
out.write(&temp_c, sizeof(temp_c)); // colourmapdepth

temp_s = 0;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // x_origin
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // y_origin

temp_s = 128;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // width (128 pixels wide)

temp_s = 64;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // height (64 pixels tall)

temp_c = 32;
out.write(&temp_c, sizeof(temp_c)); // bitsperpixel (32 bits per pixel)

temp_c = 0;
out.write(&temp_c, sizeof(temp_c)); // imagedescriptor

unsigned char r = 0;
unsigned char g = 0;
unsigned char b = 128;
unsigned char a = 0;

// output 128*64*4 bytes of data
for(short y = 0; y < 64; y++)
{
// For each line:
r += 2; // Increase red by 2
g = 0; // Reset green to 0
a = 255; // Reset alpha to 255 (fully opaque)

for(short x = 0; x < 128; x++)
{
// For each pixel in each line:
g++; // Increase green by 1
a--; // Decrease alpha by 1

out.write(reinterpret_cast<const char *>(&r), sizeof(r));
out.write(reinterpret_cast<const char *>(&g), sizeof(g));
out.write(reinterpret_cast<const char *>(&b), sizeof(b));
out.write(reinterpret_cast<const char *>(&a), sizeof(a));
}
}

return 0;
}

Share this post


Link to post
Share on other sites

This topic is 3871 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this