Merge pull request #7283 from Robyt3/ImageLoader-Indexed-Fix

Fix images using indexed color palette not being loadable
This commit is contained in:
Edgar 2023-10-04 13:55:21 +00:00 committed by GitHub
commit f5f57ee24c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -53,27 +53,19 @@ static void ReadDataFromLoadedBytes(png_structp pPNGStruct, png_bytep pOutBytes,
} }
} }
static int LibPNGGetColorChannelCount(int LibPNGColorType) static EImageFormat LibPNGGetImageFormat(int ColorChannelCount)
{ {
if(LibPNGColorType == PNG_COLOR_TYPE_GRAY) switch(ColorChannelCount)
return 1; {
else if(LibPNGColorType == PNG_COLOR_TYPE_PALETTE || LibPNGColorType == PNG_COLOR_TYPE_RGB) case 1:
return 3; return IMAGE_FORMAT_R;
else if(LibPNGColorType == PNG_COLOR_TYPE_RGBA) case 3:
return 4; return IMAGE_FORMAT_RGB;
case 4:
return 4; return IMAGE_FORMAT_RGBA;
} default:
return IMAGE_FORMAT_RGBA;
static void LibPNGSetImageFormat(EImageFormat &ImageFormat, int LibPNGColorType) }
{
ImageFormat = IMAGE_FORMAT_RGBA;
if(LibPNGColorType == PNG_COLOR_TYPE_GRAY)
ImageFormat = IMAGE_FORMAT_R;
else if(LibPNGColorType == PNG_COLOR_TYPE_PALETTE || LibPNGColorType == PNG_COLOR_TYPE_RGB)
ImageFormat = IMAGE_FORMAT_RGB;
else if(LibPNGColorType == PNG_COLOR_TYPE_RGBA)
ImageFormat = IMAGE_FORMAT_RGBA;
} }
static void LibPNGDeleteReadStruct(png_structp pPNGStruct, png_infop pPNGInfo) static void LibPNGDeleteReadStruct(png_structp pPNGStruct, png_infop pPNGInfo)
@ -225,47 +217,39 @@ bool LoadPNG(SImageByteBuffer &ByteLoader, const char *pFileName, int &PngliteIn
png_read_update_info(pPNGStruct, pPNGInfo); png_read_update_info(pPNGStruct, pPNGInfo);
const int ColorChannelCount = LibPNGGetColorChannelCount(ColorType); const int ColorChannelCount = png_get_channels(pPNGStruct, pPNGInfo);
const int BytesInRow = png_get_rowbytes(pPNGStruct, pPNGInfo); const int BytesInRow = png_get_rowbytes(pPNGStruct, pPNGInfo);
dbg_assert(BytesInRow == Width * ColorChannelCount, "bytes in row incorrect.");
if(BytesInRow == Width * ColorChannelCount) pRowPointers = new png_bytep[Height];
for(int y = 0; y < Height; ++y)
{ {
pRowPointers = new png_bytep[Height]; pRowPointers[y] = new png_byte[BytesInRow];
for(int y = 0; y < Height; ++y)
{
pRowPointers[y] = new png_byte[BytesInRow];
}
png_read_image(pPNGStruct, pRowPointers);
if(ByteLoader.m_Err == 0)
pImageBuff = (uint8_t *)malloc((size_t)Height * (size_t)Width * (size_t)ColorChannelCount * sizeof(uint8_t));
for(int i = 0; i < Height; ++i)
{
if(ByteLoader.m_Err == 0)
mem_copy(&pImageBuff[i * BytesInRow], pRowPointers[i], BytesInRow);
delete[] pRowPointers[i];
}
delete[] pRowPointers;
pRowPointers = nullptr;
if(ByteLoader.m_Err != 0)
{
LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo);
dbg_msg("png", "byte loader error.");
return false;
}
LibPNGSetImageFormat(ImageFormat, ColorType);
} }
else
png_read_image(pPNGStruct, pRowPointers);
if(ByteLoader.m_Err == 0)
pImageBuff = (uint8_t *)malloc((size_t)Height * (size_t)Width * (size_t)ColorChannelCount * sizeof(uint8_t));
for(int i = 0; i < Height; ++i)
{
if(ByteLoader.m_Err == 0)
mem_copy(&pImageBuff[i * BytesInRow], pRowPointers[i], BytesInRow);
delete[] pRowPointers[i];
}
delete[] pRowPointers;
pRowPointers = nullptr;
if(ByteLoader.m_Err != 0)
{ {
LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo); LibPNGDeleteReadStruct(pPNGStruct, pPNGInfo);
dbg_msg("png", "bytes in row incorrect."); dbg_msg("png", "byte loader error.");
return false; return false;
} }
ImageFormat = LibPNGGetImageFormat(ColorChannelCount);
png_destroy_info_struct(pPNGStruct, &pPNGInfo); png_destroy_info_struct(pPNGStruct, &pPNGInfo);
png_destroy_read_struct(&pPNGStruct, nullptr, nullptr); png_destroy_read_struct(&pPNGStruct, nullptr, nullptr);