Merge pull request #7123 from Robyt3/ImageLoader-Refactoring

Minor refactoring of the image loader
This commit is contained in:
Dennis Felsing 2023-09-02 20:00:14 +00:00 committed by GitHub
commit a17798aaa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,14 +17,14 @@ struct SLibPNGWarningItem
{ {
SLibPNGWarningItem *pUserStruct = (SLibPNGWarningItem *)png_get_error_ptr(png_ptr); SLibPNGWarningItem *pUserStruct = (SLibPNGWarningItem *)png_get_error_ptr(png_ptr);
pUserStruct->m_pByteLoader->m_Err = -1; pUserStruct->m_pByteLoader->m_Err = -1;
dbg_msg("libpng", "error for file \"%s\": %s", pUserStruct->m_pFileName, error_msg); dbg_msg("png", "error for file \"%s\": %s", pUserStruct->m_pFileName, error_msg);
std::longjmp(pUserStruct->m_Buf, 1); std::longjmp(pUserStruct->m_Buf, 1);
} }
static void LibPNGWarning(png_structp png_ptr, png_const_charp warning_msg) static void LibPNGWarning(png_structp png_ptr, png_const_charp warning_msg)
{ {
SLibPNGWarningItem *pUserStruct = (SLibPNGWarningItem *)png_get_error_ptr(png_ptr); SLibPNGWarningItem *pUserStruct = (SLibPNGWarningItem *)png_get_error_ptr(png_ptr);
dbg_msg("libpng", "warning for file \"%s\": %s", pUserStruct->m_pFileName, warning_msg); dbg_msg("png", "warning for file \"%s\": %s", pUserStruct->m_pFileName, warning_msg);
} }
static bool FileMatchesImageType(SImageByteBuffer &ByteLoader) static bool FileMatchesImageType(SImageByteBuffer &ByteLoader)
@ -80,7 +80,7 @@ static void LibPNGDeleteReadStruct(png_structp pPNGStruct, png_infop pPNGInfo)
{ {
if(pPNGInfo != nullptr) if(pPNGInfo != nullptr)
png_destroy_info_struct(pPNGStruct, &pPNGInfo); png_destroy_info_struct(pPNGStruct, &pPNGInfo);
png_destroy_read_struct(&pPNGStruct, NULL, NULL); png_destroy_read_struct(&pPNGStruct, nullptr, nullptr);
} }
static int PngliteIncompatibility(png_structp pPNGStruct, png_infop pPNGInfo) static int PngliteIncompatibility(png_structp pPNGStruct, png_infop pPNGInfo)
@ -131,23 +131,19 @@ static int PngliteIncompatibility(png_structp pPNGStruct, png_infop pPNGInfo)
bool LoadPNG(SImageByteBuffer &ByteLoader, const char *pFileName, int &PngliteIncompatible, int &Width, int &Height, uint8_t *&pImageBuff, EImageFormat &ImageFormat) bool LoadPNG(SImageByteBuffer &ByteLoader, const char *pFileName, int &PngliteIncompatible, int &Width, int &Height, uint8_t *&pImageBuff, EImageFormat &ImageFormat)
{ {
png_infop pPNGInfo = nullptr;
int ColorType;
png_byte BitDepth;
int ColorChannelCount;
int BytesInRow;
Height = 0;
png_bytepp pRowPointers = nullptr;
SLibPNGWarningItem UserErrorStruct = {&ByteLoader, pFileName, {}}; SLibPNGWarningItem UserErrorStruct = {&ByteLoader, pFileName, {}};
png_structp pPNGStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_structp pPNGStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if(pPNGStruct == NULL) if(pPNGStruct == nullptr)
{ {
dbg_msg("png", "libpng internal failure: png_create_read_struct failed."); dbg_msg("png", "libpng internal failure: png_create_read_struct failed.");
return false; return false;
} }
png_infop pPNGInfo = nullptr;
png_bytepp pRowPointers = nullptr;
Height = 0; // ensure this is not undefined for the error handler
if(setjmp(UserErrorStruct.m_Buf)) if(setjmp(UserErrorStruct.m_Buf))
{ {
if(pRowPointers != nullptr) if(pRowPointers != nullptr)
@ -165,9 +161,9 @@ bool LoadPNG(SImageByteBuffer &ByteLoader, const char *pFileName, int &PngliteIn
pPNGInfo = png_create_info_struct(pPNGStruct); pPNGInfo = png_create_info_struct(pPNGStruct);
if(pPNGInfo == NULL) if(pPNGInfo == nullptr)
{ {
png_destroy_read_struct(&pPNGStruct, NULL, NULL); png_destroy_read_struct(&pPNGStruct, nullptr, nullptr);
dbg_msg("png", "libpng internal failure: png_create_info_struct failed."); dbg_msg("png", "libpng internal failure: png_create_info_struct failed.");
return false; return false;
} }
@ -175,6 +171,7 @@ bool LoadPNG(SImageByteBuffer &ByteLoader, const char *pFileName, int &PngliteIn
if(!FileMatchesImageType(ByteLoader)) if(!FileMatchesImageType(ByteLoader))
{ {
LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo); LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo);
dbg_msg("png", "file does not match image type.");
return false; return false;
} }
@ -189,13 +186,14 @@ bool LoadPNG(SImageByteBuffer &ByteLoader, const char *pFileName, int &PngliteIn
if(ByteLoader.m_Err != 0) if(ByteLoader.m_Err != 0)
{ {
LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo); LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo);
dbg_msg("png", "byte loader error.");
return false; return false;
} }
Width = png_get_image_width(pPNGStruct, pPNGInfo); Width = png_get_image_width(pPNGStruct, pPNGInfo);
Height = png_get_image_height(pPNGStruct, pPNGInfo); Height = png_get_image_height(pPNGStruct, pPNGInfo);
ColorType = png_get_color_type(pPNGStruct, pPNGInfo); const int ColorType = png_get_color_type(pPNGStruct, pPNGInfo);
BitDepth = png_get_bit_depth(pPNGStruct, pPNGInfo); const png_byte BitDepth = png_get_bit_depth(pPNGStruct, pPNGInfo);
PngliteIncompatible = PngliteIncompatibility(pPNGStruct, pPNGInfo); PngliteIncompatible = PngliteIncompatibility(pPNGStruct, pPNGInfo);
if(BitDepth == 16) if(BitDepth == 16)
@ -227,8 +225,8 @@ bool LoadPNG(SImageByteBuffer &ByteLoader, const char *pFileName, int &PngliteIn
png_read_update_info(pPNGStruct, pPNGInfo); png_read_update_info(pPNGStruct, pPNGInfo);
ColorChannelCount = LibPNGGetColorChannelCount(ColorType); const int ColorChannelCount = LibPNGGetColorChannelCount(ColorType);
BytesInRow = png_get_rowbytes(pPNGStruct, pPNGInfo); const int BytesInRow = png_get_rowbytes(pPNGStruct, pPNGInfo);
if(BytesInRow == Width * ColorChannelCount) if(BytesInRow == Width * ColorChannelCount)
{ {
@ -255,6 +253,7 @@ bool LoadPNG(SImageByteBuffer &ByteLoader, const char *pFileName, int &PngliteIn
if(ByteLoader.m_Err != 0) if(ByteLoader.m_Err != 0)
{ {
LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo); LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo);
dbg_msg("png", "byte loader error.");
return false; return false;
} }
@ -263,11 +262,12 @@ bool LoadPNG(SImageByteBuffer &ByteLoader, const char *pFileName, int &PngliteIn
else else
{ {
LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo); LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo);
dbg_msg("png", "bytes in row incorrect.");
return false; return false;
} }
png_destroy_info_struct(pPNGStruct, &pPNGInfo); png_destroy_info_struct(pPNGStruct, &pPNGInfo);
png_destroy_read_struct(&pPNGStruct, NULL, NULL); png_destroy_read_struct(&pPNGStruct, nullptr, nullptr);
return true; return true;
} }
@ -304,9 +304,9 @@ static int ImageLoaderHelperFormatToColorChannel(EImageFormat Format)
bool SavePNG(EImageFormat ImageFormat, const uint8_t *pRawBuffer, SImageByteBuffer &WrittenBytes, int Width, int Height) bool SavePNG(EImageFormat ImageFormat, const uint8_t *pRawBuffer, SImageByteBuffer &WrittenBytes, int Width, int Height)
{ {
png_structp pPNGStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_structp pPNGStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if(pPNGStruct == NULL) if(pPNGStruct == nullptr)
{ {
dbg_msg("png", "libpng internal failure: png_create_write_struct failed."); dbg_msg("png", "libpng internal failure: png_create_write_struct failed.");
return false; return false;
@ -314,9 +314,9 @@ bool SavePNG(EImageFormat ImageFormat, const uint8_t *pRawBuffer, SImageByteBuff
png_infop pPNGInfo = png_create_info_struct(pPNGStruct); png_infop pPNGInfo = png_create_info_struct(pPNGStruct);
if(pPNGInfo == NULL) if(pPNGInfo == nullptr)
{ {
png_destroy_read_struct(&pPNGStruct, NULL, NULL); png_destroy_read_struct(&pPNGStruct, nullptr, nullptr);
dbg_msg("png", "libpng internal failure: png_create_info_struct failed."); dbg_msg("png", "libpng internal failure: png_create_info_struct failed.");
return false; return false;
} }
@ -361,7 +361,7 @@ bool SavePNG(EImageFormat ImageFormat, const uint8_t *pRawBuffer, SImageByteBuff
delete[](pRowPointers); delete[](pRowPointers);
png_destroy_info_struct(pPNGStruct, &pPNGInfo); png_destroy_info_struct(pPNGStruct, &pPNGInfo);
png_destroy_write_struct(&pPNGStruct, NULL); png_destroy_write_struct(&pPNGStruct, nullptr);
return true; return true;
} }