mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Merge pull request #7283 from Robyt3/ImageLoader-Indexed-Fix
Fix images using indexed color palette not being loadable
This commit is contained in:
commit
f5f57ee24c
|
@ -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)
|
||||
return 1;
|
||||
else if(LibPNGColorType == PNG_COLOR_TYPE_PALETTE || LibPNGColorType == PNG_COLOR_TYPE_RGB)
|
||||
return 3;
|
||||
else if(LibPNGColorType == PNG_COLOR_TYPE_RGBA)
|
||||
return 4;
|
||||
|
||||
return 4;
|
||||
}
|
||||
|
||||
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;
|
||||
switch(ColorChannelCount)
|
||||
{
|
||||
case 1:
|
||||
return IMAGE_FORMAT_R;
|
||||
case 3:
|
||||
return IMAGE_FORMAT_RGB;
|
||||
case 4:
|
||||
return IMAGE_FORMAT_RGBA;
|
||||
default:
|
||||
return IMAGE_FORMAT_RGBA;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
const int ColorChannelCount = LibPNGGetColorChannelCount(ColorType);
|
||||
const int ColorChannelCount = png_get_channels(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];
|
||||
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);
|
||||
pRowPointers[y] = new png_byte[BytesInRow];
|
||||
}
|
||||
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);
|
||||
dbg_msg("png", "bytes in row incorrect.");
|
||||
dbg_msg("png", "byte loader error.");
|
||||
return false;
|
||||
}
|
||||
|
||||
ImageFormat = LibPNGGetImageFormat(ColorChannelCount);
|
||||
|
||||
png_destroy_info_struct(pPNGStruct, &pPNGInfo);
|
||||
png_destroy_read_struct(&pPNGStruct, nullptr, nullptr);
|
||||
|
||||
|
|
Loading…
Reference in a new issue