Sign in to follow this  

.tga loading

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It tells you how many bytes to read in at a time, and what those bytes coorespond to. For example, you would read in the bytes saying how wide and how long the image is, and set your array of pixels to that size. You will read in the palette. You will read in the pixels. But pixels may be stored in 1 per byte or 2 per byte, so you will need to know how they are stored.

That is the type of information you will get. It will tell you exactly how many bytes to read in a time, and what those bytes mean.

Share this post


Link to post
Share on other sites
This would work:


typedef struct tImageTGA
{
int iChannels;
int iSizeX;
int iSizeY;
unsigned char *pData;
} TARGADATA, *PTARGADATA;

//-----------------------------------------------------------------------------
// Name: LoadTGA()
// Desc: Returns targa data
// Auth: DigiBen digiben@gametutorials.com
//-----------------------------------------------------------------------------
TARGADATA* LoadTGA( const char *strFileName )
{
TRACE_ENTER_FN( LoadTGA )

TARGADATA *pImageData = NULL;
WORD width = 0, height = 0;
byte length = 0;
byte imageType = 0;
byte bits = 0;
FILE *pFile = NULL;
int channels = 0;
int stride = 0;
int i = 0;

if( ( pFile = fopen( strFileName, "rb" ) ) == NULL ) {
MessageBox( HWND_DESKTOP, "Unable to load TGA File!", "Error", MB_OK );
return NULL; }

pImageData = (TARGADATA*)malloc( sizeof( tImageTGA ) );

fread( &length, sizeof( byte ), 1, pFile );
fseek( pFile, 1, SEEK_CUR );
fread( &imageType, sizeof( byte ), 1, pFile );
fseek( pFile, 9, SEEK_CUR );
fread( &width, sizeof( WORD ), 1, pFile );
fread( &height, sizeof( WORD ), 1, pFile );
fread( &bits, sizeof( byte ), 1, pFile );

fseek( pFile, length + 1, SEEK_CUR );

if( imageType != TGA_RLE )
{
if( bits == 24 || bits == 32 )
{
channels = bits / 8;
stride = channels * width;
pImageData->pData = new unsigned char[stride * height];
for( int y = 0; y < height; y++ )
{
unsigned char *pLine = &( pImageData->pData[stride * y] );
fread( pLine, stride, 1, pFile );
for( i = 0; i < stride; i += channels )
{
int temp = pLine[i];
pLine[i] = pLine[i + 2];
pLine[i + 2] = temp;
}
}
}
else if( bits == 16 )
{
unsigned short pixels = 0;
int r = 0, g = 0, b = 0;
channels = 3;
stride = channels * width;
pImageData->pData = new unsigned char[stride * height];

for( int i = 0; i < width * height; i++ )
{
fread( &pixels, sizeof( unsigned short ), 1, pFile );
b = ( pixels & 0x1f ) << 3;
g = ( ( pixels >> 5 ) & 0x1f ) << 3;
r = ( ( pixels >> 10 ) & 0x1f ) << 3;

pImageData->pData[i * 3 + 0] = r;
pImageData->pData[i * 3 + 1] = g;
pImageData->pData[i * 3 + 2] = b;
}
}
else
{
return NULL;
}
}
else
{
byte rleID = 0;
int colorsRead = 0;
channels = bits / 8;
stride = channels * width;

pImageData->pData = new unsigned char[stride * height];
byte *pColors = new byte [channels];

while( i < width*height )
{
fread( &rleID, sizeof( byte ), 1, pFile );

if( rleID < 128 )
{
rleID++;

while( rleID )
{
fread( pColors, sizeof( byte ) * channels, 1, pFile );

pImageData->pData[colorsRead + 0] = pColors[2];
pImageData->pData[colorsRead + 1] = pColors[1];
pImageData->pData[colorsRead + 2] = pColors[0];

if( bits == 32 )
pImageData->pData[colorsRead + 3] = pColors[3];

i++;
rleID--;
colorsRead += channels;
}
}
else
{
rleID -= 127;

fread( pColors, sizeof( byte ) * channels, 1, pFile );

while( rleID )
{
pImageData->pData[colorsRead + 0] = pColors[2];
pImageData->pData[colorsRead + 1] = pColors[1];
pImageData->pData[colorsRead + 2] = pColors[0];

if( bits == 32 )
pImageData->pData[colorsRead + 3] = pColors[3];

i++;
rleID--;
colorsRead += channels;
}
}
}
}

fclose( pFile );

pImageData->iChannels = channels;
pImageData->iSizeX = width;
pImageData->iSizeY = height;

return pImageData;
}

Share this post


Link to post
Share on other sites
Not trying to be rude or anything, but everything has been provided to you for you to make your own loader. Wait, someone even gave you code to do it for you! And then you have the bravado to tell what kind of error it gave?

:)

Again, not trying to be rude. I just think it's a little humorous how low us beginners can sink. Heh heh. We've got to be careful not to be so lazy we ask people to write our own games for us.

Right?

Take care.

Share this post


Link to post
Share on other sites

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this