3bl3gamer / wasm-mozjpeg

WebAssembly MozJPEG encoder
MIT License
2 stars 0 forks source link

Alternatives to working from canvas? #4

Open tomdav999 opened 3 years ago

tomdav999 commented 3 years ago

Hi, question as the 2 usage examples (basic and advanced) both work from canvas. I am told when jpeg is converted to canvas there is some information loss (apparently RGBA is converted to linear), see here:

https://github.com/nodeca/image-blob-reduce/issues/25

If so, should canvas conversion be avoided (unless we actually need to use canvas to resize or perform related canvas operations)? Or will it not matter if e.g. mozjpeg effectively converts to linear RGBA anyway?

Edit: it looks like the settings demo doesn't work from canvas. Perhaps there should be a usage example for non-canvas in the readme?

https://github.com/3bl3gamer/wasm-mozjpeg/blob/master/examples/settings/index.js

3bl3gamer commented 3 years ago

Hello. Canvas was used as a simple source of RGBA data and is not required. If you have pixels data in some other format (and this format is supported by MozJPEG), you can use it directly without conversion. Updated README with such demonstration: https://github.com/3bl3gamer/wasm-mozjpeg#color-spaces

But, as puzrin answered, pica and image-blob-reduce decode image by drawing it on canvas, and data is converted to RGB[A]. So there will some be loss in colors anyway. Though loss may be too small and unimportant (or not) depending on your use case.

I'm not expert in colors and compression methods and I'm not sure what exactly did puzrin mean by non-linear JPEG and linear Canvas. As I have checked, RGB buffer filled with [32,32,32], compressed to JPEG and drawn on canvas results in [32,32,32] RGB data. Same for other gray values (1, 2, 128, 192, 254, ...) which seems linear. But! For [254, 254, 253] output will be [254, 254, 252] even with max JPEG quality since there is no exact integer mapping between RGB and YCbCr (JPEG's default).

To sum up: