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