How to load a uncompressed 24 bit TGA file.
THis first part is the header. We wont bother with it in the simple loader. Its just so that you can expand your knowledge.
- Code: Select all
#define BYTE unsigned char
#define WORD unsigned short int
struct TGAheader { // we are not using this. its only to show you how the header is set up.
BYTE IDlen; // Leave this at zero... you dont need it.
BYTE MapType; // We want true color(24+ bits) so set this to 0, as we dont have a palette image
BYTE ImageType; // Always use number 2.
WORD FirstCMapEntry; // We dont have a pallete, so its ignored. See Map Type. Zero
WORD ColorMapLength; // Ignored along with FirstCMapEntry. Zero
BYTE CMapEntrySize; // Also ignored.. No palette. Zero
WORD ImageXOrigin; // 0,0 is upper left? then use zero here.
WORD ImageYOrigin; // 0,0 is upper left? then use zero here.
WORD ImageWidth; // If you dont know this....
WORD ImageHeight; // then there is not point in explaining them
BYTE BitsPerPixel; // 24 for 24 bit, 32 if you use Alpha channel.
BYTE DescriptorBits; // Set at 32 for 24 bit, and 40 for 32 bit.(I dont think this really matters though)
};
This is the code portion we will use.
- Code: Select all
#define BYTE unsigned char
#define WORD unsigned short int
BYTE red; BYTE green; BYTE blue; // temp color variables
WORD Height;
WORD Width;
WORD TMP;
char fp;
int CheckTGAheader()
{
FILE *fp=fopen("C:\\Here\\Be\\Dragons.tga", "rb"); // open the file.
if(fgetc(fp) != 0) return -1; // ID tag = bad
if(fgetc(fp) != 0) return -1; // A palette? We dont like you.
if(fgetc(fp) != 2) return -1; // Compression? No thanks.
fseek(fp, 12, SEEK_SET); // go tot he twelfth position from the beginning.
TMP = fgetc(fp) << 8; // read a BYTE into a temp variable push it over 8 bits
Height = fgetc(fp)+TMP; // read a BYTE into Height and add the temp variable
TMP = fgetc(fp) << 8; // read a BYTE into a temp variable push it over 8 bits...AGAIN
Width = fgetc(fp)+TMP; // Read the height Byte and add TMP.
fseek(fp, 17, SEEK_SET); // Zoom to the 17th byte from the beginning
if(fgetc(fp) != 24) return -1; // Its not 24 bit...no good.
fclose(fp); // Close the file.
return 0; // Return a zero.. All is well.
}
char image[16][16*3]; // whatever size the image is.. one array per image.
// learn about malloc to do allocate arrays on the fly
int ReadTGA()
{
int i = 0; int j = 0;
FILE *fp=fopen("C:\\Here\\Be\\Dragons.tga", "rb"); // open the file again.
fseek(fp, 18, SEEK_SET); // skip the header
for(i = 0; i <Height; i++)
{
for(j = 0; j <Width; j++)
{
blue = fgetc(fp); // read off in reverse order
green = fgetc(fp); // green is normal
red = fgetc(fp); // remember that the stored order is bgr not rgb
image[i][j] = red; // set the order to rgb
image[i][j+1] = green; // green is center
image[i][j+2] = blue; // last color
}
}
fclose(fp); // close the file
return 0; // all is well
}
Read it over. if you dont understand how it works, ask me here. it contains the concept of error checking, which is why you have the functions as ints and not void. make a int var, and in your code, go
int CheckOp = 0; // short for check operation
CheckOp = CheckTGAheader();
then you check the variable CheckOp. if CheckOp is NOT zero, something went wrong.
after that, you load the field
CheckOp = 0; // set it back to zero
CheckOp = ReadTGA(); // attempt to read the tga, returning an error code
and check it again.. if its not zero, you have a problem.