msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "Resources.h" |
| 9 | #include "SkCodec.h" |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 10 | #include "SkCodecPriv.h" |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
| 12 | #include "SkColorSpace.h" |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 13 | #include "SkColorSpace_A2B.h" |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 14 | #include "SkColorSpace_Base.h" |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 15 | #include "SkColorSpace_XYZ.h" |
msarett | 31d097e8 | 2016-10-11 12:15:03 -0700 | [diff] [blame] | 16 | #include "SkColorSpaceXform_Base.h" |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 17 | #include "Test.h" |
| 18 | |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 19 | static constexpr int kChannels = 3; |
| 20 | |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 21 | class ColorSpaceXformTest { |
| 22 | public: |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 23 | static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform(const sk_sp<SkGammas>& gammas) { |
| 24 | // Logically we can pass any matrix here. For simplicty, pass I(), i.e. D50 XYZ gamut. |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 25 | sk_sp<SkColorSpace> space(new SkColorSpace_XYZ( |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 26 | kNonStandard_SkGammaNamed, gammas, SkMatrix::I(), nullptr)); |
msarett | 9dc6cf6 | 2016-08-23 17:53:06 -0700 | [diff] [blame] | 27 | |
| 28 | // Use special testing entry point, so we don't skip the xform, even though src == dst. |
raftias | 9488833 | 2016-10-18 10:02:51 -0700 | [diff] [blame] | 29 | return SlowIdentityXform(static_cast<SkColorSpace_XYZ*>(space.get())); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 30 | } |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 31 | |
| 32 | static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform_A2B( |
| 33 | SkGammaNamed gammaNamed, const sk_sp<SkGammas>& gammas) { |
| 34 | std::vector<SkColorSpace_A2B::Element> srcElements; |
| 35 | // sRGB |
| 36 | const float values[16] = { |
| 37 | 0.4358f, 0.3853f, 0.1430f, 0.0f, |
| 38 | 0.2224f, 0.7170f, 0.0606f, 0.0f, |
| 39 | 0.0139f, 0.0971f, 0.7139f, 0.0f, |
| 40 | 0.0000f, 0.0000f, 0.0000f, 1.0f |
| 41 | }; |
| 42 | SkMatrix44 arbitraryMatrix{SkMatrix44::kUninitialized_Constructor}; |
| 43 | arbitraryMatrix.setRowMajorf(values); |
| 44 | if (kNonStandard_SkGammaNamed == gammaNamed) { |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 45 | SkASSERT(gammas); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 46 | srcElements.push_back(SkColorSpace_A2B::Element(gammas)); |
| 47 | } else { |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 48 | srcElements.push_back(SkColorSpace_A2B::Element(gammaNamed, kChannels)); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 49 | } |
| 50 | srcElements.push_back(SkColorSpace_A2B::Element(arbitraryMatrix)); |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 51 | auto srcSpace = |
| 52 | ColorSpaceXformTest::CreateA2BSpace(SkColorSpace_A2B::PCS::kXYZ, |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 53 | std::move(srcElements)); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 54 | sk_sp<SkColorSpace> dstSpace(new SkColorSpace_XYZ(gammaNamed, gammas, arbitraryMatrix, |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 55 | nullptr)); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 56 | |
| 57 | return SkColorSpaceXform::New(static_cast<SkColorSpace_A2B*>(srcSpace.get()), |
| 58 | static_cast<SkColorSpace_XYZ*>(dstSpace.get())); |
| 59 | } |
| 60 | |
| 61 | static sk_sp<SkColorSpace> CreateA2BSpace(SkColorSpace_A2B::PCS pcs, |
| 62 | std::vector<SkColorSpace_A2B::Element> elements) { |
Leon Scroggins III | f78b55c | 2017-10-31 13:49:14 -0400 | [diff] [blame] | 63 | return sk_sp<SkColorSpace>(new SkColorSpace_A2B(SkColorSpace::kRGB_Type, |
| 64 | std::move(elements), |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 65 | pcs, nullptr)); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 66 | } |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Mike Klein | c7d9c0b | 2017-04-17 14:43:59 -0400 | [diff] [blame] | 69 | static bool almost_equal(int x, int y, int tol=1) { |
| 70 | return SkTAbs(x-y) <= tol; |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Matt Sarett | f489886 | 2016-10-16 10:20:41 -0400 | [diff] [blame] | 73 | static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& gammas, |
| 74 | bool repeat) { |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 75 | // Arbitrary set of 10 pixels |
| 76 | constexpr int width = 10; |
| 77 | constexpr uint32_t srcPixels[width] = { |
| 78 | 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271, |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 79 | 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, }; |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 80 | uint32_t dstPixels[width]; |
| 81 | |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 82 | // Create and perform an identity xform. |
| 83 | std::unique_ptr<SkColorSpaceXform> xform = ColorSpaceXformTest::CreateIdentityXform(gammas); |
msarett | 31d097e8 | 2016-10-11 12:15:03 -0700 | [diff] [blame] | 84 | bool result = xform->apply(select_xform_format(kN32_SkColorType), dstPixels, |
| 85 | SkColorSpaceXform::kBGRA_8888_ColorFormat, srcPixels, width, |
| 86 | kOpaque_SkAlphaType); |
| 87 | REPORTER_ASSERT(r, result); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 88 | |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 89 | // Since the src->dst matrix is the identity, and the gamma curves match, |
| 90 | // the pixels should be unchanged. |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 91 | for (int i = 0; i < width; i++) { |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 92 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF), |
msarett | cf7b877 | 2016-09-22 12:37:04 -0700 | [diff] [blame] | 93 | SkGetPackedB32(dstPixels[i]))); |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 94 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF), |
msarett | 9dc6cf6 | 2016-08-23 17:53:06 -0700 | [diff] [blame] | 95 | SkGetPackedG32(dstPixels[i]))); |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 96 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF), |
msarett | cf7b877 | 2016-09-22 12:37:04 -0700 | [diff] [blame] | 97 | SkGetPackedR32(dstPixels[i]))); |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 98 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF), |
msarett | 9dc6cf6 | 2016-08-23 17:53:06 -0700 | [diff] [blame] | 99 | SkGetPackedA32(dstPixels[i]))); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 100 | } |
Matt Sarett | f489886 | 2016-10-16 10:20:41 -0400 | [diff] [blame] | 101 | |
| 102 | if (repeat) { |
| 103 | // We should cache part of the transform after the run. So it is interesting |
| 104 | // to make sure it still runs correctly the second time. |
| 105 | test_identity_xform(r, gammas, false); |
| 106 | } |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 107 | } |
| 108 | |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 109 | static void test_identity_xform_A2B(skiatest::Reporter* r, SkGammaNamed gammaNamed, |
Mike Klein | c7d9c0b | 2017-04-17 14:43:59 -0400 | [diff] [blame] | 110 | const sk_sp<SkGammas>& gammas, int tol=1) { |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 111 | // Arbitrary set of 10 pixels |
| 112 | constexpr int width = 10; |
| 113 | constexpr uint32_t srcPixels[width] = { |
| 114 | 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271, |
| 115 | 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, }; |
| 116 | uint32_t dstPixels[width]; |
| 117 | |
| 118 | // Create and perform an identity xform. |
| 119 | auto xform = ColorSpaceXformTest::CreateIdentityXform_A2B(gammaNamed, gammas); |
| 120 | bool result = xform->apply(select_xform_format(kN32_SkColorType), dstPixels, |
| 121 | SkColorSpaceXform::kBGRA_8888_ColorFormat, srcPixels, width, |
| 122 | kOpaque_SkAlphaType); |
| 123 | REPORTER_ASSERT(r, result); |
| 124 | |
| 125 | // Since the src->dst matrix is the identity, and the gamma curves match, |
Mike Klein | c7d9c0b | 2017-04-17 14:43:59 -0400 | [diff] [blame] | 126 | // the pixels should be ~unchanged. |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 127 | for (int i = 0; i < width; i++) { |
| 128 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF), |
Mike Klein | c7d9c0b | 2017-04-17 14:43:59 -0400 | [diff] [blame] | 129 | SkGetPackedB32(dstPixels[i]), tol)); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 130 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF), |
Mike Klein | c7d9c0b | 2017-04-17 14:43:59 -0400 | [diff] [blame] | 131 | SkGetPackedG32(dstPixels[i]), tol)); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 132 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF), |
Mike Klein | c7d9c0b | 2017-04-17 14:43:59 -0400 | [diff] [blame] | 133 | SkGetPackedR32(dstPixels[i]), tol)); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 134 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF), |
Mike Klein | c7d9c0b | 2017-04-17 14:43:59 -0400 | [diff] [blame] | 135 | SkGetPackedA32(dstPixels[i]), tol)); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 136 | } |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 137 | } |
| 138 | |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 139 | DEF_TEST(ColorSpaceXform_TableGamma, r) { |
| 140 | // Lookup-table based gamma curves |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 141 | constexpr size_t tableSize = 10; |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 142 | void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize); |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 143 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas(kChannels)); |
| 144 | for (int i = 0; i < kChannels; ++i) { |
| 145 | gammas->fType[i] = SkGammas::Type::kTable_Type; |
| 146 | gammas->fData[i].fTable.fSize = tableSize; |
| 147 | gammas->fData[i].fTable.fOffset = 0; |
| 148 | } |
| 149 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 150 | float* table = SkTAddOffset<float>(memory, sizeof(SkGammas)); |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 151 | table[0] = 0.00f; |
| 152 | table[1] = 0.05f; |
| 153 | table[2] = 0.10f; |
| 154 | table[3] = 0.15f; |
| 155 | table[4] = 0.25f; |
| 156 | table[5] = 0.35f; |
| 157 | table[6] = 0.45f; |
| 158 | table[7] = 0.60f; |
| 159 | table[8] = 0.75f; |
| 160 | table[9] = 1.00f; |
Mike Klein | c7d9c0b | 2017-04-17 14:43:59 -0400 | [diff] [blame] | 161 | // This table's pretty small compared to real ones in the wild (think 256), |
| 162 | // so we give test_identity_xform_A2B a wide tolerance. |
| 163 | // This lets us implement table transfer functions with a single lookup. |
| 164 | const int tolerance = 13; |
| 165 | |
Matt Sarett | f489886 | 2016-10-16 10:20:41 -0400 | [diff] [blame] | 166 | test_identity_xform(r, gammas, true); |
Mike Klein | c7d9c0b | 2017-04-17 14:43:59 -0400 | [diff] [blame] | 167 | test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas, tolerance); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | DEF_TEST(ColorSpaceXform_ParametricGamma, r) { |
| 171 | // Parametric gamma curves |
Matt Sarett | df44fc5 | 2016-10-11 16:57:50 -0400 | [diff] [blame] | 172 | void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkColorSpaceTransferFn)); |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 173 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas(kChannels)); |
| 174 | for (int i = 0; i < kChannels; ++i) { |
| 175 | gammas->fType[i] = SkGammas::Type::kParam_Type; |
| 176 | gammas->fData[i].fParamOffset = 0; |
| 177 | } |
| 178 | |
Matt Sarett | df44fc5 | 2016-10-11 16:57:50 -0400 | [diff] [blame] | 179 | SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn> |
| 180 | (memory, sizeof(SkGammas)); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 181 | |
Matt Sarett | 2410717 | 2016-12-19 14:33:35 -0500 | [diff] [blame] | 182 | // Interval. |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 183 | params->fD = 0.04045f; |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 184 | |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 185 | // First equation: |
Matt Sarett | 2410717 | 2016-12-19 14:33:35 -0500 | [diff] [blame] | 186 | params->fC = 1.0f / 12.92f; |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 187 | params->fF = 0.0f; |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 188 | |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 189 | // Second equation: |
| 190 | // Note that the function is continuous (it's actually sRGB). |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 191 | params->fA = 1.0f / 1.055f; |
| 192 | params->fB = 0.055f / 1.055f; |
Matt Sarett | 2410717 | 2016-12-19 14:33:35 -0500 | [diff] [blame] | 193 | params->fE = 0.0f; |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 194 | params->fG = 2.4f; |
Matt Sarett | f489886 | 2016-10-16 10:20:41 -0400 | [diff] [blame] | 195 | test_identity_xform(r, gammas, true); |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 196 | test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | DEF_TEST(ColorSpaceXform_ExponentialGamma, r) { |
| 200 | // Exponential gamma curves |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 201 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas(kChannels)); |
| 202 | for (int i = 0; i < kChannels; ++i) { |
| 203 | gammas->fType[i] = SkGammas::Type::kValue_Type; |
| 204 | gammas->fData[i].fValue = 1.4f; |
| 205 | } |
Matt Sarett | f489886 | 2016-10-16 10:20:41 -0400 | [diff] [blame] | 206 | test_identity_xform(r, gammas, true); |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 207 | test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas); |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 208 | } |
| 209 | |
msarett | 55bcc8e | 2016-09-09 07:48:05 -0700 | [diff] [blame] | 210 | DEF_TEST(ColorSpaceXform_NamedGamma, r) { |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 211 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas(kChannels)); |
| 212 | gammas->fType[0] = gammas->fType[1] = gammas->fType[2] = SkGammas::Type::kNamed_Type; |
| 213 | gammas->fData[0].fNamed = kSRGB_SkGammaNamed; |
| 214 | gammas->fData[1].fNamed = k2Dot2Curve_SkGammaNamed; |
| 215 | gammas->fData[2].fNamed = kLinear_SkGammaNamed; |
Matt Sarett | f489886 | 2016-10-16 10:20:41 -0400 | [diff] [blame] | 216 | test_identity_xform(r, gammas, true); |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 217 | test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas); |
| 218 | test_identity_xform_A2B(r, kSRGB_SkGammaNamed, nullptr); |
| 219 | test_identity_xform_A2B(r, k2Dot2Curve_SkGammaNamed, nullptr); |
| 220 | test_identity_xform_A2B(r, kLinear_SkGammaNamed, nullptr); |
msarett | 55bcc8e | 2016-09-09 07:48:05 -0700 | [diff] [blame] | 221 | } |
| 222 | |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 223 | DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) { |
| 224 | constexpr size_t tableSize = 10; |
| 225 | void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize + |
Matt Sarett | df44fc5 | 2016-10-11 16:57:50 -0400 | [diff] [blame] | 226 | sizeof(SkColorSpaceTransferFn)); |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 227 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas(kChannels)); |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 228 | |
| 229 | float* table = SkTAddOffset<float>(memory, sizeof(SkGammas)); |
| 230 | table[0] = 0.00f; |
| 231 | table[1] = 0.15f; |
| 232 | table[2] = 0.20f; |
| 233 | table[3] = 0.25f; |
| 234 | table[4] = 0.35f; |
| 235 | table[5] = 0.45f; |
| 236 | table[6] = 0.55f; |
| 237 | table[7] = 0.70f; |
| 238 | table[8] = 0.85f; |
| 239 | table[9] = 1.00f; |
| 240 | |
Matt Sarett | df44fc5 | 2016-10-11 16:57:50 -0400 | [diff] [blame] | 241 | SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn>(memory, |
| 242 | sizeof(SkGammas) + sizeof(float) * tableSize); |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 243 | params->fA = 1.0f / 1.055f; |
| 244 | params->fB = 0.055f / 1.055f; |
Matt Sarett | 2410717 | 2016-12-19 14:33:35 -0500 | [diff] [blame] | 245 | params->fC = 1.0f / 12.92f; |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 246 | params->fD = 0.04045f; |
Matt Sarett | 2410717 | 2016-12-19 14:33:35 -0500 | [diff] [blame] | 247 | params->fE = 0.0f; |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 248 | params->fF = 0.0f; |
| 249 | params->fG = 2.4f; |
| 250 | |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 251 | gammas->fType[0] = SkGammas::Type::kValue_Type; |
| 252 | gammas->fData[0].fValue = 1.2f; |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 253 | |
Mike Klein | 4437517 | 2017-04-17 19:32:05 -0400 | [diff] [blame] | 254 | // See ColorSpaceXform_TableGamma... we've decided to allow some tolerance |
| 255 | // for SkJumper's implementation of tables. |
| 256 | const int tolerance = 12; |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 257 | gammas->fType[1] = SkGammas::Type::kTable_Type; |
| 258 | gammas->fData[1].fTable.fSize = tableSize; |
| 259 | gammas->fData[1].fTable.fOffset = 0; |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 260 | |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 261 | gammas->fType[2] = SkGammas::Type::kParam_Type; |
| 262 | gammas->fData[2].fParamOffset = sizeof(float) * tableSize; |
msarett | 1b93bd1 | 2016-07-21 07:11:26 -0700 | [diff] [blame] | 263 | |
Matt Sarett | f489886 | 2016-10-16 10:20:41 -0400 | [diff] [blame] | 264 | test_identity_xform(r, gammas, true); |
Mike Klein | 4437517 | 2017-04-17 19:32:05 -0400 | [diff] [blame] | 265 | test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas, tolerance); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | DEF_TEST(ColorSpaceXform_A2BCLUT, r) { |
| 269 | constexpr int inputChannels = 3; |
| 270 | constexpr int gp = 4; // # grid points |
| 271 | |
| 272 | constexpr int numEntries = gp*gp*gp*3; |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 273 | const uint8_t gridPoints[3] = {gp, gp, gp}; |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 274 | void* memory = sk_malloc_throw(sizeof(SkColorLookUpTable) + sizeof(float) * numEntries); |
| 275 | sk_sp<SkColorLookUpTable> colorLUT(new (memory) SkColorLookUpTable(inputChannels, gridPoints)); |
| 276 | // make a CLUT that rotates R, G, and B ie R->G, G->B, B->R |
| 277 | float* table = SkTAddOffset<float>(memory, sizeof(SkColorLookUpTable)); |
| 278 | for (int r = 0; r < gp; ++r) { |
| 279 | for (int g = 0; g < gp; ++g) { |
| 280 | for (int b = 0; b < gp; ++b) { |
| 281 | table[3*(gp*gp*r + gp*g + b) + 0] = g * (1.f / (gp - 1.f)); |
| 282 | table[3*(gp*gp*r + gp*g + b) + 1] = b * (1.f / (gp - 1.f)); |
| 283 | table[3*(gp*gp*r + gp*g + b) + 2] = r * (1.f / (gp - 1.f)); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // build an even distribution of pixels every (7 / 255) steps |
| 289 | // to test the xform on |
| 290 | constexpr int pixelgp = 7; |
| 291 | constexpr int numPixels = pixelgp*pixelgp*pixelgp; |
| 292 | SkAutoTMalloc<uint32_t> srcPixels(numPixels); |
| 293 | int srcIndex = 0; |
| 294 | for (int r = 0; r < pixelgp; ++r) { |
| 295 | for (int g = 0; g < pixelgp; ++g) { |
| 296 | for (int b = 0; b < pixelgp; ++b) { |
| 297 | const int red = (int) (r * (255.f / (pixelgp - 1.f))); |
| 298 | const int green = (int) (g * (255.f / (pixelgp - 1.f))); |
| 299 | const int blue = (int) (b * (255.f / (pixelgp - 1.f))); |
| 300 | srcPixels[srcIndex] = SkColorSetRGB(red, green, blue); |
| 301 | ++srcIndex; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | SkAutoTMalloc<uint32_t> dstPixels(numPixels); |
| 306 | |
| 307 | // src space is identity besides CLUT |
| 308 | std::vector<SkColorSpace_A2B::Element> srcElements; |
| 309 | srcElements.push_back(SkColorSpace_A2B::Element(std::move(colorLUT))); |
| 310 | auto srcSpace = ColorSpaceXformTest::CreateA2BSpace(SkColorSpace_A2B::PCS::kXYZ, |
| 311 | std::move(srcElements)); |
| 312 | // dst space is entirely identity |
| 313 | auto dstSpace = SkColorSpace::MakeRGB(SkColorSpace::kLinear_RenderTargetGamma, SkMatrix44::I()); |
| 314 | auto xform = SkColorSpaceXform::New(srcSpace.get(), dstSpace.get()); |
| 315 | bool result = xform->apply(SkColorSpaceXform::kRGBA_8888_ColorFormat, dstPixels.get(), |
| 316 | SkColorSpaceXform::kRGBA_8888_ColorFormat, srcPixels.get(), |
| 317 | numPixels, kOpaque_SkAlphaType); |
| 318 | REPORTER_ASSERT(r, result); |
| 319 | |
| 320 | for (int i = 0; i < numPixels; ++i) { |
| 321 | REPORTER_ASSERT(r, almost_equal(SkColorGetR(srcPixels[i]), |
| 322 | SkColorGetG(dstPixels[i]))); |
| 323 | REPORTER_ASSERT(r, almost_equal(SkColorGetG(srcPixels[i]), |
| 324 | SkColorGetB(dstPixels[i]))); |
| 325 | REPORTER_ASSERT(r, almost_equal(SkColorGetB(srcPixels[i]), |
| 326 | SkColorGetR(dstPixels[i]))); |
| 327 | } |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 328 | } |
raftias | c6cc28c | 2016-09-29 14:31:44 -0400 | [diff] [blame] | 329 | |
Matt Sarett | c55bc9a | 2017-01-13 13:58:57 -0500 | [diff] [blame] | 330 | DEF_TEST(SkColorSpaceXform_LoadTail, r) { |
Matt Sarett | acdb6d3 | 2017-01-13 15:42:45 -0500 | [diff] [blame] | 331 | std::unique_ptr<uint64_t[]> srcPixel(new uint64_t[1]); |
Matt Sarett | c55bc9a | 2017-01-13 13:58:57 -0500 | [diff] [blame] | 332 | srcPixel[0] = 0; |
| 333 | uint32_t dstPixel; |
Brian Osman | 10b8142 | 2017-12-14 10:27:21 -0500 | [diff] [blame] | 334 | sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, |
| 335 | SkColorSpace::kDCIP3_D65_Gamut); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 336 | sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB(); |
Brian Osman | 10b8142 | 2017-12-14 10:27:21 -0500 | [diff] [blame] | 337 | std::unique_ptr<SkColorSpaceXform> xform = SkColorSpaceXform::New(p3.get(), srgb.get()); |
Matt Sarett | c55bc9a | 2017-01-13 13:58:57 -0500 | [diff] [blame] | 338 | |
| 339 | // ASAN will catch us if we read past the tail. |
| 340 | bool success = xform->apply(SkColorSpaceXform::kRGBA_8888_ColorFormat, &dstPixel, |
Matt Sarett | acdb6d3 | 2017-01-13 15:42:45 -0500 | [diff] [blame] | 341 | SkColorSpaceXform::kRGBA_U16_BE_ColorFormat, srcPixel.get(), 1, |
Matt Sarett | c55bc9a | 2017-01-13 13:58:57 -0500 | [diff] [blame] | 342 | kUnpremul_SkAlphaType); |
| 343 | REPORTER_ASSERT(r, success); |
| 344 | } |
| 345 | |