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" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkColorSpace.h" |
| 12 | #include "SkColorSpace_Base.h" |
| 13 | #include "SkColorSpaceXform.h" |
| 14 | #include "Test.h" |
| 15 | |
| 16 | class ColorSpaceXformTest { |
| 17 | public: |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 18 | static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform(const sk_sp<SkGammas>& gammas) { |
| 19 | // Logically we can pass any matrix here. For simplicty, pass I(), i.e. D50 XYZ gamut. |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame^] | 20 | sk_sp<SkColorSpace> space(new SkColorSpace_Base( |
| 21 | nullptr, SkColorSpace::kNonStandard_GammaNamed, gammas, SkMatrix::I(), nullptr)); |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 22 | return SkColorSpaceXform::New(space, space); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 23 | } |
| 24 | }; |
| 25 | |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 26 | static bool almost_equal(int x, int y) { |
| 27 | return SkTAbs(x - y) <= 1; |
| 28 | } |
| 29 | |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 30 | static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& gammas) { |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 31 | // Arbitrary set of 10 pixels |
| 32 | constexpr int width = 10; |
| 33 | constexpr uint32_t srcPixels[width] = { |
| 34 | 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271, |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 35 | 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, }; |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 36 | uint32_t dstPixels[width]; |
| 37 | |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 38 | // Create and perform an identity xform. |
| 39 | std::unique_ptr<SkColorSpaceXform> xform = ColorSpaceXformTest::CreateIdentityXform(gammas); |
msarett | 9ce3a54 | 2016-07-15 13:54:38 -0700 | [diff] [blame] | 40 | xform->applyTo8888(dstPixels, srcPixels, width); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 41 | |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 42 | // Since the src->dst matrix is the identity, and the gamma curves match, |
| 43 | // the pixels should be unchanged. |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 44 | for (int i = 0; i < width; i++) { |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 45 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF), |
| 46 | SkGetPackedR32(dstPixels[i]))); |
| 47 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF), |
| 48 | SkGetPackedG32(dstPixels[i]))); |
| 49 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF), |
| 50 | SkGetPackedB32(dstPixels[i]))); |
| 51 | REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF), |
| 52 | SkGetPackedA32(dstPixels[i]))); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | DEF_TEST(ColorSpaceXform_TableGamma, r) { |
| 57 | // Lookup-table based gamma curves |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 58 | constexpr size_t tableSize = 10; |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame^] | 59 | void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize); |
| 60 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas()); |
| 61 | gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kTable_Type; |
| 62 | gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize = |
| 63 | gammas->fBlueData.fTable.fSize = tableSize; |
| 64 | gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset = |
| 65 | gammas->fBlueData.fTable.fOffset = 0; |
| 66 | float* table = SkTAddOffset<float>(memory, sizeof(SkGammas)); |
| 67 | |
| 68 | table[0] = 0.00f; |
| 69 | table[1] = 0.05f; |
| 70 | table[2] = 0.10f; |
| 71 | table[3] = 0.15f; |
| 72 | table[4] = 0.25f; |
| 73 | table[5] = 0.35f; |
| 74 | table[6] = 0.45f; |
| 75 | table[7] = 0.60f; |
| 76 | table[8] = 0.75f; |
| 77 | table[9] = 1.00f; |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 78 | test_identity_xform(r, gammas); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | DEF_TEST(ColorSpaceXform_ParametricGamma, r) { |
| 82 | // Parametric gamma curves |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame^] | 83 | void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkGammas::Params)); |
| 84 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas()); |
| 85 | gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kParam_Type; |
| 86 | gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset = |
| 87 | gammas->fBlueData.fParamOffset = 0; |
| 88 | SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas)); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 89 | |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 90 | // Interval, switch xforms at 0.0031308f |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame^] | 91 | params->fD = 0.04045f; |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 92 | |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 93 | // First equation: |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame^] | 94 | params->fE = 1.0f / 12.92f; |
| 95 | params->fF = 0.0f; |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 96 | |
msarett | b390676 | 2016-06-22 14:07:48 -0700 | [diff] [blame] | 97 | // Second equation: |
| 98 | // Note that the function is continuous (it's actually sRGB). |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame^] | 99 | params->fA = 1.0f / 1.055f; |
| 100 | params->fB = 0.055f / 1.055f; |
| 101 | params->fC = 0.0f; |
| 102 | params->fG = 2.4f; |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 103 | test_identity_xform(r, gammas); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | DEF_TEST(ColorSpaceXform_ExponentialGamma, r) { |
| 107 | // Exponential gamma curves |
msarett | 959ccc1 | 2016-07-20 15:10:02 -0700 | [diff] [blame^] | 108 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas()); |
| 109 | gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kValue_Type; |
| 110 | gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fValue = 1.4f; |
| 111 | test_identity_xform(r, gammas); |
| 112 | } |
| 113 | |
| 114 | DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) { |
| 115 | constexpr size_t tableSize = 10; |
| 116 | void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize + |
| 117 | sizeof(SkGammas::Params)); |
| 118 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas()); |
| 119 | |
| 120 | float* table = SkTAddOffset<float>(memory, sizeof(SkGammas)); |
| 121 | table[0] = 0.00f; |
| 122 | table[1] = 0.15f; |
| 123 | table[2] = 0.20f; |
| 124 | table[3] = 0.25f; |
| 125 | table[4] = 0.35f; |
| 126 | table[5] = 0.45f; |
| 127 | table[6] = 0.55f; |
| 128 | table[7] = 0.70f; |
| 129 | table[8] = 0.85f; |
| 130 | table[9] = 1.00f; |
| 131 | |
| 132 | SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas) + |
| 133 | sizeof(float) * tableSize); |
| 134 | params->fA = 1.0f / 1.055f; |
| 135 | params->fB = 0.055f / 1.055f; |
| 136 | params->fC = 0.0f; |
| 137 | params->fD = 0.04045f; |
| 138 | params->fE = 1.0f / 12.92f; |
| 139 | params->fF = 0.0f; |
| 140 | params->fG = 2.4f; |
| 141 | |
| 142 | gammas->fRedType = SkGammas::Type::kValue_Type; |
| 143 | gammas->fRedData.fValue = 1.2f; |
| 144 | |
| 145 | gammas->fGreenType = SkGammas::Type::kTable_Type; |
| 146 | gammas->fGreenData.fTable.fSize = tableSize; |
| 147 | gammas->fGreenData.fTable.fOffset = 0; |
| 148 | |
| 149 | gammas->fBlueType = SkGammas::Type::kParam_Type; |
| 150 | gammas->fBlueData.fParamOffset = sizeof(float) * tableSize; |
| 151 | |
mtklein | b435963 | 2016-07-18 11:16:14 -0700 | [diff] [blame] | 152 | test_identity_xform(r, gammas); |
msarett | dc27a64 | 2016-06-06 12:02:31 -0700 | [diff] [blame] | 153 | } |