secure image_t business

no need to keep in mind its destructor
This commit is contained in:
Garux 2021-10-06 15:28:47 +03:00
parent 9660fbb6f1
commit fa1933fff5
2 changed files with 31 additions and 32 deletions

View File

@ -228,11 +228,7 @@ static std::forward_list<image_t> images;
static struct construct_default_image static struct construct_default_image
{ {
construct_default_image(){ construct_default_image(){
image_t img; images.emplace_front( DEFAULT_IMAGE, DEFAULT_IMAGE, 64, 64, void_ptr( memset( safe_malloc( 64 * 64 * 4 ), 255, 64 * 64 * 4 ) ) );
img.name = img.filename = DEFAULT_IMAGE;
img.width = img.height = 64;
img.pixels = void_ptr( memset( safe_malloc( 64 * 64 * 4 ), 255, 64 * 64 * 4 ) );
images.emplace_front( std::move( img ) );
} }
} s_construct_default_image; } s_construct_default_image;
@ -271,7 +267,7 @@ static const image_t *ImageFind( const char *name ){
const image_t *ImageLoad( const char *name ){ const image_t *ImageLoad( const char *name ){
/* dummy check */ /* dummy check */
if ( strEmptyOrNull( name ) ) { if ( strEmptyOrNull( name ) ) {
return NULL; return nullptr;
} }
/* try to find existing image */ /* try to find existing image */
@ -280,19 +276,18 @@ const image_t *ImageLoad( const char *name ){
} }
/* none found, so let's create a new one */ /* none found, so let's create a new one */
image_t image; byte *pixels = nullptr;
int width, height;
char filename[ 1024 ]; char filename[ 1024 ];
int size; int size;
byte *buffer = NULL; byte *buffer = nullptr;
bool alphaHack = false; bool alphaHack = false;
image.name = name;
/* attempt to load tga */ /* attempt to load tga */
sprintf( filename, "%s.tga", name ); // StripExtension( name ); already sprintf( filename, "%s.tga", name ); // StripExtension( name ); already
size = vfsLoadFile( filename, (void**) &buffer, 0 ); size = vfsLoadFile( filename, (void**) &buffer, 0 );
if ( size > 0 ) { if ( size > 0 ) {
LoadTGABuffer( buffer, buffer + size, &image.pixels, &image.width, &image.height ); LoadTGABuffer( buffer, buffer + size, &pixels, &width, &height );
} }
else else
{ {
@ -300,7 +295,7 @@ const image_t *ImageLoad( const char *name ){
path_set_extension( filename, ".png" ); path_set_extension( filename, ".png" );
size = vfsLoadFile( filename, (void**) &buffer, 0 ); size = vfsLoadFile( filename, (void**) &buffer, 0 );
if ( size > 0 ) { if ( size > 0 ) {
LoadPNGBuffer( buffer, size, &image.pixels, &image.width, &image.height ); LoadPNGBuffer( buffer, size, &pixels, &width, &height );
} }
else else
{ {
@ -308,10 +303,10 @@ const image_t *ImageLoad( const char *name ){
path_set_extension( filename, ".jpg" ); path_set_extension( filename, ".jpg" );
size = vfsLoadFile( filename, (void**) &buffer, 0 ); size = vfsLoadFile( filename, (void**) &buffer, 0 );
if ( size > 0 ) { if ( size > 0 ) {
if ( LoadJPGBuff( buffer, size, &image.pixels, &image.width, &image.height ) == -1 && image.pixels != NULL ) { if ( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 && pixels != nullptr ) {
// On error, LoadJPGBuff might store a pointer to the error message in image.pixels // On error, LoadJPGBuff might store a pointer to the error message in pixels
Sys_Warning( "LoadJPGBuff %s %s\n", filename, (unsigned char*) image.pixels ); Sys_Warning( "LoadJPGBuff %s %s\n", filename, (unsigned char*) pixels );
image.pixels = NULL; pixels = nullptr;
} }
alphaHack = true; alphaHack = true;
} }
@ -321,17 +316,17 @@ const image_t *ImageLoad( const char *name ){
path_set_extension( filename, ".dds" ); path_set_extension( filename, ".dds" );
size = vfsLoadFile( filename, (void**) &buffer, 0 ); size = vfsLoadFile( filename, (void**) &buffer, 0 );
if ( size > 0 ) { if ( size > 0 ) {
LoadDDSBuffer( buffer, size, &image.pixels, &image.width, &image.height ); LoadDDSBuffer( buffer, size, &pixels, &width, &height );
/* debug code */ /* debug code */
#if 0 #if 0
{ {
ddsPF_t pf; ddsPF_t pf;
DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf ); DDSGetInfo( (ddsBuffer_t*) buffer, nullptr, nullptr, &pf );
Sys_Printf( "pf = %d\n", pf ); Sys_Printf( "pf = %d\n", pf );
if ( image.width > 0 ) { if ( width > 0 ) {
path_set_extension( filename, "_converted.tga" ); path_set_extension( filename, "_converted.tga" );
WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image.pixels, image.width, image.height ); WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", pixels, width, height );
} }
} }
#endif #endif
@ -342,7 +337,7 @@ const image_t *ImageLoad( const char *name ){
path_set_extension( filename, ".ktx" ); path_set_extension( filename, ".ktx" );
size = vfsLoadFile( filename, (void**) &buffer, 0 ); size = vfsLoadFile( filename, (void**) &buffer, 0 );
if ( size > 0 ) { if ( size > 0 ) {
LoadKTXBufferFirstImage( buffer, size, &image.pixels, &image.width, &image.height ); LoadKTXBufferFirstImage( buffer, size, &pixels, &width, &height );
} }
} }
} }
@ -353,22 +348,19 @@ const image_t *ImageLoad( const char *name ){
free( buffer ); free( buffer );
/* make sure everything's kosher */ /* make sure everything's kosher */
if ( size <= 0 || image.width <= 0 || image.height <= 0 || image.pixels == NULL ) { if ( size <= 0 || width <= 0 || height <= 0 || pixels == nullptr ) {
//% Sys_Printf( "size = %d width = %d height = %d pixels = 0x%08x (%s)\n", //% Sys_Printf( "size = %d width = %d height = %d pixels = 0x%08x (%s)\n",
//% size, image.width, image.height, image.pixels, filename ); //% size, width, height, pixels, filename );
image.pixels = NULL; return nullptr;
return NULL;
} }
/* set filename */ /* everybody's in the place, create new image */
image.filename = filename; image_t& image = *images.emplace_after( images.cbegin(), name, filename, width, height, pixels );
if ( alphaHack ) { if ( alphaHack ) {
path_set_extension( filename, "_alpha.jpg" ); path_set_extension( filename, "_alpha.jpg" );
size = vfsLoadFile( (const char*) filename, (void**) &buffer, 0 ); size = vfsLoadFile( (const char*) filename, (void**) &buffer, 0 );
if ( size > 0 ) { if ( size > 0 ) {
unsigned char *pixels;
int width, height;
if ( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 ) { if ( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 ) {
if ( pixels ) { if ( pixels ) {
// On error, LoadJPGBuff might store a pointer to the error message in pixels // On error, LoadJPGBuff might store a pointer to the error message in pixels
@ -385,6 +377,6 @@ const image_t *ImageLoad( const char *name ){
} }
} }
/* cache and return the image */ /* return the image */
return &( *images.insert_after( images.cbegin(), std::move( image ) ) ); return &image;
} }

View File

@ -418,6 +418,13 @@ struct image_t
byte *pixels = nullptr; byte *pixels = nullptr;
image_t() = default; image_t() = default;
image_t( const char *name, const char *filename, int width, int height, byte *pixels ) :
name( name ),
filename( filename ),
width( width ),
height(height ),
pixels( pixels )
{}
image_t( const image_t& ) = delete; image_t( const image_t& ) = delete;
image_t( image_t&& other ) noexcept : image_t( image_t&& other ) noexcept :
name( std::move( other.name ) ), name( std::move( other.name ) ),