My Targa file loading function isn't working

I downloaded the source of a tga-loading function, but when I try to open a file, no data comes out.

For example, the Image Type doesn’t read in, the height and width fields don’t show any data, and when I try to texture map the data onto a quad it doesn’t display (naturally, if all the other data doesn’t come through). I tried downloading some documentation on how a tga file is stored, but the function was written exactly like the documentation said. Here’s the code:

 // structure for loading TGA bitmaps
typedef struct
{
 
   int imageWidth;
   int imageHeight;
   unsigned int imageTypeCode;
   unsigned int bitCount;
   unsigned char* imageData;
} STGA;

// the TGA bitmap loader
bool loadTGA(char *filename, STGA tgaFile)
{
   FILE *file;
   unsigned char		badChar;
   short int		badInt;
   long		  	imageSize;
   int			colorMode;
  
   file = fopen(filename, "r");
 
   if (!file)
      return false;
 
   fread(&badChar, sizeof(unsigned char), 1, file);
   fread(&badChar, sizeof(unsigned char), 1, file);
 
   fread(&tgaFile.imageTypeCode, sizeof(unsigned char), 1, file);
 
   //image type either 2 (color) or 3 (greyscale)
   if ((tgaFile.imageTypeCode != 2) && (tgaFile.imageTypeCode != 3))
   {
      fclose(file);
      return false;
   }
 
   //13 bytes of useless data
   fread(&badInt, sizeof(short int), 1, file);
   fread(&badInt, sizeof(short int), 1, file);
   fread(&badChar, sizeof(unsigned char), 1, file);
   fread(&badInt, sizeof(short int), 1, file);
   fread(&badInt, sizeof(short int), 1, file);
 
   //image dimensions
   fread(&tgaFile.imageWidth, sizeof(short int), 1, file);
   fread(&tgaFile.imageHeight, sizeof(short int), 1, file);
   
   if (tgaFile.imageWidth != 256)
   printf("%a x %b - %c
", tgaFile.imageWidth, tgaFile.imageHeight, tgaFile.imageTypeCode);
   
   //image bit depth
   fread(&tgaFile.bitCount, sizeof(unsigned char), 1, file);
 
   //1 byte of garbage data
   fread(&badChar, sizeof(unsigned char), 1, file);
 
   //colorMode -> 3 = BGR, 4 = BGRA 
   colorMode = tgaFile.bitCount / 8;
   imageSize = tgaFile.imageWidth * tgaFile.imageHeight * colorMode;
 
   //allocate memory for image data
   tgaFile.imageData = (unsigned char*) malloc (imageSize);
 
   //read in image data
   fread(tgaFile.imageData, sizeof(unsigned char), imageSize, file);
   
   int i;	// looping variable
 
   //change BGR to RGB (especially for OpenGL later on)
   for (i = 0; i < imageSize; i += colorMode)
   {
      //swap blue and red colour value 
      tgaFile.imageData[i] ^= tgaFile.imageData[i+2] ^=
        tgaFile.imageData[i] ^= tgaFile.imageData[i+2];
   }
 
   //close file
   fclose(file);
 
   return true;
}
 

Hi Joe,

What does this have to do with OpenGL?

Cheers,

:slight_smile:

P.S. Check out DevIL (formerly OpenIL) or google for TGA loaders (there are lots and they work outa the box).

you’re reading the wrong data. image width/height and bitcount are stored as short in a TGA file, not as int.

go here to find more about the tga file format.