The Truevision TGA (Targa) file format is a raster graphics file format commonly used for storing images, particularly in the realm of video games, digital video editing, and graphic design.
TGA files can store images with a range of color depths, from 1-bit monochrome to 32-bit RGBA, making them versatile for various applications. They support uncompressed and compressed image data, including RLE (Run-Length Encoding) compression, which efficiently reduces file sizes while preserving image quality.
TGA files also support alpha channels, allowing for transparency in images, and can store additional image metadata such as color correction data.
While TGA files are less common in modern digital workflows compared to formats like JPEG or PNG, they remain relevant in specific industries due to their flexibility and support for a wide range of image types and features.
typedef struct { unsigned char d_iif_size; // IIF size (after header), usually 0 unsigned char d_cmap_type; // ignored unsigned char d_image_type; // should be 2 unsigned char pad[5];
unsigned short d_x_origin; unsigned short d_y_origin; unsigned short d_width; unsigned short d_height;
unsigned char d_pixel_size; // 16, 24, or 32 unsigned char d_image_descriptor; // Bits 3-0: size of alpha channel // Bit 4: must be 0 (reserved) // Bit 5: should be 0 (origin) // Bits 6-7: should be 0 (interleaving) } tTGAHeader_s;
#define RGB16(r,g,b) ( ((r>>3) << 10) + ((g>>3) << 5) + (b >> 3) ) #define RGB24(r,g,b) ( ((r) << 16) + ((g) << 8) + (b) ) #define GET16R(v) (v >> 10) #define GET16G(v) ((v >> 5) & 0x1f) #define GET16B(v) (v & 0x1f)
//------------------------------------------------------------------------------ // Name: LoadTGAFile() // Desc: Given a filename of a TGA file, it loads the image in BITMAP form //------------------------------------------------------------------------------ unsigned char *LoadTGAFile( char * strFilename, tTGAHeader_s *header) { /// Local Variables /////////////////////////////////////////////////////////// short BitsPerPixel; unsigned char *buffer; int bitsize; /* Total size of bitmap */ unsigned char *newbits; /* New RGB bits */ unsigned char *from, *to; /* RGB looping vars */ int i, j, /* Looping vars */ width; /* Aligned width of bitmap */ FILE* file; long dwWidth, dwHeight;
// Open the file and read the header file = fopen( strFilename, "rb"); if( NULL == file ) return NULL;
if ( fread( header, sizeof( tTGAHeader_s ), 1, file ) != 1 ) { fclose( file ); return NULL; }
// Parse the TGA header dwWidth = (long)header->d_width; dwHeight = (long)header->d_height; BitsPerPixel = (short)header->d_pixel_size; // 16, 24, or 32
// Create a bitmap to load the data into
bitsize = dwWidth * dwHeight * (BitsPerPixel/8); if ((newbits = (unsigned char *)calloc(bitsize, 1)) == NULL) { fclose( file ); return NULL; } buffer = (unsigned char *)malloc(dwWidth*dwHeight*(BitsPerPixel / 8)); if ( fread( buffer, dwWidth*dwHeight*(BitsPerPixel / 8), 1, file ) != 1 ) { fclose( file ); free(buffer); free(newbits); return NULL; }
width = (BitsPerPixel / 8) * dwWidth;
for (i = 0; i < dwHeight; i ++) for (j = 0, from = ((unsigned char *)buffer) + i * width, to = newbits + i * width; j < dwWidth; j ++, from += (BitsPerPixel / 8), to += (BitsPerPixel / 8)) { if (BitsPerPixel == 24) { to[0] = from[2]; to[1] = from[1]; to[2] = from[0]; } else { to[0] = from[2]; to[1] = from[1]; to[2] = from[0]; to[3] = from[3]; } }; free(buffer); fclose( file );
return newbits; }
|