Advertisement

Anyone here have experience with loading TGA files?

Started by December 10, 2000 02:57 PM
14 comments, last by Densun 24 years ago
In what file exactly is the tga loading code for quake situated ?
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
I use the following code to read a TGA file header:

  	TGAHeader temp;	char t[18];	fread( &t, 18, 1, f );	temp.IDLength             = t[0] & 0xFF;	temp.ColorMap             = t[1] & 0xFF;	temp.ImageType            = t[2] & 0xFF;	temp.ColorSpec_FirstEntry = ( ( t[ 4] & 0xFF ) << 8 ) + ( t[ 3] & 0xFF );	temp.ColorSpec_Length     = ( ( t[ 6] & 0xFF ) << 8 ) + ( t[ 5] & 0xFF );	temp.ColorSpec_EntrySize  = t[7] & 0xFF;	temp.ImageSpec_XOrig      = ( ( t[ 9] & 0xFF ) << 8 ) + ( t[ 8] & 0xFF );	temp.ImageSpec_YOrig      = ( ( t[11] & 0xFF ) << 8 ) + ( t[10] & 0xFF );	temp.ImageSpec_Width      = ( ( t[13] & 0xFF ) << 8 ) + ( t[12] & 0xFF );	temp.ImageSpec_Height     = ( ( t[15] & 0xFF ) << 8 ) + ( t[14] & 0xFF );	temp.ImageSpec_Depth      = t[16] & 0xFF;	temp.ImageSpec_Desc       = t[17] & 0xFF;  


It works perfectly. I''ve got the & 0xFF in there to stop the CPU carring the MSB of the char accross the int.

When I find my code in tons of trouble,
Friends and colleages come to me,
Speaking words of wisdom:
"Write in C."
When I find my code in tons of trouble,Friends and colleages come to me,Speaking words of wisdom:"Write in C."My Web Site
Advertisement
IO Fission''s suggestion worked. I''m sure the two other latest suggestions would work as well, but I don''t feel like trying them out right now.

Choose a path:
path one or path two
Bleakcabal - In what file exactly is the tga loading code for quake situated ?

It''s located in gl_warp.c

TARGA loading starts at line 443, and includes the function LoadTGA. There''s also a LoadPCX function too. Plus these functions handle the compression I believe (I think TGA''s can have some type of run-length encoding) so it''s pretty nice to just grab them. Although I think I had the problem that the orientation the data is grabbed in is not the same for both, the TGA and PCX. One of them is upside down, depending of course upon your coordinate system.
Densun could you post your final working function ???
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
      ******************Includes******************/#include <stdio.h>/******************Data types******************/#pragma pack(1)typedef struct targaheader_s{	unsigned char idlength;  /* The number of bytes in imageid. 0 means nothing is stored. */	unsigned char cmaptype;  /* If a color map has been stored and what type it is. */	unsigned char imagetype; /* What kind of image data is stored. */	/* Color map variables. */	unsigned short firstentry; /* Index of the first color map entry. */	unsigned short cmaplength; /* Number of color map entries stored. */	unsigned char  entrysize;  /* The number of bits per entry. */	/* Image specification variables. */	unsigned short xorigin;    /* X position on screen from lower left corner. */	unsigned short yorigin;    /* Y position on screen from lower left corner. */	unsigned short width;      /* Width of the image. */	unsigned short height;     /* Height of the image. */	unsigned char  bpp;        /* Bits per pixel (pixel depth). */	unsigned char  descriptor; /* Information on the pixels. */} targaheader_t;#pragma pack/******************Prototypes******************/int LoadTGA (char* filename, targaheader_t* tga);/******************Functions******************/int main (void){	targaheader_t test;	if (!LoadTGA ("test.tga", &test))	{		puts ("Error loading TGA file.");		return 0;	}	printf ("ID Length: %d\n", test.idlength);	printf ("Color map type: %d\n", test.cmaptype);	printf ("Image type: %d\n", test.imagetype);	printf ("First entry index: %d\n", test.firstentry);	printf ("Color map length: %d\n", test.cmaplength);	printf ("Entry size: %d\n", test.entrysize);	printf ("X origin: %d\n", test.xorigin);	printf ("Y origin: %d\n", test.yorigin);	printf ("Width: %d\n", test.width);	printf ("Height: %d\n", test.height);	printf ("BPP: %d\n", test.bpp);	printf ("Descriptor: %d\n", test.descriptor);	return 0;}int LoadTGA (char* filename, targaheader_t* tga){	FILE* fp;	fp = fopen (filename, "rb");	if (fp == NULL)		return 0;	fread (tga, sizeof (targaheader_t), 1, fp);	fclose (fp);	return 1;}      


Choose a path:
path one or path two

Edited by - Densun on December 14, 2000 5:28:20 PM

Edited by - Densun on December 14, 2000 5:29:05 PM

This topic is closed to new replies.

Advertisement