Skia
2D Graphics Library
SkColorSpace.h
Go to the documentation of this file.
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/private/SkFixed.h"
13 #include "include/private/SkOnce.h"
14 #include "include/third_party/skcms/skcms.h"
15 #include <memory>
16 
17 class SkData;
18 
23  float fRX;
24  float fRY;
25  float fGX;
26  float fGY;
27  float fBX;
28  float fBY;
29  float fWX;
30  float fWY;
31 
36  bool toXYZD50(skcms_Matrix3x3* toXYZD50) const;
37 };
38 
39 namespace SkNamedTransferFn {
40 
41 // Like SkNamedGamut::kSRGB, keeping this bitwise exactly the same as skcms makes things fastest.
42 static constexpr skcms_TransferFunction kSRGB =
43  { 2.4f, (float)(1/1.055), (float)(0.055/1.055), (float)(1/12.92), 0.04045f, 0.0f, 0.0f };
44 
45 static constexpr skcms_TransferFunction k2Dot2 =
46  { 2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
47 
48 static constexpr skcms_TransferFunction kLinear =
49  { 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
50 
51 static constexpr skcms_TransferFunction kRec2020 =
52  {2.22222f, 0.909672f, 0.0903276f, 0.222222f, 0.0812429f, 0, 0};
53 
54 static constexpr skcms_TransferFunction kPQ =
55  {-2.0f, -107/128.0f, 1.0f, 32/2523.0f, 2413/128.0f, -2392/128.0f, 8192/1305.0f };
56 
57 static constexpr skcms_TransferFunction kHLG =
58  {-3.0f, 2.0f, 2.0f, 1/0.17883277f, 0.28466892f, 0.55991073f, 0.0f };
59 
60 } // namespace SkNamedTransferFn
61 
62 namespace SkNamedGamut {
63 
64 static constexpr skcms_Matrix3x3 kSRGB = {{
65  // ICC fixed-point (16.16) representation, taken from skcms. Please keep them exactly in sync.
66  // 0.436065674f, 0.385147095f, 0.143066406f,
67  // 0.222488403f, 0.716873169f, 0.060607910f,
68  // 0.013916016f, 0.097076416f, 0.714096069f,
69  { SkFixedToFloat(0x6FA2), SkFixedToFloat(0x6299), SkFixedToFloat(0x24A0) },
70  { SkFixedToFloat(0x38F5), SkFixedToFloat(0xB785), SkFixedToFloat(0x0F84) },
71  { SkFixedToFloat(0x0390), SkFixedToFloat(0x18DA), SkFixedToFloat(0xB6CF) },
72 }};
73 
74 static constexpr skcms_Matrix3x3 kAdobeRGB = {{
75  // ICC fixed-point (16.16) repesentation of:
76  // 0.60974, 0.20528, 0.14919,
77  // 0.31111, 0.62567, 0.06322,
78  // 0.01947, 0.06087, 0.74457,
79  { SkFixedToFloat(0x9c18), SkFixedToFloat(0x348d), SkFixedToFloat(0x2631) },
80  { SkFixedToFloat(0x4fa5), SkFixedToFloat(0xa02c), SkFixedToFloat(0x102f) },
81  { SkFixedToFloat(0x04fc), SkFixedToFloat(0x0f95), SkFixedToFloat(0xbe9c) },
82 }};
83 
84 static constexpr skcms_Matrix3x3 kDisplayP3 = {{
85  { 0.515102f, 0.291965f, 0.157153f },
86  { 0.241182f, 0.692236f, 0.0665819f },
87  { -0.00104941f, 0.0418818f, 0.784378f },
88 }};
89 
90 static constexpr skcms_Matrix3x3 kRec2020 = {{
91  { 0.673459f, 0.165661f, 0.125100f },
92  { 0.279033f, 0.675338f, 0.0456288f },
93  { -0.00193139f, 0.0299794f, 0.797162f },
94 }};
95 
96 static constexpr skcms_Matrix3x3 kXYZ = {{
97  { 1.0f, 0.0f, 0.0f },
98  { 0.0f, 1.0f, 0.0f },
99  { 0.0f, 0.0f, 1.0f },
100 }};
101 
102 } // namespace SkNamedGamut
103 
104 class SK_API SkColorSpace : public SkNVRefCnt<SkColorSpace> {
105 public:
110 
115 
119  static sk_sp<SkColorSpace> MakeRGB(const skcms_TransferFunction& transferFn,
120  const skcms_Matrix3x3& toXYZ);
121 
125  static sk_sp<SkColorSpace> Make(const skcms_ICCProfile&);
126 
130  void toProfile(skcms_ICCProfile*) const;
131 
135  bool gammaCloseToSRGB() const;
136 
140  bool gammaIsLinear() const;
141 
147  bool isNumericalTransferFn(skcms_TransferFunction* fn) const;
148 
153  bool toXYZD50(skcms_Matrix3x3* toXYZD50) const;
154 
159  uint32_t toXYZD50Hash() const { return fToXYZD50Hash; }
160 
167 
174 
184 
197  bool isSRGB() const;
198 
204 
209  size_t writeToMemory(void* memory) const;
210 
211  static sk_sp<SkColorSpace> Deserialize(const void* data, size_t length);
212 
217  static bool Equals(const SkColorSpace*, const SkColorSpace*);
218 
219  void transferFn(float gabcdef[7]) const; // DEPRECATED: Remove when webview usage is gone
220  void transferFn(skcms_TransferFunction* fn) const;
221  void invTransferFn(skcms_TransferFunction* fn) const;
222  void gamutTransformTo(const SkColorSpace* dst, skcms_Matrix3x3* src_to_dst) const;
223 
224  uint32_t transferFnHash() const { return fTransferFnHash; }
225  uint64_t hash() const { return (uint64_t)fTransferFnHash << 32 | fToXYZD50Hash; }
226 
227 private:
228  friend class SkColorSpaceSingletonFactory;
229 
230  SkColorSpace(const skcms_TransferFunction& transferFn, const skcms_Matrix3x3& toXYZ);
231 
232  void computeLazyDstFields() const;
233 
234  uint32_t fTransferFnHash;
235  uint32_t fToXYZD50Hash;
236 
237  skcms_TransferFunction fTransferFn;
238  skcms_Matrix3x3 fToXYZD50;
239 
240  mutable skcms_TransferFunction fInvTransferFn;
241  mutable skcms_Matrix3x3 fFromXYZD50;
242  mutable SkOnce fLazyDstFieldsOnce;
243 };
244 
245 #endif
SkColorSpace::toXYZD50
bool toXYZD50(skcms_Matrix3x3 *toXYZD50) const
Returns true and sets |toXYZD50| if the color gamut can be described as a matrix.
SkNamedTransferFn
Definition: SkColorSpace.h:39
SkColorSpace::gammaCloseToSRGB
bool gammaCloseToSRGB() const
Returns true if the color space gamma is near enough to be approximated as sRGB.
SkColorSpace::toProfile
void toProfile(skcms_ICCProfile *) const
Convert this color space to an skcms ICC profile struct.
SkColorSpace::transferFn
void transferFn(skcms_TransferFunction *fn) const
SkNamedGamut
Definition: SkColorSpace.h:62
SkColorSpace::gammaIsLinear
bool gammaIsLinear() const
Returns true if the color space gamma is linear.
SkColorSpace::MakeRGB
static sk_sp< SkColorSpace > MakeRGB(const skcms_TransferFunction &transferFn, const skcms_Matrix3x3 &toXYZ)
Create an SkColorSpace from a transfer function and a row-major 3x3 transformation to XYZ.
SkNamedGamut::kDisplayP3
static constexpr skcms_Matrix3x3 kDisplayP3
Definition: SkColorSpace.h:84
SkColorSpace::serialize
sk_sp< SkData > serialize() const
Returns nullptr on failure.
SkColorSpacePrimaries::fBY
float fBY
Definition: SkColorSpace.h:28
SkNVRefCnt
Definition: SkRefCnt.h:159
SkColorSpace::transferFnHash
uint32_t transferFnHash() const
Definition: SkColorSpace.h:224
SkColorSpace::invTransferFn
void invTransferFn(skcms_TransferFunction *fn) const
SkColorSpacePrimaries::toXYZD50
bool toXYZD50(skcms_Matrix3x3 *toXYZD50) const
Convert primaries and a white point to a toXYZD50 matrix, the preferred color gamut representation of...
SkNamedTransferFn::kSRGB
static constexpr skcms_TransferFunction kSRGB
Definition: SkColorSpace.h:42
SkColorSpace::writeToMemory
size_t writeToMemory(void *memory) const
If |memory| is nullptr, returns the size required to serialize.
SkColorSpace::makeSRGBGamma
sk_sp< SkColorSpace > makeSRGBGamma() const
Returns a color space with the same gamut as this one, with with the sRGB transfer function.
SkNamedTransferFn::kHLG
static constexpr skcms_TransferFunction kHLG
Definition: SkColorSpace.h:57
SkColorSpace::isNumericalTransferFn
bool isNumericalTransferFn(skcms_TransferFunction *fn) const
Sets |fn| to the transfer function from this color space.
SkData
SkData holds an immutable data buffer.
Definition: SkData.h:22
SkRefCnt.h
SkColorSpace::Make
static sk_sp< SkColorSpace > Make(const skcms_ICCProfile &)
Create an SkColorSpace from a parsed (skcms) ICC profile.
sk_sp< SkColorSpace >
SkColorSpace::makeLinearGamma
sk_sp< SkColorSpace > makeLinearGamma() const
Returns a color space with the same gamut as this one, but with a linear gamma.
SkNamedTransferFn::kLinear
static constexpr skcms_TransferFunction kLinear
Definition: SkColorSpace.h:48
SkNamedTransferFn::kPQ
static constexpr skcms_TransferFunction kPQ
Definition: SkColorSpace.h:54
SK_API
#define SK_API
Definition: SkTypes.h:181
SkColorSpace::transferFn
void transferFn(float gabcdef[7]) const
SkColorSpace::hash
uint64_t hash() const
Definition: SkColorSpace.h:225
SkColorSpace::isSRGB
bool isSRGB() const
Returns true if the color space is sRGB.
SkColorSpace::MakeSRGB
static sk_sp< SkColorSpace > MakeSRGB()
Create the sRGB color space.
SkColorSpacePrimaries
Describes a color gamut with primaries and a white point.
Definition: SkColorSpace.h:22
SkNamedGamut::kXYZ
static constexpr skcms_Matrix3x3 kXYZ
Definition: SkColorSpace.h:96
SkColorSpacePrimaries::fGY
float fGY
Definition: SkColorSpace.h:26
SkNamedGamut::kRec2020
static constexpr skcms_Matrix3x3 kRec2020
Definition: SkColorSpace.h:90
SkColorSpace::Equals
static bool Equals(const SkColorSpace *, const SkColorSpace *)
If both are null, we return true.
SkColorSpace::gamutTransformTo
void gamutTransformTo(const SkColorSpace *dst, skcms_Matrix3x3 *src_to_dst) const
SkNamedTransferFn::kRec2020
static constexpr skcms_TransferFunction kRec2020
Definition: SkColorSpace.h:51
SkColorSpacePrimaries::fGX
float fGX
Definition: SkColorSpace.h:25
SkColorSpace::makeColorSpin
sk_sp< SkColorSpace > makeColorSpin() const
Returns a color space with the same transfer function as this one, but with the primary colors rotate...
SkColorSpace::MakeSRGBLinear
static sk_sp< SkColorSpace > MakeSRGBLinear()
Colorspace with the sRGB primaries, but a linear (1.0) gamma.
SkNamedGamut::kAdobeRGB
static constexpr skcms_Matrix3x3 kAdobeRGB
Definition: SkColorSpace.h:74
SkColorSpace::toXYZD50Hash
uint32_t toXYZD50Hash() const
Returns a hash of the gamut transformation to XYZ D50.
Definition: SkColorSpace.h:159
SkColorSpace::Deserialize
static sk_sp< SkColorSpace > Deserialize(const void *data, size_t length)
SkColorSpacePrimaries::fRY
float fRY
Definition: SkColorSpace.h:24
SkColorSpacePrimaries::fWX
float fWX
Definition: SkColorSpace.h:29
SkNamedTransferFn::k2Dot2
static constexpr skcms_TransferFunction k2Dot2
Definition: SkColorSpace.h:45
SkNamedGamut::kSRGB
static constexpr skcms_Matrix3x3 kSRGB
Definition: SkColorSpace.h:64
SkColorSpacePrimaries::fRX
float fRX
Definition: SkColorSpace.h:23
SkColorSpacePrimaries::fBX
float fBX
Definition: SkColorSpace.h:27
SkColorSpacePrimaries::fWY
float fWY
Definition: SkColorSpace.h:30
SkColorSpace
Definition: SkColorSpace.h:104