My TGA-loading function isn't working

This is a discussion on My TGA-loading function isn't working within the C Programming forums, part of the General Programming Boards category; When I try the following function, it does read the file, but first, the image type code doesn't come through ...

  1. #1
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498

    My TGA-loading function isn't working

    When I try the following function, it does read the file, but first, the image type code doesn't come through correctly, the height and width are all funny, and when I go on websites that show how to load these files, they seem to match what I'm doing.

    Does anybody know the problem? Help would be greatly appreciated.

    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, "rb");
     
       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);
       
       int imagewidth  = 0;
       int imageheight = 0;
     
       //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\n", imagewidth, imageheight);
       
       //image bit depth
       fread(&tgaFile.bitCount, sizeof(unsigned char), 1, file);
       
       // print the depth count
    //   printf("%a\n", tgaFile.bitCount);
     
       //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;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    1. Which OS/Compiler are you using

    2. Which processor are you on - some of your reads will assume a particular endian-ess, and if your machine is different, then all your results will be wrong.

    3. bool loadTGA(char *filename, STGA tgaFile)
    If you're expecting the caller to get a modified copy of tgaFile, then you're out of luck.
    Pass a pointer to the struct you want to modify, and update accordingly.

    4. int i; // looping variable
    Now you're writing C++ or C99 code.

    5. //13 bytes of useless data
    This comment makes no sense. There's no way the next 5 lines add up to 13 bytes.

  3. #3
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498

    Oh

    I'm using a PPC Mac with XCode 2.2.

    There is no way the next 5 lines add up to 13 [bytes].
    I know, I couldn't figure that out either. I didn't actually write it, so don't blame me

    I got it working but had to use different code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21