ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 5 | #include "ui/gfx/icc_profile.h" |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 6 | #include "base/logging.h" |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | #include "ui/gfx/color_space.h" |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 9 | #include "ui/gfx/skia_color_space_util.h" |
ccameron | 83e8e9a | 2017-01-04 01:31:20 | [diff] [blame] | 10 | #include "ui/gfx/test/icc_profiles.h" |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 11 | |
| 12 | namespace gfx { |
| 13 | |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 14 | TEST(ICCProfile, Conversions) { |
| 15 | ICCProfile icc_profile = ICCProfileForTestingColorSpin(); |
| 16 | ColorSpace color_space_from_icc_profile = icc_profile.GetColorSpace(); |
| 17 | |
ccameron | 3b6fe6d | 2017-02-17 06:21:58 | [diff] [blame] | 18 | ICCProfile icc_profile_from_color_space; |
| 19 | bool result = |
| 20 | color_space_from_icc_profile.GetICCProfile(&icc_profile_from_color_space); |
| 21 | EXPECT_TRUE(result); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 22 | EXPECT_TRUE(icc_profile == icc_profile_from_color_space); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | TEST(ICCProfile, SRGB) { |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 26 | ICCProfile icc_profile = ICCProfileForTestingSRGB(); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 27 | ColorSpace color_space = ColorSpace::CreateSRGB(); |
msarett | 29c8557 | 2017-02-10 22:16:55 | [diff] [blame] | 28 | sk_sp<SkColorSpace> sk_color_space = SkColorSpace::MakeSRGB(); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 29 | |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 30 | // The ICC profile parser should note that this is SRGB. |
Christopher Cameron | a4adb41b | 2017-07-13 06:44:25 | [diff] [blame] | 31 | EXPECT_EQ(icc_profile.GetColorSpace(), ColorSpace::CreateSRGB()); |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 32 | EXPECT_EQ(icc_profile.GetColorSpace().ToSkColorSpace().get(), |
| 33 | sk_color_space.get()); |
| 34 | // The parametric generating code should recognize that this is SRGB. |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame^] | 35 | EXPECT_EQ(icc_profile.GetParametricColorSpace(), ColorSpace::CreateSRGB()); |
| 36 | EXPECT_EQ(icc_profile.GetParametricColorSpace().ToSkColorSpace().get(), |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 37 | sk_color_space.get()); |
| 38 | // The generated color space should recognize that this is SRGB. |
ccameron | 628896ee | 2017-02-03 09:29:12 | [diff] [blame] | 39 | EXPECT_EQ(color_space.ToSkColorSpace().get(), sk_color_space.get()); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 40 | } |
| 41 | |
ccameron | 549738b | 2017-01-28 17:39:32 | [diff] [blame] | 42 | TEST(ICCProfile, Equality) { |
| 43 | ICCProfile spin_profile = ICCProfileForTestingColorSpin(); |
| 44 | ICCProfile adobe_profile = ICCProfileForTestingAdobeRGB(); |
| 45 | EXPECT_TRUE(spin_profile == spin_profile); |
| 46 | EXPECT_FALSE(spin_profile != spin_profile); |
| 47 | EXPECT_FALSE(spin_profile == adobe_profile); |
| 48 | EXPECT_TRUE(spin_profile != adobe_profile); |
| 49 | |
| 50 | gfx::ColorSpace spin_space = spin_profile.GetColorSpace(); |
| 51 | gfx::ColorSpace adobe_space = adobe_profile.GetColorSpace(); |
| 52 | EXPECT_TRUE(spin_space == spin_space); |
| 53 | EXPECT_FALSE(spin_space != spin_space); |
| 54 | EXPECT_FALSE(spin_space == adobe_space); |
| 55 | EXPECT_TRUE(spin_space != adobe_space); |
| 56 | |
ccameron | 3b6fe6d | 2017-02-17 06:21:58 | [diff] [blame] | 57 | ICCProfile temp; |
| 58 | bool get_icc_result = false; |
| 59 | |
| 60 | get_icc_result = spin_space.GetICCProfile(&temp); |
| 61 | EXPECT_TRUE(get_icc_result); |
| 62 | EXPECT_TRUE(spin_profile == temp); |
| 63 | EXPECT_FALSE(spin_profile != temp); |
| 64 | |
| 65 | get_icc_result = adobe_space.GetICCProfile(&temp); |
| 66 | EXPECT_TRUE(get_icc_result); |
| 67 | EXPECT_FALSE(spin_profile == temp); |
| 68 | EXPECT_TRUE(spin_profile != temp); |
ccameron | 628896ee | 2017-02-03 09:29:12 | [diff] [blame] | 69 | |
| 70 | EXPECT_TRUE(!!spin_space.ToSkColorSpace()); |
| 71 | EXPECT_TRUE(!!adobe_space.ToSkColorSpace()); |
| 72 | EXPECT_FALSE(SkColorSpace::Equals( |
| 73 | spin_space.ToSkColorSpace().get(), |
| 74 | adobe_space.ToSkColorSpace().get())); |
ccameron | 549738b | 2017-01-28 17:39:32 | [diff] [blame] | 75 | } |
| 76 | |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 77 | TEST(ICCProfile, ParametricVersusExact) { |
| 78 | // This ICC profile has three transfer functions that differ enough that the |
| 79 | // parametric color space is considered inaccurate. |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 80 | ICCProfile multi_tr_fn = ICCProfileForTestingNoAnalyticTrFn(); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame^] | 81 | EXPECT_NE(multi_tr_fn.GetColorSpace(), multi_tr_fn.GetParametricColorSpace()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 82 | |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 83 | ICCProfile multi_tr_fn_color_space; |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 84 | EXPECT_TRUE( |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 85 | multi_tr_fn.GetColorSpace().GetICCProfile(&multi_tr_fn_color_space)); |
| 86 | EXPECT_EQ(multi_tr_fn_color_space, multi_tr_fn); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 87 | |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 88 | ICCProfile multi_tr_fn_parametric_color_space; |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame^] | 89 | EXPECT_TRUE(multi_tr_fn.GetParametricColorSpace().GetICCProfile( |
| 90 | &multi_tr_fn_parametric_color_space)); |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 91 | EXPECT_NE(multi_tr_fn_parametric_color_space, multi_tr_fn); |
| 92 | |
| 93 | // This ICC profile has a transfer function with T(1) that is greater than 1 |
| 94 | // in the approximation, but is still close enough to be considered accurate. |
| 95 | ICCProfile overshoot = ICCProfileForTestingOvershoot(); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame^] | 96 | EXPECT_EQ(overshoot.GetColorSpace(), overshoot.GetParametricColorSpace()); |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 97 | |
| 98 | ICCProfile overshoot_color_space; |
| 99 | EXPECT_TRUE(overshoot.GetColorSpace().GetICCProfile(&overshoot_color_space)); |
| 100 | EXPECT_EQ(overshoot_color_space, overshoot); |
| 101 | |
| 102 | ICCProfile overshoot_parametric_color_space; |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame^] | 103 | EXPECT_TRUE(overshoot.GetParametricColorSpace().GetICCProfile( |
| 104 | &overshoot_parametric_color_space)); |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 105 | EXPECT_EQ(overshoot_parametric_color_space, overshoot); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 106 | |
| 107 | // This ICC profile is precisely represented by the parametric color space. |
| 108 | ICCProfile accurate = ICCProfileForTestingAdobeRGB(); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame^] | 109 | EXPECT_EQ(accurate.GetColorSpace(), accurate.GetParametricColorSpace()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 110 | |
| 111 | ICCProfile accurate_color_space; |
| 112 | EXPECT_TRUE(accurate.GetColorSpace().GetICCProfile(&accurate_color_space)); |
| 113 | EXPECT_EQ(accurate_color_space, accurate); |
| 114 | |
| 115 | ICCProfile accurate_parametric_color_space; |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame^] | 116 | EXPECT_TRUE(accurate.GetParametricColorSpace().GetICCProfile( |
| 117 | &accurate_parametric_color_space)); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 118 | EXPECT_EQ(accurate_parametric_color_space, accurate); |
| 119 | |
ccameron | b3206e2 | 2017-03-25 02:09:12 | [diff] [blame] | 120 | // This ICC profile has only an A2B representation. We cannot create an |
| 121 | // SkColorSpaceXform to A2B only ICC profiles, so this should be marked |
| 122 | // as invalid. |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 123 | ICCProfile a2b = ICCProfileForTestingA2BOnly(); |
ccameron | b3206e2 | 2017-03-25 02:09:12 | [diff] [blame] | 124 | EXPECT_FALSE(a2b.GetColorSpace().IsValid()); |
Christopher Cameron | 64a55ad | 2017-09-21 05:31:22 | [diff] [blame] | 125 | |
| 126 | // Even though it is invalid, it should not be equal to the empty constructor. |
| 127 | EXPECT_NE(a2b, gfx::ICCProfile()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST(ICCProfile, GarbageData) { |
| 131 | std::vector<char> bad_data(10 * 1024); |
| 132 | const char* bad_data_string = "deadbeef"; |
| 133 | for (size_t i = 0; i < bad_data.size(); ++i) |
| 134 | bad_data[i] = bad_data_string[i % 8]; |
| 135 | ICCProfile garbage_profile = |
| 136 | ICCProfile::FromData(bad_data.data(), bad_data.size()); |
| 137 | EXPECT_FALSE(garbage_profile.IsValid()); |
| 138 | EXPECT_FALSE(garbage_profile.GetColorSpace().IsValid()); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame^] | 139 | EXPECT_FALSE(garbage_profile.GetParametricColorSpace().IsValid()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 140 | |
| 141 | ICCProfile default_ctor_profile; |
| 142 | EXPECT_FALSE(default_ctor_profile.IsValid()); |
| 143 | EXPECT_FALSE(default_ctor_profile.GetColorSpace().IsValid()); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame^] | 144 | EXPECT_FALSE(default_ctor_profile.GetParametricColorSpace().IsValid()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 145 | } |
| 146 | |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 147 | TEST(ICCProfile, GenericRGB) { |
| 148 | ColorSpace icc_profile = ICCProfileForTestingGenericRGB().GetColorSpace(); |
| 149 | ColorSpace color_space(ColorSpace::PrimaryID::APPLE_GENERIC_RGB, |
| 150 | ColorSpace::TransferID::GAMMA18); |
| 151 | |
| 152 | SkMatrix44 icc_profile_matrix; |
| 153 | SkMatrix44 color_space_matrix; |
| 154 | |
| 155 | icc_profile.GetPrimaryMatrix(&icc_profile_matrix); |
| 156 | color_space.GetPrimaryMatrix(&color_space_matrix); |
| 157 | |
| 158 | SkMatrix44 eye; |
| 159 | icc_profile_matrix.invert(&eye); |
| 160 | eye.postConcat(color_space_matrix); |
| 161 | EXPECT_TRUE(SkMatrixIsApproximatelyIdentity(eye)); |
| 162 | } |
| 163 | |
Christopher Cameron | 64a55ad | 2017-09-21 05:31:22 | [diff] [blame] | 164 | // This tests the ICCProfile MRU cache. This cache is sloppy and should be |
| 165 | // rewritten, now that some of the original design constraints have been lifted. |
| 166 | // This test exists only to ensure that we are aware of behavior changes, not to |
| 167 | // enforce that behavior does not change. |
| 168 | // https://crbug.com/766736 |
| 169 | TEST(ICCProfile, ExhaustCache) { |
| 170 | // Get an ICCProfile that can't be parametrically approximated. |
| 171 | ICCProfile original = ICCProfileForTestingNoAnalyticTrFn(); |
| 172 | ColorSpace original_color_space_0 = original.GetColorSpace(); |
| 173 | |
| 174 | // Recover the ICCProfile from its GetColorSpace. Recovery should succeed, and |
| 175 | // the ICCProfiles should be equal. |
| 176 | ICCProfile recovered_0; |
| 177 | EXPECT_TRUE(original_color_space_0.GetICCProfile(&recovered_0)); |
| 178 | EXPECT_EQ(original, recovered_0); |
| 179 | |
| 180 | // The GetColorSpace of the recovered version should match the original. |
| 181 | ColorSpace recovered_0_color_space = recovered_0.GetColorSpace(); |
| 182 | EXPECT_EQ(recovered_0_color_space, original_color_space_0); |
| 183 | |
| 184 | // Create an identical ICCProfile to the original. It should equal the |
| 185 | // original, and its GetColorSpace should equal the original. |
| 186 | ICCProfile identical_0 = ICCProfileForTestingNoAnalyticTrFn(); |
| 187 | EXPECT_EQ(original, identical_0); |
| 188 | ColorSpace identical_color_space_0 = identical_0.GetColorSpace(); |
| 189 | EXPECT_EQ(identical_color_space_0, original_color_space_0); |
| 190 | |
| 191 | // Create 128 distinct ICC profiles. This will destroy the cached |
| 192 | // ICCProfile<->ColorSpace mapping. |
| 193 | for (size_t i = 0; i < 128; ++i) { |
| 194 | SkMatrix44 toXYZD50; |
| 195 | ColorSpace::CreateSRGB().GetPrimaryMatrix(&toXYZD50); |
| 196 | SkColorSpaceTransferFn fn; |
| 197 | fn.fA = 1; |
| 198 | fn.fB = 0; |
| 199 | fn.fC = 1; |
| 200 | fn.fD = 0; |
| 201 | fn.fE = 0; |
| 202 | fn.fF = 0; |
| 203 | fn.fG = 1.5f + i / 128.f; |
| 204 | ColorSpace color_space = ColorSpace::CreateCustom(toXYZD50, fn); |
| 205 | ICCProfile icc_profile; |
| 206 | color_space.GetICCProfile(&icc_profile); |
| 207 | } |
| 208 | |
| 209 | // Recover the original ICCProfile from its GetColorSpace. Recovery should |
| 210 | // fail, because it has been pushed out of the cache. |
| 211 | ICCProfile recovered_1; |
| 212 | EXPECT_FALSE(original_color_space_0.GetICCProfile(&recovered_1)); |
| 213 | EXPECT_NE(original, recovered_1); |
| 214 | |
| 215 | // Create an identical ICCProfile to the original. It should equal the |
| 216 | // original, because the comparison is based on data. |
| 217 | ICCProfile identical_1 = ICCProfileForTestingNoAnalyticTrFn(); |
| 218 | EXPECT_EQ(original, identical_1); |
| 219 | |
| 220 | // The identical ICCProfile's GetColorSpace will not match, because the |
| 221 | // original points to the now-uncached version. |
| 222 | ColorSpace identical_1_color_space = identical_1.GetColorSpace(); |
| 223 | EXPECT_NE(identical_1_color_space, original_color_space_0); |
| 224 | |
| 225 | // The original ICCProfile is now orphaned because there exists a new entry |
| 226 | // with the same data. |
| 227 | ColorSpace original_color_space_2 = original.GetColorSpace(); |
| 228 | ICCProfile recovered_2; |
| 229 | EXPECT_FALSE(original_color_space_2.GetICCProfile(&recovered_2)); |
| 230 | EXPECT_NE(original, recovered_2); |
| 231 | |
| 232 | // Blow away the cache one more time. |
| 233 | for (size_t i = 0; i < 128; ++i) { |
| 234 | SkMatrix44 toXYZD50; |
| 235 | ColorSpace::CreateSRGB().GetPrimaryMatrix(&toXYZD50); |
| 236 | SkColorSpaceTransferFn fn; |
| 237 | fn.fA = 1; |
| 238 | fn.fB = 0; |
| 239 | fn.fC = 1; |
| 240 | fn.fD = 0; |
| 241 | fn.fE = 0; |
| 242 | fn.fF = 0; |
| 243 | fn.fG = 1.5f + i / 128.f; |
| 244 | ColorSpace color_space = ColorSpace::CreateCustom(toXYZD50, fn); |
| 245 | ICCProfile icc_profile; |
| 246 | color_space.GetICCProfile(&icc_profile); |
| 247 | } |
| 248 | |
| 249 | // Now the original ICCProfile can re-insert itself into the cache, with its |
| 250 | // original id. |
| 251 | ColorSpace original_color_space_3 = original.GetColorSpace(); |
| 252 | ICCProfile recovered_3; |
| 253 | EXPECT_TRUE(original_color_space_3.GetICCProfile(&recovered_3)); |
| 254 | EXPECT_EQ(original, recovered_3); |
| 255 | } |
| 256 | |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 257 | } // namespace gfx |