• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // https://www.w3.org/TR/css-color-4/#predefined-prophoto-rgb
116 static constexpr SkColorSpacePrimaries kProPhotoRGB = {
117         0.7347f, 0.2653f, 0.1596f, 0.8404f, 0.0366f, 0.0001f, 0.34567f, 0.35850f};
118 
119 }  // namespace SkNamedPrimaries
120 
121 namespace SkNamedTransferFn {
122 
123 // Like SkNamedGamut::kSRGB, keeping this bitwise exactly the same as skcms makes things fastest.
124 static constexpr skcms_TransferFunction kSRGB =
125     { 2.4f, (float)(1/1.055), (float)(0.055/1.055), (float)(1/12.92), 0.04045f, 0.0f, 0.0f };
126 
127 static constexpr skcms_TransferFunction k2Dot2 =
128     { 2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
129 
130 static constexpr skcms_TransferFunction kRec2020 = {
131         2.22222f, 0.909672f, 0.0903276f, 0.222222f, 0.0812429f, 0, 0};
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 // Color primaries defined by ITU-T H.273, table 3. Names are given by the first
135 // specification referenced in the value's row.
136 
137 // Rec. ITU-R BT.709-6, value 1.
138 static constexpr skcms_TransferFunction kRec709 = {2.222222222222f,
139                                                    0.909672415686f,
140                                                    0.090327584314f,
141                                                    0.222222222222f,
142                                                    0.081242858299f,
143                                                    0.f,
144                                                    0.f};
145 
146 // Rec. ITU-R BT.470-6 System M (historical) assumed display gamma 2.2, value 4.
147 static constexpr skcms_TransferFunction kRec470SystemM = {2.2f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f};
148 
149 // Rec. ITU-R BT.470-6 System B, G (historical) assumed display gamma 2.8,
150 // value 5.
151 static constexpr skcms_TransferFunction kRec470SystemBG = {2.8f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f};
152 
153 // Rec. ITU-R BT.601-7, same as kRec709, value 6.
154 static constexpr skcms_TransferFunction kRec601 = kRec709;
155 
156 // SMPTE ST 240, value 7.
157 static constexpr skcms_TransferFunction kSMPTE_ST_240 = {
158         2.222222222222f, 0.899626676224f, 0.100373323776f, 0.25f, 0.091286342118f, 0.f, 0.f};
159 
160 // Linear, value 8
161 static constexpr skcms_TransferFunction kLinear =
162     { 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
163 
164 // IEC 61966-2-4, value 11, same as kRec709 (but is explicitly extended).
165 static constexpr skcms_TransferFunction kIEC61966_2_4 = kRec709;
166 
167 // IEC 61966-2-1 sRGB, value 13.
168 static constexpr skcms_TransferFunction kIEC61966_2_1 = kSRGB;
169 
170 // Rec. ITU-R BT.2020-2 (10-bit system), value 14.
171 static constexpr skcms_TransferFunction kRec2020_10bit = kRec709;
172 
173 // Rec. ITU-R BT.2020-2 (12-bit system), value 15.
174 static constexpr skcms_TransferFunction kRec2020_12bit = kRec709;
175 
176 // Rec. ITU-R BT.2100-2 perceptual quantization (PQ) system, value 16.
177 static constexpr skcms_TransferFunction kPQ =
178     {-2.0f, -107/128.0f, 1.0f, 32/2523.0f, 2413/128.0f, -2392/128.0f, 8192/1305.0f };
179 
180 // SMPTE ST 428-1, value 17.
181 static constexpr skcms_TransferFunction kSMPTE_ST_428_1 = {
182         2.6f, 1.034080527699f, 0.f, 0.f, 0.f, 0.f, 0.f};
183 
184 // Rec. ITU-R BT.2100-2 hybrid log-gamma (HLG) system, value 18.
185 static constexpr skcms_TransferFunction kHLG =
186     {-3.0f, 2.0f, 2.0f, 1/0.17883277f, 0.28466892f, 0.55991073f, 0.0f };
187 
188 // Mapping between transfer function names and the number of the corresponding
189 // row in ITU-T H.273, table 3.  As above, the constants are named based on the
190 // first specification referenced in the value's row.
191 enum class CicpId : uint8_t {
192     // Value 0 is reserved.
193     kRec709 = 1,
194     // Value 2 is unspecified.
195     // Value 3 is reserved.
196     kRec470SystemM = 4,
197     kRec470SystemBG = 5,
198     kRec601 = 6,
199     kSMPTE_ST_240 = 7,
200     kLinear = 8,
201     // Value 9 is not supported by `SkColorSpace::MakeCICP`.
202     // Value 10 is not supported by `SkColorSpace::MakeCICP`.
203     kIEC61966_2_4 = 11,
204     // Value 12 is not supported by `SkColorSpace::MakeCICP`.
205     kIEC61966_2_1 = 13,
206     kSRGB = kIEC61966_2_1,
207     kRec2020_10bit = 14,
208     kRec2020_12bit = 15,
209     kPQ = 16,
210     kSMPTE_ST_428_1 = 17,
211     kHLG = 18,
212     // Values 19-255 are reserved.
213 };
214 
215 // https://w3.org/TR/css-color-4/#valdef-color-prophoto-rgb
216 // "The transfer curve is a gamma function with a value of 1/1.8"
217 static constexpr skcms_TransferFunction kProPhotoRGB = {1.8f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
218 
219 // https://www.w3.org/TR/css-color-4/#predefined-a98-rgb
220 static constexpr skcms_TransferFunction kA98RGB = k2Dot2;
221 
222 }  // namespace SkNamedTransferFn
223 
224 namespace SkNamedGamut {
225 
226 static constexpr skcms_Matrix3x3 kSRGB = {{
227     // ICC fixed-point (16.16) representation, taken from skcms. Please keep them exactly in sync.
228     // 0.436065674f, 0.385147095f, 0.143066406f,
229     // 0.222488403f, 0.716873169f, 0.060607910f,
230     // 0.013916016f, 0.097076416f, 0.714096069f,
231     { SkFixedToFloat(0x6FA2), SkFixedToFloat(0x6299), SkFixedToFloat(0x24A0) },
232     { SkFixedToFloat(0x38F5), SkFixedToFloat(0xB785), SkFixedToFloat(0x0F84) },
233     { SkFixedToFloat(0x0390), SkFixedToFloat(0x18DA), SkFixedToFloat(0xB6CF) },
234 }};
235 
236 static constexpr skcms_Matrix3x3 kAdobeRGB = {{
237     // ICC fixed-point (16.16) repesentation of:
238     // 0.60974, 0.20528, 0.14919,
239     // 0.31111, 0.62567, 0.06322,
240     // 0.01947, 0.06087, 0.74457,
241     { SkFixedToFloat(0x9c18), SkFixedToFloat(0x348d), SkFixedToFloat(0x2631) },
242     { SkFixedToFloat(0x4fa5), SkFixedToFloat(0xa02c), SkFixedToFloat(0x102f) },
243     { SkFixedToFloat(0x04fc), SkFixedToFloat(0x0f95), SkFixedToFloat(0xbe9c) },
244 }};
245 
246 static constexpr skcms_Matrix3x3 kDisplayP3 = {{
247     {  0.515102f,   0.291965f,  0.157153f  },
248     {  0.241182f,   0.692236f,  0.0665819f },
249     { -0.00104941f, 0.0418818f, 0.784378f  },
250 }};
251 
252 static constexpr skcms_Matrix3x3 kRec2020 = {{
253     {  0.673459f,   0.165661f,  0.125100f  },
254     {  0.279033f,   0.675338f,  0.0456288f },
255     { -0.00193139f, 0.0299794f, 0.797162f  },
256 }};
257 
258 static constexpr skcms_Matrix3x3 kXYZ = {{
259     { 1.0f, 0.0f, 0.0f },
260     { 0.0f, 1.0f, 0.0f },
261     { 0.0f, 0.0f, 1.0f },
262 }};
263 
264 }  // namespace SkNamedGamut
265 
266 class SK_API SkColorSpace : public SkNVRefCnt<SkColorSpace> {
267 public:
268     /**
269      *  Create the sRGB color space.
270      */
271     static sk_sp<SkColorSpace> MakeSRGB();
272 
273     /**
274      *  Colorspace with the sRGB primaries, but a linear (1.0) gamma.
275      */
276     static sk_sp<SkColorSpace> MakeSRGBLinear();
277 
278     /**
279      *  Create an SkColorSpace from a transfer function and a row-major 3x3 transformation to XYZ.
280      */
281     static sk_sp<SkColorSpace> MakeRGB(const skcms_TransferFunction& transferFn,
282                                        const skcms_Matrix3x3& toXYZ);
283 
284     /**
285      *  Create an SkColorSpace from code points specified in Rec. ITU-T H.273.
286      *  Null will be returned for invalid or unsupported combination of code
287      *  points.
288      *
289      *  Parameters:
290      *
291      * - `color_primaries` identifies an entry in Rec. ITU-T H.273, Table 2.
292      * - `transfer_characteristics` identifies an entry in Rec. ITU-T H.273, Table 3.
293      *
294      * `SkColorSpace` (and the underlying `skcms_ICCProfile`) only supports RGB
295      * color spaces and therefore this function does not take a
296      * `matrix_coefficients` parameter - the caller is expected to verify that
297      * `matrix_coefficients` is `0`.
298      *
299      * Narrow range images are extremely rare - see
300      * https://github.com/w3c/png/issues/312#issuecomment-2327349614.  Therefore
301      * this function doesn't take a `video_full_range_flag` - the caller is
302      * expected to verify that it is `1` (indicating a full range image).
303      */
304     static sk_sp<SkColorSpace> MakeCICP(SkNamedPrimaries::CicpId color_primaries,
305                                         SkNamedTransferFn::CicpId transfer_characteristics);
306 
307     /**
308      *  Create an SkColorSpace from a parsed (skcms) ICC profile.
309      */
310     static sk_sp<SkColorSpace> Make(const skcms_ICCProfile&);
311 
312     /**
313      *  Convert this color space to an skcms ICC profile struct.
314      */
315     void toProfile(skcms_ICCProfile*) const;
316 
317     /**
318      *  Returns true if the color space gamma is near enough to be approximated as sRGB.
319      */
320     bool gammaCloseToSRGB() const;
321 
322     /**
323      *  Returns true if the color space gamma is linear.
324      */
325     bool gammaIsLinear() const;
326 
327     /**
328      *  Sets |fn| to the transfer function from this color space. Returns true if the transfer
329      *  function can be represented as coefficients to the standard ICC 7-parameter equation.
330      *  Returns false otherwise (eg, PQ, HLG).
331      */
332     bool isNumericalTransferFn(skcms_TransferFunction* fn) const;
333 
334     /**
335      *  Returns true and sets |toXYZD50|.
336      */
337     bool toXYZD50(skcms_Matrix3x3* toXYZD50) const;
338 
339     /**
340      *  Returns a hash of the gamut transformation to XYZ D50. Allows for fast equality checking
341      *  of gamuts, at the (very small) risk of collision.
342      */
toXYZD50Hash()343     uint32_t toXYZD50Hash() const { return fToXYZD50Hash; }
344 
345     /**
346      *  Returns a color space with the same gamut as this one, but with a linear gamma.
347      */
348     sk_sp<SkColorSpace> makeLinearGamma() const;
349 
350     /**
351      *  Returns a color space with the same gamut as this one, but with the sRGB transfer
352      *  function.
353      */
354     sk_sp<SkColorSpace> makeSRGBGamma() const;
355 
356     /**
357      *  Returns a color space with the same transfer function as this one, but with the primary
358      *  colors rotated. In other words, this produces a new color space that maps RGB to GBR
359      *  (when applied to a source), and maps RGB to BRG (when applied to a destination).
360      *
361      *  This is used for testing, to construct color spaces that have severe and testable behavior.
362      */
363     sk_sp<SkColorSpace> makeColorSpin() const;
364 
365     /**
366      *  Returns true if the color space is sRGB.
367      *  Returns false otherwise.
368      *
369      *  This allows a little bit of tolerance, given that we might see small numerical error
370      *  in some cases: converting ICC fixed point to float, converting white point to D50,
371      *  rounding decisions on transfer function and matrix.
372      *
373      *  This does not consider a 2.2f exponential transfer function to be sRGB. While these
374      *  functions are similar (and it is sometimes useful to consider them together), this
375      *  function checks for logical equality.
376      */
377     bool isSRGB() const;
378 
379     /**
380      *  Returns a serialized representation of this color space.
381      */
382     sk_sp<SkData> serialize() const;
383 
384     /**
385      *  If |memory| is nullptr, returns the size required to serialize.
386      *  Otherwise, serializes into |memory| and returns the size.
387      */
388     size_t writeToMemory(void* memory) const;
389 
390     static sk_sp<SkColorSpace> Deserialize(const void* data, size_t length);
391 
392     /**
393      *  If both are null, we return true. If one is null and the other is not, we return false.
394      *  If both are non-null, we do a deeper compare.
395      */
396     static bool Equals(const SkColorSpace*, const SkColorSpace*);
397 
398     void       transferFn(float gabcdef[7]) const;  // DEPRECATED: Remove when webview usage is gone
399     void       transferFn(skcms_TransferFunction* fn) const;
400     void    invTransferFn(skcms_TransferFunction* fn) const;
401     void gamutTransformTo(const SkColorSpace* dst, skcms_Matrix3x3* src_to_dst) const;
402 
transferFnHash()403     uint32_t transferFnHash() const { return fTransferFnHash; }
hash()404     uint64_t           hash() const { return (uint64_t)fTransferFnHash << 32 | fToXYZD50Hash; }
405 
406 private:
407     friend class SkColorSpaceSingletonFactory;
408 
409     SkColorSpace(const skcms_TransferFunction& transferFn, const skcms_Matrix3x3& toXYZ);
410 
411     void computeLazyDstFields() const;
412 
413     uint32_t                            fTransferFnHash;
414     uint32_t                            fToXYZD50Hash;
415 
416     skcms_TransferFunction              fTransferFn;
417     skcms_Matrix3x3                     fToXYZD50;
418 
419     mutable skcms_TransferFunction      fInvTransferFn;
420     mutable skcms_Matrix3x3             fFromXYZD50;
421     mutable SkOnce                      fLazyDstFieldsOnce;
422 };
423 
424 #endif
425