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 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 18 | ICCProfile icc_profile_from_color_space = |
| 19 | ICCProfile::FromParametricColorSpace(color_space_from_icc_profile); |
| 20 | EXPECT_TRUE(icc_profile_from_color_space.IsValid()); |
| 21 | EXPECT_NE(icc_profile, icc_profile_from_color_space); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | TEST(ICCProfile, SRGB) { |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 25 | ICCProfile icc_profile = ICCProfileForTestingSRGB(); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 26 | ColorSpace color_space = ColorSpace::CreateSRGB(); |
msarett | 29c8557 | 2017-02-10 22:16:55 | [diff] [blame] | 27 | sk_sp<SkColorSpace> sk_color_space = SkColorSpace::MakeSRGB(); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 28 | |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 29 | // The ICC profile parser should note that this is SRGB. |
Christopher Cameron | a4adb41b | 2017-07-13 06:44:25 | [diff] [blame] | 30 | EXPECT_EQ(icc_profile.GetColorSpace(), ColorSpace::CreateSRGB()); |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 31 | EXPECT_EQ(icc_profile.GetColorSpace().ToSkColorSpace().get(), |
| 32 | sk_color_space.get()); |
| 33 | // The parametric generating code should recognize that this is SRGB. |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame] | 34 | EXPECT_EQ(icc_profile.GetParametricColorSpace(), ColorSpace::CreateSRGB()); |
| 35 | EXPECT_EQ(icc_profile.GetParametricColorSpace().ToSkColorSpace().get(), |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 36 | sk_color_space.get()); |
| 37 | // The generated color space should recognize that this is SRGB. |
ccameron | 628896ee | 2017-02-03 09:29:12 | [diff] [blame] | 38 | EXPECT_EQ(color_space.ToSkColorSpace().get(), sk_color_space.get()); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 39 | } |
| 40 | |
ccameron | 549738b | 2017-01-28 17:39:32 | [diff] [blame] | 41 | TEST(ICCProfile, Equality) { |
| 42 | ICCProfile spin_profile = ICCProfileForTestingColorSpin(); |
| 43 | ICCProfile adobe_profile = ICCProfileForTestingAdobeRGB(); |
| 44 | EXPECT_TRUE(spin_profile == spin_profile); |
| 45 | EXPECT_FALSE(spin_profile != spin_profile); |
| 46 | EXPECT_FALSE(spin_profile == adobe_profile); |
| 47 | EXPECT_TRUE(spin_profile != adobe_profile); |
| 48 | |
| 49 | gfx::ColorSpace spin_space = spin_profile.GetColorSpace(); |
| 50 | gfx::ColorSpace adobe_space = adobe_profile.GetColorSpace(); |
| 51 | EXPECT_TRUE(spin_space == spin_space); |
| 52 | EXPECT_FALSE(spin_space != spin_space); |
| 53 | EXPECT_FALSE(spin_space == adobe_space); |
| 54 | EXPECT_TRUE(spin_space != adobe_space); |
| 55 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 56 | ICCProfile temp = ICCProfile::FromCacheMac(spin_space); |
| 57 | EXPECT_TRUE(temp.IsValid()); |
ccameron | 3b6fe6d | 2017-02-17 06:21:58 | [diff] [blame] | 58 | EXPECT_TRUE(spin_profile == temp); |
| 59 | EXPECT_FALSE(spin_profile != temp); |
| 60 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 61 | temp = ICCProfile::FromCacheMac(adobe_space); |
| 62 | EXPECT_TRUE(temp.IsValid()); |
ccameron | 3b6fe6d | 2017-02-17 06:21:58 | [diff] [blame] | 63 | EXPECT_FALSE(spin_profile == temp); |
| 64 | EXPECT_TRUE(spin_profile != temp); |
ccameron | 628896ee | 2017-02-03 09:29:12 | [diff] [blame] | 65 | |
| 66 | EXPECT_TRUE(!!spin_space.ToSkColorSpace()); |
| 67 | EXPECT_TRUE(!!adobe_space.ToSkColorSpace()); |
| 68 | EXPECT_FALSE(SkColorSpace::Equals( |
| 69 | spin_space.ToSkColorSpace().get(), |
| 70 | adobe_space.ToSkColorSpace().get())); |
ccameron | 549738b | 2017-01-28 17:39:32 | [diff] [blame] | 71 | } |
| 72 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 73 | TEST(ICCProfile, ParametricVersusExactInaccurate) { |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 74 | // This ICC profile has three transfer functions that differ enough that the |
| 75 | // parametric color space is considered inaccurate. |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 76 | ICCProfile multi_tr_fn = ICCProfileForTestingNoAnalyticTrFn(); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame] | 77 | EXPECT_NE(multi_tr_fn.GetColorSpace(), multi_tr_fn.GetParametricColorSpace()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 78 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 79 | // Fails to get parametric color space because the space is not parametric. |
| 80 | ICCProfile profile; |
| 81 | profile = ICCProfile::FromParametricColorSpace(multi_tr_fn.GetColorSpace()); |
| 82 | EXPECT_FALSE(profile.IsValid()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 83 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 84 | // The Mac cache does not find the parametric approximation, because the cache |
| 85 | // only has the original. |
| 86 | profile = ICCProfile::FromCacheMac(multi_tr_fn.GetParametricColorSpace()); |
| 87 | EXPECT_FALSE(profile.IsValid()); |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 88 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 89 | // The Mac cache does find the original. |
| 90 | profile = ICCProfile::FromCacheMac(multi_tr_fn.GetColorSpace()); |
| 91 | EXPECT_TRUE(profile.IsValid()); |
| 92 | EXPECT_EQ(profile, multi_tr_fn); |
| 93 | |
| 94 | // We are capable of generating a parametric approximation. |
| 95 | profile = ICCProfile::FromParametricColorSpace( |
| 96 | multi_tr_fn.GetParametricColorSpace()); |
| 97 | EXPECT_TRUE(profile.IsValid()); |
| 98 | EXPECT_NE(profile, multi_tr_fn); |
| 99 | } |
| 100 | |
| 101 | TEST(ICCProfile, ParametricVersusExactOvershoot) { |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 102 | // This ICC profile has a transfer function with T(1) that is greater than 1 |
| 103 | // in the approximation, but is still close enough to be considered accurate. |
| 104 | ICCProfile overshoot = ICCProfileForTestingOvershoot(); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame] | 105 | EXPECT_EQ(overshoot.GetColorSpace(), overshoot.GetParametricColorSpace()); |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 106 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 107 | ICCProfile profile; |
| 108 | profile = ICCProfile::FromCacheMac(overshoot.GetColorSpace()); |
| 109 | EXPECT_TRUE(profile.IsValid()); |
| 110 | EXPECT_EQ(profile, overshoot); |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 111 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 112 | profile = ICCProfile::FromParametricColorSpace(overshoot.GetColorSpace()); |
| 113 | EXPECT_TRUE(profile.IsValid()); |
| 114 | EXPECT_NE(profile, overshoot); |
| 115 | } |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 116 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 117 | TEST(ICCProfile, ParametricVersusExactAdobe) { |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 118 | // This ICC profile is precisely represented by the parametric color space. |
| 119 | ICCProfile accurate = ICCProfileForTestingAdobeRGB(); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame] | 120 | EXPECT_EQ(accurate.GetColorSpace(), accurate.GetParametricColorSpace()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 121 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 122 | ICCProfile profile; |
| 123 | profile = ICCProfile::FromCacheMac(accurate.GetColorSpace()); |
| 124 | EXPECT_TRUE(profile.IsValid()); |
| 125 | EXPECT_EQ(profile, accurate); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 126 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 127 | profile = ICCProfile::FromParametricColorSpace(accurate.GetColorSpace()); |
| 128 | EXPECT_TRUE(profile.IsValid()); |
| 129 | EXPECT_NE(profile, accurate); |
| 130 | } |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 131 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame^] | 132 | TEST(ICCProfile, ParametricVersusExactA2B) { |
ccameron | b3206e2 | 2017-03-25 02:09:12 | [diff] [blame] | 133 | // This ICC profile has only an A2B representation. We cannot create an |
| 134 | // SkColorSpaceXform to A2B only ICC profiles, so this should be marked |
| 135 | // as invalid. |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 136 | ICCProfile a2b = ICCProfileForTestingA2BOnly(); |
ccameron | b3206e2 | 2017-03-25 02:09:12 | [diff] [blame] | 137 | EXPECT_FALSE(a2b.GetColorSpace().IsValid()); |
Christopher Cameron | 64a55ad | 2017-09-21 05:31:22 | [diff] [blame] | 138 | |
| 139 | // Even though it is invalid, it should not be equal to the empty constructor. |
| 140 | EXPECT_NE(a2b, gfx::ICCProfile()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | TEST(ICCProfile, GarbageData) { |
| 144 | std::vector<char> bad_data(10 * 1024); |
| 145 | const char* bad_data_string = "deadbeef"; |
| 146 | for (size_t i = 0; i < bad_data.size(); ++i) |
| 147 | bad_data[i] = bad_data_string[i % 8]; |
| 148 | ICCProfile garbage_profile = |
| 149 | ICCProfile::FromData(bad_data.data(), bad_data.size()); |
| 150 | EXPECT_FALSE(garbage_profile.IsValid()); |
| 151 | EXPECT_FALSE(garbage_profile.GetColorSpace().IsValid()); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame] | 152 | EXPECT_FALSE(garbage_profile.GetParametricColorSpace().IsValid()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 153 | |
| 154 | ICCProfile default_ctor_profile; |
| 155 | EXPECT_FALSE(default_ctor_profile.IsValid()); |
| 156 | EXPECT_FALSE(default_ctor_profile.GetColorSpace().IsValid()); |
ccameron | e35e232 | 2017-11-02 23:33:46 | [diff] [blame] | 157 | EXPECT_FALSE(default_ctor_profile.GetParametricColorSpace().IsValid()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 158 | } |
| 159 | |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 160 | TEST(ICCProfile, GenericRGB) { |
| 161 | ColorSpace icc_profile = ICCProfileForTestingGenericRGB().GetColorSpace(); |
| 162 | ColorSpace color_space(ColorSpace::PrimaryID::APPLE_GENERIC_RGB, |
| 163 | ColorSpace::TransferID::GAMMA18); |
| 164 | |
| 165 | SkMatrix44 icc_profile_matrix; |
| 166 | SkMatrix44 color_space_matrix; |
| 167 | |
| 168 | icc_profile.GetPrimaryMatrix(&icc_profile_matrix); |
| 169 | color_space.GetPrimaryMatrix(&color_space_matrix); |
| 170 | |
| 171 | SkMatrix44 eye; |
| 172 | icc_profile_matrix.invert(&eye); |
| 173 | eye.postConcat(color_space_matrix); |
| 174 | EXPECT_TRUE(SkMatrixIsApproximatelyIdentity(eye)); |
| 175 | } |
| 176 | |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 177 | } // namespace gfx |