blob: 477e61acdda9d4956afdb6eea2537bed421e18c9 [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"
raftias94888332016-10-18 10:02:51 -070014#include "SkColorSpace_XYZ.h"
msarett31d097e82016-10-11 12:15:03 -070015#include "SkColorSpaceXform_Base.h"
msarettdc27a642016-06-06 12:02:31 -070016#include "Test.h"
17
18class ColorSpaceXformTest {
19public:
mtkleinb4359632016-07-18 11:16:14 -070020 static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform(const sk_sp<SkGammas>& gammas) {
21 // Logically we can pass any matrix here. For simplicty, pass I(), i.e. D50 XYZ gamut.
raftias94888332016-10-18 10:02:51 -070022 sk_sp<SkColorSpace> space(new SkColorSpace_XYZ(
23 kNonStandard_SkGammaNamed, gammas, SkMatrix::I(), nullptr));
msarett9dc6cf62016-08-23 17:53:06 -070024
25 // Use special testing entry point, so we don't skip the xform, even though src == dst.
raftias94888332016-10-18 10:02:51 -070026 return SlowIdentityXform(static_cast<SkColorSpace_XYZ*>(space.get()));
msarettdc27a642016-06-06 12:02:31 -070027 }
28};
29
msarettb3906762016-06-22 14:07:48 -070030static bool almost_equal(int x, int y) {
31 return SkTAbs(x - y) <= 1;
32}
33
Matt Sarettf4898862016-10-16 10:20:41 -040034static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& gammas,
35 bool repeat) {
msarettdc27a642016-06-06 12:02:31 -070036 // Arbitrary set of 10 pixels
37 constexpr int width = 10;
38 constexpr uint32_t srcPixels[width] = {
39 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271,
msarettb3906762016-06-22 14:07:48 -070040 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, };
msarettdc27a642016-06-06 12:02:31 -070041 uint32_t dstPixels[width];
42
mtkleinb4359632016-07-18 11:16:14 -070043 // Create and perform an identity xform.
44 std::unique_ptr<SkColorSpaceXform> xform = ColorSpaceXformTest::CreateIdentityXform(gammas);
msarett31d097e82016-10-11 12:15:03 -070045 bool result = xform->apply(select_xform_format(kN32_SkColorType), dstPixels,
46 SkColorSpaceXform::kBGRA_8888_ColorFormat, srcPixels, width,
47 kOpaque_SkAlphaType);
48 REPORTER_ASSERT(r, result);
msarettdc27a642016-06-06 12:02:31 -070049
mtkleinb4359632016-07-18 11:16:14 -070050 // Since the src->dst matrix is the identity, and the gamma curves match,
51 // the pixels should be unchanged.
msarettdc27a642016-06-06 12:02:31 -070052 for (int i = 0; i < width; i++) {
msarettb3906762016-06-22 14:07:48 -070053 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF),
msarettcf7b8772016-09-22 12:37:04 -070054 SkGetPackedB32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070055 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070056 SkGetPackedG32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070057 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF),
msarettcf7b8772016-09-22 12:37:04 -070058 SkGetPackedR32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070059 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070060 SkGetPackedA32(dstPixels[i])));
msarettdc27a642016-06-06 12:02:31 -070061 }
Matt Sarettf4898862016-10-16 10:20:41 -040062
63 if (repeat) {
64 // We should cache part of the transform after the run. So it is interesting
65 // to make sure it still runs correctly the second time.
66 test_identity_xform(r, gammas, false);
67 }
msarettdc27a642016-06-06 12:02:31 -070068}
69
70DEF_TEST(ColorSpaceXform_TableGamma, r) {
71 // Lookup-table based gamma curves
msarettdc27a642016-06-06 12:02:31 -070072 constexpr size_t tableSize = 10;
msarett1b93bd12016-07-21 07:11:26 -070073 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize);
74 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
75 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kTable_Type;
76 gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize =
77 gammas->fBlueData.fTable.fSize = tableSize;
78 gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset =
79 gammas->fBlueData.fTable.fOffset = 0;
80 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
81
82 table[0] = 0.00f;
83 table[1] = 0.05f;
84 table[2] = 0.10f;
85 table[3] = 0.15f;
86 table[4] = 0.25f;
87 table[5] = 0.35f;
88 table[6] = 0.45f;
89 table[7] = 0.60f;
90 table[8] = 0.75f;
91 table[9] = 1.00f;
Matt Sarettf4898862016-10-16 10:20:41 -040092 test_identity_xform(r, gammas, true);
msarettdc27a642016-06-06 12:02:31 -070093}
94
95DEF_TEST(ColorSpaceXform_ParametricGamma, r) {
96 // Parametric gamma curves
Matt Sarettdf44fc52016-10-11 16:57:50 -040097 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkColorSpaceTransferFn));
msarett1b93bd12016-07-21 07:11:26 -070098 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
99 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kParam_Type;
100 gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset =
101 gammas->fBlueData.fParamOffset = 0;
Matt Sarettdf44fc52016-10-11 16:57:50 -0400102 SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn>
103 (memory, sizeof(SkGammas));
msarettdc27a642016-06-06 12:02:31 -0700104
msarettb3906762016-06-22 14:07:48 -0700105 // Interval, switch xforms at 0.0031308f
msarett1b93bd12016-07-21 07:11:26 -0700106 params->fD = 0.04045f;
msarettdc27a642016-06-06 12:02:31 -0700107
msarettb3906762016-06-22 14:07:48 -0700108 // First equation:
msarett1b93bd12016-07-21 07:11:26 -0700109 params->fE = 1.0f / 12.92f;
110 params->fF = 0.0f;
msarettdc27a642016-06-06 12:02:31 -0700111
msarettb3906762016-06-22 14:07:48 -0700112 // Second equation:
113 // Note that the function is continuous (it's actually sRGB).
msarett1b93bd12016-07-21 07:11:26 -0700114 params->fA = 1.0f / 1.055f;
115 params->fB = 0.055f / 1.055f;
116 params->fC = 0.0f;
117 params->fG = 2.4f;
Matt Sarettf4898862016-10-16 10:20:41 -0400118 test_identity_xform(r, gammas, true);
msarettdc27a642016-06-06 12:02:31 -0700119}
120
121DEF_TEST(ColorSpaceXform_ExponentialGamma, r) {
122 // Exponential gamma curves
msarett1b93bd12016-07-21 07:11:26 -0700123 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
124 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kValue_Type;
125 gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fValue = 1.4f;
Matt Sarettf4898862016-10-16 10:20:41 -0400126 test_identity_xform(r, gammas, true);
msarett1b93bd12016-07-21 07:11:26 -0700127}
128
msarett55bcc8e2016-09-09 07:48:05 -0700129DEF_TEST(ColorSpaceXform_NamedGamma, r) {
130 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
131 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kNamed_Type;
132 gammas->fRedData.fNamed = kSRGB_SkGammaNamed;
133 gammas->fGreenData.fNamed = k2Dot2Curve_SkGammaNamed;
134 gammas->fBlueData.fNamed = kLinear_SkGammaNamed;
Matt Sarettf4898862016-10-16 10:20:41 -0400135 test_identity_xform(r, gammas, true);
msarett55bcc8e2016-09-09 07:48:05 -0700136}
137
msarett1b93bd12016-07-21 07:11:26 -0700138DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) {
139 constexpr size_t tableSize = 10;
140 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize +
Matt Sarettdf44fc52016-10-11 16:57:50 -0400141 sizeof(SkColorSpaceTransferFn));
msarett1b93bd12016-07-21 07:11:26 -0700142 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
143
144 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
145 table[0] = 0.00f;
146 table[1] = 0.15f;
147 table[2] = 0.20f;
148 table[3] = 0.25f;
149 table[4] = 0.35f;
150 table[5] = 0.45f;
151 table[6] = 0.55f;
152 table[7] = 0.70f;
153 table[8] = 0.85f;
154 table[9] = 1.00f;
155
Matt Sarettdf44fc52016-10-11 16:57:50 -0400156 SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn>(memory,
157 sizeof(SkGammas) + sizeof(float) * tableSize);
msarett1b93bd12016-07-21 07:11:26 -0700158 params->fA = 1.0f / 1.055f;
159 params->fB = 0.055f / 1.055f;
160 params->fC = 0.0f;
161 params->fD = 0.04045f;
162 params->fE = 1.0f / 12.92f;
163 params->fF = 0.0f;
164 params->fG = 2.4f;
165
166 gammas->fRedType = SkGammas::Type::kValue_Type;
167 gammas->fRedData.fValue = 1.2f;
168
169 gammas->fGreenType = SkGammas::Type::kTable_Type;
170 gammas->fGreenData.fTable.fSize = tableSize;
171 gammas->fGreenData.fTable.fOffset = 0;
172
173 gammas->fBlueType = SkGammas::Type::kParam_Type;
174 gammas->fBlueData.fParamOffset = sizeof(float) * tableSize;
175
Matt Sarettf4898862016-10-16 10:20:41 -0400176 test_identity_xform(r, gammas, true);
msarettdc27a642016-06-06 12:02:31 -0700177}
raftiasc6cc28c2016-09-29 14:31:44 -0400178