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 SkNamedTransferFn { 43 44 // Like SkNamedGamut::kSRGB, keeping this bitwise exactly the same as skcms makes things fastest. 45 static constexpr skcms_TransferFunction kSRGB = 46 { 2.4f, (float)(1/1.055), (float)(0.055/1.055), (float)(1/12.92), 0.04045f, 0.0f, 0.0f }; 47 48 static constexpr skcms_TransferFunction k2Dot2 = 49 { 2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 50 51 static constexpr skcms_TransferFunction kLinear = 52 { 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 53 54 static constexpr skcms_TransferFunction kRec2020 = 55 {2.22222f, 0.909672f, 0.0903276f, 0.222222f, 0.0812429f, 0, 0}; 56 57 static constexpr skcms_TransferFunction kPQ = 58 {-2.0f, -107/128.0f, 1.0f, 32/2523.0f, 2413/128.0f, -2392/128.0f, 8192/1305.0f }; 59 60 static constexpr skcms_TransferFunction kHLG = 61 {-3.0f, 2.0f, 2.0f, 1/0.17883277f, 0.28466892f, 0.55991073f, 0.0f }; 62 63 } // namespace SkNamedTransferFn 64 65 namespace SkNamedGamut { 66 67 static constexpr skcms_Matrix3x3 kSRGB = {{ 68 // ICC fixed-point (16.16) representation, taken from skcms. Please keep them exactly in sync. 69 // 0.436065674f, 0.385147095f, 0.143066406f, 70 // 0.222488403f, 0.716873169f, 0.060607910f, 71 // 0.013916016f, 0.097076416f, 0.714096069f, 72 { SkFixedToFloat(0x6FA2), SkFixedToFloat(0x6299), SkFixedToFloat(0x24A0) }, 73 { SkFixedToFloat(0x38F5), SkFixedToFloat(0xB785), SkFixedToFloat(0x0F84) }, 74 { SkFixedToFloat(0x0390), SkFixedToFloat(0x18DA), SkFixedToFloat(0xB6CF) }, 75 }}; 76 77 static constexpr skcms_Matrix3x3 kAdobeRGB = {{ 78 // ICC fixed-point (16.16) repesentation of: 79 // 0.60974, 0.20528, 0.14919, 80 // 0.31111, 0.62567, 0.06322, 81 // 0.01947, 0.06087, 0.74457, 82 { SkFixedToFloat(0x9c18), SkFixedToFloat(0x348d), SkFixedToFloat(0x2631) }, 83 { SkFixedToFloat(0x4fa5), SkFixedToFloat(0xa02c), SkFixedToFloat(0x102f) }, 84 { SkFixedToFloat(0x04fc), SkFixedToFloat(0x0f95), SkFixedToFloat(0xbe9c) }, 85 }}; 86 87 static constexpr skcms_Matrix3x3 kDisplayP3 = {{ 88 { 0.515102f, 0.291965f, 0.157153f }, 89 { 0.241182f, 0.692236f, 0.0665819f }, 90 { -0.00104941f, 0.0418818f, 0.784378f }, 91 }}; 92 93 static constexpr skcms_Matrix3x3 kRec2020 = {{ 94 { 0.673459f, 0.165661f, 0.125100f }, 95 { 0.279033f, 0.675338f, 0.0456288f }, 96 { -0.00193139f, 0.0299794f, 0.797162f }, 97 }}; 98 99 static constexpr skcms_Matrix3x3 kXYZ = {{ 100 { 1.0f, 0.0f, 0.0f }, 101 { 0.0f, 1.0f, 0.0f }, 102 { 0.0f, 0.0f, 1.0f }, 103 }}; 104 105 } // namespace SkNamedGamut 106 107 class SK_API SkColorSpace : public SkNVRefCnt<SkColorSpace> { 108 public: 109 /** 110 * Create the sRGB color space. 111 */ 112 static sk_sp<SkColorSpace> MakeSRGB(); 113 114 /** 115 * Colorspace with the sRGB primaries, but a linear (1.0) gamma. 116 */ 117 static sk_sp<SkColorSpace> MakeSRGBLinear(); 118 119 /** 120 * Create an SkColorSpace from a transfer function and a row-major 3x3 transformation to XYZ. 121 */ 122 static sk_sp<SkColorSpace> MakeRGB(const skcms_TransferFunction& transferFn, 123 const skcms_Matrix3x3& toXYZ); 124 125 /** 126 * Create an SkColorSpace from a parsed (skcms) ICC profile. 127 */ 128 static sk_sp<SkColorSpace> Make(const skcms_ICCProfile&); 129 130 /** 131 * Convert this color space to an skcms ICC profile struct. 132 */ 133 void toProfile(skcms_ICCProfile*) const; 134 135 /** 136 * Returns true if the color space gamma is near enough to be approximated as sRGB. 137 */ 138 bool gammaCloseToSRGB() const; 139 140 /** 141 * Returns true if the color space gamma is linear. 142 */ 143 bool gammaIsLinear() const; 144 145 /** 146 * Sets |fn| to the transfer function from this color space. Returns true if the transfer 147 * function can be represented as coefficients to the standard ICC 7-parameter equation. 148 * Returns false otherwise (eg, PQ, HLG). 149 */ 150 bool isNumericalTransferFn(skcms_TransferFunction* fn) const; 151 152 /** 153 * Returns true and sets |toXYZD50|. 154 */ 155 bool toXYZD50(skcms_Matrix3x3* toXYZD50) const; 156 157 /** 158 * Returns a hash of the gamut transformation to XYZ D50. Allows for fast equality checking 159 * of gamuts, at the (very small) risk of collision. 160 */ toXYZD50Hash()161 uint32_t toXYZD50Hash() const { return fToXYZD50Hash; } 162 163 /** 164 * Returns a color space with the same gamut as this one, but with a linear gamma. 165 */ 166 sk_sp<SkColorSpace> makeLinearGamma() const; 167 168 /** 169 * Returns a color space with the same gamut as this one, but with the sRGB transfer 170 * function. 171 */ 172 sk_sp<SkColorSpace> makeSRGBGamma() const; 173 174 /** 175 * Returns a color space with the same transfer function as this one, but with the primary 176 * colors rotated. In other words, this produces a new color space that maps RGB to GBR 177 * (when applied to a source), and maps RGB to BRG (when applied to a destination). 178 * 179 * This is used for testing, to construct color spaces that have severe and testable behavior. 180 */ 181 sk_sp<SkColorSpace> makeColorSpin() const; 182 183 /** 184 * Returns true if the color space is sRGB. 185 * Returns false otherwise. 186 * 187 * This allows a little bit of tolerance, given that we might see small numerical error 188 * in some cases: converting ICC fixed point to float, converting white point to D50, 189 * rounding decisions on transfer function and matrix. 190 * 191 * This does not consider a 2.2f exponential transfer function to be sRGB. While these 192 * functions are similar (and it is sometimes useful to consider them together), this 193 * function checks for logical equality. 194 */ 195 bool isSRGB() const; 196 197 /** 198 * Returns a serialized representation of this color space. 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; // DEPRECATED: Remove when webview usage is gone 217 void transferFn(skcms_TransferFunction* fn) const; 218 void invTransferFn(skcms_TransferFunction* fn) const; 219 void gamutTransformTo(const SkColorSpace* dst, skcms_Matrix3x3* src_to_dst) const; 220 transferFnHash()221 uint32_t transferFnHash() const { return fTransferFnHash; } hash()222 uint64_t hash() const { return (uint64_t)fTransferFnHash << 32 | fToXYZD50Hash; } 223 224 private: 225 friend class SkColorSpaceSingletonFactory; 226 227 SkColorSpace(const skcms_TransferFunction& transferFn, const skcms_Matrix3x3& toXYZ); 228 229 void computeLazyDstFields() const; 230 231 uint32_t fTransferFnHash; 232 uint32_t fToXYZD50Hash; 233 234 skcms_TransferFunction fTransferFn; 235 skcms_Matrix3x3 fToXYZD50; 236 237 mutable skcms_TransferFunction fInvTransferFn; 238 mutable skcms_Matrix3x3 fFromXYZD50; 239 mutable SkOnce fLazyDstFieldsOnce; 240 }; 241 242 #endif 243