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

Loading…
Cancel
Save