Problem loading TGA file (OpenGL SuperBible)

Hi, I’m currently trying to get the sample program IMAGELOAD from chapter 7 to work from the book OpenGL SuperBible. The problem is in the gltLoadTGA function. I’ve enclosed the most relevant lines in the code below with “// ========” lines. The function returns NULL after failing to read the file again. I’ve tried with different TGA-files. Thanx.


GLbyte *gltLoadTGA(const char *szFileName, GLint *iWidth, GLint *iHeight, GLint *iComponents, GLenum *eFormat)
{
    FILE *pFile;			// File pointer
    TGAHEADER tgaHeader;		// TGA file header
    unsigned long lImageSize;		// Size in bytes of image
    short sDepth;			// Pixel depth;
    GLbyte	*pBits = NULL;          // Pointer to bits
    
    // Default/Failed values
    *iWidth = 0;
    *iHeight = 0;
    *eFormat = GL_BGR_EXT;
    *iComponents = GL_RGB8;
    
    // Attempt to open the fil
    pFile = fopen(szFileName, "rb");
    if(pFile == NULL)
	{
		printf("Error: file not found
");
        return NULL;
	}
	
    // Read in header (binary)
    fread(&tgaHeader, /*18*/sizeof(TGAHEADER), 1, pFile);
    	
    // Get width, height, and depth of texture
    *iWidth = tgaHeader.width;
    *iHeight = tgaHeader.height;
    sDepth = tgaHeader.bits / 8;
    
	
	// ======================================================
	// !!! I wrote this lines myself since the function seems
	// to read in the file wrong. Or is it something wrong
        // with the order of my struct?!!!
	unsigned short temp = tgaHeader.width;
	tgaHeader.bits = tgaHeader.height;
	tgaHeader.width = tgaHeader.ystart;
	tgaHeader.height = temp;
	// ======================================================


	tgaHeader.ystart = 0;

	print_tga(tgaHeader);

    // Put some validity checks here. Very simply, I only understand
    // or care about 8, 24, or 32 bit targa's.
    if(tgaHeader.bits != 8 && tgaHeader.bits != 24 && tgaHeader.bits != 32)
	{
		printf("Error: invalid bitdepth
");
		return NULL;
	}
	
    // Calculate size of image buffer
    lImageSize = tgaHeader.width * tgaHeader.height * sDepth;
    
    // Allocate memory and check for success
    pBits = (GLbyte*)malloc(lImageSize * sizeof(GLbyte));
    if(pBits == NULL)
	{
		printf("Error: while allocating
");
		return NULL;
	}
    
    // Read in the bits
    // Check for read error. This should catch RLE or other 
    // weird formats that I don't want to recognize

	// ======================================================
	// !!! The function returns NULL here. Here is the problem !!!
    if(fread(pBits, lImageSize, 1, pFile) != 1)
	{
		printf("Error: read error
");
        free(pBits);
        return NULL;
	}
	// ======================================================
    
    // Set OpenGL format expected
    switch(sDepth)
		{
        case 3:     // Most likely case
            *eFormat = GL_BGR_EXT;
            *iComponents = GL_RGB8;
            break;
        case 4:
            *eFormat = GL_BGRA_EXT;
            *iComponents = GL_RGBA8;
            break;
        case 1:
            *eFormat = GL_LUMINANCE;
            *iComponents = GL_LUMINANCE8;
            break;
		};
	
    
    // Done with File
    fclose(pFile);
	
    // Return pointer to image data
    return pBits;
}


Are you sure your TGA are in the raw form, and not RLE-compressed ?

Both the function and the TGA-file are from the book’s webpage, so I guess the TGA-file is in the right format. Thanx.

How can I look up if it is in raw-form or RLE-compressed?

Multiply width by height by bytes per pixel; if the result is roughly equal to the file size in bytes then it’s raw, otherwise it’s most likely compressed (it could also be corrupt, but that’s another story).

So I multiplied the width, height and bytes per pixel and ended up with the same size as the file size, so I guess it’s raw then. Now when we know that, what might be the problem?

Since the function is done by the author and he also contributed the TGA-file I find it odd that it’s not working.