• 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/SkMatrix44.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/private/SkFixed.h"
14 #include "include/private/SkOnce.h"
15 #include "include/third_party/skcms/skcms.h"
16 #include <memory>
17 
18 class SkData;
19 
20 /**
21  *  Describes a color gamut with primaries and a white point.
22  */
23 struct SK_API SkColorSpacePrimaries {
24     float fRX;
25     float fRY;
26     float fGX;
27     float fGY;
28     float fBX;
29     float fBY;
30     float fWX;
31     float fWY;
32 
33     /**
34      *  Convert primaries and a white point to a toXYZD50 matrix, the preferred color gamut
35      *  representation of SkColorSpace.
36      */
37     bool toXYZD50(skcms_Matrix3x3* toXYZD50) const;
38 };
39 
40 namespace SkNamedTransferFn {
41 
42 // Like SkNamedGamut::kSRGB, keeping this bitwise exactly the same as skcms makes things fastest.
43 static constexpr skcms_TransferFunction kSRGB =
44     { 2.4f, (float)(1/1.055), (float)(0.055/1.055), (float)(1/12.92), 0.04045f, 0.0f, 0.0f };
45 
46 static constexpr skcms_TransferFunction k2Dot2 =
47     { 2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
48 
49 static constexpr skcms_TransferFunction kLinear =
50     { 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
51 
52 static constexpr skcms_TransferFunction kRec2020 =
53     {2.22222f, 0.909672f, 0.0903276f, 0.222222f, 0.0812429f, 0, 0};
54 }
55 
56 namespace SkNamedGamut {
57 
58 static constexpr skcms_Matrix3x3 kSRGB = {{
59     // ICC fixed-point (16.16) representation, taken from skcms. Please keep them exactly in sync.
60     // 0.436065674f, 0.385147095f, 0.143066406f,
61     // 0.222488403f, 0.716873169f, 0.060607910f,
62     // 0.013916016f, 0.097076416f, 0.714096069f,
63     { SkFixedToFloat(0x6FA2), SkFixedToFloat(0x6299), SkFixedToFloat(0x24A0) },
64     { SkFixedToFloat(0x38F5), SkFixedToFloat(0xB785), SkFixedToFloat(0x0F84) },
65     { SkFixedToFloat(0x0390), SkFixedToFloat(0x18DA), SkFixedToFloat(0xB6CF) },
66 }};
67 
68 static constexpr skcms_Matrix3x3 kAdobeRGB = {{
69     // ICC fixed-point (16.16) repesentation of:
70     // 0.60974, 0.20528, 0.14919,
71     // 0.31111, 0.62567, 0.06322,
72     // 0.01947, 0.06087, 0.74457,
73     { SkFixedToFloat(0x9c18), SkFixedToFloat(0x348d), SkFixedToFloat(0x2631) },
74     { SkFixedToFloat(0x4fa5), SkFixedToFloat(0xa02c), SkFixedToFloat(0x102f) },
75     { SkFixedToFloat(0x04fc), SkFixedToFloat(0x0f95), SkFixedToFloat(0xbe9c) },
76 }};
77 
78 static constexpr skcms_Matrix3x3 kDCIP3 = {{
79     {  0.515102f,   0.291965f,  0.157153f  },
80     {  0.241182f,   0.692236f,  0.0665819f },
81     { -0.00104941f, 0.0418818f, 0.784378f  },
82 }};
83 
84 static constexpr skcms_Matrix3x3 kRec2020 = {{
85     {  0.673459f,   0.165661f,  0.125100f  },
86     {  0.279033f,   0.675338f,  0.0456288f },
87     { -0.00193139f, 0.0299794f, 0.797162f  },
88 }};
89 
90 static constexpr skcms_Matrix3x3 kXYZ = {{
91     { 1.0f, 0.0f, 0.0f },
92     { 0.0f, 1.0f, 0.0f },
93     { 0.0f, 0.0f, 1.0f },
94 }};
95 
96 }
97 
98 class SK_API SkColorSpace : public SkNVRefCnt<SkColorSpace> {
99 public:
100     /**
101      *  Create the sRGB color space.
102      */
103     static sk_sp<SkColorSpace> MakeSRGB();
104 
105     /**
106      *  Colorspace with the sRGB primaries, but a linear (1.0) gamma.
107      */
108     static sk_sp<SkColorSpace> MakeSRGBLinear();
109 
110     /**
111      *  Create an SkColorSpace from a transfer function and a row-major 3x3 transformation to XYZ.
112      */
113     static sk_sp<SkColorSpace> MakeRGB(const skcms_TransferFunction& transferFn,
114                                        const skcms_Matrix3x3& toXYZ);
115 
116     /**
117      *  Create an SkColorSpace from a parsed (skcms) ICC profile.
118      */
119     static sk_sp<SkColorSpace> Make(const skcms_ICCProfile&);
120 
121     /**
122      *  Convert this color space to an skcms ICC profile struct.
123      */
124     void toProfile(skcms_ICCProfile*) const;
125 
126     /**
127      *  Returns true if the color space gamma is near enough to be approximated as sRGB.
128      */
129     bool gammaCloseToSRGB() const;
130 
131     /**
132      *  Returns true if the color space gamma is linear.
133      */
134     bool gammaIsLinear() const;
135 
136     /**
137      *  If the transfer function can be represented as coefficients to the standard
138      *  equation, returns true and sets |fn| to the proper values.
139      *
140      *  If not, returns false.
141      */
142     bool isNumericalTransferFn(skcms_TransferFunction* fn) const;
143 
144     /**
145      *  Returns true and sets |toXYZD50| if the color gamut can be described as a matrix.
146      *  Returns false otherwise.
147      */
148     bool toXYZD50(SkMatrix44* toXYZD50) const;
149 
150     bool toXYZD50(skcms_Matrix3x3* toXYZD50) const;
151 
152     /**
153      *  Returns a hash of the gamut transformation to XYZ D50. Allows for fast equality checking
154      *  of gamuts, at the (very small) risk of collision.
155      */
toXYZD50Hash()156     uint32_t toXYZD50Hash() const { return fToXYZD50Hash; }
157 
158     /**
159      *  Returns a color space with the same gamut as this one, but with a linear gamma.
160      *  For color spaces whose gamut can not be described in terms of XYZ D50, returns
161      *  linear sRGB.
162      */
163     sk_sp<SkColorSpace> makeLinearGamma() const;
164 
165     /**
166      *  Returns a color space with the same gamut as this one, with with the sRGB transfer
167      *  function. For color spaces whose gamut can not be described in terms of XYZ D50, returns
168      *  sRGB.
169      */
170     sk_sp<SkColorSpace> makeSRGBGamma() const;
171 
172     /**
173      *  Returns a color space with the same transfer function as this one, but with the primary
174      *  colors rotated. For any XYZ space, this produces a new color space that maps RGB to GBR
175      *  (when applied to a source), and maps RGB to BRG (when applied to a destination). For other
176      *  types of color spaces, returns nullptr.
177      *
178      *  This is used for testing, to construct color spaces that have severe and testable behavior.
179      */
180     sk_sp<SkColorSpace> makeColorSpin() const;
181 
182     /**
183      *  Returns true if the color space is sRGB.
184      *  Returns false otherwise.
185      *
186      *  This allows a little bit of tolerance, given that we might see small numerical error
187      *  in some cases: converting ICC fixed point to float, converting white point to D50,
188      *  rounding decisions on transfer function and matrix.
189      *
190      *  This does not consider a 2.2f exponential transfer function to be sRGB.  While these
191      *  functions are similar (and it is sometimes useful to consider them together), this
192      *  function checks for logical equality.
193      */
194     bool isSRGB() const;
195 
196     /**
197      *  Returns nullptr on failure.  Fails when we fallback to serializing ICC data and
198      *  the data is too large to serialize.
199      */
200     sk_sp<SkData> serialize() const;
201 
202     /**
203      *  If |memory| is nullptr, returns the size required to serialize.
204      *  Otherwise, serializes into |memory| and returns the size.
205      */
206     size_t writeToMemory(void* memory) const;
207 
208     static sk_sp<SkColorSpace> Deserialize(const void* data, size_t length);
209 
210     /**
211      *  If both are null, we return true.  If one is null and the other is not, we return false.
212      *  If both are non-null, we do a deeper compare.
213      */
214     static bool Equals(const SkColorSpace*, const SkColorSpace*);
215 
216     void       transferFn(float gabcdef[7]) const;
217     void    invTransferFn(float gabcdef[7]) const;
218     void gamutTransformTo(const SkColorSpace* dst, float src_to_dst_row_major[9]) const;
219 
transferFnHash()220     uint32_t transferFnHash() const { return fTransferFnHash; }
hash()221     uint64_t           hash() const { return (uint64_t)fTransferFnHash << 32 | fToXYZD50Hash; }
222 
223 private:
224     friend class SkColorSpaceSingletonFactory;
225 
226     SkColorSpace(const float transferFn[7],
227                  const skcms_Matrix3x3& toXYZ);
228 
229     void computeLazyDstFields() const;
230 
231     uint32_t                            fTransferFnHash;
232     uint32_t                            fToXYZD50Hash;
233 
234     float                               fTransferFn[7];
235     float                               fToXYZD50_3x3[9];    // row-major
236 
237     mutable float                       fInvTransferFn[7];
238     mutable float                       fFromXYZD50_3x3[9];  // row-major
239     mutable SkOnce                      fLazyDstFieldsOnce;
240 };
241 
242 #endif
243