photopea / UPNG.js

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

Encode function should accept an explicit palette for writing ctype = 3 PNGs #31

Closed pineapplemachine closed 6 years ago

pineapplemachine commented 6 years ago

I have some pixel data read from another file which I would like to export as a PNG. Since the images already use a 256-color palette, it would be convenient if I could give UPNG the palette and all of the pixels as indexes in that palette.

photopea commented 6 years ago

Hi, we wanted users to avoid such low-level work with images. I recommend to do following steps:

Convert your image to RGBA8 format (4 bytes per pixel). Then, use UPNG.encode() with cnum=0 (lossless compression).

UPNG uses palettes whenever possible, to save space. So it will store the image with a palette (ctype=3). Since you asked for lossless, the palette will be identical to yours. Only the colors within a palette may be reordered.

pineapplemachine commented 6 years ago

I am aware of this option, but it requires an unnecessary quantize step.

photopea commented 6 years ago

There is no quantization inside UPNG during the lossless mode. It just finds unique colors again, and puts them into palette. It should not take more than 3 milliseconds of time and 4 MB of RAM for a 1Mpx image.

You can also prepare the input for UPNG.encode._main, but it is not documented anywhere.