| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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 |
| 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "ui/gfx/color_space.h" |
| 7 #include "ui/gfx/color_transform.h" |
| 8 #include "ui/gfx/transform.h" |
| 9 |
| 10 namespace gfx { |
| 11 |
| 12 // Internal functions, exposted for testing. |
| 13 GFX_EXPORT Transform GetPrimaryMatrix(ColorSpace::PrimaryID id); |
| 14 GFX_EXPORT Transform GetTransferMatrix(ColorSpace::MatrixID id); |
| 15 GFX_EXPORT float ToLinear(ColorSpace::TransferID id, float v); |
| 16 GFX_EXPORT float FromLinear(ColorSpace::TransferID id, float v); |
| 17 |
| 18 ColorSpace::PrimaryID all_primaries[] = { |
| 19 ColorSpace::PrimaryID::BT709, ColorSpace::PrimaryID::BT470M, |
| 20 ColorSpace::PrimaryID::BT470BG, ColorSpace::PrimaryID::SMPTE170M, |
| 21 ColorSpace::PrimaryID::SMPTE240M, ColorSpace::PrimaryID::FILM, |
| 22 ColorSpace::PrimaryID::BT2020, ColorSpace::PrimaryID::SMPTEST428_1, |
| 23 ColorSpace::PrimaryID::SMPTEST431_2, ColorSpace::PrimaryID::SMPTEST432_1, |
| 24 }; |
| 25 |
| 26 ColorSpace::TransferID all_transfers[] = { |
| 27 ColorSpace::TransferID::BT709, ColorSpace::TransferID::GAMMA22, |
| 28 ColorSpace::TransferID::GAMMA28, ColorSpace::TransferID::SMPTE170M, |
| 29 ColorSpace::TransferID::SMPTE240M, ColorSpace::TransferID::LINEAR, |
| 30 ColorSpace::TransferID::LOG, ColorSpace::TransferID::LOG_SQRT, |
| 31 ColorSpace::TransferID::IEC61966_2_4, ColorSpace::TransferID::BT1361_ECG, |
| 32 ColorSpace::TransferID::IEC61966_2_1, ColorSpace::TransferID::BT2020_10, |
| 33 ColorSpace::TransferID::BT2020_12, ColorSpace::TransferID::SMPTEST2084, |
| 34 // This one is weird as the non-linear numbers are not between 0 and 1. |
| 35 // TODO(hubbe): Test this separately. |
| 36 // ColorSpace::TransferID::SMPTEST428_1, |
| 37 }; |
| 38 |
| 39 ColorSpace::MatrixID all_matrices[] = { |
| 40 ColorSpace::MatrixID::RGB, ColorSpace::MatrixID::BT709, |
| 41 ColorSpace::MatrixID::UNSPECIFIED, ColorSpace::MatrixID::RESERVED, |
| 42 ColorSpace::MatrixID::FCC, ColorSpace::MatrixID::BT470BG, |
| 43 ColorSpace::MatrixID::SMPTE170M, ColorSpace::MatrixID::SMPTE240M, |
| 44 |
| 45 // YCOCG produces lots of negative values which isn't compatible with many |
| 46 // transfer functions. |
| 47 // TODO(hubbe): Test this separately. |
| 48 // ColorSpace::MatrixID::YCOCG, |
| 49 ColorSpace::MatrixID::BT2020_NCL, ColorSpace::MatrixID::BT2020_CL, |
| 50 ColorSpace::MatrixID::YDZDX, |
| 51 }; |
| 52 |
| 53 ColorSpace::RangeID all_ranges[] = {ColorSpace::RangeID::FULL, |
| 54 ColorSpace::RangeID::LIMITED}; |
| 55 |
| 56 TEST(SimpleColorSpace, BT709toSRGB) { |
| 57 ColorSpace bt709 = ColorSpace::CreateREC709(); |
| 58 ColorSpace sRGB = ColorSpace::CreateSRGB(); |
| 59 std::unique_ptr<ColorTransform> t(ColorTransform::NewColorTransform( |
| 60 bt709, sRGB, ColorTransform::Intent::ABSOLUTE)); |
| 61 |
| 62 ColorTransform::TriStim tmp(16.0f / 255.0f, 0.5f, 0.5f); |
| 63 t->transform(&tmp, 1); |
| 64 EXPECT_NEAR(tmp.x(), 0.0f, 0.001f); |
| 65 EXPECT_NEAR(tmp.y(), 0.0f, 0.001f); |
| 66 EXPECT_NEAR(tmp.z(), 0.0f, 0.001f); |
| 67 |
| 68 tmp = ColorTransform::TriStim(235.0f / 255.0f, 0.5f, 0.5f); |
| 69 t->transform(&tmp, 1); |
| 70 EXPECT_NEAR(tmp.x(), 1.0f, 0.001f); |
| 71 EXPECT_NEAR(tmp.y(), 1.0f, 0.001f); |
| 72 EXPECT_NEAR(tmp.z(), 1.0f, 0.001f); |
| 73 |
| 74 // Test a blue color |
| 75 tmp = ColorTransform::TriStim(128.0f / 255.0f, 240.0f / 255.0f, 0.5f); |
| 76 t->transform(&tmp, 1); |
| 77 EXPECT_GT(tmp.z(), tmp.x()); |
| 78 EXPECT_GT(tmp.z(), tmp.y()); |
| 79 } |
| 80 |
| 81 TEST(SimpleColorSpace, UnknownToSRGB) { |
| 82 ColorSpace unknown; |
| 83 ColorSpace sRGB = ColorSpace::CreateSRGB(); |
| 84 std::unique_ptr<ColorTransform> t(ColorTransform::NewColorTransform( |
| 85 unknown, sRGB, ColorTransform::Intent::PERCEPTUAL)); |
| 86 |
| 87 ColorTransform::TriStim tmp(16.0f / 255.0f, 0.5f, 0.5f); |
| 88 t->transform(&tmp, 1); |
| 89 EXPECT_NEAR(tmp.x(), 0.0f, 0.001f); |
| 90 EXPECT_NEAR(tmp.y(), 0.0f, 0.001f); |
| 91 EXPECT_NEAR(tmp.z(), 0.0f, 0.001f); |
| 92 |
| 93 tmp = ColorTransform::TriStim(235.0f / 255.0f, 0.5f, 0.5f); |
| 94 t->transform(&tmp, 1); |
| 95 EXPECT_NEAR(tmp.x(), 1.0f, 0.001f); |
| 96 EXPECT_NEAR(tmp.y(), 1.0f, 0.001f); |
| 97 EXPECT_NEAR(tmp.z(), 1.0f, 0.001f); |
| 98 |
| 99 // Test a blue color |
| 100 tmp = ColorTransform::TriStim(128.0f / 255.0f, 240.0f / 255.0f, 0.5f); |
| 101 t->transform(&tmp, 1); |
| 102 EXPECT_GT(tmp.z(), tmp.x()); |
| 103 EXPECT_GT(tmp.z(), tmp.y()); |
| 104 } |
| 105 |
| 106 class PrimaryTest : public testing::TestWithParam<ColorSpace::PrimaryID> {}; |
| 107 |
| 108 TEST_P(PrimaryTest, checkInvertible) { |
| 109 EXPECT_EQ(GetPrimaryMatrix(GetParam()).matrix().get(3, 3), 1.0f); |
| 110 // Check that all primary matrices are invertable. |
| 111 EXPECT_TRUE(GetPrimaryMatrix(GetParam()).IsInvertible()); |
| 112 } |
| 113 |
| 114 INSTANTIATE_TEST_CASE_P(ColorSpace, |
| 115 PrimaryTest, |
| 116 testing::ValuesIn(all_primaries)); |
| 117 |
| 118 class MatrixTest : public testing::TestWithParam<ColorSpace::MatrixID> {}; |
| 119 |
| 120 TEST_P(MatrixTest, checkInvertible) { |
| 121 EXPECT_EQ(GetTransferMatrix(GetParam()).matrix().get(3, 3), 1.0f); |
| 122 // Check that all transfer matrices are invertable. |
| 123 EXPECT_TRUE(GetTransferMatrix(GetParam()).IsInvertible()); |
| 124 }; |
| 125 |
| 126 INSTANTIATE_TEST_CASE_P(ColorSpace, |
| 127 MatrixTest, |
| 128 testing::ValuesIn(all_matrices)); |
| 129 |
| 130 class TransferTest : public testing::TestWithParam<ColorSpace::TransferID> {}; |
| 131 |
| 132 TEST_P(TransferTest, basicTest) { |
| 133 for (float x = 0.0f; x <= 1.0f; x += 1.0f / 128.0f) { |
| 134 float linear = ToLinear(GetParam(), x); |
| 135 float x2 = FromLinear(GetParam(), linear); |
| 136 EXPECT_NEAR(x, x2, 0.001f); |
| 137 } |
| 138 } |
| 139 |
| 140 INSTANTIATE_TEST_CASE_P(ColorSpace, |
| 141 TransferTest, |
| 142 testing::ValuesIn(all_transfers)); |
| 143 |
| 144 typedef std::tr1::tuple<ColorSpace::PrimaryID, |
| 145 ColorSpace::TransferID, |
| 146 ColorSpace::MatrixID, |
| 147 ColorSpace::RangeID> |
| 148 ColorSpaceTestData; |
| 149 |
| 150 class ColorSpaceTest : public testing::TestWithParam<ColorSpaceTestData> { |
| 151 public: |
| 152 ColorSpaceTest() |
| 153 : color_space_(std::tr1::get<0>(GetParam()), |
| 154 std::tr1::get<1>(GetParam()), |
| 155 std::tr1::get<2>(GetParam()), |
| 156 std::tr1::get<3>(GetParam())) {} |
| 157 |
| 158 protected: |
| 159 ColorSpace color_space_; |
| 160 }; |
| 161 |
| 162 TEST_P(ColorSpaceTest, testNullTransform) { |
| 163 std::unique_ptr<ColorTransform> t(ColorTransform::NewColorTransform( |
| 164 color_space_, color_space_, ColorTransform::Intent::ABSOLUTE)); |
| 165 ColorTransform::TriStim tristim(0.4f, 0.5f, 0.6f); |
| 166 t->transform(&tristim, 1); |
| 167 EXPECT_NEAR(tristim.x(), 0.4f, 0.001f); |
| 168 EXPECT_NEAR(tristim.y(), 0.5f, 0.001f); |
| 169 EXPECT_NEAR(tristim.z(), 0.6f, 0.001f); |
| 170 } |
| 171 |
| 172 TEST_P(ColorSpaceTest, toXYZandBack) { |
| 173 std::unique_ptr<ColorTransform> t1(ColorTransform::NewColorTransform( |
| 174 color_space_, ColorSpace::CreateXYZD50(), |
| 175 ColorTransform::Intent::ABSOLUTE)); |
| 176 std::unique_ptr<ColorTransform> t2(ColorTransform::NewColorTransform( |
| 177 ColorSpace::CreateXYZD50(), color_space_, |
| 178 ColorTransform::Intent::ABSOLUTE)); |
| 179 ColorTransform::TriStim tristim(0.4f, 0.5f, 0.6f); |
| 180 t1->transform(&tristim, 1); |
| 181 t2->transform(&tristim, 1); |
| 182 EXPECT_NEAR(tristim.x(), 0.4f, 0.001f); |
| 183 EXPECT_NEAR(tristim.y(), 0.5f, 0.001f); |
| 184 EXPECT_NEAR(tristim.z(), 0.6f, 0.001f); |
| 185 } |
| 186 |
| 187 INSTANTIATE_TEST_CASE_P( |
| 188 A, |
| 189 ColorSpaceTest, |
| 190 testing::Combine(testing::ValuesIn(all_primaries), |
| 191 testing::ValuesIn(all_transfers), |
| 192 testing::Values(ColorSpace::MatrixID::BT709), |
| 193 testing::Values(ColorSpace::RangeID::LIMITED))); |
| 194 |
| 195 INSTANTIATE_TEST_CASE_P( |
| 196 B, |
| 197 ColorSpaceTest, |
| 198 testing::Combine(testing::Values(ColorSpace::PrimaryID::BT709), |
| 199 testing::ValuesIn(all_transfers), |
| 200 testing::ValuesIn(all_matrices), |
| 201 testing::ValuesIn(all_ranges))); |
| 202 |
| 203 INSTANTIATE_TEST_CASE_P( |
| 204 C, |
| 205 ColorSpaceTest, |
| 206 testing::Combine(testing::ValuesIn(all_primaries), |
| 207 testing::Values(ColorSpace::TransferID::BT709), |
| 208 testing::ValuesIn(all_matrices), |
| 209 testing::ValuesIn(all_ranges))); |
| 210 } // namespace |
| OLD | NEW |