blob: 23b08ce8aecc9a2a30808422d5f4bf5ab817146b [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));
mtkleinb4359632016-07-18 11:16:14 -070022 return SkColorSpaceXform::New(space, space);
msarettdc27a642016-06-06 12:02:31 -070023 }
24};
25
msarettb3906762016-06-22 14:07:48 -070026static bool almost_equal(int x, int y) {
27 return SkTAbs(x - y) <= 1;
28}
29
mtkleinb4359632016-07-18 11:16:14 -070030static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& gammas) {
msarettdc27a642016-06-06 12:02:31 -070031 // Arbitrary set of 10 pixels
32 constexpr int width = 10;
33 constexpr uint32_t srcPixels[width] = {
34 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271,
msarettb3906762016-06-22 14:07:48 -070035 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, };
msarettdc27a642016-06-06 12:02:31 -070036 uint32_t dstPixels[width];
37
mtkleinb4359632016-07-18 11:16:14 -070038 // Create and perform an identity xform.
39 std::unique_ptr<SkColorSpaceXform> xform = ColorSpaceXformTest::CreateIdentityXform(gammas);
msarett9ce3a542016-07-15 13:54:38 -070040 xform->applyTo8888(dstPixels, srcPixels, width);
msarettdc27a642016-06-06 12:02:31 -070041
mtkleinb4359632016-07-18 11:16:14 -070042 // Since the src->dst matrix is the identity, and the gamma curves match,
43 // the pixels should be unchanged.
msarettdc27a642016-06-06 12:02:31 -070044 for (int i = 0; i < width; i++) {
msarettb3906762016-06-22 14:07:48 -070045 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF),
46 SkGetPackedR32(dstPixels[i])));
47 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF),
48 SkGetPackedG32(dstPixels[i])));
49 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF),
50 SkGetPackedB32(dstPixels[i])));
51 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF),
52 SkGetPackedA32(dstPixels[i])));
msarettdc27a642016-06-06 12:02:31 -070053 }
54}
55
56DEF_TEST(ColorSpaceXform_TableGamma, r) {
57 // Lookup-table based gamma curves
msarettdc27a642016-06-06 12:02:31 -070058 constexpr size_t tableSize = 10;
msarett1b93bd12016-07-21 07:11:26 -070059 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize);
60 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
61 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kTable_Type;
62 gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize =
63 gammas->fBlueData.fTable.fSize = tableSize;
64 gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset =
65 gammas->fBlueData.fTable.fOffset = 0;
66 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
67
68 table[0] = 0.00f;
69 table[1] = 0.05f;
70 table[2] = 0.10f;
71 table[3] = 0.15f;
72 table[4] = 0.25f;
73 table[5] = 0.35f;
74 table[6] = 0.45f;
75 table[7] = 0.60f;
76 table[8] = 0.75f;
77 table[9] = 1.00f;
mtkleinb4359632016-07-18 11:16:14 -070078 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -070079}
80
81DEF_TEST(ColorSpaceXform_ParametricGamma, r) {
82 // Parametric gamma curves
msarett1b93bd12016-07-21 07:11:26 -070083 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkGammas::Params));
84 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
85 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kParam_Type;
86 gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset =
87 gammas->fBlueData.fParamOffset = 0;
88 SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas));
msarettdc27a642016-06-06 12:02:31 -070089
msarettb3906762016-06-22 14:07:48 -070090 // Interval, switch xforms at 0.0031308f
msarett1b93bd12016-07-21 07:11:26 -070091 params->fD = 0.04045f;
msarettdc27a642016-06-06 12:02:31 -070092
msarettb3906762016-06-22 14:07:48 -070093 // First equation:
msarett1b93bd12016-07-21 07:11:26 -070094 params->fE = 1.0f / 12.92f;
95 params->fF = 0.0f;
msarettdc27a642016-06-06 12:02:31 -070096
msarettb3906762016-06-22 14:07:48 -070097 // Second equation:
98 // Note that the function is continuous (it's actually sRGB).
msarett1b93bd12016-07-21 07:11:26 -070099 params->fA = 1.0f / 1.055f;
100 params->fB = 0.055f / 1.055f;
101 params->fC = 0.0f;
102 params->fG = 2.4f;
mtkleinb4359632016-07-18 11:16:14 -0700103 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700104}
105
106DEF_TEST(ColorSpaceXform_ExponentialGamma, r) {
107 // Exponential gamma curves
msarett1b93bd12016-07-21 07:11:26 -0700108 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
109 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kValue_Type;
110 gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fValue = 1.4f;
111 test_identity_xform(r, gammas);
112}
113
114DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) {
115 constexpr size_t tableSize = 10;
116 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize +
117 sizeof(SkGammas::Params));
118 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
119
120 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
121 table[0] = 0.00f;
122 table[1] = 0.15f;
123 table[2] = 0.20f;
124 table[3] = 0.25f;
125 table[4] = 0.35f;
126 table[5] = 0.45f;
127 table[6] = 0.55f;
128 table[7] = 0.70f;
129 table[8] = 0.85f;
130 table[9] = 1.00f;
131
132 SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas) +
133 sizeof(float) * tableSize);
134 params->fA = 1.0f / 1.055f;
135 params->fB = 0.055f / 1.055f;
136 params->fC = 0.0f;
137 params->fD = 0.04045f;
138 params->fE = 1.0f / 12.92f;
139 params->fF = 0.0f;
140 params->fG = 2.4f;
141
142 gammas->fRedType = SkGammas::Type::kValue_Type;
143 gammas->fRedData.fValue = 1.2f;
144
145 gammas->fGreenType = SkGammas::Type::kTable_Type;
146 gammas->fGreenData.fTable.fSize = tableSize;
147 gammas->fGreenData.fTable.fOffset = 0;
148
149 gammas->fBlueType = SkGammas::Type::kParam_Type;
150 gammas->fBlueData.fParamOffset = sizeof(float) * tableSize;
151
mtkleinb4359632016-07-18 11:16:14 -0700152 test_identity_xform(r, gammas);
msarettdc27a642016-06-06 12:02:31 -0700153}