View Full Version : .tga file loading
Bossa Nova
2002.06.26, 07:39 PM
I'm kinda new to Mac programming. I really need help with something.
I'm trying to load a .tga file. I do things like normal: open a stream, read the type code, then go onto read the data. However, I've noticed that I can't read anything like a normally do. The type code is blank, and the data is garbage.
I'm fairly certain the code works on Windows, is there anything different about file loading on a Mac?
Any ideas are greatly appreciated.
CODE BELOW:
---------------
filePtr = fopen(filename, "rb");
if (!filePtr)
{
cout << "can't find file";
return 0;
}
fread(&bad, sizeof(unsigned char), 1, filePtr);
fread(&bad, sizeof(unsigned char), 1, filePtr);
fread(&tgaFile->imageTypeCode,sizeof(unsigned char),1,filePtr);
if((tgaFile->imageTypeCode !=2) && (tgaFile->imageTypeCode !=3))
{
cout << tgaFile->imageTypeCode <<endl;
cout << "Wrong type!";
fclose(filePtr);
return 0;
}
-------------------
The type code member has nothing in it, and even when I rem this out the file is junk.
I'm sure I'm doing something really stupid (as always is the case :) Any thoughts?
CMagicPoker
2002.06.26, 08:20 PM
You need to convert the data.
On PC processors, it's little endian, and on PPC processors, it's big endian. Still following me? ;-)
For example, for a 32-bits value, you need to switch the first byte with the last byte, and the flipping the posibiton of the 2 middle bytes.
On 16-bits just flip the 2 bytes.
For the rgb pixels you need to flip too, I think.
OneSadCookie
2002.06.26, 09:20 PM
http://www.crissman.net/macgamewiki/index.php/CrossPlatformGotchas
There is a source code sample here at iDevGames called "QTValuePak" that shows how to load all kinds of graphics. For code showing how to specifically load TGA using C i/o calls, look at lesson 34 at http://nehe.gamedev.net
Johan
2002.06.27, 03:14 AM
Originally posted by CMagicPoker
You need to convert the data.
On PC processors, it's little endian, and on PPC processors, it's big endian. Still following me?
This is hardly the problem here since he's only reading one byte. I tried the code and it works perfect for me.
You need to cast the unsigned char to an int if you wan't cout to give you a number and not the ASCII character.
cout << (int)tgaFile->imageTypeCode << endl;
Cheers,
Johan
Bossa Nova
2002.06.28, 11:41 PM
Thanks to everyone that replied.
The big/little endian problem was going to cause me a lot of head aches (its a good thing I got it figured out so early). I wrote two simple swaptwo/ swapfour byte functions and now things are working fine. I did need to cast the tga type to an int to make it show up.
I have a question though. How can one tell how a file is saved. What I mean is .tga's are saved for little endian, .sgi's are big endian. Is there any comprehensive list or something that I can reference.
Johan
2002.06.29, 03:47 AM
Originally posted by Bossa Nova
Is there any comprehensive list or something that I can reference.
www.wotsit.org ?!?
Originally posted by Bossa Nova
I have a question though. How can one tell how a file is saved. What I mean is .tga's are saved for little endian, .sgi's are big endian. Is there any comprehensive list or something that I can reference.
I haven't messed with TGA recently (I really love the Pixar format - photoshop saves it, easy to load, just a bit on the undocumented side), but iirc the only multi-byte values you'd need to worry about are the width and height. An easy partial fix is to set minimum/maximimum image dimentions, and figure out which byte ordering fits into them. (is the image 640 or 32770 pixels wide?)
That won't work all the time though (257-259 and 513-515 generate reasonable dimentions when byte swapped) , so you're probably better off just figuring out what byte ordering your apps save to (I don't think I've ever seen a Big Endian targa), and sticking with that.
ClarustheDogCow
2002.07.03, 09:07 PM
I actually have some code on my Sourceforge site for loading/saving pixar format...because it was just that easy=] Inio recommended it to me in the first place. I guess that makes him famous;-]
Use it more as a guide than a tool=]
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/seppuku-editor/Apoded/pixar.h?rev=HEAD&content-type=text/plain
Nimrod
2002.07.04, 11:42 AM
I should point out that PNG is widely supported, and also very easy to use, and has lossless compression. Writing a basic PNG loader with libpng takes only a few minutes.
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.