Ogeon / palette

A Rust library for linear color calculations and conversion
Apache License 2.0
751 stars 60 forks source link

Support for ITU-R such as BT.709 #121

Open HeroicKatora opened 5 years ago

HeroicKatora commented 5 years ago

Requested features

It would be awesome to have support for the ITU-R Recommendations family of color representations. The primary ones of concern for today are BT.709, BT.2020 and BT.2100 for HD, 4k and high-dynamic range respectively.

Motivation

These are in use by video codes and other media alike, most prominently mpeg using the YCbCr encoding of BT.709. This would lay the ground work for implementing correct consumers and producers of these raw video codecs in rust. Additionally, it would improve the state of interoperability with external libraries such as ffmpeg without sacrificing performance (such as copying/converting image data with another ffi).

Implementation notes

All standards use the same whitepoint and sRGB and BT.709 even use the same primaries. The difference is a slightly changed transfer function and a quantization function for digital output data (8-bit or 10-bit). A different primary configuration is employed by the other two standards as well as a slightly more involved definition of the transfer function but still similar in effect. The same holds for the quantization.

Open questions

There standards are defined for various different bit depths. It is an open question on how this is best mapped to the api. Staying in floating point representation is not enough, as the quantization is not defined as a simple scaling.

References

https://en.wikipedia.org/wiki/Rec._709 http://www.itu.int/rec/R-REC-BT.709-6-201506-I/en https://en.wikipedia.org/wiki/Rec._2020 https://en.wikipedia.org/wiki/Rec._2100

Checklist

Ogeon commented 5 years ago

That sounds useful. Thanks for the detailed suggestion! I suppose the bit depths can be solved by having a more opaque version that doesn't expose individual channels as properties, but I'm not familiar enough with them to say for sure. If they are only meant for encoding they may not have to be as flexible as RGB, for example. They should at least be implemented as close to specification as possible.

HeroicKatora commented 5 years ago

I've started with 601 and 709 for now expecting 2020 to also be included. I'm not going to attempt Rec.2100 though since I'm not completely sure about the patent situation with regards to both transfer functions and encoded color spaces. Furthermore, it adds to the electro-optical transfer function and its inverse an additional optic-optical transfer function which can either appear before the encoding (applied to linear scene luminance), or after decoding (applied to intended display luminance) depending the chosen model. This seems a bit too complex for the first contribution :)

Ogeon commented 5 years ago

Excellent! And yes, take it in small steps. It's better to iterate than trying to swallow it whole if it's a large effort. Start with the simple ones. You can always submit an early PR if you want feedback!

Keavon commented 3 years ago

With the groundwork from this, could the ACES AP0 and AP1 color spaces be added in the future? I am also wondering about how the SDR vs. HDR color models work and what's involved in converting an sRGB to HDR RGB color space. I understand a key difference is basically whether it's luminance-relative or luminance-absolute, although I don't know how those play into things like ACES or BT.709 and BT.2020/2100 color spaces. Is any of what I mentioned out of scope for the project or could this all be added at some point in the future?

(For context, I'm asking in reference to work on an open source graphics editor project that should ideally support every color visible to the human eye and HDR color for editing things like EXRs.)

ACES chromaticity

Ogeon commented 3 years ago

That looks like a really cool project! I believe it all of what you mention should be possible to some extent. I haven't personally looked into all of the details, so I may miss some technical obstacle, but the current situation is this:

I'm unfortunately a bit out of my depth regarding HDR and this feature, since I don't have much insight into what's important when working with these formats. That's why much of it rests in the hands of contributors. That said, I'm happy to help fitting it into the rest of the library.