photopea / UPNG.js

Fast and advanced PNG (APNG) decoder and encoder (lossy / lossless)
MIT License
2.1k stars 259 forks source link

Support zTXt chunks #28

Closed adam-snyder closed 4 years ago

adam-snyder commented 6 years ago

Support reading compressed data from the zTXt chunk

photopea commented 5 years ago

Could you show me a PNG file with such chunks? Are they frequent? Is there useful data in these chunks?

adam-snyder commented 4 years ago

These chunks are similar to tEXt but take advantage of compression. They would only be present for specific use cases. In our case, we were embedding per-pixel data as text content (JSON) and, because of the size of these PNGs, we needed to compress the chunks for performance. I can't really provide an example, but it is part of the spec: https://www.w3.org/TR/PNG-Chunks.html

photopea commented 4 years ago

I know what zTXt is and how it works. But I have never seen a PNG with such data. I don't like making my library 500 Bytes larger becaus of something, what is never used in a real world.

If you need to decode a PNG with zTXt chunk, could you share it with me? I am used to test an implementation, even if it is simple and seems to be correct.

hbs commented 4 years ago

I concur to the usefulness of supporting compressed text chunks, not only zTXt chunks but also iTXt chunks with compressionFlag set to true.

photopea commented 4 years ago

As I said, I have never seen a PNG with a zTXt and iTXt chung. So I think nobody uses it. Do you have such PNGs?

hbs commented 4 years ago

Many libraries allow to set the compressionFlag to true when generating iTXt chunks, so such PNGs can easily be generated. If you do not want to add decompression you should at least consider adding a flag in the case of iTXt so the user can decide to deflate compressed content. Or maybe use an ArrayBuffer as the value instead of a string.

photopea commented 4 years ago

Do you need to create PNGs with these chunks with UPNG.js , or to parse them?

I would be happy to implement it, but could you share such PNG with me? I do not use native apps other than a browser and a notepad (I develop in Javascript).

hbs commented 4 years ago

Hi, my need is to be able to read compressed iTXt chunks using UPNG.js. Here is a data URL for a 1x1 PNG with an iTXt chunk with keyword 'UPNG.js' and a zlib compressed text associated with it:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAUmlUWHRVUE5HLmpzAAEAAAB4nAvJyCxWAKLi/NxUheT83IKi1OLi1BSFktSKEoXE4uL85MzEEiC/PLMkQ6EkI1UhNMDPXS+rWCE7tbI8vyhFDwAcphfYZIu9eAAAAAtJREFUeNpjYAACAAAFAAHp+tzYAAAAAElFTkSuQmCC

photopea commented 4 years ago

It is implemented now :)

hbs commented 4 years ago

Awesome!

photopea commented 4 years ago

thanks for sharing a PNG with us.

Although, I still doubt anybody will use this chunk. It is more convenient to store metadata in exif or xmp chunks.

hbs commented 4 years ago

Supporting zTXt chunks should be as easy then, they are basically tEXt chunks except the text is deflated see https://www.w3.org/TR/PNG-Chunks.html

hbs commented 4 years ago

XMP data is stored in iTXt chunks when taking a snapshot on MacOS, depending on data length maybe it may be compressed.

photopea commented 4 years ago

I would like to make UPNG.js for files, which are made in practice. There exist e.g. a lossless JPG format, which was defined together with a lossy JPG, but is not supported by any decoder, as nobody ever used it. Are you sure zTXt are used in practice?