mozilla / mozjpeg

Improved JPEG encoder.
Other
5.48k stars 415 forks source link

Gamma-correct chroma subsampling #193

Open kornelski opened 8 years ago

kornelski commented 8 years ago

This image (source) should show RULES on an sRGB-ish monitor and/or browser with a good color management.

and if it's resized with appropriate gamma should resize to:

gamma 2.2

However, libjpeg blends pixels in Cr/Cb color space, which has non-linear gamma, and chroma subsampling results in:

bad gamma

It could be better. I've prototyped a very slow and hacky chroma downsampling that generates:

gamma-1 0-or-2 2-red-trianglefilter

so there's definitely a room for improvement.

kornelski commented 8 years ago

Coincidentally the next version of WebP will solve that too, by using an iterative algorithm:

https://chromium.googlesource.com/webm/libwebp/+/master/src/enc/picture_csp.c#487

kornelski commented 8 years ago

Prototyped https://github.com/pornel/jpeg-compressor#luma-from-chroma-subsampling

CoolOppo commented 8 years ago

That prototype is pretty impressive. It's far better than the current subsampling.

Edit: I made a little comparison slider. The image on the right is the prototype:

https://visionless.me/u/subsample-comparison.html

Artoria2e5 commented 4 years ago

The webp source code link moved to https://github.com/webmproject/libwebp/blob/master/src/enc/picture_csp_enc.c. Search PreprocessARGB for the iterative code. As Ser-Gen memntioned, sjpeg calls this scaling "sharp YUV". https://github.com/webmproject/sjpeg/blob/5ce9c14cceba85a83ac6efcef94658eb28e304ea/src/yuv_convert.cc#L101

The luma-scaled code is at https://github.com/kornelski/jpeg-compressor/blob/9eeb009fdb8673224de5be978574cb139e4b8bf6/jpge.cpp#L611.


sharp YUV is now a separate libsharpyuv in the webp project, with its own pkg-config file and everything. It is an optional choice in AVIF.

Artoria2e5 commented 1 year ago

Huh, apparently there's yet another approach , the HDRTools way. Pic is from doi:10.5594/m001639. No idea how well it works in SDR, when "OETF" is just "gamma". image

Oh god a google for "chroma subsampling" gave me some arxiv magic (possible published version) involving gradient descent in H.266. oh it's friendlier when you say "iterative" and it lists some prior work too. code's gone