engine: common: imagelib: refactoring

This commit is contained in:
Alibek Omarov
2025-12-13 23:11:48 +05:00
parent e2835a7614
commit 16c849d085
3 changed files with 36 additions and 70 deletions

View File

@@ -8,22 +8,9 @@ typically expanded to rgba buffer
NOTE: number at end of pixelformat name it's a total bitscount e.g. PF_RGB_24 == PF_RGB_888
========================================================================
*/
#define ImageRAW( type ) (type == PF_RGBA_32 || type == PF_BGRA_32 || type == PF_RGB_24 || type == PF_BGR_24 || type == PF_LUMINANCE)
#define ImageCompressed( type ) \
( type == PF_DXT1 \
|| type == PF_DXT3 \
|| type == PF_DXT5 \
|| type == PF_ATI2 \
|| type == PF_BC4_SIGNED \
|| type == PF_BC4_UNSIGNED \
|| type == PF_BC5_SIGNED \
|| type == PF_BC5_UNSIGNED \
|| type == PF_BC6H_SIGNED \
|| type == PF_BC6H_UNSIGNED \
|| type == PF_BC7_UNORM \
|| type == PF_BC7_SRGB \
|| type == PF_KTX2_RAW )
#define ImageBigEndian( type ) ( type == PF_BGRA_32 || type == PF_BGR_24 )
#define ImageRAW( type ) ( type >= PF_RGBA_32 && type <= PF_LUMINANCE )
#define ImageCompressed( type ) ( type >= PF_DXT1 && type <= PF_KTX2_RAW )
#define ImageBigEndian( type ) ( type == PF_BGRA_32 || type == PF_BGR_24 )
typedef enum
{

View File

@@ -334,10 +334,8 @@ qboolean Image_LoadBMP( const char *name, const byte *buffer, fs_offset_t filesi
qboolean Image_SaveBMP( const char *name, rgbdata_t *pix )
{
file_t *pfile = NULL;
size_t total_size, cur_size;
rgba_t rgrgbPalette[256];
dword cbBmpBits;
byte *clipbuf = NULL;
byte *pb, *pbBmpBits;
dword cbPalBytes;
dword biTrueWidth;
@@ -417,7 +415,8 @@ qboolean Image_SaveBMP( const char *name, rgbdata_t *pix )
// some viewers e.g. fimg.exe can show alpha-chanell for it
if( pix->type == PF_INDEXED_32 )
rgrgbPalette[i][3] = *pb++;
else rgrgbPalette[i][3] = 0;
else
rgrgbPalette[i][3] = 0;
}
// write palette
@@ -430,26 +429,25 @@ qboolean Image_SaveBMP( const char *name, rgbdata_t *pix )
{
i = (hdr.height - 1 - y ) * (hdr.width);
for( x = 0; x < pix->width; x++ )
if( pixel_size == 1 )
{
if( pixel_size == 1 )
{
// 8-bit
pbBmpBits[i] = pb[x];
}
else
memcpy( &pbBmpBits[i], pb, pix->width );
}
else
{
for( x = 0; x < pix->width; x++ )
{
// 24 bit
qboolean be = ImageBigEndian( pix->type );
pbBmpBits[i*pixel_size+(be?2:0)] = pb[x*pixel_size+2];
pbBmpBits[i*pixel_size+(be?1:1)] = pb[x*pixel_size+1];
pbBmpBits[i*pixel_size+(be?0:2)] = pb[x*pixel_size+0];
}
pbBmpBits[i * pixel_size + 0] = be ? pb[x * pixel_size + 0] : pb[x * pixel_size + 2];
pbBmpBits[i * pixel_size + 1] = pb[x * pixel_size + 1];
pbBmpBits[i * pixel_size + 2] = be ? pb[x * pixel_size + 2] : pb[x * pixel_size + 0];
if( pixel_size == 4 ) // write alpha channel
pbBmpBits[i*pixel_size+3] = pb[x*pixel_size+3];
i++;
if( pixel_size == 4 ) // write alpha channel
pbBmpBits[i * pixel_size + 3] = pb[x * pixel_size + 3];
i++;
}
}
pb += pix->width * pixel_size;

View File

@@ -513,6 +513,7 @@ qboolean Image_SavePNG( const char *name, rgbdata_t *pix )
z_stream stream = {0};
png_t png_hdr;
png_footer_t png_ftr;
const qboolean be = ImageBigEndian( pix->type );
if( FS_FileExists( name, false ) && !Image_CheckFlag( IL_ALLOW_OVERWRITE ))
return false; // already existed
@@ -525,9 +526,13 @@ qboolean Image_SavePNG( const char *name, rgbdata_t *pix )
switch( pix->type )
{
case PF_BGR_24:
case PF_RGB_24: pixel_size = 3; break;
case PF_RGB_24:
pixel_size = 3;
break;
case PF_BGRA_32:
case PF_RGBA_32: pixel_size = 4; break;
case PF_RGBA_32:
pixel_size = 4;
break;
default:
return false;
}
@@ -540,46 +545,22 @@ qboolean Image_SavePNG( const char *name, rgbdata_t *pix )
out = filtered_buffer = Mem_Malloc( host.imagepool, filtered_size );
// apply adaptive filter to image
switch( pix->type )
for( y = 0; y < pix->height; y++ )
{
case PF_RGB_24:
case PF_RGBA_32:
for( y = 0; y < pix->height; y++ )
in = pix->buffer + y * pix->width * pixel_size;
*out++ = PNG_F_NONE;
rowend = in + rowsize;
for( ; in < rowend; in += pixel_size )
{
in = pix->buffer + y * pix->width * pixel_size;
*out++ = PNG_F_NONE;
rowend = in + rowsize;
for( ; in < rowend; in += pixel_size )
{
*out++ = in[0];
*out++ = in[1];
*out++ = in[2];
if( pix->flags & IMAGE_HAS_ALPHA )
*out++ = in[3];
}
*out++ = be ? in[2] : in[0];
*out++ = in[1];
*out++ = be ? in[0] : in[2];
if( pix->flags & IMAGE_HAS_ALPHA )
*out++ = in[3];
}
break;
case PF_BGR_24:
case PF_BGRA_32:
for( y = 0; y < pix->height; y++ )
{
in = pix->buffer + y * pix->width * pixel_size;
*out++ = PNG_F_NONE;
rowend = in + rowsize;
for( ; in < rowend; in += pixel_size )
{
*out++ = in[2];
*out++ = in[1];
*out++ = in[0];
if( pix->flags & IMAGE_HAS_ALPHA )
*out++ = in[3];
}
}
break;
}
// get IHDR chunk length
ihdr_len = sizeof( png_ihdr_t );