blob: 6cf0dbef21bb5815978f39b6e1f61bc3f73290ea [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
Matt Sarettf4898862016-10-16 10:20:41 -040033static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& gammas,
34 bool repeat) {
msarettdc27a642016-06-06 12:02:31 -070035 // Arbitrary set of 10 pixels
36 constexpr int width = 10;
37 constexpr uint32_t srcPixels[width] = {
38 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271,
msarettb3906762016-06-22 14:07:48 -070039 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, };
msarettdc27a642016-06-06 12:02:31 -070040 uint32_t dstPixels[width];
41
mtkleinb4359632016-07-18 11:16:14 -070042 // Create and perform an identity xform.
43 std::unique_ptr<SkColorSpaceXform> xform = ColorSpaceXformTest::CreateIdentityXform(gammas);
msarett31d097e82016-10-11 12:15:03 -070044 bool result = xform->apply(select_xform_format(kN32_SkColorType), dstPixels,
45 SkColorSpaceXform::kBGRA_8888_ColorFormat, srcPixels, width,
46 kOpaque_SkAlphaType);
47 REPORTER_ASSERT(r, result);
msarettdc27a642016-06-06 12:02:31 -070048
mtkleinb4359632016-07-18 11:16:14 -070049 // Since the src->dst matrix is the identity, and the gamma curves match,
50 // the pixels should be unchanged.
msarettdc27a642016-06-06 12:02:31 -070051 for (int i = 0; i < width; i++) {
msarettb3906762016-06-22 14:07:48 -070052 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF),
msarettcf7b8772016-09-22 12:37:04 -070053 SkGetPackedB32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070054 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070055 SkGetPackedG32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070056 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF),
msarettcf7b8772016-09-22 12:37:04 -070057 SkGetPackedR32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070058 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070059 SkGetPackedA32(dstPixels[i])));
msarettdc27a642016-06-06 12:02:31 -070060 }
Matt Sarettf4898862016-10-16 10:20:41 -040061
62 if (repeat) {
63 // We should cache part of the transform after the run. So it is interesting
64 // to make sure it still runs correctly the second time.
65 test_identity_xform(r, gammas, false);
66 }
msarettdc27a642016-06-06 12:02:31 -070067}
68
69DEF_TEST(ColorSpaceXform_TableGamma, r) {
70 // Lookup-table based gamma curves
msarettdc27a642016-06-06 12:02:31 -070071 constexpr size_t tableSize = 10;
msarett1b93bd12016-07-21 07:11:26 -070072 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize);
73 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
74 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kTable_Type;
75 gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize =
76 gammas->fBlueData.fTable.fSize = tableSize;
77 gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset =
78 gammas->fBlueData.fTable.fOffset = 0;
79 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
80
81 table[0] = 0.00f;
82 table[1] = 0.05f;
83 table[2] = 0.10f;
84 table[3] = 0.15f;
85 table[4] = 0.25f;
86 table[5] = 0.35f;
87 table[6] = 0.45f;
88 table[7] = 0.60f;
89 table[8] = 0.75f;
90 table[9] = 1.00f;
Matt Sarettf4898862016-10-16 10:20:41 -040091 test_identity_xform(r, gammas, true);
msarettdc27a642016-06-06 12:02:31 -070092}
93
94DEF_TEST(ColorSpaceXform_ParametricGamma, r) {
95 // Parametric gamma curves
Matt Sarettdf44fc52016-10-11 16:57:50 -040096 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkColorSpaceTransferFn));
msarett1b93bd12016-07-21 07:11:26 -070097 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
98 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kParam_Type;
99 gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset =
100 gammas->fBlueData.fParamOffset = 0;
Matt Sarettdf44fc52016-10-11 16:57:50 -0400101 SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn>
102 (memory, sizeof(SkGammas));
msarettdc27a642016-06-06 12:02:31 -0700103
msarettb3906762016-06-22 14:07:48 -0700104 // Interval, switch xforms at 0.0031308f
msarett1b93bd12016-07-21 07:11:26 -0700105 params->fD = 0.04045f;
msarettdc27a642016-06-06 12:02:31 -0700106
msarettb3906762016-06-22 14:07:48 -0700107 // First equation:
msarett1b93bd12016-07-21 07:11:26 -0700108 params->fE = 1.0f / 12.92f;
109 params->fF = 0.0f;
msarettdc27a642016-06-06 12:02:31 -0700110
msarettb3906762016-06-22 14:07:48 -0700111 // Second equation:
112 // Note that the function is continuous (it's actually sRGB).
msarett1b93bd12016-07-21 07:11:26 -0700113 params->fA = 1.0f / 1.055f;
114 params->fB = 0.055f / 1.055f;
115 params->fC = 0.0f;
116 params->fG = 2.4f;
Matt Sarettf4898862016-10-16 10:20:41 -0400117 test_identity_xform(r, gammas, true);
msarettdc27a642016-06-06 12:02:31 -0700118}
119
120DEF_TEST(ColorSpaceXform_ExponentialGamma, r) {
121 // Exponential gamma curves
msarett1b93bd12016-07-21 07:11:26 -0700122 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
123 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kValue_Type;
124 gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fValue = 1.4f;
Matt Sarettf4898862016-10-16 10:20:41 -0400125 test_identity_xform(r, gammas, true);
msarett1b93bd12016-07-21 07:11:26 -0700126}
127
msarett55bcc8e2016-09-09 07:48:05 -0700128DEF_TEST(ColorSpaceXform_NamedGamma, r) {
129 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
130 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kNamed_Type;
131 gammas->fRedData.fNamed = kSRGB_SkGammaNamed;
132 gammas->fGreenData.fNamed = k2Dot2Curve_SkGammaNamed;
133 gammas->fBlueData.fNamed = kLinear_SkGammaNamed;
Matt Sarettf4898862016-10-16 10:20:41 -0400134 test_identity_xform(r, gammas, true);
msarett55bcc8e2016-09-09 07:48:05 -0700135}
136
msarett1b93bd12016-07-21 07:11:26 -0700137DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) {
138 constexpr size_t tableSize = 10;
139 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize +
Matt Sarettdf44fc52016-10-11 16:57:50 -0400140 sizeof(SkColorSpaceTransferFn));
msarett1b93bd12016-07-21 07:11:26 -0700141 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
142
143 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
144 table[0] = 0.00f;
145 table[1] = 0.15f;
146 table[2] = 0.20f;
147 table[3] = 0.25f;
148 table[4] = 0.35f;
149 table[5] = 0.45f;
150 table[6] = 0.55f;
151 table[7] = 0.70f;
152 table[8] = 0.85f;
153 table[9] = 1.00f;
154
Matt Sarettdf44fc52016-10-11 16:57:50 -0400155 SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn>(memory,
156 sizeof(SkGammas) + sizeof(float) * tableSize);
msarett1b93bd12016-07-21 07:11:26 -0700157 params->fA = 1.0f / 1.055f;
158 params->fB = 0.055f / 1.055f;
159 params->fC = 0.0f;
160 params->fD = 0.04045f;
161 params->fE = 1.0f / 12.92f;
162 params->fF = 0.0f;
163 params->fG = 2.4f;
164
165 gammas->fRedType = SkGammas::Type::kValue_Type;
166 gammas->fRedData.fValue = 1.2f;
167
168 gammas->fGreenType = SkGammas::Type::kTable_Type;
169 gammas->fGreenData.fTable.fSize = tableSize;
170 gammas->fGreenData.fTable.fOffset = 0;
171
172 gammas->fBlueType = SkGammas::Type::kParam_Type;
173 gammas->fBlueData.fParamOffset = sizeof(float) * tableSize;
174
Matt Sarettf4898862016-10-16 10:20:41 -0400175 test_identity_xform(r, gammas, true);
msarettdc27a642016-06-06 12:02:31 -0700176}
raftiasc6cc28c2016-09-29 14:31:44 -0400177
178DEF_TEST(ColorSpaceXform_applyCLUTMemoryAccess, r) {
179 // buffers larger than 1024 (or 256 in GOOGLE3) will force ColorSpaceXform_Base::apply()
180 // to heap-allocate a buffer that is used for CLUT application, and this test is here to
181 // ensure that it no longer causes potential invalid memory accesses when this happens
182 const size_t len = 2048;
183 SkAutoTMalloc<uint32_t> src(len);
184 SkAutoTMalloc<uint32_t> dst(len);
185 for (uint32_t i = 0; i < len; ++i) {
186 src[i] = i;
187 }
188 // this ICC profile has a CLUT in it
189 const SkString filename(GetResourcePath("icc_profiles/upperRight.icc"));
190 sk_sp<SkData> iccData = SkData::MakeFromFileName(filename.c_str());
191 REPORTER_ASSERT_MESSAGE(r, iccData, "upperRight.icc profile required for test");
192 sk_sp<SkColorSpace> srcSpace = SkColorSpace::NewICC(iccData->bytes(), iccData->size());
193 sk_sp<SkColorSpace> dstSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
194 auto xform = SkColorSpaceXform::New(srcSpace.get(), dstSpace.get());
msarett31d097e82016-10-11 12:15:03 -0700195 bool result = xform->apply(SkColorSpaceXform::kRGBA_8888_ColorFormat, dst.get(),
196 SkColorSpaceXform::kRGBA_8888_ColorFormat, src.get(), len,
197 kUnpremul_SkAlphaType);
198 REPORTER_ASSERT(r, result);
raftiasc6cc28c2016-09-29 14:31:44 -0400199}