Debian Codegraph @

include/core/SkColorSpace.h

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