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 #include "Resources.h"
9 #include "SkCodec.h"
10 #include "SkColorSpace.h"
11 #include "SkColorSpacePriv.h"
12 #include "SkData.h"
13 #include "SkImageInfo.h"
14 #include "SkRefCnt.h"
15 #include "SkStream.h"
16 #include "SkTypes.h"
17 #include "Test.h"
18
19 #include "../third_party/skcms/skcms.h"
20 #include "png.h"
21
22 #include <memory>
23 #include <utility>
24
almost_equal(float a,float b)25 static bool almost_equal(float a, float b) {
26 return SkTAbs(a - b) < 0.001f;
27 }
28
test_space(skiatest::Reporter * r,SkColorSpace * space,const float red[],const float green[],const float blue[],bool expectSRGB=false)29 static void test_space(skiatest::Reporter* r, SkColorSpace* space,
30 const float red[], const float green[], const float blue[],
31 bool expectSRGB = false) {
32
33 REPORTER_ASSERT(r, nullptr != space);
34 REPORTER_ASSERT(r, expectSRGB == space->gammaCloseToSRGB());
35
36 skcms_Matrix3x3 mat;
37 space->toXYZD50(&mat);
38 const float* ref[3] = { red, green, blue };
39 for (int i = 0; i < 3; ++i) {
40 REPORTER_ASSERT(r, almost_equal(ref[i][0], mat.vals[0][i]));
41 REPORTER_ASSERT(r, almost_equal(ref[i][1], mat.vals[1][i]));
42 REPORTER_ASSERT(r, almost_equal(ref[i][2], mat.vals[2][i]));
43 }
44 }
45
test_path(skiatest::Reporter * r,const char * path,const float red[],const float green[],const float blue[],bool expectSRGB=false)46 static void test_path(skiatest::Reporter* r, const char* path,
47 const float red[], const float green[], const float blue[],
48 bool expectSRGB = false) {
49 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
50 REPORTER_ASSERT(r, nullptr != stream);
51 if (!stream) {
52 return;
53 }
54
55 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
56 REPORTER_ASSERT(r, nullptr != codec);
57 if (!codec) {
58 return;
59 }
60
61 auto colorSpace = codec->getInfo().refColorSpace();
62 test_space(r, colorSpace.get(), red, green, blue, expectSRGB);
63 }
64
65 static constexpr float g_sRGB_R[]{ 0.4358f, 0.2224f, 0.0139f };
66 static constexpr float g_sRGB_G[]{ 0.3853f, 0.7170f, 0.0971f };
67 static constexpr float g_sRGB_B[]{ 0.1430f, 0.0606f, 0.7139f };
68
DEF_TEST(ColorSpace_sRGB,r)69 DEF_TEST(ColorSpace_sRGB, r) {
70 test_space(r, sk_srgb_singleton(), g_sRGB_R, g_sRGB_G, g_sRGB_B, true);
71
72 }
73
DEF_TEST(ColorSpaceParseICCProfiles,r)74 DEF_TEST(ColorSpaceParseICCProfiles, r) {
75
76 #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6)
77 test_path(r, "images/color_wheel_with_profile.png", g_sRGB_R, g_sRGB_G, g_sRGB_B, true);
78 #endif
79
80 const float red[] = { 0.385117f, 0.716904f, 0.0970612f };
81 const float green[] = { 0.143051f, 0.0606079f, 0.713913f };
82 const float blue[] = { 0.436035f, 0.222488f, 0.013916f };
83 test_path(r, "images/icc-v2-gbr.jpg", red, green, blue);
84
85 test_path(r, "images/webp-color-profile-crash.webp",
86 red, green, blue);
87 test_path(r, "images/webp-color-profile-lossless.webp",
88 red, green, blue);
89 test_path(r, "images/webp-color-profile-lossy.webp",
90 red, green, blue);
91 test_path(r, "images/webp-color-profile-lossy-alpha.webp",
92 red, green, blue);
93 }
94
test_serialize(skiatest::Reporter * r,sk_sp<SkColorSpace> space,bool isNamed)95 static void test_serialize(skiatest::Reporter* r, sk_sp<SkColorSpace> space, bool isNamed) {
96 sk_sp<SkData> data1 = space->serialize();
97
98 size_t bytes = space->writeToMemory(nullptr);
99 sk_sp<SkData> data2 = SkData::MakeUninitialized(bytes);
100 space->writeToMemory(data2->writable_data());
101
102 sk_sp<SkColorSpace> newSpace1 = SkColorSpace::Deserialize(data1->data(), data1->size());
103 sk_sp<SkColorSpace> newSpace2 = SkColorSpace::Deserialize(data2->data(), data2->size());
104
105 if (isNamed) {
106 REPORTER_ASSERT(r, space.get() == newSpace1.get());
107 REPORTER_ASSERT(r, space.get() == newSpace2.get());
108 } else {
109 REPORTER_ASSERT(r, SkColorSpace::Equals(space.get(), newSpace1.get()));
110 REPORTER_ASSERT(r, SkColorSpace::Equals(space.get(), newSpace2.get()));
111 }
112 }
113
DEF_TEST(ColorSpace_Serialize,r)114 DEF_TEST(ColorSpace_Serialize, r) {
115 test_serialize(r, SkColorSpace::MakeSRGB(), true);
116 test_serialize(r, SkColorSpace::MakeSRGBLinear(), true);
117
118 auto test = [&](const char* path) {
119 sk_sp<SkData> data = GetResourceAsData(path);
120
121 skcms_ICCProfile profile;
122 REPORTER_ASSERT(r, skcms_Parse(data->data(), data->size(), &profile));
123
124 sk_sp<SkColorSpace> space = SkColorSpace::Make(profile);
125 REPORTER_ASSERT(r, space);
126
127 test_serialize(r, space, false);
128 };
129 test("icc_profiles/HP_ZR30w.icc");
130 test("icc_profiles/HP_Z32x.icc");
131
132 skcms_TransferFunction fn;
133 fn.a = 1.0f;
134 fn.b = 0.0f;
135 fn.c = 1.0f;
136 fn.d = 0.5f;
137 fn.e = 0.0f;
138 fn.f = 0.0f;
139 fn.g = 1.0f;
140 skcms_Matrix3x3 toXYZ = {{
141 { 1, 0, 0 },
142 { 0, 1, 0 },
143 { 0, 0, 1 },
144 }};
145 test_serialize(r, SkColorSpace::MakeRGB(fn, toXYZ), false);
146 }
147
DEF_TEST(ColorSpace_Equals,r)148 DEF_TEST(ColorSpace_Equals, r) {
149 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
150
151 auto parse = [&](const char* path) {
152 sk_sp<SkData> data = GetResourceAsData(path);
153
154 skcms_ICCProfile profile;
155 REPORTER_ASSERT(r, skcms_Parse(data->data(), data->size(), &profile));
156
157 sk_sp<SkColorSpace> space = SkColorSpace::Make(profile);
158 REPORTER_ASSERT(r, space);
159
160 return space;
161 };
162 sk_sp<SkColorSpace> z30 = parse("icc_profiles/HP_ZR30w.icc");
163 sk_sp<SkColorSpace> z32 = parse("icc_profiles/HP_Z32x.icc");
164
165 skcms_TransferFunction fn;
166 fn.a = 1.0f;
167 fn.b = 0.0f;
168 fn.c = 1.0f;
169 fn.d = 0.5f;
170 fn.e = 0.0f;
171 fn.f = 0.0f;
172 fn.g = 1.0f;
173 skcms_Matrix3x3 toXYZ = {{
174 { 1, 0, 0 },
175 { 0, 1, 0 },
176 { 0, 0, 1 },
177 }};
178 sk_sp<SkColorSpace> rgb4 = SkColorSpace::MakeRGB(fn, toXYZ);
179
180 REPORTER_ASSERT(r, SkColorSpace::Equals(nullptr, nullptr));
181 REPORTER_ASSERT(r, SkColorSpace::Equals(srgb.get(), srgb.get()));
182 REPORTER_ASSERT(r, SkColorSpace::Equals(z30.get(), z30.get()));
183 REPORTER_ASSERT(r, SkColorSpace::Equals(z32.get(), z32.get()));
184 REPORTER_ASSERT(r, SkColorSpace::Equals(rgb4.get(), rgb4.get()));
185
186 REPORTER_ASSERT(r, !SkColorSpace::Equals(nullptr, srgb.get()));
187 REPORTER_ASSERT(r, !SkColorSpace::Equals(srgb.get(), nullptr));
188 REPORTER_ASSERT(r, !SkColorSpace::Equals(z30.get(), srgb.get()));
189 REPORTER_ASSERT(r, !SkColorSpace::Equals(z32.get(), z30.get()));
190 REPORTER_ASSERT(r, !SkColorSpace::Equals(z30.get(), rgb4.get()));
191 REPORTER_ASSERT(r, !SkColorSpace::Equals(srgb.get(), rgb4.get()));
192 }
193
matrix_almost_equal(const skcms_Matrix3x3 & a,const skcms_Matrix3x3 & b)194 static inline bool matrix_almost_equal(const skcms_Matrix3x3& a, const skcms_Matrix3x3& b) {
195 for (int r = 0; r < 3; ++r) {
196 for (int c = 0; c < 3; ++c) {
197 if (!almost_equal(a.vals[r][c], b.vals[r][c])) {
198 return false;
199 }
200 }
201 }
202 return true;
203 }
204
check_primaries(skiatest::Reporter * r,const SkColorSpacePrimaries & primaries,const skcms_Matrix3x3 & reference)205 static inline void check_primaries(skiatest::Reporter* r, const SkColorSpacePrimaries& primaries,
206 const skcms_Matrix3x3& reference) {
207 skcms_Matrix3x3 toXYZ;
208 bool result = primaries.toXYZD50(&toXYZ);
209 REPORTER_ASSERT(r, result);
210 REPORTER_ASSERT(r, matrix_almost_equal(toXYZ, reference));
211 }
212
DEF_TEST(ColorSpace_Primaries,r)213 DEF_TEST(ColorSpace_Primaries, r) {
214 // sRGB primaries (D65)
215 skcms_Matrix3x3 srgbToXYZ;
216 bool result = skcms_PrimariesToXYZD50(
217 0.64f, 0.33f,
218 0.30f, 0.60f,
219 0.15f, 0.06f,
220 0.3127f, 0.3290f,
221 &srgbToXYZ);
222 REPORTER_ASSERT(r, result);
223
224 sk_sp<SkColorSpace> space = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, srgbToXYZ);
225 REPORTER_ASSERT(r, SkColorSpace::MakeSRGB() == space);
226
227 // ProPhoto (D50)
228 SkColorSpacePrimaries proPhoto;
229 proPhoto.fRX = 0.7347f;
230 proPhoto.fRY = 0.2653f;
231 proPhoto.fGX = 0.1596f;
232 proPhoto.fGY = 0.8404f;
233 proPhoto.fBX = 0.0366f;
234 proPhoto.fBY = 0.0001f;
235 proPhoto.fWX = 0.34567f;
236 proPhoto.fWY = 0.35850f;
237 skcms_Matrix3x3 proToXYZ = {{
238 { 0.7976749f, 0.1351917f, 0.0313534f },
239 { 0.2880402f, 0.7118741f, 0.0000857f },
240 { 0.0000000f, 0.0000000f, 0.8252100f },
241 }};
242 check_primaries(r, proPhoto, proToXYZ);
243
244 // NTSC (C)
245 SkColorSpacePrimaries ntsc;
246 ntsc.fRX = 0.67f;
247 ntsc.fRY = 0.33f;
248 ntsc.fGX = 0.21f;
249 ntsc.fGY = 0.71f;
250 ntsc.fBX = 0.14f;
251 ntsc.fBY = 0.08f;
252 ntsc.fWX = 0.31006f;
253 ntsc.fWY = 0.31616f;
254 skcms_Matrix3x3 ntscToXYZ = {{
255 { 0.6343706f, 0.1852204f, 0.1446290f },
256 { 0.3109496f, 0.5915984f, 0.0974520f },
257 { -0.0011817f, 0.0555518f, 0.7708399f }
258 }};
259 check_primaries(r, ntsc, ntscToXYZ);
260
261 // DCI P3 (D65)
262 SkColorSpacePrimaries p3;
263 p3.fRX = 0.680f;
264 p3.fRY = 0.320f;
265 p3.fGX = 0.265f;
266 p3.fGY = 0.690f;
267 p3.fBX = 0.150f;
268 p3.fBY = 0.060f;
269 p3.fWX = 0.3127f;
270 p3.fWY = 0.3290f;
271 space = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
272 skcms_Matrix3x3 reference;
273 SkAssertResult(space->toXYZD50(&reference));
274 check_primaries(r, p3, reference);
275
276 // Rec 2020 (D65)
277 SkColorSpacePrimaries rec2020;
278 rec2020.fRX = 0.708f;
279 rec2020.fRY = 0.292f;
280 rec2020.fGX = 0.170f;
281 rec2020.fGY = 0.797f;
282 rec2020.fBX = 0.131f;
283 rec2020.fBY = 0.046f;
284 rec2020.fWX = 0.3127f;
285 rec2020.fWY = 0.3290f;
286 space = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kRec2020);
287 SkAssertResult(space->toXYZD50(&reference));
288 check_primaries(r, rec2020, reference);
289 }
290
DEF_TEST(ColorSpace_MatrixHash,r)291 DEF_TEST(ColorSpace_MatrixHash, r) {
292 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
293
294 skcms_TransferFunction fn;
295 fn.a = 1.0f;
296 fn.b = 0.0f;
297 fn.c = 0.0f;
298 fn.d = 0.0f;
299 fn.e = 0.0f;
300 fn.f = 0.0f;
301 fn.g = 3.0f;
302
303 sk_sp<SkColorSpace> strange = SkColorSpace::MakeRGB(fn, SkNamedGamut::kSRGB);
304
305 REPORTER_ASSERT(r, srgb->toXYZD50Hash() == strange->toXYZD50Hash());
306 }
307
DEF_TEST(ColorSpace_IsSRGB,r)308 DEF_TEST(ColorSpace_IsSRGB, r) {
309 sk_sp<SkColorSpace> srgb0 = SkColorSpace::MakeSRGB();
310
311 skcms_TransferFunction fn;
312 fn.a = 1.0f;
313 fn.b = 0.0f;
314 fn.c = 0.0f;
315 fn.d = 0.0f;
316 fn.e = 0.0f;
317 fn.f = 0.0f;
318 fn.g = 2.2f;
319 sk_sp<SkColorSpace> twoDotTwo = SkColorSpace::MakeRGB(fn, SkNamedGamut::kSRGB);
320
321 REPORTER_ASSERT(r, srgb0->isSRGB());
322 REPORTER_ASSERT(r, !twoDotTwo->isSRGB());
323 }
324
DEF_TEST(ColorSpace_skcms_IsSRGB,r)325 DEF_TEST(ColorSpace_skcms_IsSRGB, r) {
326 sk_sp<SkColorSpace> srgb = SkColorSpace::Make(*skcms_sRGB_profile());
327 REPORTER_ASSERT(r, srgb->isSRGB());
328 }
329
DEF_TEST(ColorSpace_skcms_sRGB_exact,r)330 DEF_TEST(ColorSpace_skcms_sRGB_exact, r) {
331 skcms_ICCProfile profile;
332 sk_srgb_singleton()->toProfile(&profile);
333
334 REPORTER_ASSERT(r, 0 == memcmp(&profile, skcms_sRGB_profile(), sizeof(skcms_ICCProfile)));
335 }
336