Image Loading Troubles

This is a discussion on Image Loading Troubles within the C++ Programming forums, part of the General Programming Boards category; I have some code to load a uncompressed BTF image file (look at my web site for specs) and my ...

  1. #1
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552

    Image Loading Troubles

    I have some code to load a uncompressed BTF image file (look at my web site for specs) and my loader keeps crashing. I ran a debug and it crashes where I call the last fread(). I think I'm reading too far into the file, but I can't see how that's possible. I tried adding an extra byte to the end of the file, but it still crashed.

    Code:
    // Macro to find the size of the data block.
    #define DATABLOCK_SIZE		(width*height*bytedepth)
    
    unsigned char BTFImage::Load(char FileName[FILENAME_MAX], int )
    {
    	FILE *FileHandle = fopen(FileName, "rb");	// Load the file
    	if (!FileHandle)	// If it can't load the file, it more than likely doesn't exist
    		return BTF_NOTEXIST;
    
    	unsigned char header[BTF_HEADERSIZE];	// Buffer to hold the header in
    	fread(header, BTF_HEADERSIZE, 1, FileHandle); // Read the header
    	
    	if (header[0] != 0x42 || header[1] != 0x54 || header[2] != 0x46) // Check the key
    		return BTF_BADFILE;
    
    	switch (header[3])	// Decode the flag byte
    	{
    	case 0x10:
    		compression = BTF_CLEAN;
    		bytedepth = 1;
    		break;
    	case 0x12:
    		compression = BTF_RLELOOKUP;
    		bytedepth = 1;
    		break;
    	case 0x13:
    		compression = BTF_RLELOOKUPINV;
    		bytedepth = 1;
    		break;
    	case 0x30:
    		compression = BTF_CLEAN;
    		bytedepth = 3;
    		break;
    	case 0x31:
    		compression = BTF_LOOKUP;
    		bytedepth = 3;
    		break;
    	case 0x32:
    		compression = BTF_RLELOOKUP;
    		bytedepth = 3;
    		break;
    	case 0x33:
    		compression = BTF_RLELOOKUPINV;
    		bytedepth = 3;
    		break;
    	case 0x40:
    		compression = BTF_CLEAN;
    		bytedepth = 4;
    		break;
    	case 0x41:
    		compression = BTF_LOOKUP;
    		bytedepth = 4;
    		break;
    	case 0x42:
    		compression = BTF_RLELOOKUP;
    		bytedepth = 4;
    		break;
    	case 0x43:
    		compression = BTF_RLELOOKUPINV;
    		bytedepth = 4;
    		break;
    	default:
    		return BTF_BADFLAGBYTE;
    	}
    
    	width = header[4] * 256 + header[5];	// Find width and height
    	height = header[6] * 256 + header[7];
    
    	data = new unsigned char[DATABLOCK_SIZE];	// Allocate space for the image data
    	if (data = 0)	// If the file is GIGANTIC and you are out of RAM, return BTF_NORAM
    		return BTF_NORAM;
    
    	if (compression == BTF_CLEAN)	// If no compression
    		fread(data, DATABLOCK_SIZE, 1, FileHandle);	// Read the whole block directly
    
    	footer = 0;
    	fclose(FileHandle);	// Close the file
    //	filename = FileName;	// Set the filename to the one passed to us
    }
    Here is the file I'm loading (in hexadecimal format)
    Code:
    42 54 46 10 00 01 00 01 05

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    > if (data = 0)
    Oops, you just lost your memory
    Try
    if (data == 0)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    OMG, I'm such an idiot. That's a major n00b mistake.

    I guess everyone does something like this every once in a while. Even John Carmack probably does little things like this once in a while.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    Most modern compilers will warn you about such assignments where a comparison was intended.

    I'd suggest testing your compiler to see what you get.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 05:17 AM
  2. Loading image from Resource
    By parad0x13 in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2009, 11:36 PM
  3. Loading an image in from a resource file
    By starcatcher in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 07:44 AM
  4. Loading an image into memory using OpenGL
    By Kaelin in forum C++ Programming
    Replies: 3
    Last Post: 02-08-2005, 12:03 PM
  5. Image class - loading image file
    By GaPe in forum Windows Programming
    Replies: 2
    Last Post: 07-11-2004, 02:35 PM

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