www.xbdev.net
xbdev - software development
Sunday March 30, 2025
Home | Contact | Support | Image File Format... Storing Graphics using Bits and Bytes.. | TGA File Format... TGA Image Format.. ..
     
 

TGA File Format...

TGA Image Format.. ..

 


TGA File Format (.tga)



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 *LoadTGAFilechar 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        ij,            /* Looping vars */
    
width;            /* Aligned width of bitmap */
    
FILEfile;
    
long dwWidthdwHeight;

    
// Open the file and read the header
    
file fopenstrFilename"rb");
    if( 
NULL == file )
        return 
NULL;

    if ( 
freadheadersizeoftTGAHeader_s ), 1file ) != )
    {
        
fclosefile );
        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(bitsize1)) == NULL)
    {
        
fclosefile );
        return 
NULL;
    }
     
buffer = (unsigned char *)malloc(dwWidth*dwHeight*(BitsPerPixel 8));
    if ( 
freadbufferdwWidth*dwHeight*(BitsPerPixel 8), 1file ) != )
    {
        
fclosefile );
        
free(buffer);
        
free(newbits);
        return 
NULL;
    }

    
width   = (BitsPerPixel 8) * dwWidth;

    for (
0dwHeight++)
        for (
0from = ((unsigned char *)buffer) + width,
            
to newbits width;
            
dwWidth;
            
++, 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);
    
fclosefile );

    return 
newbits;
}













 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.