@@ -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 *>(¤tChunk.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
217220void 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 ]);
0 commit comments