OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Resources.h" | 8 #include "Resources.h" |
9 #include "SkCodec.h" | 9 #include "SkCodec.h" |
10 #include "SkColorSpace.h" | 10 #include "SkColorSpace.h" |
11 #include "Test.h" | 11 #include "Test.h" |
12 | 12 |
13 #include "png.h" | 13 #include "png.h" |
14 | 14 |
15 static bool almost_equal(float a, float b) { | |
16 return SkTAbs(a - b) < 0.001f; | |
17 } | |
18 | |
19 static void test_space(skiatest::Reporter* r, SkColorSpace* space, | |
msarett
2016/05/03 13:01:13
Nice!
| |
20 const float red[], const float green[], const float blue[ ], | |
21 const float gamma[]) { | |
22 SkMatrix44 mat = space->xyz(); | |
23 | |
24 const float src[] = { | |
25 1, 0, 0, 1, | |
26 0, 1, 0, 1, | |
27 0, 0, 1, 1, | |
28 }; | |
29 float dst[4]; | |
30 SkDebugf("\n"); | |
msarett
2016/05/03 13:01:13
Delete SkDebugf statements?
reed1
2016/05/03 17:44:51
Done.
| |
31 for (int i = 0; i < 3; ++i) { | |
32 REPORTER_ASSERT(r, almost_equal(gamma[i], space->gamma().fVec[i])); | |
33 | |
34 mat.mapScalars(&src[i*4], dst); | |
35 REPORTER_ASSERT(r, almost_equal(red[i], dst[0])); | |
36 REPORTER_ASSERT(r, almost_equal(green[i], dst[1])); | |
37 REPORTER_ASSERT(r, almost_equal(blue[i], dst[2])); | |
38 // SkDebugf("%g %g %g\n", dst[0], dst[1], dst[2]); | |
39 } | |
40 } | |
41 | |
42 DEF_TEST(ColorSpace_sRGB, r) { | |
43 const float srgb_r[] = { 0.4358f, 0.2224f, 0.0139f }; | |
44 const float srgb_g[] = { 0.3853f, 0.7170f, 0.0971f }; | |
45 const float srgb_b[] = { 0.1430f, 0.0606f, 0.7139f }; | |
46 const float srgb_gamma[] = { 2.2f, 2.2f, 2.2f }; | |
47 test_space(r, SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named).get(), | |
48 srgb_r, srgb_g, srgb_b, srgb_gamma); | |
49 | |
50 } | |
51 | |
15 static SkStreamAsset* resource(const char path[]) { | 52 static SkStreamAsset* resource(const char path[]) { |
16 SkString fullPath = GetResourcePath(path); | 53 SkString fullPath = GetResourcePath(path); |
17 return SkStream::NewFromFile(fullPath.c_str()); | 54 return SkStream::NewFromFile(fullPath.c_str()); |
18 } | 55 } |
19 | 56 |
20 static bool almost_equal(float a, float b) { | |
21 return SkTAbs(a - b) < 0.001f; | |
22 } | |
23 | |
24 DEF_TEST(ColorSpaceParsePngICCProfile, r) { | 57 DEF_TEST(ColorSpaceParsePngICCProfile, r) { |
25 SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png")); | 58 SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png")); |
26 REPORTER_ASSERT(r, nullptr != stream); | 59 REPORTER_ASSERT(r, nullptr != stream); |
27 if (!stream) { | 60 if (!stream) { |
28 return; | 61 return; |
29 } | 62 } |
30 | 63 |
31 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); | 64 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
32 REPORTER_ASSERT(r, nullptr != codec); | 65 REPORTER_ASSERT(r, nullptr != codec); |
33 | 66 |
34 #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_M INOR >= 6) | 67 #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_M INOR >= 6) |
35 SkColorSpace* colorSpace = codec->getColorSpace(); | 68 SkColorSpace* colorSpace = codec->getColorSpace(); |
36 REPORTER_ASSERT(r, nullptr != colorSpace); | 69 REPORTER_ASSERT(r, nullptr != colorSpace); |
37 | 70 |
38 // No need to use almost equal here. The color profile that we have extract ed | 71 const float red[] = { 0.436066f, 0.222488f, 0.013916f }; |
39 // actually has a table of gammas. And our current implementation guesses 2 .2f. | 72 const float green[] = { 0.385147f, 0.716873f, 0.0970764f }; |
40 SkFloat3 gammas = colorSpace->gamma(); | 73 const float blue[] = { 0.143066f, 0.0606079f, 0.714096f }; |
41 REPORTER_ASSERT(r, 2.2f == gammas.fVec[0]); | 74 const float gamma[] = { 2.2f, 2.2f, 2.2f }; |
42 REPORTER_ASSERT(r, 2.2f == gammas.fVec[1]); | 75 test_space(r, colorSpace, red, green, blue, gamma); |
43 REPORTER_ASSERT(r, 2.2f == gammas.fVec[2]); | |
44 | |
45 // These nine values were extracted from the color profile in isolation (bef ore | |
46 // we embedded it in the png). Here we check that we still extract the same values. | |
47 SkFloat3x3 xyz = colorSpace->xyz(); | |
48 REPORTER_ASSERT(r, almost_equal(0.436066f, xyz.fMat[0])); | |
49 REPORTER_ASSERT(r, almost_equal(0.222488f, xyz.fMat[1])); | |
50 REPORTER_ASSERT(r, almost_equal(0.013916f, xyz.fMat[2])); | |
51 REPORTER_ASSERT(r, almost_equal(0.385147f, xyz.fMat[3])); | |
52 REPORTER_ASSERT(r, almost_equal(0.716873f, xyz.fMat[4])); | |
53 REPORTER_ASSERT(r, almost_equal(0.0970764f, xyz.fMat[5])); | |
54 REPORTER_ASSERT(r, almost_equal(0.143066f, xyz.fMat[6])); | |
55 REPORTER_ASSERT(r, almost_equal(0.0606079f, xyz.fMat[7])); | |
56 REPORTER_ASSERT(r, almost_equal(0.714096f, xyz.fMat[8])); | |
57 #endif | 76 #endif |
58 } | 77 } |
59 | 78 |
60 DEF_TEST(ColorSpaceParseJpegICCProfile, r) { | 79 DEF_TEST(ColorSpaceParseJpegICCProfile, r) { |
61 SkAutoTDelete<SkStream> stream(resource("icc-v2-gbr.jpg")); | 80 SkAutoTDelete<SkStream> stream(resource("icc-v2-gbr.jpg")); |
62 REPORTER_ASSERT(r, nullptr != stream); | 81 REPORTER_ASSERT(r, nullptr != stream); |
63 if (!stream) { | 82 if (!stream) { |
64 return; | 83 return; |
65 } | 84 } |
66 | 85 |
67 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); | 86 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
68 REPORTER_ASSERT(r, nullptr != codec); | 87 REPORTER_ASSERT(r, nullptr != codec); |
69 if (!codec) { | 88 if (!codec) { |
70 return; | 89 return; |
71 } | 90 } |
72 | 91 |
73 SkColorSpace* colorSpace = codec->getColorSpace(); | 92 SkColorSpace* colorSpace = codec->getColorSpace(); |
74 REPORTER_ASSERT(r, nullptr != colorSpace); | 93 REPORTER_ASSERT(r, nullptr != colorSpace); |
75 | 94 |
76 // It's important to use almost equal here. This profile sets gamma as | 95 const float red[] = { 0.385117f, 0.716904f, 0.0970612f }; |
77 // 563 / 256, which actually comes out to about 2.19922. | 96 const float green[] = { 0.143051f, 0.0606079f, 0.713913f }; |
78 SkFloat3 gammas = colorSpace->gamma(); | 97 const float blue[] = { 0.436035f, 0.222488f, 0.013916f }; |
79 REPORTER_ASSERT(r, almost_equal(2.2f, gammas.fVec[0])); | 98 const float gamma[] = { 2.2f, 2.2f, 2.2f }; |
80 REPORTER_ASSERT(r, almost_equal(2.2f, gammas.fVec[1])); | 99 test_space(r, colorSpace, red, green, blue, gamma); |
81 REPORTER_ASSERT(r, almost_equal(2.2f, gammas.fVec[2])); | |
82 | |
83 // These nine values were extracted from the color profile. Until we know a ny | |
84 // better, we'll assume these are the right values and test that we continue | |
85 // to extract them properly. | |
86 SkFloat3x3 xyz = colorSpace->xyz(); | |
87 REPORTER_ASSERT(r, almost_equal(0.385117f, xyz.fMat[0])); | |
88 REPORTER_ASSERT(r, almost_equal(0.716904f, xyz.fMat[1])); | |
89 REPORTER_ASSERT(r, almost_equal(0.0970612f, xyz.fMat[2])); | |
90 REPORTER_ASSERT(r, almost_equal(0.143051f, xyz.fMat[3])); | |
91 REPORTER_ASSERT(r, almost_equal(0.0606079f, xyz.fMat[4])); | |
92 REPORTER_ASSERT(r, almost_equal(0.713913f, xyz.fMat[5])); | |
93 REPORTER_ASSERT(r, almost_equal(0.436035f, xyz.fMat[6])); | |
94 REPORTER_ASSERT(r, almost_equal(0.222488f, xyz.fMat[7])); | |
95 REPORTER_ASSERT(r, almost_equal(0.013916f, xyz.fMat[8])); | |
96 } | 100 } |
OLD | NEW |