Skip to content

Commit 4b3e194

Browse files
committed
[Specs] png: tRNS fix
1 parent 6f8dd2c commit 4b3e194

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

include/openminecraft/specs/png/om_png.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class OMPngFile : public OMBlockedFile<OMPngChunkType>, public OMImage
108108
std::shared_ptr<zlib::OMZLibInflater> inflater;
109109
OMPngHead head;
110110
std::vector<int, mem::OMStlAllocator<allocatorTag, int>> palette;
111+
uint16_t greyThreshold;
111112

112113
auto crc(OMPngChunk chunk) -> uint64_t;
113114
};

src/openminecraft-core/specs/media/png/om_png.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ OMPngFile::OMPngFile()
2828
processorMap[ImageEnd] = [&](std::shared_ptr<std::istream> istr) -> void {};
2929
}
3030

31-
static void writePixel(uint8_t *result, uint8_t *source, OMPngColorType type, uint8_t bitdepth, int x, int *palette)
31+
static void writePixel(uint8_t *result, uint8_t *source, OMPngColorType type, uint8_t bitdepth, int x, int *palette,
32+
uint16_t greyThreshold)
3233
{
3334
switch (type)
3435
{
@@ -77,7 +78,7 @@ static void writePixel(uint8_t *result, uint8_t *source, OMPngColorType type, ui
7778
if (bitdepth == 8)
7879
{
7980
std::memset(result, source[x * bitdepth / 8], 3);
80-
result[3] = 0xff;
81+
result[3] = result[0] == greyThreshold ? 0x00 : 0xff;
8182
}
8283
else
8384
{
@@ -90,7 +91,7 @@ static void writePixel(uint8_t *result, uint8_t *source, OMPngColorType type, ui
9091
value = static_cast<uint8_t>((value * 255) / ((1 << bitdepth) - 1));
9192

9293
std::memset(result, value, 3);
93-
result[3] = 0xff;
94+
result[3] = value == greyThreshold ? 0x00 : 0xff;
9495
}
9596
break;
9697
default:
@@ -153,6 +154,8 @@ auto OMPngFile::parseBlockHeader(std::shared_ptr<std::istream> istr, OMPngChunkT
153154
istr->read(reinterpret_cast<char *>(&currentChunk.crc), 4);
154155
currentChunk.crc = binary::be32ToNative(currentChunk.crc);
155156

157+
std::cout << "[PNG] " << currentChunk.name.data() << " " << length << std::endl;
158+
156159
auto res = crc(currentChunk);
157160
if (res != currentChunk.crc)
158161
{
@@ -216,6 +219,10 @@ void OMPngFile::parsePTLE(std::shared_ptr<std::istream> istr)
216219

217220
void OMPngFile::parseTRNS(std::shared_ptr<std::istream> istr)
218221
{
222+
if (palette.empty())
223+
{
224+
greyThreshold = currentChunk.data[0] << 16 | currentChunk.data[1];
225+
}
219226
int i = 0;
220227
for (auto &pp : palette)
221228
{
@@ -354,7 +361,7 @@ void OMPngFile::writeIntoBufferAdam(std::vector<uint8_t, mem::OMStlAllocator<all
354361
int pixelIndex = finalY * head.width + finalX;
355362

356363
writePixel(dataBuffer.data() + pixelIndex * 4, current.data(), head.type, head.bitDepth, x,
357-
palette.data());
364+
palette.data(), greyThreshold);
358365
}
359366
}
360367
}
@@ -364,7 +371,7 @@ void OMPngFile::writeIntoBuffer(std::vector<uint8_t, mem::OMStlAllocator<allocat
364371
for (int x = 0; x < head.width; x++)
365372
{
366373
std::array<uint8_t, 4> buffer;
367-
writePixel(buffer.data(), current.data(), head.type, head.bitDepth, x, palette.data());
374+
writePixel(buffer.data(), current.data(), head.type, head.bitDepth, x, palette.data(), greyThreshold);
368375
dataBuffer.push_back(buffer[0]);
369376
dataBuffer.push_back(buffer[1]);
370377
dataBuffer.push_back(buffer[2]);

src/openminecraft-core/vfs/om_vfs_base.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ auto compressPath(std::string vp) -> std::string
142142
}
143143
auto fsfetch(std::string fullPath) -> std::shared_ptr<std::istream>
144144
{
145+
logger.debug("fetching {}", fullPath);
145146
auto pth = compressPath(fullPath);
146147
for (auto p : m)
147148
{

0 commit comments

Comments
 (0)