2
0
mirror of https://github.com/FWGS/xash3d-fwgs synced 2024-11-22 18:07:09 +01:00

engine: common: imagelib: img_tga.c: refactor Targa encoder.

This commit is contained in:
Andrey Akhmichin 2020-11-18 18:44:56 +05:00
parent f60c398f32
commit c53985c0bc

View File

@ -236,9 +236,10 @@ Image_SaveTGA
qboolean Image_SaveTGA( const char *name, rgbdata_t *pix )
{
int y, outsize, pixel_size;
const byte *bufend, *in;
byte *buffer, *out;
const char *comment = "Generated by Xash ImageLib\0";
const uint8_t *bufend, *in;
uint8_t *buffer, *out;
tga_t targa_header = {0};
const char comment[] = "Generated by Xash ImageLib";
if( FS_FileExists( name, false ) && !Image_CheckFlag( IL_ALLOW_OVERWRITE ))
return false; // already existed
@ -258,21 +259,36 @@ qboolean Image_SaveTGA( const char *name, rgbdata_t *pix )
return false;
}
outsize = pix->width * pix->height * pixel_size + 18 + Q_strlen( comment );
outsize = pix->width * pix->height * pixel_size;
outsize += sizeof( tga_t );
outsize += sizeof( comment ) - 1;
buffer = (byte *)Mem_Calloc( host.imagepool, outsize );
buffer = (uint8_t *)Mem_Malloc( host.imagepool, outsize );
// prepare header
buffer[0] = Q_strlen( comment ); // tga comment length
buffer[2] = 2; // uncompressed type
buffer[12] = (pix->width >> 0) & 0xFF;
buffer[13] = (pix->width >> 8) & 0xFF;
buffer[14] = (pix->height >> 0) & 0xFF;
buffer[15] = (pix->height >> 8) & 0xFF;
buffer[16] = ( pix->flags & IMAGE_HAS_ALPHA ) ? 32 : 24;
buffer[17] = ( pix->flags & IMAGE_HAS_ALPHA ) ? 8 : 0; // 8 bits of alpha
Q_strncpy( buffer + 18, comment, Q_strlen( comment ));
out = buffer + 18 + Q_strlen( comment );
targa_header.id_length = sizeof( comment ) - 1; // tga comment length
targa_header.image_type = 2; // uncompressed type
targa_header.width = pix->width;
targa_header.height = pix->height;
if( pix->flags & IMAGE_HAS_ALPHA )
{
targa_header.pixel_size = 32;
targa_header.attributes = 8; // 8 bits of alpha
}
else
{
targa_header.pixel_size = 24;
targa_header.attributes = 0;
}
out = buffer;
memcpy( out, &targa_header, sizeof( tga_t ) );
out += sizeof( tga_t );
memcpy( out, comment, sizeof( comment ) - 1 );
out += sizeof( comment ) - 1;
switch( pix->type )
{
@ -311,6 +327,7 @@ qboolean Image_SaveTGA( const char *name, rgbdata_t *pix )
}
break;
}
FS_WriteFile( name, buffer, outsize );
Mem_Free( buffer );