🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

tga files

Started by
7 comments, last by +AA_970+ 24 years, 5 months ago
i tried to load tga files but i failed, does anyone have any source code i can look at to get an idea of how to read the image data from the file?
Advertisement
try www.wotsit.org or www.neutralzone.org/home/faqsys/
Unfortunatly those sites only have the file format info and no source code
struct TARGA_HEADER{	unsigned char	IDLength;	unsigned char	ColorMapType;	unsigned char	ImageType;	unsigned short	CMapStart;	unsigned char	CMapDepth;	unsigned short	XOffSet;	unsigned short	YOffSet;	unsigned short	Width;	unsigned short	Height;	unsigned char	PixelDepth;	unsigned char	ImageDescriptor;};void LoadTGA( char* file_name, DDrawSurface surface ){        //Create the surface.        //Lock it to get a pointer to mess with                DWORD bytes_read;        TARGA_HEADER header;        HANDLE file = CreateFile( file_name, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL / FILE_FLAG_RANDOM_ACCESS, 0 );                ReadFile( file, &header, sizeof( header ), &bytes_read, NULL );        //Get what you need from the header        for( int i = 0; i < Height; i++ )        {                unsigned short ptr = (unsigned short*)surfaceptr+1*Width;                ReadFile( file, ptr, x*Width, &bytes_read, NULL );        }             CloseHandle( file );}  


The function I use to read the file are part of the windows api. Also, the value for "x" depends on the type of images you are using. Here is a chart of correct values:

32 Bit: 4
24 Bit: 4
16 Bit: 2
8 Bit: 1

The code is not exact and I just wrote it out of my head. However, it should get you started.

I forget to mention, when you get a pointer to the surface, it will not always be an unsigned short. Here is another chart:

32 Bit: unsigned int
24 Bit: unsigned int
16 Bit: unsigned short
8 Bit: unsigned char

Keep in mind that the type of image you load must mach your screen resolution or the images must be converted to the correct format before you can use it (for example, if you are using a 16 Bit mode and loading a 24 Bit image, you must convert it to a 16 Bit image first).

Edited by - Mike on 1/20/00 7:22:44 PM
Whoa, there Mike, you''re missing whole chunks of the Targa specification. Such as interlacing (2 way/4 way). Targa files also pad their widths to a DWORD boundary, so you need to take that into account too.

I guess that depends on whether or not he is writing for all possible combinations of targa formats out there.

Seriously... I wasn''t aware that a targa file width was padded, similar to a windows bitmap? Or that they supported interlacing for that matter. Looks like I have been out of touch!

Of course, my tga decoder was specifically written to read tga files produced by POV Ray and PSP, so I can''t really say that mine would be compatible either.

Say, I just checked truevisions site (first time in a long time), and noticed that they have been bought out and the targa docs aren''t available anymore (at least that I saw). Happen to know where they keep ''em now?

Thanks!

-mordell





__________________________________________

Yeah, sure... we are laughing WITH you ...
The header as I have will load a targa created with photoshop 4 and 5 and paintshop pro. It works for me.
Mike, never said anything was wrong with your header. It''s just that the image reading loop can be a bit misleading.

Mordell, interlacing in targa formatted images has been there at least since the days of Win 3.1. Found that out the really hard way. I don''t know where the current official docs are. I just use my file format bible.

I''ve got the code for a tga loader lying around on floppy (or maybe tape) somewhere. Give my a few days to find it, then I''ll post it.

SiCrane -

yikes! Tape!!? hehehe..

You used to be able to find information on the targa format from truevisions website (www.truevision.com), although sometimes you had to dig for it.

Of course, more recently I have relied on www.wotsit.org for image formats, including tga.

I used to develop for the now defuct commercial image library T-BASE (http://www.videotexsystems.com/tbwin.htm) and luckily never ran into interlaced tga''s! Although, it was a one man show, so feature enhancements were slow in coming..hehehe..

By your format bible, are you referring to "Encyclopedia of Graphic File Formats" ? If not, which reference is it? I know that I would like to get a hold of an updated reference.

Thanks!

-mordell

__________________________________________

Yeah, sure... we are laughing WITH you ...

This topic is closed to new replies.

Advertisement