blob: 415063d46238797c925e32d6952eacda73f8e24b [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"
10#include "SkColorPriv.h"
11#include "SkColorSpace.h"
12#include "SkColorSpace_Base.h"
13#include "SkColorSpaceXform.h"
14#include "Test.h"
15
16class ColorSpaceXformTest {
17public:
mtkleinb4359632016-07-18 11:16:14 -070018 static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform(const sk_sp<SkGammas>& gammas) {
19 // Logically we can pass any matrix here. For simplicty, pass I(), i.e. D50 XYZ gamut.
msarett1b93bd12016-07-21 07:11:26 -070020 sk_sp<SkColorSpace> space(new SkColorSpace_Base(
21 nullptr, SkColorSpace::kNonStandard_GammaNamed, gammas, SkMatrix::I(), nullptr));
msarett9dc6cf62016-08-23 17:53:06 -070022
23 // Use special testing entry point, so we don't skip the xform, even though src == dst.
24 return SlowIdentityXform(space);
msarettdc27a642016-06-06 12:02:31 -070025 }
26};
27
msarettb3906762016-06-22 14:07:48 -070028static bool almost_equal(int x, int y) {
29 return SkTAbs(x - y) <= 1;
30}
31
mtkleinb4359632016-07-18 11:16:14 -070032static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& gammas) {
msarettdc27a642016-06-06 12:02:31 -070033 // Arbitrary set of 10 pixels
34 constexpr int width = 10;
35 constexpr uint32_t srcPixels[width] = {
36 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271,
msarettb3906762016-06-22 14:07:48 -070037 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, };
msarettdc27a642016-06-06 12:02:31 -070038 uint32_t dstPixels[width];
39
mtkleinb4359632016-07-18 11:16:14 -070040 // Create and perform an identity xform.
41 std::unique_ptr<SkColorSpaceXform> xform = ColorSpaceXformTest::CreateIdentityXform(gammas);
msarett9dc6cf62016-08-23 17:53:06 -070042 xform->apply(dstPixels, srcPixels, width, kN32_SkColorType, kOpaque_SkAlphaType);
msarettdc27a642016-06-06 12:02:31 -070043
mtkleinb4359632016-07-18 11:16:14 -070044 // Since the src->dst matrix is the identity, and the gamma curves match,
45 // the pixels should be unchanged.
msarettdc27a642016-06-06 12:02:31 -070046 for (int i = 0; i < width; i++) {
msarettb3906762016-06-22 14:07:48 -070047 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070048 SkGetPackedR32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070049 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070050 SkGetPackedG32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070051 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070052 SkGetPackedB32(dstPixels[i])));
msarettb3906762016-06-22 14:07:48 -070053 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF),
msarett9dc6cf62016-08-23 17:53:06 -070054 SkGetPackedA32(dstPixels[i])));
msarettdc27a642016-06-06 12:02:31 -070055 }
56}
57
58DEF_TEST(ColorSpaceXform_TableGamma, r) {
59 // Lookup-table based gamma curves
msarettdc27a642016-06-06 12:02:31 -070060 constexpr size_t tableSize = 10;
msarett1b93bd12016-07-21 07:11:26 -070061 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize);
62 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
63 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kTable_Type;
64 gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize =
65 gammas->fBlueData.fTable.fSize = tableSize;
66 gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset =
67 gammas->fBlueData.fTable.fOffset = 0;
68 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
69
70 table[0] = 0.00f;
71 table[1] = 0.05f;
72 table[2] = 0.10f;
73 table[3] = 0.15f;
74 table[4] = 0.25f;
75 table[5] = 0.35f;
76 table[6] = 0.45f;
77 table[7] = 0.60f;
78 table[8] = 0.75f;
79 table[9] = 1.00f;
mtkleinb4359632016-07-18 11:16:14 -070080 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -070081}
82
83DEF_TEST(ColorSpaceXform_ParametricGamma, r) {
84 // Parametric gamma curves
msarett1b93bd12016-07-21 07:11:26 -070085 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkGammas::Params));
86 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
87 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kParam_Type;
88 gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset =
89 gammas->fBlueData.fParamOffset = 0;
90 SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas));
msarettdc27a642016-06-06 12:02:31 -070091
msarettb3906762016-06-22 14:07:48 -070092 // Interval, switch xforms at 0.0031308f
msarett1b93bd12016-07-21 07:11:26 -070093 params->fD = 0.04045f;
msarettdc27a642016-06-06 12:02:31 -070094
msarettb3906762016-06-22 14:07:48 -070095 // First equation:
msarett1b93bd12016-07-21 07:11:26 -070096 params->fE = 1.0f / 12.92f;
97 params->fF = 0.0f;
msarettdc27a642016-06-06 12:02:31 -070098
msarettb3906762016-06-22 14:07:48 -070099 // Second equation:
100 // Note that the function is continuous (it's actually sRGB).
msarett1b93bd12016-07-21 07:11:26 -0700101 params->fA = 1.0f / 1.055f;
102 params->fB = 0.055f / 1.055f;
103 params->fC = 0.0f;
104 params->fG = 2.4f;
mtkleinb4359632016-07-18 11:16:14 -0700105 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700106}
107
108DEF_TEST(ColorSpaceXform_ExponentialGamma, r) {
109 // Exponential gamma curves
msarett1b93bd12016-07-21 07:11:26 -0700110 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
111 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kValue_Type;
112 gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fValue = 1.4f;
113 test_identity_xform(r, gammas);
114}
115
116DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) {
117 constexpr size_t tableSize = 10;
118 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize +
119 sizeof(SkGammas::Params));
120 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
121
122 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
123 table[0] = 0.00f;
124 table[1] = 0.15f;
125 table[2] = 0.20f;
126 table[3] = 0.25f;
127 table[4] = 0.35f;
128 table[5] = 0.45f;
129 table[6] = 0.55f;
130 table[7] = 0.70f;
131 table[8] = 0.85f;
132 table[9] = 1.00f;
133
134 SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas) +
135 sizeof(float) * tableSize);
136 params->fA = 1.0f / 1.055f;
137 params->fB = 0.055f / 1.055f;
138 params->fC = 0.0f;
139 params->fD = 0.04045f;
140 params->fE = 1.0f / 12.92f;
141 params->fF = 0.0f;
142 params->fG = 2.4f;
143
144 gammas->fRedType = SkGammas::Type::kValue_Type;
145 gammas->fRedData.fValue = 1.2f;
146
147 gammas->fGreenType = SkGammas::Type::kTable_Type;
148 gammas->fGreenData.fTable.fSize = tableSize;
149 gammas->fGreenData.fTable.fOffset = 0;
150
151 gammas->fBlueType = SkGammas::Type::kParam_Type;
152 gammas->fBlueData.fParamOffset = sizeof(float) * tableSize;
153
mtkleinb4359632016-07-18 11:16:14 -0700154 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700155}