Browse Source

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

pull/2/head
Andrey Akhmichin 4 years ago
parent
commit
c53985c0bc
  1. 47
      engine/common/imagelib/img_tga.c

47
engine/common/imagelib/img_tga.c

@ -236,9 +236,10 @@ Image_SaveTGA @@ -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 ) @@ -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 ) @@ -311,6 +327,7 @@ qboolean Image_SaveTGA( const char *name, rgbdata_t *pix )
}
break;
}
FS_WriteFile( name, buffer, outsize );
Mem_Free( buffer );

Loading…
Cancel
Save