imagelib: deduplicate Image_ComputeSize function

Make so that img_ktx2 and img_dds use a common copy of the function
This commit is contained in:
Ivan Avdeev 2023-10-16 12:37:58 -04:00
parent 1d90bb1835
commit c5b6599be7
4 changed files with 37 additions and 64 deletions

View File

@ -142,6 +142,7 @@ void Image_CopyPalette32bit( void );
void Image_SetPixelFormat( void );
void Image_GetPaletteQ1( void );
void Image_GetPaletteHL( void );
size_t Image_ComputeSize( int type, int width, int height, int depth );
//
// formats load

View File

@ -215,33 +215,6 @@ void Image_DXTGetPixelFormat( dds_t *hdr, dds_header_dxt10_t *headerExt )
image.num_mips = hdr->dwMipMapCount; // get actual mip count
}
size_t Image_DXTGetLinearSize( int type, int width, int height, int depth )
{
switch( type )
{
case PF_DXT1:
case PF_BC4_SIGNED:
case PF_BC4_UNSIGNED:
return ((( width + 3 ) / 4 ) * (( height + 3 ) / 4 ) * depth * 8 );
case PF_DXT3:
case PF_DXT5:
case PF_BC6H_SIGNED:
case PF_BC6H_UNSIGNED:
case PF_BC7_UNORM:
case PF_BC7_SRGB:
case PF_BC5_UNSIGNED:
case PF_BC5_SIGNED:
case PF_ATI2: return ((( width + 3 ) / 4 ) * (( height + 3 ) / 4 ) * depth * 16 );
case PF_LUMINANCE: return (width * height * depth);
case PF_BGR_24:
case PF_RGB_24: return (width * height * depth * 3);
case PF_BGRA_32:
case PF_RGBA_32: return (width * height * depth * 4);
}
return 0;
}
size_t Image_DXTCalcMipmapSize( dds_t *hdr )
{
size_t buffsize = 0;
@ -252,7 +225,7 @@ size_t Image_DXTCalcMipmapSize( dds_t *hdr )
{
width = Q_max( 1, ( hdr->dwWidth >> i ));
height = Q_max( 1, ( hdr->dwHeight >> i ));
buffsize += Image_DXTGetLinearSize( image.type, width, height, image.depth );
buffsize += Image_ComputeSize( image.type, width, height, image.depth );
}
return buffsize;
@ -301,7 +274,7 @@ void Image_DXTAdjustVolume( dds_t *hdr )
if( hdr->dwDepth <= 1 )
return;
hdr->dwLinearSize = Image_DXTGetLinearSize( image.type, hdr->dwWidth, hdr->dwHeight, hdr->dwDepth );
hdr->dwLinearSize = Image_ComputeSize( image.type, hdr->dwWidth, hdr->dwHeight, hdr->dwDepth );
hdr->dwFlags |= DDS_LINEARSIZE;
}

View File

@ -67,40 +67,6 @@ static void Image_KTX2Format( uint32_t ktx2_format )
}
}
// FIXME this codebase has too many copies of this function:
// - ref_gl has one
// - ref_vk has one
// - ref_soft has one
// - img_dds has one
// - many more places probably have one too
// - and now img_ktx2 also has one!
static size_t ImageSizeForType( int type, int width, int height, int depth )
{
switch( type )
{
case PF_DXT1:
case PF_BC4_SIGNED:
case PF_BC4_UNSIGNED:
return ((( width + 3 ) / 4 ) * (( height + 3 ) / 4 ) * depth * 8 );
case PF_DXT3:
case PF_DXT5:
case PF_ATI2:
case PF_BC5_UNSIGNED:
case PF_BC5_SIGNED:
case PF_BC6H_SIGNED:
case PF_BC6H_UNSIGNED:
case PF_BC7_UNORM:
case PF_BC7_SRGB: return ((( width + 3 ) / 4 ) * (( height + 3 ) / 4 ) * depth * 16 );
case PF_LUMINANCE: return ( width * height * depth );
case PF_BGR_24:
case PF_RGB_24: return ( width * height * depth * 3 );
case PF_BGRA_32:
case PF_RGBA_32: return ( width * height * depth * 4 );
}
return 0;
}
static qboolean Image_KTX2Parse( const ktx2_header_t *header, const byte *buffer, fs_offset_t filesize )
{
ktx2_index_t index;
@ -160,7 +126,7 @@ static qboolean Image_KTX2Parse( const ktx2_header_t *header, const byte *buffer
{
const uint32_t width = Q_max( 1, ( header->pixelWidth >> mip ));
const uint32_t height = Q_max( 1, ( header->pixelHeight >> mip ));
const uint32_t mip_size = ImageSizeForType( image.type, width, height, image.depth );
const uint32_t mip_size = Image_ComputeSize( image.type, width, height, image.depth );
ktx2_level_t level;
memcpy( &level, levels_begin + mip * sizeof level, sizeof level );

View File

@ -1447,3 +1447,36 @@ qboolean Image_Process(rgbdata_t **pix, int width, int height, uint flags, float
return result;
}
// This codebase has too many copies of this function:
// - ref_gl has one
// - ref_vk has one
// - ref_soft has one
// - many more places probably have one too
// TODO figure out how to make it available for ref_*
size_t Image_ComputeSize( int type, int width, int height, int depth )
{
switch( type )
{
case PF_DXT1:
case PF_BC4_SIGNED:
case PF_BC4_UNSIGNED:
return ((( width + 3 ) / 4 ) * (( height + 3 ) / 4 ) * depth * 8 );
case PF_DXT3:
case PF_DXT5:
case PF_ATI2:
case PF_BC5_UNSIGNED:
case PF_BC5_SIGNED:
case PF_BC6H_SIGNED:
case PF_BC6H_UNSIGNED:
case PF_BC7_UNORM:
case PF_BC7_SRGB: return ((( width + 3 ) / 4 ) * (( height + 3 ) / 4 ) * depth * 16 );
case PF_LUMINANCE: return ( width * height * depth );
case PF_BGR_24:
case PF_RGB_24: return ( width * height * depth * 3 );
case PF_BGRA_32:
case PF_RGBA_32: return ( width * height * depth * 4 );
}
return 0;
}