photopea / UPNG.js

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

PNG Encoding sometimes fails #24

Closed friksa closed 6 years ago

friksa commented 6 years ago

PNG encoding sometimes fails... This is an example that frequently fails: var bin = UPNG.encode([newData.data], w, h, 0);

Using 256 colors instead of 0 (unlimited) mostly works, but not always.

This is an example that failed with 0 (unlimited colors) - resulting PNG at: https://x1.gatherworks.com:443/doc/572BD2A2CCA63DBA5EB3C21DE115908B/ffa002be102547399238a7af4a8b3e6f.png

Attaching a screen shot of the application window that was PNG encoded. 2018-04-03_04-12-30

I am not sure what the best way is to give you a recreatable example since it depends on the contents of the buffer... but it frequently happens when using 0 (unlimited colors). Encoding mostly works when using this instead (but not always): var bin = UPNG.encode([newData.data], w, h, 0, null, true);

photopea commented 6 years ago

Hi, can you try to send an ArrayBuffer as a frame? That would be newData.data.buffer in your case.

friksa commented 6 years ago

This buffer is 160x46 buffer.zip

photopea commented 6 years ago

Did you try to give it an ArrayBuffer instead of ArrayBufferView?

friksa commented 6 years ago

Not sure I understand the question. This is how I get the buffer. var newData = context.getImageData(0, 0, w, h);

photopea commented 6 years ago

Then, you need to call

var bin = UPNG.encode([newData.data.buffer], w, h, 0);
friksa commented 6 years ago

You called it. After testing, it seems that was the issue. Curious that it worked pretty well most of the time for non 0 color values.

It might help others too by documenting how to take content from a canvas and encode it to PNG using this library.

Thanks again for a great library!!

photopea commented 6 years ago

Ok thank you for the donation! btw. you are the first person to donate to UPNG.js :)

friksa commented 6 years ago

That's a bummer since this is such a great library. At least you know donations work now :)

You're welcome.