- use stb-image to handle PNGs with 16 bit color channels.

It is still not recommended to use this format - images tend to be huge and setup time is normally prohibitive - but at least they work now.
This commit is contained in:
Christoph Oelckers 2021-05-19 19:20:51 +02:00
parent 8bb36bd479
commit a3a65f184f
2 changed files with 44 additions and 8 deletions

View file

@ -74,6 +74,7 @@ protected:
uint32_t StartOfPalette = 0;
};
FImageSource* StbImage_TryCreate(FileReader& file, int lumpnum);
//==========================================================================
//
@ -127,6 +128,46 @@ FImageSource *PNGImage_TryCreate(FileReader & data, int lumpnum)
}
if (!((1 << bitdepth) & 0x116))
{
// Try STBImage for 16 bit PNGs.
auto tex = StbImage_TryCreate(data, lumpnum);
if (tex)
{
// STBImage does not handle grAb, so do that here and insert the data into the texture.
data.Seek(33, FileReader::SeekSet);
int len = data.ReadInt32BE();
int id = data.ReadInt32();
while (id != MAKE_ID('I', 'D', 'A', 'T') && id != MAKE_ID('I', 'E', 'N', 'D'))
{
if (id != MAKE_ID('g', 'r', 'A', 'b'))
{
data.Seek(len, FileReader::SeekCur);
}
else
{
int ihotx = data.ReadInt32BE();
int ihoty = data.ReadInt32BE();
if (ihotx < -32768 || ihotx > 32767)
{
Printf("X-Offset for PNG texture %s is bad: %d (0x%08x)\n", fileSystem.GetFileFullName(lumpnum), ihotx, ihotx);
ihotx = 0;
}
if (ihoty < -32768 || ihoty > 32767)
{
Printf("Y-Offset for PNG texture %s is bad: %d (0x%08x)\n", fileSystem.GetFileFullName(lumpnum), ihoty, ihoty);
ihoty = 0;
}
tex->SetOffsets(ihotx, ihoty);
}
data.Seek(4, FileReader::SeekCur); // Skip CRC
len = data.ReadInt32BE();
id = MAKE_ID('I', 'E', 'N', 'D');
id = data.ReadInt32();
}
return tex;
}
Printf(TEXTCOLOR_YELLOW"WARNING: failed to load PNG %s: the bit-depth (%u) is not supported!\n", fileSystem.GetFileFullName(lumpnum), bitdepth);
return NULL;
}
@ -185,13 +226,8 @@ FPNGTexture::FPNGTexture (FileReader &lump, int lumpnum, int width, int height,
case MAKE_ID('g','r','A','b'):
// This is like GRAB found in an ILBM, except coordinates use 4 bytes
{
uint32_t hotx, hoty;
int ihotx, ihoty;
lump.Read(&hotx, 4);
lump.Read(&hoty, 4);
ihotx = BigLong((int)hotx);
ihoty = BigLong((int)hoty);
int ihotx = lump.ReadInt32BE();
int ihoty = lump.ReadInt32BE();
if (ihotx < -32768 || ihotx > 32767)
{
Printf ("X-Offset for PNG texture %s is bad: %d (0x%08x)\n", fileSystem.GetFileFullName (lumpnum), ihotx, ihotx);

View file

@ -37,7 +37,7 @@
#define STBI_NO_STDIO
// Undefine formats we do not want to support here.
#define STBI_NO_JPEG
#define STBI_NO_PNG
//#define STBI_NO_PNG we need PNG for 16 bit channel images. Regular ones still use our own, more flexible decoder.
#define STBI_NO_TGA
#define STBI_NO_PSD
#define STBI_NO_HDR