Archived

This topic is now archived and is closed to further replies.

rexizt

TGA file loader problem

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites