ImageOptim / mozjpeg-rust

Safe Rust wrapper for the MozJPEG library
https://lib.rs/mozjpeg
Other
74 stars 19 forks source link

Ability to write icc profile #38

Closed SalOne22 closed 7 months ago

SalOne22 commented 7 months ago

I found that the mozjpeg library has the ability to write the icc profile to the encoded image, but neither mozjpeg-rust nor mozjpeg-sys provide this functionality. How can I achieve similar results using this wrapper or mozjpeg-sys crate?

Or is the better alternative to apply the icc profile using lcms2 or a similar tool?

kornelski commented 7 months ago

In JPEG ICC profiles are written as a series of app2 markers with some quirky syntax (https://gitlab.com/kornelski/load_image/-/blob/master/src/mozjpeg.rs?ref_type=heads#L19)

You should be able to do it via write_marker.

It would be nice to have a proper API for this in the high level wrapper, so PRs welcome.

If you don't really need a special color profile for wide gamut color, then it's best to convert pixels to sRGB and either not write any profile at all, or write a tiny sRGB one. This is because most JPEG decoders use 8-bit libjpeg API (despite JPEG supporting 12-bit-ish depth) and larger-than-sRGB profiles end up using fewer bits.

SalOne22 commented 7 months ago

Okay, it would be better if this library provided the ability to write an ICC profile. If I do everything right, I will create a new PR here.