.tga file loading errors

Started by
43 comments, last by Myopic Rhino 19 years, 10 months ago
I am reading the book "OpenGL Game Programming" and I am getting strange results when trying to load .tga files the way shown. I cannot load .tga files created in paint shop pro 8, I assume this is because these are compressed, and I am only loading in .tga files with an image type of 2 or 3, meaning they are uncompressed RGB or uncompressed black and white images. But the real trouble happens when I try to load a .tga file that does have the correct image type. I get no error messages but the program just seems to automatically closes. Here is the code:

bool Texture::loadTGA(char *fileName)
{
	FILE				*filePtr;				// the file pointer
	TgaFile				tgaFile;				// 4 for RGBA or 3 for RGB
	unsigned char		ucharBad;				// garbage unsigned char data
	short int			sintBad;				// garbage short int data
	long				imageSize;				// size of the TGA image
	int					colorMode;				// 4
	unsigned int		imageIdx = 0;			// image index counter
	unsigned char		colorSwap;				// swap variable
	// open filename in "read binary" mode
	filePtr = fopen(fileName, "rb");
	if (filePtr == NULL)
		return false;

	// read the first two bytes of data we don't need
	fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
	fread(&ucharBad, sizeof(unsigned char), 1, filePtr);

	// read in the image type
	fread(&tgaFile.imageTypeCode, sizeof(unsigned char), 1, filePtr);

	// for our purposes, the image type should be either 2 or 3 (meaning they are uncompressed
	// RGB or uncompressed black and white images respectivly)
	if( (tgaFile.imageTypeCode != 2) && (tgaFile.imageTypeCode != 3) )
	{
		fclose(filePtr);
		return false;
	}

	// read 13 bytes of data we don't need
	fread(&sintBad, sizeof(short int), 1, filePtr);
	fread(&sintBad, sizeof(short int), 1, filePtr);
	fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
	fread(&sintBad, sizeof(short int), 1, filePtr);
	fread(&sintBad, sizeof(short int), 1, filePtr);

	// read image dimensions
	fread(&tgaFile.imageWidth, sizeof(short int), 1, filePtr);
	fread(&tgaFile.imageHeight, sizeof(short int), 1, filePtr);

	// read image bit depth
	fread(&tgaFile.bitCount, sizeof(unsigned char), 1, filePtr);

	// read 1 byte of data we don't need
	fread(&ucharBad, sizeof(unsigned char), 1, filePtr);

	// colorMode -> 3 = BRG, 4 = BGRA
	colorMode = tgaFile.bitCount / 8;
	imageSize = tgaFile.imageWidth * tgaFile.imageHeight * colorMode;

	// allocate memory for image data
	tgaFile.imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize);

	// read in image data
	fread(&tgaFile.imageData, sizeof(unsigned char), imageSize, filePtr);

	// change BGR to RGB so OpenGL can read the image data
	for(imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode)
	{
		colorSwap = tgaFile.imageData[imageIdx];
		tgaFile.imageData[imageIdx] = tgaFile.imageData[imageIdx + 2];
		tgaFile.imageData[imageIdx + 2] = colorSwap;
	}

	// close the file
	fclose(filePtr);

	glGenTextures(1, &_myTexture);
	glBindTexture(GL_TEXTURE_2D, _myTexture);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

	// load the texture image
	//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
	//				bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, tgaFile.imageWidth, tgaFile.imageHeight,
					GL_RGBA, GL_UNSIGNED_BYTE, tgaFile.imageData);

	return true;
}

Thanks for any help Tera_Dragon
____________________________________________________________Programmers Resource Central
Advertisement
I didn't look over the code, but for simple things like this, where the program closes (that means a crash) a debugger can be VERY useful. Do you use one?
I use MSVC++ 6.0 but I don't realy have an idea how to use the debugger.
Could you jus give me some quick instructions please?
Tera_Dragon
____________________________________________________________Programmers Resource Central
Well, than it's about time to learn. You know, that VC++ comes with help file right.


didn't look over your code but u might try loading tga files tut among other things

For debugging just compile in debug mode and run, when something goes wrong an error will pop up and you can hit debug to see where it is (line of code its on). Might be different for VS6, i'm using 2003. You can also set break points, there should be a gray vertical bar on the left of your code, click on it and a red circle will appear (for a line of code) then when your program reaches that line of code (with the red circle) the program will stop and allow you to see whats going on. You can check the variable values and everything. When u make sure everything is fine u can continue execution again.

Hope that helped
After quite a bit of messing about with the debugger I think that the error is on this line:

fread(&tgaFile.imageData, sizeof(unsigned char), imageSize, filePtr);
(look at the start my first post to find what the data types are etc).

I don't see why this doesn't work. Please help.
____________________________________________________________Programmers Resource Central
I got rid of the '&' but I now get a windows error message.

Edit: I'm running the debugger, but I'm in a loop that will take about 196000 loops, is there any quick way out of this? I'm hitting F10 to move forward a statement at a time.
____________________________________________________________Programmers Resource Central
I am using DevC++/Insight so I can't help you. But I am sure there ar eloads of docs and tutorials on the VCC debugger.
Quote:Original post by Tera_Dragon
I got rid of the '&' but I now get a windows error message.

Edit: I'm running the debugger, but I'm in a loop that will take about 196000 loops, is there any quick way out of this? I'm hitting F10 to move forward a statement at a time.


Set a breakpoint after the loop, click on continue/run/whatever
Rather than using fread and assigning it to a dodgy, unused value, you could google and investigate the wonders of fseek, allowing you to skip the cursor that reads the file on to whatever point you feel like.

This topic is closed to new replies.

Advertisement