TGA file loader problem

Started by
4 comments, last by rexizt 21 years, 11 months ago
This is my TGA file loader. Can anyone spot any errors? When i use the loader the colors get screwed up. Help? int LoadTGAFile(char* filename, TGAFILE *tgaFile) { FILE* filePtr; // file pointer unsigned char ucharBad; // garbage uchar data short int sintBad; // grabage sint data long imageSize; // size of TGA file int colorMode; // 4 = RGBA, 3 = RGB long imageIdx; // counter variable unsigned char colorSwap; // swap variable filePtr = fopen(filename,"rb"); if (!filePtr) { MessageBox(NULL,"Could not find file", "ERROR", MB_OK); return 0; } fread(&ucharBad, sizeof(unsigned char), 1, filePtr); // read the two first bytes of data fread(&ucharBad, sizeof(unsigned char), 1, filePtr); // cause we don''t need them fread(&tgaFile->imageTypeCode, sizeof(unsigned char), 1, filePtr); if ((tgaFile->imageTypeCode != 2) && (tgaFile->imageTypeCode != 3)) { MessageBox(NULL, "Wrong imageTypeCode", "ERROR", NULL); fclose(filePtr); return 0; } //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 height / widht / bit fread(&tgaFile->imageHeight, sizeof(short int), 1, filePtr); fread(&tgaFile->imageWidth, sizeof(short int), 1, filePtr); fread(&tgaFile->bitCount, sizeof(unsigned char), 1, filePtr); colorMode = tgaFile->bitCount /8; imageSize = tgaFile->imageWidth * tgaFile->imageHeight * colorMode; tgaFile->imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize); fread(tgaFile->imageData, sizeof(unsigned char), imageSize, filePtr); // change BGR to RGB so we can read the image for (imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode) { colorSwap = tgaFile->imageData[imageIdx]; tgaFile->imageData[imageIdx] = tgaFile->imageData[imageIdx +2]; tgaFile->imageData[imageIdx +2] = colorSwap; } fclose(filePtr); return 1; }
"may the source be with you"
Advertisement
What exacty does it look like? are the colors just wrong or is it distorted/random looking

TGA is a very simple format, altough I would read the whole header in to an array instead of using multiple freads, just incase you miss the actual bitmap data
As well as the potential performance issue Cybertron mentioned, I noticed three definate problems with that code:

1. You read height before width - in the TGA file header, width comes before height!!! - it should be:
fread(&tgaFile->imageWidth, sizeof(short int), 1, filePtr);
fread(&tgaFile->imageHeight, sizeof(short int), 1, filePtr);

2. There is one more byte of the header after the bitcount - so you need to read an extra byte after you read the bitcount.

3. In some TGA files, the image data DOES NOT come immediately after the header, there is an option "image ID" chunk followed by an optional "colour map" chunk. The sizes of these chunks are specified in the header.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

thanks for the replies. a given example of a problem is that a TGA file with some snow and mountains has a light shade of green. Other images have wrong colors too.
"may the source be with you"
thanks S1CA. it works just fine:D performance is not an issue yet
"may the source be with you"
I believe not all TGAs are stored in BGR format. I had some issues with this, different programs store them in different ways. I think there is a bit in the imageDescriptor (in the TARGA header) which specifies whether it is in BGR or RGB format, but I''m too lazy to look through the specs for exactly where it is.

Have you tried removing the BGR conversion part of the routine?

This topic is closed to new replies.

Advertisement