Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Elly Fong-Jones | a8ea66f1 | 2024-07-24 18:37:10 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/354829279): Remove this and convert code to safer constructs. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 10 | #include "ui/gfx/icc_profile.h" |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | #include "ui/gfx/color_space.h" |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 13 | #include "ui/gfx/skia_color_space_util.h" |
ccameron | 83e8e9a | 2017-01-04 01:31:20 | [diff] [blame] | 14 | #include "ui/gfx/test/icc_profiles.h" |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 15 | |
| 16 | namespace gfx { |
| 17 | |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 18 | TEST(ICCProfile, Conversions) { |
| 19 | ICCProfile icc_profile = ICCProfileForTestingColorSpin(); |
| 20 | ColorSpace color_space_from_icc_profile = icc_profile.GetColorSpace(); |
| 21 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 22 | ICCProfile icc_profile_from_color_space = |
Christopher Cameron | 20eeb0c | 2019-04-05 23:40:00 | [diff] [blame] | 23 | ICCProfile::FromColorSpace(color_space_from_icc_profile); |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 24 | EXPECT_TRUE(icc_profile_from_color_space.IsValid()); |
| 25 | EXPECT_NE(icc_profile, icc_profile_from_color_space); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | TEST(ICCProfile, SRGB) { |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 29 | ICCProfile icc_profile = ICCProfileForTestingSRGB(); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 30 | ColorSpace color_space = ColorSpace::CreateSRGB(); |
msarett | 29c8557 | 2017-02-10 22:16:55 | [diff] [blame] | 31 | sk_sp<SkColorSpace> sk_color_space = SkColorSpace::MakeSRGB(); |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 32 | |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 33 | // The ICC profile parser should note that this is SRGB. |
Christopher Cameron | a4adb41b | 2017-07-13 06:44:25 | [diff] [blame] | 34 | EXPECT_EQ(icc_profile.GetColorSpace(), ColorSpace::CreateSRGB()); |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 35 | EXPECT_EQ(icc_profile.GetColorSpace().ToSkColorSpace().get(), |
| 36 | sk_color_space.get()); |
ccameron | fa21c64 | 2017-05-10 19:38:36 | [diff] [blame] | 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 | |
ccameron | 628896ee | 2017-02-03 09:29:12 | [diff] [blame] | 56 | EXPECT_TRUE(!!spin_space.ToSkColorSpace()); |
| 57 | EXPECT_TRUE(!!adobe_space.ToSkColorSpace()); |
| 58 | EXPECT_FALSE(SkColorSpace::Equals( |
| 59 | spin_space.ToSkColorSpace().get(), |
| 60 | adobe_space.ToSkColorSpace().get())); |
ccameron | 549738b | 2017-01-28 17:39:32 | [diff] [blame] | 61 | } |
| 62 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 63 | TEST(ICCProfile, ParametricVersusExactInaccurate) { |
Brian Osman | 37233e3 | 2018-05-17 16:53:17 | [diff] [blame] | 64 | // This ICC profile has three transfer functions that differ significantly, |
| 65 | // but ICCProfiles are always either invalid or considered accurate (and in |
| 66 | // this case, each curve is approximated, so the profile is "accurate"). |
| 67 | // See comments in ICCProfile::Internals::Analyze. |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 68 | ICCProfile multi_tr_fn = ICCProfileForTestingNoAnalyticTrFn(); |
Christopher Cameron | 20eeb0c | 2019-04-05 23:40:00 | [diff] [blame] | 69 | EXPECT_TRUE(multi_tr_fn.IsColorSpaceAccurate()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 70 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 71 | // We are capable of generating a parametric approximation. |
Christopher Cameron | 79358bd | 2018-08-02 04:31:44 | [diff] [blame] | 72 | ICCProfile profile; |
Christopher Cameron | 20eeb0c | 2019-04-05 23:40:00 | [diff] [blame] | 73 | profile = ICCProfile::FromColorSpace(multi_tr_fn.GetColorSpace()); |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 74 | EXPECT_TRUE(profile.IsValid()); |
| 75 | EXPECT_NE(profile, multi_tr_fn); |
| 76 | } |
| 77 | |
| 78 | TEST(ICCProfile, ParametricVersusExactOvershoot) { |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 79 | // This ICC profile has a transfer function with T(1) that is greater than 1 |
| 80 | // in the approximation, but is still close enough to be considered accurate. |
| 81 | ICCProfile overshoot = ICCProfileForTestingOvershoot(); |
Christopher Cameron | 20eeb0c | 2019-04-05 23:40:00 | [diff] [blame] | 82 | EXPECT_TRUE(overshoot.IsColorSpaceAccurate()); |
ccameron | 95f83c51 | 2017-03-31 20:16:12 | [diff] [blame] | 83 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 84 | ICCProfile profile; |
Christopher Cameron | 20eeb0c | 2019-04-05 23:40:00 | [diff] [blame] | 85 | profile = ICCProfile::FromColorSpace(overshoot.GetColorSpace()); |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 86 | EXPECT_TRUE(profile.IsValid()); |
| 87 | EXPECT_NE(profile, overshoot); |
| 88 | } |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 89 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 90 | TEST(ICCProfile, ParametricVersusExactAdobe) { |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 91 | // This ICC profile is precisely represented by the parametric color space. |
| 92 | ICCProfile accurate = ICCProfileForTestingAdobeRGB(); |
Christopher Cameron | 20eeb0c | 2019-04-05 23:40:00 | [diff] [blame] | 93 | EXPECT_TRUE(accurate.IsColorSpaceAccurate()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 94 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 95 | ICCProfile profile; |
Christopher Cameron | 20eeb0c | 2019-04-05 23:40:00 | [diff] [blame] | 96 | profile = ICCProfile::FromColorSpace(accurate.GetColorSpace()); |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 97 | EXPECT_TRUE(profile.IsValid()); |
| 98 | EXPECT_NE(profile, accurate); |
| 99 | } |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 100 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 101 | TEST(ICCProfile, ParametricVersusExactA2B) { |
Brian Osman | 37233e3 | 2018-05-17 16:53:17 | [diff] [blame] | 102 | // This ICC profile has only an A2B representation. We cannot transform to |
| 103 | // A2B only ICC profiles, so this should be marked as invalid. |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 104 | ICCProfile a2b = ICCProfileForTestingA2BOnly(); |
ccameron | b3206e2 | 2017-03-25 02:09:12 | [diff] [blame] | 105 | EXPECT_FALSE(a2b.GetColorSpace().IsValid()); |
Christopher Cameron | 64a55ad | 2017-09-21 05:31:22 | [diff] [blame] | 106 | |
Brian Osman | 37233e3 | 2018-05-17 16:53:17 | [diff] [blame] | 107 | // Even though it is invalid, it should not be equal to the empty constructor |
Christopher Cameron | 64a55ad | 2017-09-21 05:31:22 | [diff] [blame] | 108 | EXPECT_NE(a2b, gfx::ICCProfile()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST(ICCProfile, GarbageData) { |
| 112 | std::vector<char> bad_data(10 * 1024); |
| 113 | const char* bad_data_string = "deadbeef"; |
| 114 | for (size_t i = 0; i < bad_data.size(); ++i) |
| 115 | bad_data[i] = bad_data_string[i % 8]; |
| 116 | ICCProfile garbage_profile = |
| 117 | ICCProfile::FromData(bad_data.data(), bad_data.size()); |
| 118 | EXPECT_FALSE(garbage_profile.IsValid()); |
| 119 | EXPECT_FALSE(garbage_profile.GetColorSpace().IsValid()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 120 | |
| 121 | ICCProfile default_ctor_profile; |
| 122 | EXPECT_FALSE(default_ctor_profile.IsValid()); |
| 123 | EXPECT_FALSE(default_ctor_profile.GetColorSpace().IsValid()); |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 124 | } |
| 125 | |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 126 | TEST(ICCProfile, GenericRGB) { |
| 127 | ColorSpace icc_profile = ICCProfileForTestingGenericRGB().GetColorSpace(); |
| 128 | ColorSpace color_space(ColorSpace::PrimaryID::APPLE_GENERIC_RGB, |
| 129 | ColorSpace::TransferID::GAMMA18); |
| 130 | |
Xianzhu Wang | 239702f | 2022-02-18 08:02:38 | [diff] [blame] | 131 | SkM44 icc_profile_matrix = icc_profile.GetPrimaryMatrix(); |
| 132 | SkM44 color_space_matrix = color_space.GetPrimaryMatrix(); |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 133 | |
Xianzhu Wang | 239702f | 2022-02-18 08:02:38 | [diff] [blame] | 134 | SkM44 eye; |
| 135 | EXPECT_TRUE(icc_profile_matrix.invert(&eye)); |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 136 | eye.postConcat(color_space_matrix); |
Xianzhu Wang | 239702f | 2022-02-18 08:02:38 | [diff] [blame] | 137 | EXPECT_TRUE(SkM44IsApproximatelyIdentity(eye)); |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 138 | } |
| 139 | |
ccameron | 87b65e9b6 | 2016-12-27 10:19:07 | [diff] [blame] | 140 | } // namespace gfx |