32bit TGA loader

Started by
8 comments, last by Aldacron 24 years, 5 months ago
I don't have any tutorials, but I can offer advice via email if you're interested in working out where you're going wrong - I can probably also send you a few snippets of source. I've never read in compressed files, but from what I understand they just use simple RLE encoding. Also you might want to check out Truevision's official specification which you can get in PDF format, if you haven't already. My address is phoenix@divineweb.com, if you're interested.

Regards

Starwynd

Advertisement
Thanks, but I finally got it working. For some reason my header struct was weighing in at 20 bytes, although it should have been 18. I still can't figure what I did wrong. Anyway, I just loaded the header a byte at a time and everything works now.
Well, if you want to make your life that much easier, this is why you had a 20 byte struct when you only specified 18: struct alignment. The compiler tries to make access easier on your program by aligning members of structures on certain boundaries. In Visual C++ 6.0 the default is 8-bte boundaries. This will cause the compiler to add padding to the struct in the middle to make it nice. If you have Visual C++, you can use:

#pragma pack(push, original)
#pragma pack(1)

right before the structure definition and:

#pragma pack(pop, original)

right after, and that will change the struct alignment to 1-byte, which is exactly what you need.

- Splat

Thanks Splat. Time to dig into the knowledge base and see what else I don't know.
Yeah. Reading will go a long way to helping you program. Right now, almost find the best way to learn more (aside from programming my game engine / game) is to traverse these messageboards and try to solve EVERYONE's problem, no matter how hard. I mean, I've posted 32 (now 33) messages since I found this place 2.5 weeks ago. And I've learned A LOT.

- Splat

Explain how you came up with 20 bytes... if the compiler packs to 8-byte boundries, wouldn't it either be 16 or 24 bytes?

Are you sure you didn't have a regular int somewhere where you needed a short int or something?

Mason McCuskey
Spin Studios
www.spin-studios.com

[This message has been edited by mason (edited October 26, 1999).]

Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
I am curious of how it was packed to 20 as well.

The default packing size for Win32 (or 95/NT) is at 8 byte boundries.

If you are using Win 3.X the packing size is at 2 byte boundries, which would have kept the size at 18.

-Gilderoot

[This message has been edited by Gilderoot (edited October 26, 1999).]

mason: Trust me on this one - he had the structure right. How do I know? Because I didn't believe him and wrote the struct out to spec, and found it's size was 20. The key thing to understand is that the 8-byte alignment setting is the maximum data type that the compiler will align on a data type boundary.

So, say we have a short (2 byte). Ideally, we want its address to be a multiple to 2 (aligned). Or a long (4 bytes) on a multiple of 4. What 8-byte alignment says is that: "compiler, you can align any data type that is less than or equal to 8 bytes"

So if it gets an 8 byte data member that is off by one byte forward, it will add its maximum of 7 bytes padding to get that type on an 8 byte boundary.

With the TGA header, the first short is 3 bytes in (unaligned). There goes one byte padding. Then, after than padding, the third short is 9 bytes in, so we have another byte of padding. This pushes the key elements of width, height, and bits per pixel exactly one short back, which really messes up your header ;(

However, setting the compiler's packing settings fixes all this. In fact, in the Windows header files they use this setting for several key structures that are commonly read in from files.

- Splat

Oh yeah, Gilderoot, the packing size of 2 bytes would NOT work because the compiler would still align the shorts (which are 2 bytes)

[This message has been edited by Splat (edited October 26, 1999).]

I did a sizeof to get the 20 byte result. I triple checked my struct with the TGA specs and was at a total loss.

Of course, when I made the call with an 18 instead of sizeof(TGA_HEADER), the results were fine for the first few entries, but got out of whack near the middle. Now I understand why, thanks to the explanation from Splat.


Can anyone point me to a 32bit TGA loader, or perhaps a tutorial on how to write one? My attempts repeatedly fail and I'm getting frustrated.

Thanks.

This topic is closed to new replies.

Advertisement