HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
colorSpace.h
Go to the documentation of this file.
1 //
2 // Copyright 2024 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_GF_COLORSPACE_H
8 #define PXR_BASE_GF_COLORSPACE_H
9 
10 /// \file gf/color.h
11 /// \ingroup group_gf_Color
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/vec2f.h"
15 #include "pxr/base/gf/matrix3f.h"
16 #include "pxr/base/gf/api.h"
17 #include "pxr/base/tf/span.h"
19 
21 
22 /// \enum GfColorSpace
23 /// \ingroup group_gf_Color
24 ///
25 /// GfColorSpace defines color spaces natively supported by Gf to define scene
26 /// referred color values. The descriptive names are those published by the
27 /// Color Interop Forum.
28 ///
29 /// The short names take the form of three components joined by underscores.
30 ///
31 /// The first component is the encoding of the RGB tuple; either `lin` for
32 /// linear encoding, `srgb` for IEC 61966-2-1:1999 encoding, or gNN where NN
33 /// indicates a gamma value.
34 ///
35 /// The second component designates the color primaries and white point, and
36 /// are a short name of a corresponding CIF color space.
37 ///
38 /// Finally, the third component names the image state, as named in ISO 22028-1,
39 /// drawing a distinction between scene-referred and display-referred color
40 /// spaces. Scene referenced color spaces are used to describe the color
41 /// appearance of objects in the real world, while display-referred color spaces
42 /// are used to describe color as emitted by projectors or monitor screen.
43 ///
44 /// ACEScg: lin_ap1_scene
45 /// ACES2065-1: lin_ap0_scene
46 /// Linear Rec.709 (sRGB): lin_rec709_scene
47 /// Linear P3-D65: lin_p3d65_scene
48 /// Linear Rec.2020: lin_rec2020_scene
49 /// Linear AdobeRGB: lin_adobergb_scene
50 /// CIE XYZ-D65 - Scene-referred: lin_ciexyzd65_scene
51 /// sRGB Encoded Rec.709 (sRGB): srgb_rec709_scene
52 /// Gamma 2.2 Encoded Rec.709: g22_rec709_scene
53 /// Gamma 1.8 Encoded Rec.709: g18_rec709_scene
54 /// sRGB Encoded AP1: srgb_ap1_scene
55 /// Gamma 2.2 Encoded AP1: g22_ap1_scene
56 /// sRGB Encoded P3-D65: srgb_p3d65_scene
57 /// Gamma 2.2 Encoded AdobeRGB: g22_adobergb_scene
58 /// Data: data
59 /// Unknown: unknown
60 ///
61 /// `lin_ciexyzd65_scene` bears some additional explanation. The `d65` component
62 /// in the name is meant to indicate that values transformed to this color space
63 /// should be adapted to the D65 white point.
64 ///
65 /// In addition the `data` and `unknown` color space names, `raw` and `identity`
66 /// are provided for compatibility with existing production assets. The CIEXYZ
67 /// and LinearDisplayP3 names are deprecated and will be removed in a future
68 /// release.
69 ///
70 /// User defined color spaces outside of this set may be defined through
71 /// explicit construction.
72 ///
73 #define GF_COLORSPACE_NAME_TOKENS \
74  ((LinearAP1, "lin_ap1_scene")) \
75  ((LinearAP0, "lin_ap0_scene")) \
76  ((LinearRec709, "lin_rec709_scene")) \
77  ((LinearP3D65, "lin_p3d65_scene")) \
78  ((LinearRec2020, "lin_rec2020_scene")) \
79  ((LinearAdobeRGB, "lin_adobergb_scene")) \
80  ((LinearCIEXYZD65, "lin_ciexyzd65_scene")) \
81  ((SRGBRec709, "srgb_rec709_scene")) \
82  ((G22Rec709, "g22_rec709_scene")) \
83  ((G18Rec709, "g18_rec709_scene")) \
84  ((SRGBAP1, "srgb_ap1_scene")) \
85  ((G22AP1, "g22_ap1_scene")) \
86  ((SRGBP3D65, "srgb_p3d65_scene")) \
87  ((G22AdobeRGB, "g22_adobergb_scene")) \
88  ((Identity, "identity")) \
89  ((Data, "data")) \
90  ((Raw, "raw")) \
91  ((Unknown, "unknown")) \
92  ((CIEXYZ, "lin_ciexyzd65_scene")) \
93  ((LinearDisplayP3, "lin_p3d65_scene"))
94 
95 TF_DECLARE_PUBLIC_TOKENS(GfColorSpaceNames, GF_API,
97 
98 class GfColor;
99 
100 /// \class GfColorSpace
101 /// \ingroup group_gf_Color
102 ///
103 /// Basic type: ColorSpace
104 ///
105 /// This class represents a colorspace. Color spaces may be created by
106 /// name, parameterization, or by a 3x3 matrix and a gamma operator.
107 ///
108 /// The parameters used to construct the color space are not available for
109 /// introspection ~ the color space object is intended for color conversion
110 /// operations on a GfColor.
111 ///
112 /// The color spaces natively recognized by GfColorSpace are listed in
113 /// GfColorSpaceNames.
114 
116  friend class GfColor;
117 public:
118  /// Construct a GfColorSpace from a name token.
119  ///
120  /// \param name The name token of the color space.
121  GF_API
122  explicit GfColorSpace(const TfToken& name);
123 
124  /// Check if a color space name is valid for constructing
125  /// a GfColorSpace by name.
126  GF_API
127  static bool IsValid(const TfToken& name);
128 
129  /// Construct a custom color space from raw values.
130  ///
131  /// \param name The name token of the color space.
132  /// \param redChroma The red chromaticity coordinates.
133  /// \param greenChroma The green chromaticity coordinates.
134  /// \param blueChroma The blue chromaticity coordinates.
135  /// \param whitePoint The white point chromaticity coordinates.
136  /// \param gamma The gamma value of the log section.
137  /// \param linearBias The linear bias of the log section.
138  GF_API
139  explicit GfColorSpace(const TfToken& name,
140  const GfVec2f &redChroma,
141  const GfVec2f &greenChroma,
142  const GfVec2f &blueChroma,
143  const GfVec2f &whitePoint,
144  float gamma,
145  float linearBias);
146 
147  /// Construct a color space from a 3x3 matrix and linearization parameters.
148  ///
149  /// \param name The name token of the color space.
150  /// \param rgbToXYZ The RGB to XYZ conversion matrix.
151  /// \param gamma The gamma value of the log section.
152  /// \param linearBias The linear bias of the log section.
153  GF_API
154  explicit GfColorSpace(const TfToken& name,
155  const GfMatrix3f &rgbToXYZ,
156  float gamma,
157  float linearBias);
158 
159  /// Get the name of the color space.
160  ///
161  /// \return The name of the color space.
162  GF_API
163  TfToken GetName() const;
164 
165  /// Check if two color spaces are equal.
166  ///
167  /// \param lh The left-hand side color space.
168  /// \return True if the color spaces are equal, false otherwise.
169  GF_API
170  bool operator ==(const GfColorSpace &rh) const;
171 
172  /// Check if two color spaces are not equal.
173  ///
174  /// \param rh The rigt-hand side color space.
175  /// \return True if the color spaces are not equal, false otherwise.
176  bool operator !=(const GfColorSpace &rh) const { return !(*this == rh); }
177 
178  /// Convert in place a packed array of RGB values from one color space to "this" one.
179  ///
180  /// \param to The target color space.
181  /// \param rgb The packed array of RGB values to convert.
182  GF_API
183  void ConvertRGBSpan(const GfColorSpace& srcColorSpace, TfSpan<float> rgb) const;
184 
185  /// Convert in place a packed array of RGBA values from one color space to "this one.
186  ///
187  /// \param to The target color space.
188  /// \param rgba The packed array of RGBA values to convert.
189  GF_API
190  void ConvertRGBASpan(const GfColorSpace& srcColorSpace, TfSpan<float> rgba) const;
191 
192  /// Convert a rgb triplet in a certain color space to "this" color space.
193  GF_API
194  GfColor Convert(const GfColorSpace& srcColorSpace, const GfVec3f& rgb) const;
195 
196  /// Get the RGB to XYZ conversion matrix.
197  ///
198  /// \return The RGB to XYZ conversion matrix.
199  GF_API
200  GfMatrix3f GetRGBToXYZ() const;
201 
202  /// Get the gamma value of the color space.
203  ///
204  /// \return The gamma value of the color space.
205  GF_API
206  float GetGamma() const;
207 
208  /// Get the linear bias of the color space.
209  ///
210  /// \return The linear bias of the color space.
211  GF_API
212  float GetLinearBias() const;
213 
214  /// Get the computed K0 and Phi values for use in the transfer function.
215  ///
216  GF_API
217  std::pair<float, float> GetTransferFunctionParams() const;
218 
219  /// Get the chromaticity coordinates and white point if the color space
220  /// was constructed from primaries. The primaries and white points will
221  /// be in the order red, green, blue, white. The values will be
222  /// valid if the color space was constructed from primaries or a well formed
223  /// primary matrix.
224  ///
225  /// \return The chromaticity coordinates and white point;
226  /// an empty optional if the color space was not constructed from primaries.
227  GF_API
228  std::tuple<GfVec2f, GfVec2f, GfVec2f, GfVec2f>
230 
231 private:
232  struct _Data;
233  std::shared_ptr<_Data> _data;
234 };
235 
237 
238 #endif // PXR_BASE_GF_COLORSPACE_H
GF_API std::tuple< GfVec2f, GfVec2f, GfVec2f, GfVec2f > GetPrimariesAndWhitePoint() const
GF_API void ConvertRGBASpan(const GfColorSpace &srcColorSpace, TfSpan< float > rgba) const
GF_API bool operator==(const GfColorSpace &rh) const
GF_API std::pair< float, float > GetTransferFunctionParams() const
Definition: vec3f.h:45
TF_DECLARE_PUBLIC_TOKENS(GfColorSpaceNames, GF_API, GF_COLORSPACE_NAME_TOKENS)
Definition: token.h:70
Definition: span.h:70
GF_API GfMatrix3f GetRGBToXYZ() const
GF_API GfColor Convert(const GfColorSpace &srcColorSpace, const GfVec3f &rgb) const
Convert a rgb triplet in a certain color space to "this" color space.
GF_API TfToken GetName() const
GF_API float GetGamma() const
GF_API void ConvertRGBSpan(const GfColorSpace &srcColorSpace, TfSpan< float > rgb) const
#define GF_COLORSPACE_NAME_TOKENS
Definition: colorSpace.h:73
GLuint const GLchar * name
Definition: glcorearb.h:786
Represents a color in a specific color space.Basic type: Color.
Definition: color.h:41
static GF_API bool IsValid(const TfToken &name)
bool operator!=(const GfColorSpace &rh) const
Definition: colorSpace.h:176
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
Definition: vec2f.h:45
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GF_API float GetLinearBias() const
GF_API GfColorSpace(const TfToken &name)
#define GF_API
Definition: api.h:23