blob: f792c6a72c643b1704395091ceab283da239038c [file] [log] [blame]
msarettdc27a642016-06-06 12:02:31 -07001/*
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"
msarettc0444612016-09-16 11:45:58 -070010#include "SkCodecPriv.h"
msarettdc27a642016-06-06 12:02:31 -070011#include "SkColorPriv.h"
12#include "SkColorSpace.h"
13#include "SkColorSpace_Base.h"
msarett31d097e82016-10-11 12:15:03 -070014#include "SkColorSpaceXform_Base.h"
msarettdc27a642016-06-06 12:02:31 -070015#include "Test.h"
16
17class ColorSpaceXformTest {
18public:
mtkleinb4359632016-07-18 11:16:14 -070019 static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform(const sk_sp<SkGammas>& gammas) {
20 // Logically we can pass any matrix here. For simplicty, pass I(), i.e. D50 XYZ gamut.
msarett1b93bd12016-07-21 07:11:26 -070021 sk_sp<SkColorSpace> space(new SkColorSpace_Base(
msarett600c7372016-09-07 12:03:53 -070022 nullptr, kNonStandard_SkGammaNamed, gammas, SkMatrix::I(), nullptr));
msarett9dc6cf62016-08-23 17:53:06 -070023
24 // Use special testing entry point, so we don't skip the xform, even though src == dst.
msarett4be0e7c2016-09-22 07:02:24 -070025 return SlowIdentityXform(space.get());
msarettdc27a642016-06-06 12:02:31 -070026 }
27};
28
msarettb3906762016-06-22 14:07:48 -070029static bool almost_equal(int x, int y) {
30 return SkTAbs(x - y) <= 1;
31}
32
mtkleinb4359632016-07-18 11:16:14 -070033static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& gammas) {
msarettdc27a642016-06-06 12:02:31 -070034 // Arbitrary set of 10 pixels
35 constexpr int width = 10;
36 constexpr uint32_t srcPixels[width] = {
37 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271,
msarettb3906762016-06-22 14:07:48 -070038 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, };
msarettdc27a642016-06-06 12:02:31 -070039 uint32_t dstPixels[width];
40
mtkleinb4359632016-07-18 11:16:14 -070041 // Create and perform an identity xform.
42 std::unique_ptr<SkColorSpaceXform> xform = ColorSpaceXformTest::CreateIdentityXform(gammas);
msarett31d097e82016-10-11 12:15:03 -070043 bool result = xform->apply(select_xform_format(kN32_SkColorType), dstPixels,
44 SkColorSpaceXform::kBGRA_8888_ColorFormat, srcPixels, width,
45 kOpaque_SkAlphaType);
46 REPORTER_ASSERT(r, result);
msarettdc27a642016-06-06 12:02:31 -070047
mtkleinb4359632016-07-18 11:16:14 -070048 // Since the src->dst matrix is the identity, and the gamma curves match,
49 // the pixels should be unchanged.
msarettdc27a642016-06-06 12:02:31 -070050 for (int i = 0; i < width; i++) {
msarettb3906762016-06-22 14:07:48 -070051 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF),
msarettcf7b8772016-09-22 12:37:04 -070052 SkGetPackedB32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070053 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070054 SkGetPackedG32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070055 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF),
msarettcf7b8772016-09-22 12:37:04 -070056 SkGetPackedR32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070057 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070058 SkGetPackedA32(dstPixels[i])));
msarettdc27a642016-06-06 12:02:31 -070059 }
60}
61
62DEF_TEST(ColorSpaceXform_TableGamma, r) {
63 // Lookup-table based gamma curves
msarettdc27a642016-06-06 12:02:31 -070064 constexpr size_t tableSize = 10;
msarett1b93bd12016-07-21 07:11:26 -070065 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize);
66 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
67 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kTable_Type;
68 gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize =
69 gammas->fBlueData.fTable.fSize = tableSize;
70 gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset =
71 gammas->fBlueData.fTable.fOffset = 0;
72 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
73
74 table[0] = 0.00f;
75 table[1] = 0.05f;
76 table[2] = 0.10f;
77 table[3] = 0.15f;
78 table[4] = 0.25f;
79 table[5] = 0.35f;
80 table[6] = 0.45f;
81 table[7] = 0.60f;
82 table[8] = 0.75f;
83 table[9] = 1.00f;
mtkleinb4359632016-07-18 11:16:14 -070084 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -070085}
86
87DEF_TEST(ColorSpaceXform_ParametricGamma, r) {
88 // Parametric gamma curves
msarett1b93bd12016-07-21 07:11:26 -070089 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkGammas::Params));
90 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
91 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kParam_Type;
92 gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset =
93 gammas->fBlueData.fParamOffset = 0;
94 SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas));
msarettdc27a642016-06-06 12:02:31 -070095
msarettb3906762016-06-22 14:07:48 -070096 // Interval, switch xforms at 0.0031308f
msarett1b93bd12016-07-21 07:11:26 -070097 params->fD = 0.04045f;
msarettdc27a642016-06-06 12:02:31 -070098
msarettb3906762016-06-22 14:07:48 -070099 // First equation:
msarett1b93bd12016-07-21 07:11:26 -0700100 params->fE = 1.0f / 12.92f;
101 params->fF = 0.0f;
msarettdc27a642016-06-06 12:02:31 -0700102
msarettb3906762016-06-22 14:07:48 -0700103 // Second equation:
104 // Note that the function is continuous (it's actually sRGB).
msarett1b93bd12016-07-21 07:11:26 -0700105 params->fA = 1.0f / 1.055f;
106 params->fB = 0.055f / 1.055f;
107 params->fC = 0.0f;
108 params->fG = 2.4f;
mtkleinb4359632016-07-18 11:16:14 -0700109 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700110}
111
112DEF_TEST(ColorSpaceXform_ExponentialGamma, r) {
113 // Exponential gamma curves
msarett1b93bd12016-07-21 07:11:26 -0700114 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
115 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kValue_Type;
116 gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fValue = 1.4f;
117 test_identity_xform(r, gammas);
118}
119
msarett55bcc8e2016-09-09 07:48:05 -0700120DEF_TEST(ColorSpaceXform_NamedGamma, r) {
121 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
122 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kNamed_Type;
123 gammas->fRedData.fNamed = kSRGB_SkGammaNamed;
124 gammas->fGreenData.fNamed = k2Dot2Curve_SkGammaNamed;
125 gammas->fBlueData.fNamed = kLinear_SkGammaNamed;
126 test_identity_xform(r, gammas);
127}
128
msarett1b93bd12016-07-21 07:11:26 -0700129DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) {
130 constexpr size_t tableSize = 10;
131 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize +
132 sizeof(SkGammas::Params));
133 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
134
135 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
136 table[0] = 0.00f;
137 table[1] = 0.15f;
138 table[2] = 0.20f;
139 table[3] = 0.25f;
140 table[4] = 0.35f;
141 table[5] = 0.45f;
142 table[6] = 0.55f;
143 table[7] = 0.70f;
144 table[8] = 0.85f;
145 table[9] = 1.00f;
146
147 SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas) +
148 sizeof(float) * tableSize);
149 params->fA = 1.0f / 1.055f;
150 params->fB = 0.055f / 1.055f;
151 params->fC = 0.0f;
152 params->fD = 0.04045f;
153 params->fE = 1.0f / 12.92f;
154 params->fF = 0.0f;
155 params->fG = 2.4f;
156
157 gammas->fRedType = SkGammas::Type::kValue_Type;
158 gammas->fRedData.fValue = 1.2f;
159
160 gammas->fGreenType = SkGammas::Type::kTable_Type;
161 gammas->fGreenData.fTable.fSize = tableSize;
162 gammas->fGreenData.fTable.fOffset = 0;
163
164 gammas->fBlueType = SkGammas::Type::kParam_Type;
165 gammas->fBlueData.fParamOffset = sizeof(float) * tableSize;
166
mtkleinb4359632016-07-18 11:16:14 -0700167 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700168}
raftiasc6cc28c2016-09-29 14:31:44 -0400169
170DEF_TEST(ColorSpaceXform_applyCLUTMemoryAccess, r) {
171 // buffers larger than 1024 (or 256 in GOOGLE3) will force ColorSpaceXform_Base::apply()
172 // to heap-allocate a buffer that is used for CLUT application, and this test is here to
173 // ensure that it no longer causes potential invalid memory accesses when this happens
174 const size_t len = 2048;
175 SkAutoTMalloc<uint32_t> src(len);
176 SkAutoTMalloc<uint32_t> dst(len);
177 for (uint32_t i = 0; i < len; ++i) {
178 src[i] = i;
179 }
180 // this ICC profile has a CLUT in it
181 const SkString filename(GetResourcePath("icc_profiles/upperRight.icc"));
182 sk_sp<SkData> iccData = SkData::MakeFromFileName(filename.c_str());
183 REPORTER_ASSERT_MESSAGE(r, iccData, "upperRight.icc profile required for test");
184 sk_sp<SkColorSpace> srcSpace = SkColorSpace::NewICC(iccData->bytes(), iccData->size());
185 sk_sp<SkColorSpace> dstSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
186 auto xform = SkColorSpaceXform::New(srcSpace.get(), dstSpace.get());
msarett31d097e82016-10-11 12:15:03 -0700187 bool result = xform->apply(SkColorSpaceXform::kRGBA_8888_ColorFormat, dst.get(),
188 SkColorSpaceXform::kRGBA_8888_ColorFormat, src.get(), len,
189 kUnpremul_SkAlphaType);
190 REPORTER_ASSERT(r, result);
raftiasc6cc28c2016-09-29 14:31:44 -0400191}