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