• 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 #ifndef SkColorSpacePriv_DEFINED
8 #define SkColorSpacePriv_DEFINED
9 
10 #include <math.h>
11 
12 #include "SkColorSpace_Base.h"
13 
14 #define SkColorSpacePrintf(...)
15 
16 static constexpr float gSRGB_toXYZD50[] {
17     0.4360747f, 0.3850649f, 0.1430804f, // Rx, Gx, Bx
18     0.2225045f, 0.7168786f, 0.0606169f, // Ry, Gy, Gz
19     0.0139322f, 0.0971045f, 0.7141733f, // Rz, Gz, Bz
20 };
21 
22 static constexpr float gAdobeRGB_toXYZD50[] {
23     0.6097559f, 0.2052401f, 0.1492240f, // Rx, Gx, Bx
24     0.3111242f, 0.6256560f, 0.0632197f, // Ry, Gy, Gz
25     0.0194811f, 0.0608902f, 0.7448387f, // Rz, Gz, Bz
26 };
27 
28 static constexpr float gDCIP3_toXYZD50[] {
29     0.515102f,   0.291965f,  0.157153f,  // Rx, Gx, Bx
30     0.241182f,   0.692236f,  0.0665819f, // Ry, Gy, Gz
31    -0.00104941f, 0.0418818f, 0.784378f,  // Rz, Gz, Bz
32 };
33 
34 static constexpr float gRec2020_toXYZD50[] {
35     0.673459f,   0.165661f,  0.125100f,  // Rx, Gx, Bx
36     0.279033f,   0.675338f,  0.0456288f, // Ry, Gy, Gz
37    -0.00193139f, 0.0299794f, 0.797162f,  // Rz, Gz, Bz
38 };
39 
40 static constexpr SkColorSpaceTransferFn gSRGB_TransferFn =
41         { 2.4f, 1.0f / 1.055f, 0.055f / 1.055f, 1.0f / 12.92f, 0.04045f, 0.0f, 0.0f };
42 
43 static constexpr SkColorSpaceTransferFn g2Dot2_TransferFn =
44         { 2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
45 
46 // gLinear_TransferFn.fD > 1.0f: Make sure that we use the linear segment of
47 // the transfer function even when the x-value is 1.0f.
48 static constexpr SkColorSpaceTransferFn gLinear_TransferFn =
49         { 0.0f, 0.0f, 0.0f, 1.0f, 1.0000001f, 0.0f, 0.0f };
50 
51 static constexpr SkColorSpaceTransferFn gDCIP3_TransferFn =
52     { 2.399994f, 0.947998047f, 0.0520019531f, 0.0769958496f, 0.0390014648f, 0.0f, 0.0f };
53 
to_xyz_d50(SkMatrix44 * toXYZD50,SkColorSpace::Gamut gamut)54 static inline void to_xyz_d50(SkMatrix44* toXYZD50, SkColorSpace::Gamut gamut) {
55     switch (gamut) {
56         case SkColorSpace::kSRGB_Gamut:
57             toXYZD50->set3x3RowMajorf(gSRGB_toXYZD50);
58             break;
59         case SkColorSpace::kAdobeRGB_Gamut:
60             toXYZD50->set3x3RowMajorf(gAdobeRGB_toXYZD50);
61             break;
62         case SkColorSpace::kDCIP3_D65_Gamut:
63             toXYZD50->set3x3RowMajorf(gDCIP3_toXYZD50);
64             break;
65         case SkColorSpace::kRec2020_Gamut:
66             toXYZD50->set3x3RowMajorf(gRec2020_toXYZD50);
67             break;
68     }
69 }
70 
color_space_almost_equal(float a,float b)71 static inline bool color_space_almost_equal(float a, float b) {
72     return SkTAbs(a - b) < 0.01f;
73 }
74 
75 // Let's use a stricter version for transfer functions.  Worst case, these are encoded
76 // in ICC format, which offers 16-bits of fractional precision.
transfer_fn_almost_equal(float a,float b)77 static inline bool transfer_fn_almost_equal(float a, float b) {
78     return SkTAbs(a - b) < 0.001f;
79 }
80 
is_zero_to_one(float v)81 static inline bool is_zero_to_one(float v) {
82     // Because we allow a value just barely larger than 1, the client can use an
83     // entirely linear transfer function.
84     return (0.0f <= v) && (v <= nextafterf(1.0f, 2.0f));
85 }
86 
is_valid_transfer_fn(const SkColorSpaceTransferFn & coeffs)87 static inline bool is_valid_transfer_fn(const SkColorSpaceTransferFn& coeffs) {
88     if (SkScalarIsNaN(coeffs.fA) || SkScalarIsNaN(coeffs.fB) ||
89         SkScalarIsNaN(coeffs.fC) || SkScalarIsNaN(coeffs.fD) ||
90         SkScalarIsNaN(coeffs.fE) || SkScalarIsNaN(coeffs.fF) ||
91         SkScalarIsNaN(coeffs.fG))
92     {
93         return false;
94     }
95 
96     if (!is_zero_to_one(coeffs.fD)) {
97         return false;
98     }
99 
100     if (coeffs.fD == 0.0f) {
101         // Y = (aX + b)^g + e  for always
102         if (0.0f == coeffs.fA || 0.0f == coeffs.fG) {
103             SkColorSpacePrintf("A or G is zero, constant transfer function "
104                                "is nonsense");
105             return false;
106         }
107     }
108 
109     if (coeffs.fD >= 1.0f) {
110         // Y = cX + f          for always
111         if (0.0f == coeffs.fC) {
112             SkColorSpacePrintf("C is zero, constant transfer function is "
113                                "nonsense");
114             return false;
115         }
116     }
117 
118     if ((0.0f == coeffs.fA || 0.0f == coeffs.fG) && 0.0f == coeffs.fC) {
119         SkColorSpacePrintf("A or G, and C are zero, constant transfer function "
120                            "is nonsense");
121         return false;
122     }
123 
124     if (coeffs.fC < 0.0f) {
125         SkColorSpacePrintf("Transfer function must be increasing");
126         return false;
127     }
128 
129     if (coeffs.fA < 0.0f || coeffs.fG < 0.0f) {
130         SkColorSpacePrintf("Transfer function must be positive or increasing");
131         return false;
132     }
133 
134     return true;
135 }
136 
is_almost_srgb(const SkColorSpaceTransferFn & coeffs)137 static inline bool is_almost_srgb(const SkColorSpaceTransferFn& coeffs) {
138     return transfer_fn_almost_equal(gSRGB_TransferFn.fA, coeffs.fA) &&
139            transfer_fn_almost_equal(gSRGB_TransferFn.fB, coeffs.fB) &&
140            transfer_fn_almost_equal(gSRGB_TransferFn.fC, coeffs.fC) &&
141            transfer_fn_almost_equal(gSRGB_TransferFn.fD, coeffs.fD) &&
142            transfer_fn_almost_equal(gSRGB_TransferFn.fE, coeffs.fE) &&
143            transfer_fn_almost_equal(gSRGB_TransferFn.fF, coeffs.fF) &&
144            transfer_fn_almost_equal(gSRGB_TransferFn.fG, coeffs.fG);
145 }
146 
is_almost_2dot2(const SkColorSpaceTransferFn & coeffs)147 static inline bool is_almost_2dot2(const SkColorSpaceTransferFn& coeffs) {
148     return transfer_fn_almost_equal(1.0f, coeffs.fA) &&
149            transfer_fn_almost_equal(0.0f, coeffs.fB) &&
150            transfer_fn_almost_equal(0.0f, coeffs.fE) &&
151            transfer_fn_almost_equal(2.2f, coeffs.fG) &&
152            coeffs.fD <= 0.0f;
153 }
154 
is_almost_linear(const SkColorSpaceTransferFn & coeffs)155 static inline bool is_almost_linear(const SkColorSpaceTransferFn& coeffs) {
156     // OutputVal = InputVal ^ 1.0f
157     const bool linearExp =
158             transfer_fn_almost_equal(1.0f, coeffs.fA) &&
159             transfer_fn_almost_equal(0.0f, coeffs.fB) &&
160             transfer_fn_almost_equal(0.0f, coeffs.fE) &&
161             transfer_fn_almost_equal(1.0f, coeffs.fG) &&
162             coeffs.fD <= 0.0f;
163 
164     // OutputVal = 1.0f * InputVal
165     const bool linearFn =
166             transfer_fn_almost_equal(1.0f, coeffs.fC) &&
167             transfer_fn_almost_equal(0.0f, coeffs.fF) &&
168             coeffs.fD >= 1.0f;
169 
170     return linearExp || linearFn;
171 }
172 
value_to_parametric(SkColorSpaceTransferFn * coeffs,float exponent)173 static inline void value_to_parametric(SkColorSpaceTransferFn* coeffs, float exponent) {
174     coeffs->fA = 1.0f;
175     coeffs->fB = 0.0f;
176     coeffs->fC = 0.0f;
177     coeffs->fD = 0.0f;
178     coeffs->fE = 0.0f;
179     coeffs->fF = 0.0f;
180     coeffs->fG = exponent;
181 }
182 
named_to_parametric(SkColorSpaceTransferFn * coeffs,SkGammaNamed gammaNamed)183 static inline bool named_to_parametric(SkColorSpaceTransferFn* coeffs,
184                                        SkGammaNamed gammaNamed) {
185     switch (gammaNamed) {
186         case kSRGB_SkGammaNamed:
187             *coeffs = gSRGB_TransferFn;
188             return true;
189         case k2Dot2Curve_SkGammaNamed:
190             *coeffs = g2Dot2_TransferFn;
191             return true;
192         case kLinear_SkGammaNamed:
193             *coeffs = gLinear_TransferFn;
194             return true;
195         default:
196             return false;
197     }
198 }
199 #endif  // SkColorSpacePriv_DEFINED
200