aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Tonemap.cppm
Go to the documentation of this file.
1
8module;
9#include <array>
10export module aest.colors:tonemap;
11
12import aest.blas;
13import :color;
14import :converter;
15import :gamut;
16
19{
24constexpr Matrix3 ACES_INPUT = {{
25 {0.59719, 0.35458, 0.04823},
26 {0.07600, 0.90834, 0.01566},
27 {0.02840, 0.13383, 0.83777},
28}};
29
34constexpr Matrix3 ACES_OUTPUT = {{
35 {1.60475, -0.53108, -0.07367},
36 {-0.10208, 1.10813, -0.00605},
37 {-0.00327, -0.07276, 1.07602},
38}};
39
45constexpr double rrt_and_odt_fit(const double value)
46{
47 const double numerator = value * (value + 0.0245786) - 0.000090537;
48 const double denominator = value * (0.983729 * value + 0.4329510) + 0.238081;
49 return numerator / denominator;
50}
51
64export template <LinearRGBSpace Color>
65constexpr Color reinhard(const Color& color)
66{
67 return Color{color.red() / (1 + color.red()), color.green() / (1 + color.green()),
68 color.blue() / (1 + color.blue()), color.alpha()};
69}
70
86export template <LinearRGBSpace Color>
87constexpr Color reinhard_extended(const Color& color, const float white)
88{
89 const float whiteSquared = white * white;
90 const auto map = [whiteSquared](const float channel) -> float {
91 return channel * (1 + channel / whiteSquared) / (1 + channel);
92 };
93 return Color{map(color.red()), map(color.green()), map(color.blue()), color.alpha()};
94}
95
110export template <LinearRGBSpace Color>
111constexpr Color aces_fitted(const Color& color)
112{
113 const LinSRGB linear = convert<LinSRGB>(color);
114 const Vector3 input = blas::multiply(ACES_INPUT, {linear.red(), linear.green(), linear.blue()});
115 const Vector3 fit = {rrt_and_odt_fit(input[0]), rrt_and_odt_fit(input[1]),
116 rrt_and_odt_fit(input[2])};
117 const Vector3 output = blas::multiply(ACES_OUTPUT, fit);
118
119 return convert<Color>(LinSRGB{clamp(static_cast<float>(output[0])),
120 clamp(static_cast<float>(output[1])),
121 clamp(static_cast<float>(output[2])), color.alpha()});
122}
123} // namespace aest::colors::tonemap
constexpr float clamp(const float value)
Clamp a floating-point value to the 0-1 range.
Definition Color.cppm:29
constexpr ColorOut convert(const ColorIn &color)
Convert a color from one color space to another.
blas::Vector< double, 3 > Vector3
A column of three values, such as XYZ or linear RGB channels.
Definition Gamut.cppm:113
blas::Matrix< double, 3, 3 > Matrix3
A 3x3 matrix, stored as three rows.
Definition Gamut.cppm:116
BasicRGB< gamut::SRGB, tf::Linear > LinSRGB
Linear sRGB: the sRGB primaries without the gamma encoding, so channels are proportional to light and...
Definition Gamut.cppm:376
constexpr Vector< T, Rows > multiply(const Matrix< T, Rows, Columns > &matrix, const Vector< T, Columns > &vector)
Multiplies a matrix by a column vector.
Definition Matrix.cppm:69
constexpr float green() const noexcept
Get the green channel value.
Definition Gamut.cppm:330
constexpr float red() const noexcept
Get the red channel value.
Definition Gamut.cppm:324
constexpr float blue() const noexcept
Get the blue channel value.
Definition Gamut.cppm:336
Tonemapping operators, from HDR linear light to the 0-1 range of a display.
Definition Tonemap.cppm:19
constexpr Color reinhard_extended(const Color &color, const float white)
Extended Reinhard tonemapping, c (1 + c / w²) / (1 + c) on each channel, which maps white to 1.
Definition Tonemap.cppm:87
constexpr Matrix3 ACES_OUTPUT
Output matrix of the ACES fit by Stephen Hill: back from the ODT to linear sRGB, with the saturation ...
Definition Tonemap.cppm:34
constexpr Matrix3 ACES_INPUT
Input matrix of the ACES fit by Stephen Hill: linear sRGB to the ACES AP1 primaries,...
Definition Tonemap.cppm:24
constexpr Color aces_fitted(const Color &color)
Fitted ACES tonemapping by Stephen Hill: an approximation of the ACES Reference Rendering Transform a...
Definition Tonemap.cppm:111
constexpr double rrt_and_odt_fit(const double value)
Rational fit of the ACES RRT and sRGB ODT curve, by Stephen Hill.
Definition Tonemap.cppm:45
constexpr Color reinhard(const Color &color)
Reinhard tonemapping, c / (1 + c) on each channel.
Definition Tonemap.cppm:65