TGA loading problems

Started by
5 comments, last by TheGrudge 21 years, 7 months ago
Hi I''m trying to write some code that will create a TGA file which will eventually be used to provide alpha information but currently just testing the code. However, whenever I create the file I can''t open it in paint shop pro it gives an error. This is my header setup for TGA: struct TGAHEADER{ BYTE IDField; BYTE ColorMapType; BYTE ImageTypeCode; unsigned short int ColorOrigin; unsigned short int ColorLength; BYTE ColorMapEntrySize; unsigned short int XOrigin; unsigned short int YOrigin; unsigned short int Width; unsigned short int Height; BYTE PixelSize; BYTE ImageDescription; }; I am trying to make a basic 32bit file so fill in the headers as this: FileHeader.ColorMapType=0; FileHeader.ColorMapEntrySize=0; FileHeader.ColorOrigin=0; FileHeader.IDField=0; FileHeader.ImageTypeCode=2; FileHeader.ColorLength= FileHeader.Width=h_size*2; FileHeader.Height=v_size*2; FileHeader.XOrigin=0; FileHeader.YOrigin=0; FileHeader.PixelSize=32; FileHeader.ImageDescription=1; and then I just write the header out and then follow it by the image data in raw format (just 4 bytes per pixel). Can anyone tell me what I am doing wrong? I''m thinking it could be something to do with the origin as I couldn''t understand what that meant.... Thanks
Advertisement
What is the error PSP gives you?

Your image description field should be zero, I think.

Also, random comments:
Origin shouldn''t hurt, all it means is where the image''s origin is, basically if you don''t account for it then the image may be upside down or whatever.

Are you writing your pixel data in the correct order, TGAs work with swapped byte orders so you actually have a stream of BGR instead of RGB. Again, I don''t see why that would error - you''d just see an image with the colours all trippy.


-Mezz
PSP says it is not a valid TGA file so i assume it must be a header error...

What program are you using to create the file?

I found that if I use Adobe Photoshop all is well. But if I use JASC PaintShop Pro it won''t load the file. I''m thinking maybe Paintshop Pro defaults to using RLE compression for TGAs.
------------------------------------------[New Delta Games] | [Sliders]
try to enclose your header struct declaration with a

#pragma pack(push,1)

...


#pragma pack(pop)

pair. Some of the struct elements are byte-aligned and will be moved to 32bit boundaries by most compilers. As a result you write an invalid header into the file.

skynet
another way to fix it is to let ofstream know how many bytes you're
writing..
i'm assuming you ARE using ofstream in binary mode right?

[edit: ofstream, not ifstream ]

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

[edited by - eldee on September 3, 2002 1:56:48 PM]

-eldee;another space monkey;[ Forced Evolution Studios ]
quote:Original post by Anonymous Poster
try to enclose your header struct declaration with a

#pragma pack(push,1)

...


#pragma pack(pop)

pair. Some of the struct elements are byte-aligned and will be moved to 32bit boundaries by most compilers. As a result you write an invalid header into the file.

skynet



You are the winner!

Thank you - after doing that it worked fine. The compiler was filling out the header too large and hence it wouldn''t open. Thanks everyone else for giving me advice its great to know people out there wanna help!!

Hopefully one day I''ll know enough to help others!

This topic is closed to new replies.

Advertisement