xref: /aosp_15_r20/external/skia/include/core/SkColorSpace.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkColorSpace_DEFINED
9 #define SkColorSpace_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/base/SkFixed.h"
14 #include "include/private/base/SkOnce.h"
15 #include "modules/skcms/skcms.h"
16 
17 #include <cstddef>
18 #include <cstdint>
19 
20 class SkData;
21 
22 /**
23  *  Describes a color gamut with primaries and a white point.
24  */
25 struct SK_API SkColorSpacePrimaries {
26     float fRX;
27     float fRY;
28     float fGX;
29     float fGY;
30     float fBX;
31     float fBY;
32     float fWX;
33     float fWY;
34 
35     /**
36      *  Convert primaries and a white point to a toXYZD50 matrix, the preferred color gamut
37      *  representation of SkColorSpace.
38      */
39     bool toXYZD50(skcms_Matrix3x3* toXYZD50) const;
40 };
41 
42 namespace SkNamedPrimaries {
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 // Color primaries defined by ITU-T H.273, table 2. Names are given by the first
46 // specification referenced in the value's row.
47 
48 // Rec. ITU-R BT.709-6, value 1.
49 static constexpr SkColorSpacePrimaries kRec709 = {
50         0.64f, 0.33f, 0.3f, 0.6f, 0.15f, 0.06f, 0.3127f, 0.329f};
51 
52 // Rec. ITU-R BT.470-6 System M (historical), value 4.
53 static constexpr SkColorSpacePrimaries kRec470SystemM = {
54         0.67f, 0.33f, 0.21f, 0.71f, 0.14f, 0.08f, 0.31f, 0.316f};
55 
56 // Rec. ITU-R BT.470-6 System B, G (historical), value 5.
57 static constexpr SkColorSpacePrimaries kRec470SystemBG = {
58         0.64f, 0.33f, 0.29f, 0.60f, 0.15f, 0.06f, 0.3127f, 0.3290f};
59 
60 // Rec. ITU-R BT.601-7 525, value 6.
61 static constexpr SkColorSpacePrimaries kRec601 = {
62         0.630f, 0.340f, 0.310f, 0.595f, 0.155f, 0.070f, 0.3127f, 0.3290f};
63 
64 // SMPTE ST 240, value 7 (functionally the same as value 6).
65 static constexpr SkColorSpacePrimaries kSMPTE_ST_240 = kRec601;
66 
67 // Generic film (colour filters using Illuminant C), value 8.
68 static constexpr SkColorSpacePrimaries kGenericFilm = {
69         0.681f, 0.319f, 0.243f, 0.692f, 0.145f, 0.049f, 0.310f, 0.316f};
70 
71 // Rec. ITU-R BT.2020-2, value 9.
72 static constexpr SkColorSpacePrimaries kRec2020{
73         0.708f, 0.292f, 0.170f, 0.797f, 0.131f, 0.046f, 0.3127f, 0.3290f};
74 
75 // SMPTE ST 428-1, value 10.
76 static constexpr SkColorSpacePrimaries kSMPTE_ST_428_1 = {
77         1.f, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f / 3.f, 1.f / 3.f};
78 
79 // SMPTE RP 431-2, value 11.
80 static constexpr SkColorSpacePrimaries kSMPTE_RP_431_2 = {
81         0.680f, 0.320f, 0.265f, 0.690f, 0.150f, 0.060f, 0.314f, 0.351f};
82 
83 // SMPTE EG 432-1, value 12.
84 static constexpr SkColorSpacePrimaries kSMPTE_EG_432_1 = {
85         0.680f, 0.320f, 0.265f, 0.690f, 0.150f, 0.060f, 0.3127f, 0.3290f};
86 
87 // No corresponding industry specification identified, value 22.
88 // This is sometimes referred to as EBU 3213-E, but that document doesn't
89 // specify these values.
90 static constexpr SkColorSpacePrimaries kITU_T_H273_Value22 = {
91         0.630f, 0.340f, 0.295f, 0.605f, 0.155f, 0.077f, 0.3127f, 0.3290f};
92 
93 // Mapping between names of color primaries and the number of the corresponding
94 // row in ITU-T H.273, table 2.  As above, the constants are named based on the
95 // first specification referenced in the value's row.
96 enum class CicpId : uint8_t {
97     // Value 0 is reserved.
98     kRec709 = 1,
99     // Value 2 is unspecified.
100     // Value 3 is reserved.
101     kRec470SystemM = 4,
102     kRec470SystemBG = 5,
103     kRec601 = 6,
104     kSMPTE_ST_240 = 7,
105     kGenericFilm = 8,
106     kRec2020 = 9,
107     kSMPTE_ST_428_1 = 10,
108     kSMPTE_RP_431_2 = 11,
109     kSMPTE_EG_432_1 = 12,
110     // Values 13-21 are reserved.
111     kITU_T_H273_Value22 = 22,
112     // Values 23-255 are reserved.
113 };
114 
115 }  // namespace SkNamedPrimaries
116 
117 namespace SkNamedTransferFn {
118 
119 // Like SkNamedGamut::kSRGB, keeping this bitwise exactly the same as skcms makes things fastest.
120 static constexpr skcms_TransferFunction kSRGB =
121     { 2.4f, (float)(1/1.055), (float)(0.055/1.055), (float)(1/12.92), 0.04045f, 0.0f, 0.0f };
122 
123 static constexpr skcms_TransferFunction k2Dot2 =
124     { 2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
125 
126 static constexpr skcms_TransferFunction kRec2020 = {
127         2.22222f, 0.909672f, 0.0903276f, 0.222222f, 0.0812429f, 0, 0};
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 // Color primaries defined by ITU-T H.273, table 3. Names are given by the first
131 // specification referenced in the value's row.
132 
133 // Rec. ITU-R BT.709-6, value 1.
134 static constexpr skcms_TransferFunction kRec709 = {2.222222222222f,
135                                                    0.909672415686f,
136                                                    0.090327584314f,
137                                                    0.222222222222f,
138                                                    0.081242858299f,
139                                                    0.f,
140                                                    0.f};
141 
142 // Rec. ITU-R BT.470-6 System M (historical) assumed display gamma 2.2, value 4.
143 static constexpr skcms_TransferFunction kRec470SystemM = {2.2f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f};
144 
145 // Rec. ITU-R BT.470-6 System B, G (historical) assumed display gamma 2.8,
146 // value 5.
147 static constexpr skcms_TransferFunction kRec470SystemBG = {2.8f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f};
148 
149 // Rec. ITU-R BT.601-7, same as kRec709, value 6.
150 static constexpr skcms_TransferFunction kRec601 = kRec709;
151 
152 // SMPTE ST 240, value 7.
153 static constexpr skcms_TransferFunction kSMPTE_ST_240 = {
154         2.222222222222f, 0.899626676224f, 0.100373323776f, 0.25f, 0.091286342118f, 0.f, 0.f};
155 
156 // Linear, value 8
157 static constexpr skcms_TransferFunction kLinear =
158     { 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
159 
160 // IEC 61966-2-4, value 11, same as kRec709 (but is explicitly extended).
161 static constexpr skcms_TransferFunction kIEC61966_2_4 = kRec709;
162 
163 // IEC 61966-2-1 sRGB, value 13.
164 static constexpr skcms_TransferFunction kIEC61966_2_1 = kSRGB;
165 
166 // Rec. ITU-R BT.2020-2 (10-bit system), value 14.
167 static constexpr skcms_TransferFunction kRec2020_10bit = kRec709;
168 
169 // Rec. ITU-R BT.2020-2 (12-bit system), value 15.
170 static constexpr skcms_TransferFunction kRec2020_12bit = kRec709;
171 
172 // Rec. ITU-R BT.2100-2 perceptual quantization (PQ) system, value 16.
173 static constexpr skcms_TransferFunction kPQ =
174     {-2.0f, -107/128.0f, 1.0f, 32/2523.0f, 2413/128.0f, -2392/128.0f, 8192/1305.0f };
175 
176 // SMPTE ST 428-1, value 17.
177 static constexpr skcms_TransferFunction kSMPTE_ST_428_1 = {
178         2.6f, 1.034080527699f, 0.f, 0.f, 0.f, 0.f, 0.f};
179 
180 // Rec. ITU-R BT.2100-2 hybrid log-gamma (HLG) system, value 18.
181 static constexpr skcms_TransferFunction kHLG =
182     {-3.0f, 2.0f, 2.0f, 1/0.17883277f, 0.28466892f, 0.55991073f, 0.0f };
183 
184 // Mapping between transfer function names and the number of the corresponding
185 // row in ITU-T H.273, table 3.  As above, the constants are named based on the
186 // first specification referenced in the value's row.
187 enum class CicpId : uint8_t {
188     // Value 0 is reserved.
189     kRec709 = 1,
190     // Value 2 is unspecified.
191     // Value 3 is reserved.
192     kRec470SystemM = 4,
193     kRec470SystemBG = 5,
194     kRec601 = 6,
195     kSMPTE_ST_240 = 7,
196     kLinear = 8,
197     // Value 9 is not supported by `SkColorSpace::MakeCICP`.
198     // Value 10 is not supported by `SkColorSpace::MakeCICP`.
199     kIEC61966_2_4 = 11,
200     // Value 12 is not supported by `SkColorSpace::MakeCICP`.
201     kIEC61966_2_1 = 13,
202     kSRGB = kIEC61966_2_1,
203     kRec2020_10bit = 14,
204     kRec2020_12bit = 15,
205     kPQ = 16,
206     kSMPTE_ST_428_1 = 17,
207     kHLG = 18,
208     // Values 19-255 are reserved.
209 };
210 
211 }  // namespace SkNamedTransferFn
212 
213 namespace SkNamedGamut {
214 
215 static constexpr skcms_Matrix3x3 kSRGB = {{
216     // ICC fixed-point (16.16) representation, taken from skcms. Please keep them exactly in sync.
217     // 0.436065674f, 0.385147095f, 0.143066406f,
218     // 0.222488403f, 0.716873169f, 0.060607910f,
219     // 0.013916016f, 0.097076416f, 0.714096069f,
220     { SkFixedToFloat(0x6FA2), SkFixedToFloat(0x6299), SkFixedToFloat(0x24A0) },
221     { SkFixedToFloat(0x38F5), SkFixedToFloat(0xB785), SkFixedToFloat(0x0F84) },
222     { SkFixedToFloat(0x0390), SkFixedToFloat(0x18DA), SkFixedToFloat(0xB6CF) },
223 }};
224 
225 static constexpr skcms_Matrix3x3 kAdobeRGB = {{
226     // ICC fixed-point (16.16) repesentation of:
227     // 0.60974, 0.20528, 0.14919,
228     // 0.31111, 0.62567, 0.06322,
229     // 0.01947, 0.06087, 0.74457,
230     { SkFixedToFloat(0x9c18), SkFixedToFloat(0x348d), SkFixedToFloat(0x2631) },
231     { SkFixedToFloat(0x4fa5), SkFixedToFloat(0xa02c), SkFixedToFloat(0x102f) },
232     { SkFixedToFloat(0x04fc), SkFixedToFloat(0x0f95), SkFixedToFloat(0xbe9c) },
233 }};
234 
235 static constexpr skcms_Matrix3x3 kDisplayP3 = {{
236     {  0.515102f,   0.291965f,  0.157153f  },
237     {  0.241182f,   0.692236f,  0.0665819f },
238     { -0.00104941f, 0.0418818f, 0.784378f  },
239 }};
240 
241 static constexpr skcms_Matrix3x3 kRec2020 = {{
242     {  0.673459f,   0.165661f,  0.125100f  },
243     {  0.279033f,   0.675338f,  0.0456288f },
244     { -0.00193139f, 0.0299794f, 0.797162f  },
245 }};
246 
247 static constexpr skcms_Matrix3x3 kXYZ = {{
248     { 1.0f, 0.0f, 0.0f },
249     { 0.0f, 1.0f, 0.0f },
250     { 0.0f, 0.0f, 1.0f },
251 }};
252 
253 }  // namespace SkNamedGamut
254 
255 class SK_API SkColorSpace : public SkNVRefCnt<SkColorSpace> {
256 public:
257     /**
258      *  Create the sRGB color space.
259      */
260     static sk_sp<SkColorSpace> MakeSRGB();
261 
262     /**
263      *  Colorspace with the sRGB primaries, but a linear (1.0) gamma.
264      */
265     static sk_sp<SkColorSpace> MakeSRGBLinear();
266 
267     /**
268      *  Create an SkColorSpace from a transfer function and a row-major 3x3 transformation to XYZ.
269      */
270     static sk_sp<SkColorSpace> MakeRGB(const skcms_TransferFunction& transferFn,
271                                        const skcms_Matrix3x3& toXYZ);
272 
273     /**
274      *  Create an SkColorSpace from code points specified in Rec. ITU-T H.273.
275      *  Null will be returned for invalid or unsupported combination of code
276      *  points.
277      *
278      *  Parameters:
279      *
280      * - `color_primaries` identifies an entry in Rec. ITU-T H.273, Table 2.
281      * - `transfer_characteristics` identifies an entry in Rec. ITU-T H.273, Table 3.
282      *
283      * `SkColorSpace` (and the underlying `skcms_ICCProfile`) only supports RGB
284      * color spaces and therefore this function does not take a
285      * `matrix_coefficients` parameter - the caller is expected to verify that
286      * `matrix_coefficients` is `0`.
287      *
288      * Narrow range images are extremely rare - see
289      * https://github.com/w3c/png/issues/312#issuecomment-2327349614.  Therefore
290      * this function doesn't take a `video_full_range_flag` - the caller is
291      * expected to verify that it is `1` (indicating a full range image).
292      */
293     static sk_sp<SkColorSpace> MakeCICP(SkNamedPrimaries::CicpId color_primaries,
294                                         SkNamedTransferFn::CicpId transfer_characteristics);
295 
296     /**
297      *  Create an SkColorSpace from a parsed (skcms) ICC profile.
298      */
299     static sk_sp<SkColorSpace> Make(const skcms_ICCProfile&);
300 
301     /**
302      *  Convert this color space to an skcms ICC profile struct.
303      */
304     void toProfile(skcms_ICCProfile*) const;
305 
306     /**
307      *  Returns true if the color space gamma is near enough to be approximated as sRGB.
308      */
309     bool gammaCloseToSRGB() const;
310 
311     /**
312      *  Returns true if the color space gamma is linear.
313      */
314     bool gammaIsLinear() const;
315 
316     /**
317      *  Sets |fn| to the transfer function from this color space. Returns true if the transfer
318      *  function can be represented as coefficients to the standard ICC 7-parameter equation.
319      *  Returns false otherwise (eg, PQ, HLG).
320      */
321     bool isNumericalTransferFn(skcms_TransferFunction* fn) const;
322 
323     /**
324      *  Returns true and sets |toXYZD50|.
325      */
326     bool toXYZD50(skcms_Matrix3x3* toXYZD50) const;
327 
328     /**
329      *  Returns a hash of the gamut transformation to XYZ D50. Allows for fast equality checking
330      *  of gamuts, at the (very small) risk of collision.
331      */
toXYZD50Hash()332     uint32_t toXYZD50Hash() const { return fToXYZD50Hash; }
333 
334     /**
335      *  Returns a color space with the same gamut as this one, but with a linear gamma.
336      */
337     sk_sp<SkColorSpace> makeLinearGamma() const;
338 
339     /**
340      *  Returns a color space with the same gamut as this one, but with the sRGB transfer
341      *  function.
342      */
343     sk_sp<SkColorSpace> makeSRGBGamma() const;
344 
345     /**
346      *  Returns a color space with the same transfer function as this one, but with the primary
347      *  colors rotated. In other words, this produces a new color space that maps RGB to GBR
348      *  (when applied to a source), and maps RGB to BRG (when applied to a destination).
349      *
350      *  This is used for testing, to construct color spaces that have severe and testable behavior.
351      */
352     sk_sp<SkColorSpace> makeColorSpin() const;
353 
354     /**
355      *  Returns true if the color space is sRGB.
356      *  Returns false otherwise.
357      *
358      *  This allows a little bit of tolerance, given that we might see small numerical error
359      *  in some cases: converting ICC fixed point to float, converting white point to D50,
360      *  rounding decisions on transfer function and matrix.
361      *
362      *  This does not consider a 2.2f exponential transfer function to be sRGB. While these
363      *  functions are similar (and it is sometimes useful to consider them together), this
364      *  function checks for logical equality.
365      */
366     bool isSRGB() const;
367 
368     /**
369      *  Returns a serialized representation of this color space.
370      */
371     sk_sp<SkData> serialize() const;
372 
373     /**
374      *  If |memory| is nullptr, returns the size required to serialize.
375      *  Otherwise, serializes into |memory| and returns the size.
376      */
377     size_t writeToMemory(void* memory) const;
378 
379     static sk_sp<SkColorSpace> Deserialize(const void* data, size_t length);
380 
381     /**
382      *  If both are null, we return true. If one is null and the other is not, we return false.
383      *  If both are non-null, we do a deeper compare.
384      */
385     static bool Equals(const SkColorSpace*, const SkColorSpace*);
386 
387     void       transferFn(float gabcdef[7]) const;  // DEPRECATED: Remove when webview usage is gone
388     void       transferFn(skcms_TransferFunction* fn) const;
389     void    invTransferFn(skcms_TransferFunction* fn) const;
390     void gamutTransformTo(const SkColorSpace* dst, skcms_Matrix3x3* src_to_dst) const;
391 
transferFnHash()392     uint32_t transferFnHash() const { return fTransferFnHash; }
hash()393     uint64_t           hash() const { return (uint64_t)fTransferFnHash << 32 | fToXYZD50Hash; }
394 
395 private:
396     friend class SkColorSpaceSingletonFactory;
397 
398     SkColorSpace(const skcms_TransferFunction& transferFn, const skcms_Matrix3x3& toXYZ);
399 
400     void computeLazyDstFields() const;
401 
402     uint32_t                            fTransferFnHash;
403     uint32_t                            fToXYZD50Hash;
404 
405     skcms_TransferFunction              fTransferFn;
406     skcms_Matrix3x3                     fToXYZD50;
407 
408     mutable skcms_TransferFunction      fInvTransferFn;
409     mutable skcms_Matrix3x3             fFromXYZD50;
410     mutable SkOnce                      fLazyDstFieldsOnce;
411 };
412 
413 #endif
414