blob: ed990fc6c4c4c49fd05ddfac8b996d28468cc2a5 [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"
14#include "SkColorSpaceXform.h"
15#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);
msarettc0444612016-09-16 11:45:58 -070043 xform->apply(dstPixels, srcPixels, width, select_xform_format(kN32_SkColorType),
msarettcf7b8772016-09-22 12:37:04 -070044 SkColorSpaceXform::kBGRA_8888_ColorFormat, kOpaque_SkAlphaType);
msarettdc27a642016-06-06 12:02:31 -070045
mtkleinb4359632016-07-18 11:16:14 -070046 // Since the src->dst matrix is the identity, and the gamma curves match,
47 // the pixels should be unchanged.
msarettdc27a642016-06-06 12:02:31 -070048 for (int i = 0; i < width; i++) {
msarettb3906762016-06-22 14:07:48 -070049 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF),
msarettcf7b8772016-09-22 12:37:04 -070050 SkGetPackedB32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070051 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070052 SkGetPackedG32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070053 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF),
msarettcf7b8772016-09-22 12:37:04 -070054 SkGetPackedR32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070055 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070056 SkGetPackedA32(dstPixels[i])));
msarettdc27a642016-06-06 12:02:31 -070057 }
58}
59
60DEF_TEST(ColorSpaceXform_TableGamma, r) {
61 // Lookup-table based gamma curves
msarettdc27a642016-06-06 12:02:31 -070062 constexpr size_t tableSize = 10;
msarett1b93bd12016-07-21 07:11:26 -070063 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize);
64 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
65 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kTable_Type;
66 gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize =
67 gammas->fBlueData.fTable.fSize = tableSize;
68 gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset =
69 gammas->fBlueData.fTable.fOffset = 0;
70 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
71
72 table[0] = 0.00f;
73 table[1] = 0.05f;
74 table[2] = 0.10f;
75 table[3] = 0.15f;
76 table[4] = 0.25f;
77 table[5] = 0.35f;
78 table[6] = 0.45f;
79 table[7] = 0.60f;
80 table[8] = 0.75f;
81 table[9] = 1.00f;
mtkleinb4359632016-07-18 11:16:14 -070082 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -070083}
84
85DEF_TEST(ColorSpaceXform_ParametricGamma, r) {
86 // Parametric gamma curves
msarett1b93bd12016-07-21 07:11:26 -070087 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkGammas::Params));
88 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
89 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kParam_Type;
90 gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset =
91 gammas->fBlueData.fParamOffset = 0;
92 SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas));
msarettdc27a642016-06-06 12:02:31 -070093
msarettb3906762016-06-22 14:07:48 -070094 // Interval, switch xforms at 0.0031308f
msarett1b93bd12016-07-21 07:11:26 -070095 params->fD = 0.04045f;
msarettdc27a642016-06-06 12:02:31 -070096
msarettb3906762016-06-22 14:07:48 -070097 // First equation:
msarett1b93bd12016-07-21 07:11:26 -070098 params->fE = 1.0f / 12.92f;
99 params->fF = 0.0f;
msarettdc27a642016-06-06 12:02:31 -0700100
msarettb3906762016-06-22 14:07:48 -0700101 // Second equation:
102 // Note that the function is continuous (it's actually sRGB).
msarett1b93bd12016-07-21 07:11:26 -0700103 params->fA = 1.0f / 1.055f;
104 params->fB = 0.055f / 1.055f;
105 params->fC = 0.0f;
106 params->fG = 2.4f;
mtkleinb4359632016-07-18 11:16:14 -0700107 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700108}
109
110DEF_TEST(ColorSpaceXform_ExponentialGamma, r) {
111 // Exponential gamma curves
msarett1b93bd12016-07-21 07:11:26 -0700112 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
113 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kValue_Type;
114 gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fValue = 1.4f;
115 test_identity_xform(r, gammas);
116}
117
msarett55bcc8e2016-09-09 07:48:05 -0700118DEF_TEST(ColorSpaceXform_NamedGamma, r) {
119 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
120 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kNamed_Type;
121 gammas->fRedData.fNamed = kSRGB_SkGammaNamed;
122 gammas->fGreenData.fNamed = k2Dot2Curve_SkGammaNamed;
123 gammas->fBlueData.fNamed = kLinear_SkGammaNamed;
124 test_identity_xform(r, gammas);
125}
126
msarett1b93bd12016-07-21 07:11:26 -0700127DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) {
128 constexpr size_t tableSize = 10;
129 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize +
130 sizeof(SkGammas::Params));
131 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
132
133 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
134 table[0] = 0.00f;
135 table[1] = 0.15f;
136 table[2] = 0.20f;
137 table[3] = 0.25f;
138 table[4] = 0.35f;
139 table[5] = 0.45f;
140 table[6] = 0.55f;
141 table[7] = 0.70f;
142 table[8] = 0.85f;
143 table[9] = 1.00f;
144
145 SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas) +
146 sizeof(float) * tableSize);
147 params->fA = 1.0f / 1.055f;
148 params->fB = 0.055f / 1.055f;
149 params->fC = 0.0f;
150 params->fD = 0.04045f;
151 params->fE = 1.0f / 12.92f;
152 params->fF = 0.0f;
153 params->fG = 2.4f;
154
155 gammas->fRedType = SkGammas::Type::kValue_Type;
156 gammas->fRedData.fValue = 1.2f;
157
158 gammas->fGreenType = SkGammas::Type::kTable_Type;
159 gammas->fGreenData.fTable.fSize = tableSize;
160 gammas->fGreenData.fTable.fOffset = 0;
161
162 gammas->fBlueType = SkGammas::Type::kParam_Type;
163 gammas->fBlueData.fParamOffset = sizeof(float) * tableSize;
164
mtkleinb4359632016-07-18 11:16:14 -0700165 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700166}
raftiasc6cc28c2016-09-29 14:31:44 -0400167
168DEF_TEST(ColorSpaceXform_applyCLUTMemoryAccess, r) {
169 // buffers larger than 1024 (or 256 in GOOGLE3) will force ColorSpaceXform_Base::apply()
170 // to heap-allocate a buffer that is used for CLUT application, and this test is here to
171 // ensure that it no longer causes potential invalid memory accesses when this happens
172 const size_t len = 2048;
173 SkAutoTMalloc<uint32_t> src(len);
174 SkAutoTMalloc<uint32_t> dst(len);
175 for (uint32_t i = 0; i < len; ++i) {
176 src[i] = i;
177 }
178 // this ICC profile has a CLUT in it
179 const SkString filename(GetResourcePath("icc_profiles/upperRight.icc"));
180 sk_sp<SkData> iccData = SkData::MakeFromFileName(filename.c_str());
181 REPORTER_ASSERT_MESSAGE(r, iccData, "upperRight.icc profile required for test");
182 sk_sp<SkColorSpace> srcSpace = SkColorSpace::NewICC(iccData->bytes(), iccData->size());
183 sk_sp<SkColorSpace> dstSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
184 auto xform = SkColorSpaceXform::New(srcSpace.get(), dstSpace.get());
185 xform->apply(dst.get(), src.get(), len, SkColorSpaceXform::kRGBA_8888_ColorFormat,
186 SkColorSpaceXform::kRGBA_8888_ColorFormat, kUnpremul_SkAlphaType);
187}