OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Resources.h" | 8 #include "Resources.h" |
9 #include "SkCodec.h" | 9 #include "SkCodec.h" |
10 #include "SkCodecPriv.h" | 10 #include "SkCodecPriv.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkColorSpace.h" | 12 #include "SkColorSpace.h" |
| 13 #include "SkColorSpace_A2B.h" |
13 #include "SkColorSpace_Base.h" | 14 #include "SkColorSpace_Base.h" |
14 #include "SkColorSpace_XYZ.h" | 15 #include "SkColorSpace_XYZ.h" |
15 #include "SkColorSpaceXform_Base.h" | 16 #include "SkColorSpaceXform_Base.h" |
16 #include "Test.h" | 17 #include "Test.h" |
17 | 18 |
18 class ColorSpaceXformTest { | 19 class ColorSpaceXformTest { |
19 public: | 20 public: |
20 static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform(const sk_sp<Sk
Gammas>& gammas) { | 21 static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform(const sk_sp<Sk
Gammas>& gammas) { |
21 // Logically we can pass any matrix here. For simplicty, pass I(), i.e.
D50 XYZ gamut. | 22 // Logically we can pass any matrix here. For simplicty, pass I(), i.e.
D50 XYZ gamut. |
22 sk_sp<SkColorSpace> space(new SkColorSpace_XYZ( | 23 sk_sp<SkColorSpace> space(new SkColorSpace_XYZ( |
23 kNonStandard_SkGammaNamed, gammas, SkMatrix::I(), nullptr)); | 24 kNonStandard_SkGammaNamed, gammas, SkMatrix::I(), nullptr)); |
24 | 25 |
25 // Use special testing entry point, so we don't skip the xform, even tho
ugh src == dst. | 26 // Use special testing entry point, so we don't skip the xform, even tho
ugh src == dst. |
26 return SlowIdentityXform(static_cast<SkColorSpace_XYZ*>(space.get())); | 27 return SlowIdentityXform(static_cast<SkColorSpace_XYZ*>(space.get())); |
27 } | 28 } |
| 29 |
| 30 static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform_A2B( |
| 31 SkGammaNamed gammaNamed, const sk_sp<SkGammas>& gammas) { |
| 32 std::vector<SkColorSpace_A2B::Element> srcElements; |
| 33 // sRGB |
| 34 const float values[16] = { |
| 35 0.4358f, 0.3853f, 0.1430f, 0.0f, |
| 36 0.2224f, 0.7170f, 0.0606f, 0.0f, |
| 37 0.0139f, 0.0971f, 0.7139f, 0.0f, |
| 38 0.0000f, 0.0000f, 0.0000f, 1.0f |
| 39 }; |
| 40 SkMatrix44 arbitraryMatrix{SkMatrix44::kUninitialized_Constructor}; |
| 41 arbitraryMatrix.setRowMajorf(values); |
| 42 if (kNonStandard_SkGammaNamed == gammaNamed) { |
| 43 srcElements.push_back(SkColorSpace_A2B::Element(gammas)); |
| 44 } else { |
| 45 srcElements.push_back(SkColorSpace_A2B::Element(gammaNamed)); |
| 46 } |
| 47 srcElements.push_back(SkColorSpace_A2B::Element(arbitraryMatrix)); |
| 48 auto srcSpace = ColorSpaceXformTest::CreateA2BSpace(SkColorSpace_A2B::PC
S::kXYZ, |
| 49 std::move(srcElement
s)); |
| 50 sk_sp<SkColorSpace> dstSpace(new SkColorSpace_XYZ(gammaNamed, gammas, ar
bitraryMatrix, |
| 51 nullptr)); |
| 52 |
| 53 return SkColorSpaceXform::New(static_cast<SkColorSpace_A2B*>(srcSpace.ge
t()), |
| 54 static_cast<SkColorSpace_XYZ*>(dstSpace.ge
t())); |
| 55 } |
| 56 |
| 57 static sk_sp<SkColorSpace> CreateA2BSpace(SkColorSpace_A2B::PCS pcs, |
| 58 std::vector<SkColorSpace_A2B::Elem
ent> elements) { |
| 59 return sk_sp<SkColorSpace>(new SkColorSpace_A2B(pcs, nullptr, std::move(
elements))); |
| 60 } |
28 }; | 61 }; |
29 | 62 |
30 static bool almost_equal(int x, int y) { | 63 static bool almost_equal(int x, int y) { |
31 return SkTAbs(x - y) <= 1; | 64 return SkTAbs(x - y) <= 1 ; |
32 } | 65 } |
33 | 66 |
34 static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& ga
mmas, | 67 static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& ga
mmas, |
35 bool repeat) { | 68 bool repeat) { |
36 // Arbitrary set of 10 pixels | 69 // Arbitrary set of 10 pixels |
37 constexpr int width = 10; | 70 constexpr int width = 10; |
38 constexpr uint32_t srcPixels[width] = { | 71 constexpr uint32_t srcPixels[width] = { |
39 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271, | 72 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271, |
40 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, }; | 73 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, }; |
41 uint32_t dstPixels[width]; | 74 uint32_t dstPixels[width]; |
(...skipping 18 matching lines...) Expand all Loading... |
60 SkGetPackedA32(dstPixels[i]))); | 93 SkGetPackedA32(dstPixels[i]))); |
61 } | 94 } |
62 | 95 |
63 if (repeat) { | 96 if (repeat) { |
64 // We should cache part of the transform after the run. So it is intere
sting | 97 // We should cache part of the transform after the run. So it is intere
sting |
65 // to make sure it still runs correctly the second time. | 98 // to make sure it still runs correctly the second time. |
66 test_identity_xform(r, gammas, false); | 99 test_identity_xform(r, gammas, false); |
67 } | 100 } |
68 } | 101 } |
69 | 102 |
| 103 static void test_identity_xform_A2B(skiatest::Reporter* r, SkGammaNamed gammaNam
ed, |
| 104 const sk_sp<SkGammas>& gammas, bool repeat)
{ |
| 105 // Arbitrary set of 10 pixels |
| 106 constexpr int width = 10; |
| 107 constexpr uint32_t srcPixels[width] = { |
| 108 0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271, |
| 109 0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, }; |
| 110 uint32_t dstPixels[width]; |
| 111 |
| 112 // Create and perform an identity xform. |
| 113 auto xform = ColorSpaceXformTest::CreateIdentityXform_A2B(gammaNamed, gammas
); |
| 114 bool result = xform->apply(select_xform_format(kN32_SkColorType), dstPixels, |
| 115 SkColorSpaceXform::kBGRA_8888_ColorFormat, srcPix
els, width, |
| 116 kOpaque_SkAlphaType); |
| 117 REPORTER_ASSERT(r, result); |
| 118 |
| 119 // Since the src->dst matrix is the identity, and the gamma curves match, |
| 120 // the pixels should be unchanged. |
| 121 for (int i = 0; i < width; i++) { |
| 122 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF), |
| 123 SkGetPackedB32(dstPixels[i]))); |
| 124 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF), |
| 125 SkGetPackedG32(dstPixels[i]))); |
| 126 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF), |
| 127 SkGetPackedR32(dstPixels[i]))); |
| 128 REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF), |
| 129 SkGetPackedA32(dstPixels[i]))); |
| 130 } |
| 131 |
| 132 if (repeat) { |
| 133 // We should cache part of the transform after the run. So it is intere
sting |
| 134 // to make sure it still runs correctly the second time. |
| 135 test_identity_xform_A2B(r, gammaNamed, gammas, false); |
| 136 } |
| 137 } |
| 138 |
70 DEF_TEST(ColorSpaceXform_TableGamma, r) { | 139 DEF_TEST(ColorSpaceXform_TableGamma, r) { |
71 // Lookup-table based gamma curves | 140 // Lookup-table based gamma curves |
72 constexpr size_t tableSize = 10; | 141 constexpr size_t tableSize = 10; |
73 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize)
; | 142 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize)
; |
74 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas()); | 143 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas()); |
75 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::
kTable_Type; | 144 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::
kTable_Type; |
76 gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize = | 145 gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize = |
77 gammas->fBlueData.fTable.fSize = tableSize; | 146 gammas->fBlueData.fTable.fSize = tableSize; |
78 gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset = | 147 gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset = |
79 gammas->fBlueData.fTable.fOffset = 0; | 148 gammas->fBlueData.fTable.fOffset = 0; |
80 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas)); | 149 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas)); |
81 | 150 |
82 table[0] = 0.00f; | 151 table[0] = 0.00f; |
83 table[1] = 0.05f; | 152 table[1] = 0.05f; |
84 table[2] = 0.10f; | 153 table[2] = 0.10f; |
85 table[3] = 0.15f; | 154 table[3] = 0.15f; |
86 table[4] = 0.25f; | 155 table[4] = 0.25f; |
87 table[5] = 0.35f; | 156 table[5] = 0.35f; |
88 table[6] = 0.45f; | 157 table[6] = 0.45f; |
89 table[7] = 0.60f; | 158 table[7] = 0.60f; |
90 table[8] = 0.75f; | 159 table[8] = 0.75f; |
91 table[9] = 1.00f; | 160 table[9] = 1.00f; |
92 test_identity_xform(r, gammas, true); | 161 test_identity_xform(r, gammas, true); |
| 162 test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas, true); |
93 } | 163 } |
94 | 164 |
95 DEF_TEST(ColorSpaceXform_ParametricGamma, r) { | 165 DEF_TEST(ColorSpaceXform_ParametricGamma, r) { |
96 // Parametric gamma curves | 166 // Parametric gamma curves |
97 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkColorSpaceTransfe
rFn)); | 167 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkColorSpaceTransfe
rFn)); |
98 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas()); | 168 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas()); |
99 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::
kParam_Type; | 169 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::
kParam_Type; |
100 gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset = | 170 gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset = |
101 gammas->fBlueData.fParamOffset = 0; | 171 gammas->fBlueData.fParamOffset = 0; |
102 SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn> | 172 SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn> |
103 (memory, sizeof(SkGammas)); | 173 (memory, sizeof(SkGammas)); |
104 | 174 |
105 // Interval, switch xforms at 0.0031308f | 175 // Interval, switch xforms at 0.0031308f |
106 params->fD = 0.04045f; | 176 params->fD = 0.04045f; |
107 | 177 |
108 // First equation: | 178 // First equation: |
109 params->fE = 1.0f / 12.92f; | 179 params->fE = 1.0f / 12.92f; |
110 params->fF = 0.0f; | 180 params->fF = 0.0f; |
111 | 181 |
112 // Second equation: | 182 // Second equation: |
113 // Note that the function is continuous (it's actually sRGB). | 183 // Note that the function is continuous (it's actually sRGB). |
114 params->fA = 1.0f / 1.055f; | 184 params->fA = 1.0f / 1.055f; |
115 params->fB = 0.055f / 1.055f; | 185 params->fB = 0.055f / 1.055f; |
116 params->fC = 0.0f; | 186 params->fC = 0.0f; |
117 params->fG = 2.4f; | 187 params->fG = 2.4f; |
118 test_identity_xform(r, gammas, true); | 188 test_identity_xform(r, gammas, true); |
| 189 test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas, true); |
119 } | 190 } |
120 | 191 |
121 DEF_TEST(ColorSpaceXform_ExponentialGamma, r) { | 192 DEF_TEST(ColorSpaceXform_ExponentialGamma, r) { |
122 // Exponential gamma curves | 193 // Exponential gamma curves |
123 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas()); | 194 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas()); |
124 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::
kValue_Type; | 195 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::
kValue_Type; |
125 gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fVal
ue = 1.4f; | 196 gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fVal
ue = 1.4f; |
126 test_identity_xform(r, gammas, true); | 197 test_identity_xform(r, gammas, true); |
| 198 test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas, true); |
127 } | 199 } |
128 | 200 |
129 DEF_TEST(ColorSpaceXform_NamedGamma, r) { | 201 DEF_TEST(ColorSpaceXform_NamedGamma, r) { |
130 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas()); | 202 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas()); |
131 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::
kNamed_Type; | 203 gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::
kNamed_Type; |
132 gammas->fRedData.fNamed = kSRGB_SkGammaNamed; | 204 gammas->fRedData.fNamed = kSRGB_SkGammaNamed; |
133 gammas->fGreenData.fNamed = k2Dot2Curve_SkGammaNamed; | 205 gammas->fGreenData.fNamed = k2Dot2Curve_SkGammaNamed; |
134 gammas->fBlueData.fNamed = kLinear_SkGammaNamed; | 206 gammas->fBlueData.fNamed = kLinear_SkGammaNamed; |
135 test_identity_xform(r, gammas, true); | 207 test_identity_xform(r, gammas, true); |
| 208 test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas, true); |
| 209 test_identity_xform_A2B(r, kSRGB_SkGammaNamed, nullptr, true); |
| 210 test_identity_xform_A2B(r, k2Dot2Curve_SkGammaNamed, nullptr, true); |
| 211 test_identity_xform_A2B(r, kLinear_SkGammaNamed, nullptr, true); |
136 } | 212 } |
137 | 213 |
138 DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) { | 214 DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) { |
139 constexpr size_t tableSize = 10; | 215 constexpr size_t tableSize = 10; |
140 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize
+ | 216 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize
+ |
141 sizeof(SkColorSpaceTransferFn)); | 217 sizeof(SkColorSpaceTransferFn)); |
142 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas()); | 218 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas()); |
143 | 219 |
144 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas)); | 220 float* table = SkTAddOffset<float>(memory, sizeof(SkGammas)); |
145 table[0] = 0.00f; | 221 table[0] = 0.00f; |
(...skipping 21 matching lines...) Expand all Loading... |
167 gammas->fRedData.fValue = 1.2f; | 243 gammas->fRedData.fValue = 1.2f; |
168 | 244 |
169 gammas->fGreenType = SkGammas::Type::kTable_Type; | 245 gammas->fGreenType = SkGammas::Type::kTable_Type; |
170 gammas->fGreenData.fTable.fSize = tableSize; | 246 gammas->fGreenData.fTable.fSize = tableSize; |
171 gammas->fGreenData.fTable.fOffset = 0; | 247 gammas->fGreenData.fTable.fOffset = 0; |
172 | 248 |
173 gammas->fBlueType = SkGammas::Type::kParam_Type; | 249 gammas->fBlueType = SkGammas::Type::kParam_Type; |
174 gammas->fBlueData.fParamOffset = sizeof(float) * tableSize; | 250 gammas->fBlueData.fParamOffset = sizeof(float) * tableSize; |
175 | 251 |
176 test_identity_xform(r, gammas, true); | 252 test_identity_xform(r, gammas, true); |
| 253 test_identity_xform_A2B(r, kNonStandard_SkGammaNamed, gammas, true); |
177 } | 254 } |
178 | 255 |
| 256 DEF_TEST(ColorSpaceXform_A2BCLUT, r) { |
| 257 constexpr int inputChannels = 3; |
| 258 constexpr int gp = 4; // # grid points |
| 259 |
| 260 constexpr int numEntries = gp*gp*gp*3; |
| 261 uint8_t gridPoints[3] = {gp, gp, gp}; |
| 262 void* memory = sk_malloc_throw(sizeof(SkColorLookUpTable) + sizeof(float) *
numEntries); |
| 263 sk_sp<SkColorLookUpTable> colorLUT(new (memory) SkColorLookUpTable(inputChan
nels, gridPoints)); |
| 264 // make a CLUT that rotates R, G, and B ie R->G, G->B, B->R |
| 265 float* table = SkTAddOffset<float>(memory, sizeof(SkColorLookUpTable)); |
| 266 for (int r = 0; r < gp; ++r) { |
| 267 for (int g = 0; g < gp; ++g) { |
| 268 for (int b = 0; b < gp; ++b) { |
| 269 table[3*(gp*gp*r + gp*g + b) + 0] = g * (1.f / (gp - 1.f)); |
| 270 table[3*(gp*gp*r + gp*g + b) + 1] = b * (1.f / (gp - 1.f)); |
| 271 table[3*(gp*gp*r + gp*g + b) + 2] = r * (1.f / (gp - 1.f)); |
| 272 } |
| 273 } |
| 274 } |
| 275 |
| 276 // build an even distribution of pixels every (7 / 255) steps |
| 277 // to test the xform on |
| 278 constexpr int pixelgp = 7; |
| 279 constexpr int numPixels = pixelgp*pixelgp*pixelgp; |
| 280 SkAutoTMalloc<uint32_t> srcPixels(numPixels); |
| 281 int srcIndex = 0; |
| 282 for (int r = 0; r < pixelgp; ++r) { |
| 283 for (int g = 0; g < pixelgp; ++g) { |
| 284 for (int b = 0; b < pixelgp; ++b) { |
| 285 const int red = (int) (r * (255.f / (pixelgp - 1.f))); |
| 286 const int green = (int) (g * (255.f / (pixelgp - 1.f))); |
| 287 const int blue = (int) (b * (255.f / (pixelgp - 1.f))); |
| 288 srcPixels[srcIndex] = SkColorSetRGB(red, green, blue); |
| 289 ++srcIndex; |
| 290 } |
| 291 } |
| 292 } |
| 293 SkAutoTMalloc<uint32_t> dstPixels(numPixels); |
| 294 |
| 295 // src space is identity besides CLUT |
| 296 std::vector<SkColorSpace_A2B::Element> srcElements; |
| 297 srcElements.push_back(SkColorSpace_A2B::Element(std::move(colorLUT))); |
| 298 auto srcSpace = ColorSpaceXformTest::CreateA2BSpace(SkColorSpace_A2B::PCS::k
XYZ, |
| 299 std::move(srcElements)); |
| 300 // dst space is entirely identity |
| 301 auto dstSpace = SkColorSpace::MakeRGB(SkColorSpace::kLinear_RenderTargetGamm
a, SkMatrix44::I()); |
| 302 auto xform = SkColorSpaceXform::New(srcSpace.get(), dstSpace.get()); |
| 303 bool result = xform->apply(SkColorSpaceXform::kRGBA_8888_ColorFormat, dstPix
els.get(), |
| 304 SkColorSpaceXform::kRGBA_8888_ColorFormat, srcPix
els.get(), |
| 305 numPixels, kOpaque_SkAlphaType); |
| 306 REPORTER_ASSERT(r, result); |
| 307 |
| 308 for (int i = 0; i < numPixels; ++i) { |
| 309 REPORTER_ASSERT(r, almost_equal(SkColorGetR(srcPixels[i]), |
| 310 SkColorGetG(dstPixels[i]))); |
| 311 REPORTER_ASSERT(r, almost_equal(SkColorGetG(srcPixels[i]), |
| 312 SkColorGetB(dstPixels[i]))); |
| 313 REPORTER_ASSERT(r, almost_equal(SkColorGetB(srcPixels[i]), |
| 314 SkColorGetR(dstPixels[i]))); |
| 315 } |
| 316 } |
| 317 |
OLD | NEW |