-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BT.709 and BT.2020 color spaces. Improve RGB color space functions.
- Loading branch information
Showing
19 changed files
with
428 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ bin | |
build | ||
res/data | ||
.~lock* | ||
.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// SPDX-FileCopyrightText: 2023 C. J. Howard | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#ifndef ANTKEEPER_COLOR_BT2020_HPP | ||
#define ANTKEEPER_COLOR_BT2020_HPP | ||
|
||
#include <engine/color/rgb.hpp> | ||
#include <cmath> | ||
|
||
namespace color { | ||
|
||
/// @name ITU-R BT.2020 color space | ||
/// @{ | ||
|
||
/// @{ | ||
/** | ||
* ITU-R BT.2020 Opto-Electronic Transfer Function (OETF). | ||
* | ||
* @param x Linear BT.2020 tristimulus. | ||
* @param alpha OETF alpha constant. | ||
* @param beta OETF beta constant. | ||
* | ||
* @return Non-linear BT.2020 signal. | ||
*/ | ||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt2020_oetf(const math::vec3<T>& x, T alpha, T beta) | ||
{ | ||
auto f = [alpha, beta](T x) -> T | ||
{ | ||
return x < beta ? T{4.5} * x : alpha * std::pow(x, T{0.45}) - (alpha - T{1}); | ||
}; | ||
|
||
return {f(x[0]), f(x[1]), f(x[2])}; | ||
} | ||
|
||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt2020_oetf_precise(const math::vec3<T>& x) | ||
{ | ||
return bt2020_oetf(x, T{1} + T{5.5} * T{0.018053968510807}, T{0.018053968510807}); | ||
} | ||
|
||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt2020_oetf_12_bits(const math::vec3<T>& x) | ||
{ | ||
return bt2020_oetf(x, T{1.0993}, T{0.0181}); | ||
} | ||
|
||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt2020_oetf_10_bits(const math::vec3<T>& x) | ||
{ | ||
return bt2020_oetf(x, T{1.099}, T{0.018}); | ||
} | ||
/// @} | ||
|
||
/// @{ | ||
/** | ||
* ITU-R BT.2020 inverse OETF. | ||
* | ||
* @param x Non-linear BT.2020 signal. | ||
* @param alpha OETF alpha constant. | ||
* @param beta OETF beta constant. | ||
* | ||
* @return Linear BT.2020 tristimulus. | ||
*/ | ||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt2020_inverse_oetf(const math::vec3<T>& x, T alpha, T beta) | ||
{ | ||
const auto oetf_beta = alpha * std::pow(beta, T{0.45}) - (alpha - T{1}); | ||
|
||
auto f = [alpha, oetf_beta](T x) -> T | ||
{ | ||
return x < oetf_beta ? x / T{4.5} : std::pow((x + alpha - T{1}) / alpha, T{1} / T{0.45}); | ||
}; | ||
|
||
return {f(x[0]), f(x[1]), f(x[2])}; | ||
} | ||
|
||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt2020_inverse_oetf_precise(const math::vec3<T>& x) | ||
{ | ||
return bt2020_inverse_oetf(x, T{1} + T{5.5} * T{0.018053968510807}, T{0.018053968510807}); | ||
} | ||
|
||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt2020_inverse_oetf_12_bits(const math::vec3<T>& x) | ||
{ | ||
return bt2020_inverse_oetf(x, T{1.0993}, T{0.0181}); | ||
} | ||
|
||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt2020_inverse_oetf_10_bits(const math::vec3<T>& x) | ||
{ | ||
return bt2020_inverse_oetf(x, T{1.099}, T{0.018}); | ||
} | ||
/// @} | ||
|
||
/** | ||
* ITU-R BT.2020 color space. | ||
*/ | ||
template <class T> | ||
constexpr rgb_color_space<T> bt2020 | ||
( | ||
{T{0.7080}, T{0.2920}}, | ||
{T{0.1700}, T{0.7970}}, | ||
{T{0.1310}, T{0.0460}}, | ||
{T{0.3127}, T{0.3290}}, | ||
&bt2020_oetf_10_bits<T>, | ||
&bt2020_inverse_oetf_10_bits<T> | ||
); | ||
|
||
/// @} | ||
|
||
} // namespace color | ||
|
||
#endif // ANTKEEPER_COLOR_BT2020_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// SPDX-FileCopyrightText: 2023 C. J. Howard | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#ifndef ANTKEEPER_COLOR_BT709_HPP | ||
#define ANTKEEPER_COLOR_BT709_HPP | ||
|
||
#include <engine/color/rgb.hpp> | ||
#include <cmath> | ||
|
||
namespace color { | ||
|
||
/// @name ITU-R BT.709 color space | ||
/// @{ | ||
|
||
/** | ||
* ITU-R BT.709 Opto-Electronic Transfer Function (OETF). | ||
* | ||
* @param x Linear BT.709 tristimulus. | ||
* | ||
* @return Non-linear BT.709 signal. | ||
*/ | ||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt709_oetf(const math::vec3<T>& x) | ||
{ | ||
auto f = [](T x) -> T | ||
{ | ||
return x < T{0.018} ? T{4.5} * x : T{1.099} * std::pow(x, T{0.45}) - T{0.099}; | ||
}; | ||
|
||
return {f(x[0]), f(x[1]), f(x[2])}; | ||
} | ||
|
||
/** | ||
* ITU-R BT.709 inverse OETF. | ||
* | ||
* @param x Non-linear BT.709 signal. | ||
* | ||
* @return Linear BT.709 tristimulus. | ||
*/ | ||
template <class T> | ||
[[nodiscard]] math::vec3<T> bt709_inverse_oetf(const math::vec3<T>& x) | ||
{ | ||
const auto oetf_beta = T{1.099} * std::pow(T{0.018}, T{0.45}) - T{0.099}; | ||
|
||
auto f = [oetf_beta](T x) -> T | ||
{ | ||
return x < oetf_beta ? x / T{4.5} : std::pow((x + T{0.099}) / T{1.099}, T{1} / T{0.45}); | ||
}; | ||
|
||
return {f(x[0]), f(x[1]), f(x[2])}; | ||
} | ||
|
||
/** | ||
* ITU-R BT.709 color space. | ||
*/ | ||
template <class T> | ||
constexpr rgb_color_space<T> bt709 | ||
( | ||
{T{0.6400}, T{0.3300}}, | ||
{T{0.3000}, T{0.6000}}, | ||
{T{0.1500}, T{0.0600}}, | ||
{T{0.3127}, T{0.3290}}, | ||
&bt709_oetf<T>, | ||
&bt709_inverse_oetf<T> | ||
); | ||
|
||
/// @} | ||
|
||
} // namespace color | ||
|
||
#endif // ANTKEEPER_COLOR_BT709_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.