blob: 0885d588527fc8954d290e16fc29b0781ba4798f [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
Matt Sarettdf44fc52016-10-11 16:57:50 -040089 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkColorSpaceTransferFn));
msarett1b93bd12016-07-21 07:11:26 -070090 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;
Matt Sarettdf44fc52016-10-11 16:57:50 -040094 SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn>
95 (memory, sizeof(SkGammas));
msarettdc27a642016-06-06 12:02:31 -070096
msarettb3906762016-06-22 14:07:48 -070097 // Interval, switch xforms at 0.0031308f
msarett1b93bd12016-07-21 07:11:26 -070098 params->fD = 0.04045f;
msarettdc27a642016-06-06 12:02:31 -070099
msarettb3906762016-06-22 14:07:48 -0700100 // First equation:
msarett1b93bd12016-07-21 07:11:26 -0700101 params->fE = 1.0f / 12.92f;
102 params->fF = 0.0f;
msarettdc27a642016-06-06 12:02:31 -0700103
msarettb3906762016-06-22 14:07:48 -0700104 // Second equation:
105 // Note that the function is continuous (it's actually sRGB).
msarett1b93bd12016-07-21 07:11:26 -0700106 params->fA = 1.0f / 1.055f;
107 params->fB = 0.055f / 1.055f;
108 params->fC = 0.0f;
109 params->fG = 2.4f;
mtkleinb4359632016-07-18 11:16:14 -0700110 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700111}
112
113DEF_TEST(ColorSpaceXform_ExponentialGamma, r) {
114 // Exponential gamma curves
msarett1b93bd12016-07-21 07:11:26 -0700115 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
116 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kValue_Type;
117 gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fValue = 1.4f;
118 test_identity_xform(r, gammas);
119}
120
msarett55bcc8e2016-09-09 07:48:05 -0700121DEF_TEST(ColorSpaceXform_NamedGamma, r) {
122 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
123 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kNamed_Type;
124 gammas->fRedData.fNamed = kSRGB_SkGammaNamed;
125 gammas->fGreenData.fNamed = k2Dot2Curve_SkGammaNamed;
126 gammas->fBlueData.fNamed = kLinear_SkGammaNamed;
127 test_identity_xform(r, gammas);
128}
129
msarett1b93bd12016-07-21 07:11:26 -0700130DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) {
131 constexpr size_t tableSize = 10;
132 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize +
Matt Sarettdf44fc52016-10-11 16:57:50 -0400133 sizeof(SkColorSpaceTransferFn));
msarett1b93bd12016-07-21 07:11:26 -0700134 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
135
136 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
137 table[0] = 0.00f;
138 table[1] = 0.15f;
139 table[2] = 0.20f;
140 table[3] = 0.25f;
141 table[4] = 0.35f;
142 table[5] = 0.45f;
143 table[6] = 0.55f;
144 table[7] = 0.70f;
145 table[8] = 0.85f;
146 table[9] = 1.00f;
147
Matt Sarettdf44fc52016-10-11 16:57:50 -0400148 SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn>(memory,
149 sizeof(SkGammas) + sizeof(float) * tableSize);
msarett1b93bd12016-07-21 07:11:26 -0700150 params->fA = 1.0f / 1.055f;
151 params->fB = 0.055f / 1.055f;
152 params->fC = 0.0f;
153 params->fD = 0.04045f;
154 params->fE = 1.0f / 12.92f;
155 params->fF = 0.0f;
156 params->fG = 2.4f;
157
158 gammas->fRedType = SkGammas::Type::kValue_Type;
159 gammas->fRedData.fValue = 1.2f;
160
161 gammas->fGreenType = SkGammas::Type::kTable_Type;
162 gammas->fGreenData.fTable.fSize = tableSize;
163 gammas->fGreenData.fTable.fOffset = 0;
164
165 gammas->fBlueType = SkGammas::Type::kParam_Type;
166 gammas->fBlueData.fParamOffset = sizeof(float) * tableSize;
167
mtkleinb4359632016-07-18 11:16:14 -0700168 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700169}
raftiasc6cc28c2016-09-29 14:31:44 -0400170
171DEF_TEST(ColorSpaceXform_applyCLUTMemoryAccess, r) {
172 // buffers larger than 1024 (or 256 in GOOGLE3) will force ColorSpaceXform_Base::apply()
173 // to heap-allocate a buffer that is used for CLUT application, and this test is here to
174 // ensure that it no longer causes potential invalid memory accesses when this happens
175 const size_t len = 2048;
176 SkAutoTMalloc<uint32_t> src(len);
177 SkAutoTMalloc<uint32_t> dst(len);
178 for (uint32_t i = 0; i < len; ++i) {
179 src[i] = i;
180 }
181 // this ICC profile has a CLUT in it
182 const SkString filename(GetResourcePath("icc_profiles/upperRight.icc"));
183 sk_sp<SkData> iccData = SkData::MakeFromFileName(filename.c_str());
184 REPORTER_ASSERT_MESSAGE(r, iccData, "upperRight.icc profile required for test");
185 sk_sp<SkColorSpace> srcSpace = SkColorSpace::NewICC(iccData->bytes(), iccData->size());
186 sk_sp<SkColorSpace> dstSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
187 auto xform = SkColorSpaceXform::New(srcSpace.get(), dstSpace.get());
msarett31d097e82016-10-11 12:15:03 -0700188 bool result = xform->apply(SkColorSpaceXform::kRGBA_8888_ColorFormat, dst.get(),
189 SkColorSpaceXform::kRGBA_8888_ColorFormat, src.get(), len,
190 kUnpremul_SkAlphaType);
191 REPORTER_ASSERT(r, result);
raftiasc6cc28c2016-09-29 14:31:44 -0400192}