Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: My Targa file loading function isn't working

  1. #1
    Intern Newbie
    Join Date
    Jan 2006
    Location
    Pacific Northwest
    Posts
    41

    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:

    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\n", 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;
    }
    "The first person to succeed is the first person to make 100 mistakes" - Albert Einstien

  2. #2
    Guest

    Re: My Targa file loading function isn't working

    Hi Joe,

    What does this have to do with OpenGL?

    Cheers,



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

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    658

    Re: My Targa file loading function isn't working

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •