Registry Mirror
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/doc/imageioapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,20 @@ just exist in the OIIO namespace as general utilities. (See
:::{doxygenfunction} OIIO::equivalent_colorspace
:::

(sec-colorspaceproperties)=

## Color space properties

`ColorConfig::get_chromaticities()` and `ColorConfig::get_transfer_gamma()`
report a color space's primaries and transfer function. See
{ref}`sec-pythoncolorconfig` for the Python bindings.

:::{doxygenfunction} OIIO::ColorConfig::get_chromaticities
:::

:::{doxygenfunction} OIIO::ColorConfig::get_transfer_gamma
:::

> (sec-startupshutdown)=

## Startup and Shutdown
Expand Down
30 changes: 30 additions & 0 deletions src/doc/pythonbindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -4040,6 +4040,36 @@ is provided for minimal color support.

% TODO: The documentation for this class is incomplete.

:::{py:method} get_chromaticities (colorspace)
Get the RGB primaries and white point of a color space with a known Color
Interop ID, as `get_color_interop_id()` finds it: a sequence of eight floats
(Rx, Ry, Gx, Gy, Bx, By, Wx, Wy, CIE 1931 xy), or None if they are unknown.

Example:

```python
xy = colorconfig.get_chromaticities("ACEScg")
if xy:
rx, ry, gx, gy, bx, by, wx, wy = xy
```

This function was added in OpenImageIO 3.2.
:::

:::{py:method} get_transfer_gamma (colorspace)
Get the exponent of a color space's transfer function if it decodes as a pure
power, 1.0 if it is linear, or 0 if it is neither or is unknown. Piecewise
curves such as sRGB's report 0.

Example:

```python
gamma = colorconfig.get_transfer_gamma("g22_rec709_scene") # 2.2
```

This function was added in OpenImageIO 3.2.
:::

:::{py:method} get_cicp (colorspace)
Find CICP code corresponding to the colorspace.
Return a sequence of 4 ints, or None if not found.
Expand Down
20 changes: 20 additions & 0 deletions src/include/OpenImageIO/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,26 @@ class OIIO_API ColorConfig {
OIIO_NODISCARD string_view
get_color_interop_id(string_view colorspace) const;

/// Get the RGB primaries and white point of a color space with a known
/// Color Interop ID
/// (https://github.com/AcademySoftwareFoundation/ColorInterop), as
/// `get_color_interop_id()` finds it: eight floats (Rx, Ry, Gx, Gy, Bx,
/// By, Wx, Wy, CIE 1931 xy), or an empty span if they are unknown. The
/// span points into OIIO's own table, so it stays valid.
///
/// @version 3.2
OIIO_NODISCARD cspan<float> get_chromaticities(string_view colorspace) const;

/// Get the exponent of a color space's transfer function if it decodes
/// as a pure power, 1.0 if it is linear, or 0 if it is neither or is
/// unknown. Piecewise curves such as sRGB's report 0. Like
/// `get_chromaticities()`, it answers for a known Color Interop ID, and
/// it reports 1.0 for any color space whose config declares a linear
/// encoding.
///
/// @version 3.2
OIIO_NODISCARD float get_transfer_gamma(string_view colorspace) const;

/// Find color interop ID corresponding to the CICP code.
/// Returns empty string if not found.
///
Expand Down
164 changes: 130 additions & 34 deletions src/libOpenImageIO/color_ocio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ class ColorConfig::Impl {
OCIO::ConfigRcPtr config_;
OCIO::ConfigRcPtr builtinconfig_;

// A color space's chromaticities (in the ColorInteropID table, or null)
// and transfer function gamma.
struct Properties {
const float* xy = nullptr;
float gamma = 0.0f;
};

private:
std::vector<CSInfo> colorspaces;
std::string scene_linear_alias; // Alias for a scene-linear color space
Expand Down Expand Up @@ -305,6 +312,9 @@ class ColorConfig::Impl {
return handle;
}

// The properties get_chromaticities() and get_transfer_gamma() report.
Properties properties(string_view colorspace) const;

int getNumColorSpaces() const { return (int)colorspaces.size(); }

const char* getColorSpaceNameByIndex(int index) const
Expand Down Expand Up @@ -2339,30 +2349,62 @@ enum class CICPRange : int {
Full = 1,
};

// Chromaticities (Rx, Ry, Gx, Gy, Bx, By, Wx, Wy) of the published gamuts.
constexpr float xy_rec709[] = { .64f, .33f, .30f, .60f,
.15f, .06f, .3127f, .329f };
constexpr float xy_p3d65[] = { .68f, .32f, .265f, .69f,
.15f, .06f, .3127f, .329f };
constexpr float xy_rec2020[] = { .708f, .292f, .17f, .797f,
.131f, .046f, .3127f, .329f };
constexpr float xy_adobergb[] = { .64f, .33f, .21f, .71f,
.15f, .06f, .3127f, .329f };
constexpr float xy_ap0[] = { .7347f, .2653f, 0.0f, 1.0f,
.0001f, -.077f, .32168f, .33767f };
constexpr float xy_ap1[] = { .713f, .293f, .165f, .83f,
.128f, .044f, .32168f, .33767f };
// CIE XYZ taken as RGB, so R = G = B is illuminant E.
constexpr float xy_xyz[] = { 1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f / 3, 1.0f / 3 };

// A color interop ID with what OIIO knows about it: a legacy alias for older
// configs, its CICP code if it has one, and its chromaticities (Rx, Ry, Gx, Gy,
// Bx, By, Wx, Wy) and gamma if it has RGB primaries. Gamma is 1 for linear,
// the exponent of a pure power, or 0 for any other curve (sRGB, extended
// sRGB, PQ, HLG).
struct ColorInteropID {
constexpr ColorInteropID(const char* interop_id, const char* legacy_alias)
constexpr ColorInteropID(const char* interop_id, const char* legacy_alias,
const float* chromaticities = nullptr,
float gamma = 0.0f)
: interop_id(interop_id)
, legacy_alias(legacy_alias)
, cicp({ 0, 0, 0, 0 })
, has_cicp(false)
, chromaticities(chromaticities)
, gamma(gamma)
{
}

constexpr ColorInteropID(const char* interop_id, const char* legacy_alias,
CICPPrimaries primaries, CICPTransfer transfer,
CICPMatrix matrix)
CICPMatrix matrix,
const float* chromaticities = nullptr,
float gamma = 0.0f)
: interop_id(interop_id)
, legacy_alias(legacy_alias)
, cicp({ int(primaries), int(transfer), int(matrix),
int(CICPRange::Full) })
, has_cicp(true)
, chromaticities(chromaticities)
, gamma(gamma)
{
}

const char* interop_id;
const char* legacy_alias;
std::array<int, 4> cicp;
bool has_cicp;
const float* chromaticities;
float gamma;
};

// Mapping between color interop ID and CICP, based on Color Interop Forum
Expand All @@ -2371,66 +2413,68 @@ constexpr ColorInteropID color_interop_ids[] = {
// Scene referred interop IDs first so they are the default in automatic
// conversion from CICP to interop ID. Some are not display color spaces
// at all, but can be represented by CICP anyway.
{ "lin_ap1_scene", "lin_ap1" },
{ "lin_ap0_scene", "lin_ap0" },
{ "lin_ap1_scene", "lin_ap1", xy_ap1, 1.0f },
{ "lin_ap0_scene", "lin_ap0", xy_ap0, 1.0f },
{ "lin_rec709_scene", "lin_rec709", CICPPrimaries::Rec709,
CICPTransfer::Linear, CICPMatrix::BT709 },
CICPTransfer::Linear, CICPMatrix::BT709, xy_rec709, 1.0f },
{ "lin_p3d65_scene", "lin_p3d65", CICPPrimaries::P3D65,
CICPTransfer::Linear, CICPMatrix::BT709 },
CICPTransfer::Linear, CICPMatrix::BT709, xy_p3d65, 1.0f },
{ "lin_rec2020_scene", "lin_rec2020", CICPPrimaries::Rec2020,
CICPTransfer::Linear, CICPMatrix::Rec2020_CL },
{ "lin_adobergb_scene", "lin_adobergb" },
CICPTransfer::Linear, CICPMatrix::Rec2020_CL, xy_rec2020, 1.0f },
{ "lin_adobergb_scene", "lin_adobergb", xy_adobergb, 1.0f },
{ "lin_ciexyzd65_scene", "cie_xyz_d65", CICPPrimaries::XYZD65,
CICPTransfer::Linear, CICPMatrix::Unspecified },
CICPTransfer::Linear, CICPMatrix::Unspecified, xy_xyz, 1.0f },
{ "srgb_rec709_scene", "srgb_texture", CICPPrimaries::Rec709,
CICPTransfer::sRGB, CICPMatrix::BT709 },
{ "g24_rec709_scene", "g24_rec709" },
CICPTransfer::sRGB, CICPMatrix::BT709, xy_rec709, 0.0f },
{ "g24_rec709_scene", "g24_rec709", xy_rec709, 2.4f },
{ "g22_rec709_scene", "g22_rec709", CICPPrimaries::Rec709,
CICPTransfer::Gamma22, CICPMatrix::BT709 },
{ "g18_rec709_scene", "g18_rec709" },
{ "srgb_ap1_scene", "srgb_ap1" },
{ "g22_ap1_scene", "g22_ap1" },
CICPTransfer::Gamma22, CICPMatrix::BT709, xy_rec709, 2.2f },
{ "g18_rec709_scene", "g18_rec709", xy_rec709, 1.8f },
{ "srgb_ap1_scene", "srgb_ap1", xy_ap1, 0.0f },
{ "g22_ap1_scene", "g22_ap1", xy_ap1, 2.2f },
{ "srgb_p3d65_scene", "srgb_p3d65", CICPPrimaries::P3D65,
CICPTransfer::sRGB, CICPMatrix::BT709 },
{ "g22_adobergb_scene", nullptr },
CICPTransfer::sRGB, CICPMatrix::BT709, xy_p3d65, 0.0f },
{ "g22_adobergb_scene", nullptr, xy_adobergb, 563.0f / 256.0f },
{ "data", nullptr },
{ "unknown", nullptr },

// Display referred interop IDs.
// The linear displays share their scene rows' codes, which come first,
// so CICP input still reads as the scene identity.
{ "lin_rec709_display", nullptr, CICPPrimaries::Rec709,
CICPTransfer::Linear, CICPMatrix::BT709 },
CICPTransfer::Linear, CICPMatrix::BT709, xy_rec709, 1.0f },
{ "lin_p3d65_display", nullptr, CICPPrimaries::P3D65, CICPTransfer::Linear,
CICPMatrix::BT709 },
CICPMatrix::BT709, xy_p3d65, 1.0f },
{ "lin_rec2020_display", nullptr, CICPPrimaries::Rec2020,
CICPTransfer::Linear, CICPMatrix::Rec2020_CL },
CICPTransfer::Linear, CICPMatrix::Rec2020_CL, xy_rec2020, 1.0f },
{ "srgb_rec709_display", "srgb_display", CICPPrimaries::Rec709,
CICPTransfer::sRGB, CICPMatrix::BT709 },
CICPTransfer::sRGB, CICPMatrix::BT709, xy_rec709, 0.0f },
// Not all software interprets this CICP the same, see the
// "QuickTime Gamma Shift" issue. We follow the CIF recommendation and
// interpret it as BT.1886.
{ "g24_rec709_display", "rec1886_rec709_display", CICPPrimaries::Rec709,
CICPTransfer::BT709, CICPMatrix::BT709 },
CICPTransfer::BT709, CICPMatrix::BT709, xy_rec709, 2.4f },
{ "srgb_p3d65_display", "displayp3_display", CICPPrimaries::P3D65,
CICPTransfer::sRGB, CICPMatrix::BT709 },
CICPTransfer::sRGB, CICPMatrix::BT709, xy_p3d65, 0.0f },
{ "srgbe_p3d65_display", "displayp3_hdr_display", CICPPrimaries::P3D65,
CICPTransfer::sRGB, CICPMatrix::BT709 },
CICPTransfer::sRGB, CICPMatrix::BT709, xy_p3d65, 0.0f },
{ "pq_p3d65_display", "st2084_p3d65_display", CICPPrimaries::P3D65,
CICPTransfer::PQ, CICPMatrix::Rec2020_NCL },
CICPTransfer::PQ, CICPMatrix::Rec2020_NCL, xy_p3d65, 0.0f },
{ "pq_rec2020_display", "rec2100_pq_display", CICPPrimaries::Rec2020,
CICPTransfer::PQ, CICPMatrix::Rec2020_NCL },
CICPTransfer::PQ, CICPMatrix::Rec2020_NCL, xy_rec2020, 0.0f },
{ "hlg_rec2020_display", "rec2100_hlg_display", CICPPrimaries::Rec2020,
CICPTransfer::HLG, CICPMatrix::Rec2020_NCL },
// No CICP mapping to keep previous behavior unchanged, as Gamma 2.2
// display is more likely meant to be written as sRGB. On read the
// scene referred interop ID will be used.
{ "g22_rec709_display", nullptr
/* CICPPrimaries::Rec709, CICPTransfer::Gamma22, CICPMatrix::BT709 */ },
CICPTransfer::HLG, CICPMatrix::Rec2020_NCL, xy_rec2020, 0.0f },
// No CICP mapping (Rec709, Gamma22, BT709) to keep previous behavior
// unchanged, as Gamma 2.2 display is more likely meant to be written as
// sRGB. On read the scene referred interop ID will be used.
{ "g22_rec709_display", nullptr, xy_rec709, 2.2f },
// No CICP code for Adobe RGB primaries.
{ "g22_adobergb_display", nullptr },
{ "g22_adobergb_display", nullptr, xy_adobergb, 563.0f / 256.0f },
{ "g26_p3d65_display", "p3d65_display", CICPPrimaries::P3D65,
CICPTransfer::Gamma26, CICPMatrix::BT709 },
CICPTransfer::Gamma26, CICPMatrix::BT709, xy_p3d65, 2.6f },
// The DCDM XYZ encodings have no properties: neither curve is a pure
// power (DCI headroom scaling precedes the 2.6 power), and no RGB
// primaries are reported for display XYZ encodings.
{ "g26_xyzd65_display", nullptr, CICPPrimaries::XYZD65,
CICPTransfer::Gamma26, CICPMatrix::Unspecified },
{ "pq_xyzd65_display", nullptr, CICPPrimaries::XYZD65, CICPTransfer::PQ,
Expand Down Expand Up @@ -2504,6 +2548,58 @@ ColorConfig::get_cicp(string_view colorspace) const
}



ColorConfig::Impl::Properties
ColorConfig::Impl::properties(string_view colorspace) const
{
// Identify the space as get_cicp() does, so that every name it accepts
// gives the same answer here.
const string_view interop_id = m_self->get_color_interop_id(colorspace);
OCIO::ConstColorSpaceRcPtr cs;
if (config_ && !disable_ocio && !colorspace.empty()) {
try {
cs = config_->getColorSpace(c_str(m_self->resolve(colorspace)));
} catch (const std::exception& e) {
DBG("OCIO exception looking up {}: {}\n", colorspace, e.what());
}
}
if (cs && cs->isData())
return {};
const string_view encoding(cs ? cs->getEncoding() : "");
const bool linear = encoding == "scene-linear"
|| encoding == "display-linear";
Properties result;
for (const ColorInteropID& interop : color_interop_ids) {
if (interop.chromaticities && interop_id == interop.interop_id) {
// An ID contradicting a declared linear encoding is ignored.
if (!(linear && interop.gamma != 1.0f))
result = { interop.chromaticities, interop.gamma };
break;
}
}
if (linear)
result.gamma = 1.0f;
return result;
}



cspan<float>
ColorConfig::get_chromaticities(string_view colorspace) const
{
const float* xy = getImpl()->properties(colorspace).xy;
return xy ? cspan<float>(xy, 8) : cspan<float>();
}



float
ColorConfig::get_transfer_gamma(string_view colorspace) const
{
return getImpl()->properties(colorspace).gamma;
}


//////////////////////////////////////////////////////////////////////////
//
// Image Processing Implementations
Expand Down
Loading
Loading