lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.03k stars 420 forks source link

Support for multiple IDAT chunks ? #153

Open Raphael-Boichot opened 2 years ago

Raphael-Boichot commented 2 years ago

Hello.

I'm trying to embed lodepng in this project: https://github.com/zenaro147/NeoGB-Printer It's working great apart that the ESP32 I'm using have a very limited memory, so that I can convert very small images only (typically 160*144 pixels, 2bbp).

I've tried to cut bigger images into small parts to pass them to lodepng encode function and get the IDAT packets to reconstitute a "franken" png. I discovered that it is not allowed by the PNG format where several IDAT chunks must originate from the same unique compressed stream. So the image I got with this method shows only the first IDAT chunks and black pixels elsewhere.

So my question: is there a way to use the lodepng functions to generate a stream of IDAT chunks from a unique stream of pixels (but without storing the whole stream in memory) so that I can pass big images line by line and get each IDAT chunk from a single compressed stream ? I can then make the other chunks without any difficulty to reconstitute my image.

Thanks by advance for your response and amazing library !

Raphael.

lvandeve commented 2 years ago

That does look difficult since streaming functionality is not added to the library. Indeed, if you have multiple IDAT chunks in a single PNG, there is a zlib header in the first one only, and other interdependencies between the IDAT chunks. Unfortunately I can't think of any simple hack using the current functionality, not even something like disabling compression, since creating the IDAT chunks yourself would require rewriting quite a lot of the PNG functionality. This currently is most suited for a library that supports streaming.

Raphael-Boichot commented 2 years ago

Thanks for your response. I finally came to thank conclusion too. I'm now trying to adapt this project for ESP32: https://github.com/bitbank2/PNGenc It seems to work with some quirks but I know how to correct them.

Raphael