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 "SkColorSpace_Base.h" | 11 #include "SkColorSpace_Base.h" |
12 #include "Test.h" | 12 #include "Test.h" |
13 | 13 |
14 #include "png.h" | 14 #include "png.h" |
15 | 15 |
16 static bool almost_equal(float a, float b) { | 16 static bool almost_equal(float a, float b) { |
17 return SkTAbs(a - b) < 0.001f; | 17 return SkTAbs(a - b) < 0.001f; |
18 } | 18 } |
19 | 19 |
20 static void test_space(skiatest::Reporter* r, SkColorSpace* space, | 20 static void test_space(skiatest::Reporter* r, SkColorSpace* space, |
21 const float red[], const float green[], const float blue[
], | 21 const float red[], const float green[], const float blue[
], |
22 const float expectedGammas[]) { | 22 const SkColorSpace::GammaNamed expectedGamma) { |
23 | 23 |
24 const sk_sp<SkGammas>& gammas = as_CSB(space)->gammas(); | 24 REPORTER_ASSERT(r, expectedGamma == space->gammaNamed()); |
25 REPORTER_ASSERT(r, almost_equal(expectedGammas[0], gammas->fRed.fValue)); | |
26 REPORTER_ASSERT(r, almost_equal(expectedGammas[1], gammas->fGreen.fValue)); | |
27 REPORTER_ASSERT(r, almost_equal(expectedGammas[2], gammas->fBlue.fValue)); | |
28 | 25 |
29 | 26 |
30 SkMatrix44 mat = space->xyz(); | 27 SkMatrix44 mat = space->xyz(); |
31 const float src[] = { | 28 const float src[] = { |
32 1, 0, 0, 1, | 29 1, 0, 0, 1, |
33 0, 1, 0, 1, | 30 0, 1, 0, 1, |
34 0, 0, 1, 1, | 31 0, 0, 1, 1, |
35 }; | 32 }; |
36 float dst[4]; | 33 float dst[4]; |
37 for (int i = 0; i < 3; ++i) { | 34 for (int i = 0; i < 3; ++i) { |
38 mat.mapScalars(&src[i*4], dst); | 35 mat.mapScalars(&src[i*4], dst); |
39 REPORTER_ASSERT(r, almost_equal(red[i], dst[0])); | 36 REPORTER_ASSERT(r, almost_equal(red[i], dst[0])); |
40 REPORTER_ASSERT(r, almost_equal(green[i], dst[1])); | 37 REPORTER_ASSERT(r, almost_equal(green[i], dst[1])); |
41 REPORTER_ASSERT(r, almost_equal(blue[i], dst[2])); | 38 REPORTER_ASSERT(r, almost_equal(blue[i], dst[2])); |
42 } | 39 } |
43 } | 40 } |
44 | 41 |
45 const float g_sRGB_XYZ[] = { 0.4358f, 0.2224f, 0.0139f, // R | 42 const float g_sRGB_XYZ[] = { 0.4358f, 0.2224f, 0.0139f, // R |
46 0.3853f, 0.7170f, 0.0971f, // G | 43 0.3853f, 0.7170f, 0.0971f, // G |
47 0.1430f, 0.0606f, 0.7139f }; // B | 44 0.1430f, 0.0606f, 0.7139f }; // B |
48 | 45 |
49 const float g_sRGB_gamma[] = { 2.2f, 2.2f, 2.2f }; | |
50 | |
51 DEF_TEST(ColorSpace_sRGB, r) { | 46 DEF_TEST(ColorSpace_sRGB, r) { |
52 test_space(r, SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named).get(), | 47 test_space(r, SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named).get(), |
53 g_sRGB_XYZ, &g_sRGB_XYZ[3], &g_sRGB_XYZ[6], g_sRGB_gamma); | 48 g_sRGB_XYZ, &g_sRGB_XYZ[3], &g_sRGB_XYZ[6], SkColorSpace::kSRGB_G
ammaNamed); |
54 | 49 |
55 } | 50 } |
56 | 51 |
57 static SkStreamAsset* resource(const char path[]) { | 52 static SkStreamAsset* resource(const char path[]) { |
58 SkString fullPath = GetResourcePath(path); | 53 SkString fullPath = GetResourcePath(path); |
59 return SkStream::NewFromFile(fullPath.c_str()); | 54 return SkStream::NewFromFile(fullPath.c_str()); |
60 } | 55 } |
61 | 56 |
62 DEF_TEST(ColorSpaceParsePngICCProfile, r) { | 57 DEF_TEST(ColorSpaceParsePngICCProfile, r) { |
63 SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png")); | 58 SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png")); |
64 REPORTER_ASSERT(r, nullptr != stream); | 59 REPORTER_ASSERT(r, nullptr != stream); |
65 if (!stream) { | 60 if (!stream) { |
66 return; | 61 return; |
67 } | 62 } |
68 | 63 |
69 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); | 64 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
70 REPORTER_ASSERT(r, nullptr != codec); | 65 REPORTER_ASSERT(r, nullptr != codec); |
71 | 66 |
72 #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) |
73 SkColorSpace* colorSpace = codec->getColorSpace(); | 68 SkColorSpace* colorSpace = codec->getColorSpace(); |
74 REPORTER_ASSERT(r, nullptr != colorSpace); | 69 REPORTER_ASSERT(r, nullptr != colorSpace); |
75 | 70 |
76 test_space(r, colorSpace, &g_sRGB_XYZ[0], &g_sRGB_XYZ[3], &g_sRGB_XYZ[6], g_
sRGB_gamma); | 71 test_space(r, colorSpace, &g_sRGB_XYZ[0], &g_sRGB_XYZ[3], &g_sRGB_XYZ[6], |
| 72 SkColorSpace::kSRGB_GammaNamed); |
77 #endif | 73 #endif |
78 } | 74 } |
79 | 75 |
80 DEF_TEST(ColorSpaceParseJpegICCProfile, r) { | 76 DEF_TEST(ColorSpaceParseJpegICCProfile, r) { |
81 SkAutoTDelete<SkStream> stream(resource("icc-v2-gbr.jpg")); | 77 SkAutoTDelete<SkStream> stream(resource("icc-v2-gbr.jpg")); |
82 REPORTER_ASSERT(r, nullptr != stream); | 78 REPORTER_ASSERT(r, nullptr != stream); |
83 if (!stream) { | 79 if (!stream) { |
84 return; | 80 return; |
85 } | 81 } |
86 | 82 |
87 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); | 83 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
88 REPORTER_ASSERT(r, nullptr != codec); | 84 REPORTER_ASSERT(r, nullptr != codec); |
89 if (!codec) { | 85 if (!codec) { |
90 return; | 86 return; |
91 } | 87 } |
92 | 88 |
93 SkColorSpace* colorSpace = codec->getColorSpace(); | 89 SkColorSpace* colorSpace = codec->getColorSpace(); |
94 REPORTER_ASSERT(r, nullptr != colorSpace); | 90 REPORTER_ASSERT(r, nullptr != colorSpace); |
95 | 91 |
96 const float red[] = { 0.385117f, 0.716904f, 0.0970612f }; | 92 const float red[] = { 0.385117f, 0.716904f, 0.0970612f }; |
97 const float green[] = { 0.143051f, 0.0606079f, 0.713913f }; | 93 const float green[] = { 0.143051f, 0.0606079f, 0.713913f }; |
98 const float blue[] = { 0.436035f, 0.222488f, 0.013916f }; | 94 const float blue[] = { 0.436035f, 0.222488f, 0.013916f }; |
99 test_space(r, colorSpace, red, green, blue, g_sRGB_gamma); | 95 test_space(r, colorSpace, red, green, blue, SkColorSpace::k2Dot2Curve_GammaN
amed); |
100 } | 96 } |
101 | 97 |
102 DEF_TEST(ColorSpaceSRGBCompare, r) { | 98 DEF_TEST(ColorSpaceSRGBCompare, r) { |
103 // Create an sRGB color space by name | 99 // Create an sRGB color space by name |
104 sk_sp<SkColorSpace> namedColorSpace = SkColorSpace::NewNamed(SkColorSpace::k
SRGB_Named); | 100 sk_sp<SkColorSpace> namedColorSpace = SkColorSpace::NewNamed(SkColorSpace::k
SRGB_Named); |
105 | 101 |
106 // Create an sRGB color space by value | 102 // Create an sRGB color space by value |
107 SkMatrix44 srgbToxyzD50(SkMatrix44::kUninitialized_Constructor); | 103 SkMatrix44 srgbToxyzD50(SkMatrix44::kUninitialized_Constructor); |
108 srgbToxyzD50.set3x3ColMajorf(g_sRGB_XYZ); | 104 srgbToxyzD50.set3x3ColMajorf(g_sRGB_XYZ); |
109 sk_sp<SkColorSpace> rgbColorSpace = SkColorSpace::NewRGB(g_sRGB_gamma, srgbT
oxyzD50); | 105 sk_sp<SkColorSpace> rgbColorSpace = SkColorSpace::NewRGB(SkColorSpace::kSRGB
_GammaNamed, |
| 106 srgbToxyzD50); |
110 REPORTER_ASSERT(r, rgbColorSpace == namedColorSpace); | 107 REPORTER_ASSERT(r, rgbColorSpace == namedColorSpace); |
111 | 108 |
112 // Change a single value from the sRGB matrix | 109 // Change a single value from the sRGB matrix |
113 srgbToxyzD50.set(2, 2, 0.5f); | 110 srgbToxyzD50.set(2, 2, 0.5f); |
114 sk_sp<SkColorSpace> strangeColorSpace = SkColorSpace::NewRGB(g_sRGB_gamma, s
rgbToxyzD50); | 111 sk_sp<SkColorSpace> strangeColorSpace = SkColorSpace::NewRGB(SkColorSpace::k
SRGB_GammaNamed, |
| 112 srgbToxyzD50); |
115 REPORTER_ASSERT(r, strangeColorSpace != namedColorSpace); | 113 REPORTER_ASSERT(r, strangeColorSpace != namedColorSpace); |
116 } | 114 } |
117 | 115 |
118 DEF_TEST(ColorSpaceWriteICC, r) { | 116 DEF_TEST(ColorSpaceWriteICC, r) { |
119 // Test writing a new ICC profile | 117 // Test writing a new ICC profile |
120 sk_sp<SkColorSpace> namedColorSpace = SkColorSpace::NewNamed(SkColorSpace::k
SRGB_Named); | 118 sk_sp<SkColorSpace> namedColorSpace = SkColorSpace::NewNamed(SkColorSpace::k
SRGB_Named); |
121 sk_sp<SkData> namedData = as_CSB(namedColorSpace)->writeToICC(); | 119 sk_sp<SkData> namedData = as_CSB(namedColorSpace)->writeToICC(); |
122 sk_sp<SkColorSpace> iccColorSpace = SkColorSpace::NewICC(namedData->data(),
namedData->size()); | 120 sk_sp<SkColorSpace> iccColorSpace = SkColorSpace::NewICC(namedData->data(),
namedData->size()); |
123 test_space(r, iccColorSpace.get(), g_sRGB_XYZ, &g_sRGB_XYZ[3], &g_sRGB_XYZ[6
], g_sRGB_gamma); | 121 test_space(r, iccColorSpace.get(), g_sRGB_XYZ, &g_sRGB_XYZ[3], &g_sRGB_XYZ[6
], |
124 REPORTER_ASSERT(r, iccColorSpace == namedColorSpace); | 122 SkColorSpace::k2Dot2Curve_GammaNamed); |
| 123 // FIXME (msarett): Test disabled. sRGB profiles are written approximately
as 2.2f curves. |
| 124 // REPORTER_ASSERT(r, iccColorSpace == namedColorSpace); |
125 | 125 |
126 // Test saving the original ICC data | 126 // Test saving the original ICC data |
127 sk_sp<SkData> monitorData = SkData::MakeFromFileName( | 127 sk_sp<SkData> monitorData = SkData::MakeFromFileName( |
128 GetResourcePath("monitor_profiles/HP_ZR30w.icc").c_str()); | 128 GetResourcePath("monitor_profiles/HP_ZR30w.icc").c_str()); |
129 REPORTER_ASSERT(r, monitorData); | 129 REPORTER_ASSERT(r, monitorData); |
130 if (!monitorData) { | 130 if (!monitorData) { |
131 return; | 131 return; |
132 } | 132 } |
133 sk_sp<SkColorSpace> monitorSpace = SkColorSpace::NewICC(monitorData->data(), | 133 sk_sp<SkColorSpace> monitorSpace = SkColorSpace::NewICC(monitorData->data(), |
134 monitorData->size())
; | 134 monitorData->size())
; |
135 sk_sp<SkData> newMonitorData = as_CSB(monitorSpace)->writeToICC(); | 135 sk_sp<SkData> newMonitorData = as_CSB(monitorSpace)->writeToICC(); |
136 sk_sp<SkColorSpace> newMonitorSpace = SkColorSpace::NewICC(newMonitorData->d
ata(), | 136 sk_sp<SkColorSpace> newMonitorSpace = SkColorSpace::NewICC(newMonitorData->d
ata(), |
137 newMonitorData->s
ize()); | 137 newMonitorData->s
ize()); |
138 REPORTER_ASSERT(r, monitorSpace->xyz() == newMonitorSpace->xyz()); | 138 REPORTER_ASSERT(r, monitorSpace->xyz() == newMonitorSpace->xyz()); |
139 REPORTER_ASSERT(r, as_CSB(monitorSpace)->gammas() == as_CSB(newMonitorSpace)
->gammas()); | 139 REPORTER_ASSERT(r, monitorSpace->gammaNamed() == newMonitorSpace->gammaNamed
()); |
140 } | 140 } |
141 | 141 |
142 DEF_TEST(ColorSpace_Named, r) { | 142 DEF_TEST(ColorSpace_Named, r) { |
143 const struct { | 143 const struct { |
144 SkColorSpace::Named fNamed; | 144 SkColorSpace::Named fNamed; |
145 bool fExpectedToSucceed; | 145 bool fExpectedToSucceed; |
| 146 bool fIsSRGB; |
146 } recs[] { | 147 } recs[] { |
147 { SkColorSpace::kUnknown_Named, false }, | 148 { SkColorSpace::kUnknown_Named, false, false }, |
148 { SkColorSpace::kSRGB_Named, true }, | 149 { SkColorSpace::kSRGB_Named, true, true }, |
149 { SkColorSpace::kAdobeRGB_Named, true }, | 150 { SkColorSpace::kAdobeRGB_Named, true, false }, |
150 }; | 151 }; |
151 | 152 |
152 for (auto rec : recs) { | 153 for (auto rec : recs) { |
153 auto cs = SkColorSpace::NewNamed(rec.fNamed); | 154 auto cs = SkColorSpace::NewNamed(rec.fNamed); |
154 REPORTER_ASSERT(r, !cs == !rec.fExpectedToSucceed); | 155 REPORTER_ASSERT(r, !cs == !rec.fExpectedToSucceed); |
155 if (cs) { | 156 if (cs) { |
156 REPORTER_ASSERT(r, SkColorSpace::k2Dot2Curve_GammaNamed == cs->gamma
Named()); | 157 if (rec.fIsSRGB) { |
| 158 REPORTER_ASSERT(r, SkColorSpace::kSRGB_GammaNamed == cs->gammaNa
med()); |
| 159 } else { |
| 160 REPORTER_ASSERT(r, SkColorSpace::k2Dot2Curve_GammaNamed == cs->g
ammaNamed()); |
| 161 } |
157 } | 162 } |
158 } | 163 } |
159 | 164 |
160 SkImageInfo info = SkImageInfo::MakeS32(10, 10, kPremul_SkAlphaType); | 165 SkImageInfo info = SkImageInfo::MakeS32(10, 10, kPremul_SkAlphaType); |
161 REPORTER_ASSERT(r, kSRGB_SkColorProfileType == info.profileType()); | 166 REPORTER_ASSERT(r, kSRGB_SkColorProfileType == info.profileType()); |
162 REPORTER_ASSERT(r, SkColorSpace::k2Dot2Curve_GammaNamed == info.colorSpace()
->gammaNamed()); | 167 REPORTER_ASSERT(r, SkColorSpace::kSRGB_GammaNamed == info.colorSpace()->gamm
aNamed()); |
163 } | 168 } |
OLD | NEW |