2016-06-06 19:02:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCodec.h"
|
2016-09-16 18:45:58 +00:00
|
|
|
#include "SkCodecPriv.h"
|
2016-06-06 19:02:31 +00:00
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorSpace.h"
|
|
|
|
#include "SkColorSpace_Base.h"
|
|
|
|
#include "SkColorSpaceXform.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
class ColorSpaceXformTest {
|
|
|
|
public:
|
2016-07-18 18:16:14 +00:00
|
|
|
static std::unique_ptr<SkColorSpaceXform> CreateIdentityXform(const sk_sp<SkGammas>& gammas) {
|
|
|
|
// Logically we can pass any matrix here. For simplicty, pass I(), i.e. D50 XYZ gamut.
|
2016-07-21 14:11:26 +00:00
|
|
|
sk_sp<SkColorSpace> space(new SkColorSpace_Base(
|
2016-09-07 19:03:53 +00:00
|
|
|
nullptr, kNonStandard_SkGammaNamed, gammas, SkMatrix::I(), nullptr));
|
2016-08-24 00:53:06 +00:00
|
|
|
|
|
|
|
// Use special testing entry point, so we don't skip the xform, even though src == dst.
|
|
|
|
return SlowIdentityXform(space);
|
2016-06-06 19:02:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-22 21:07:48 +00:00
|
|
|
static bool almost_equal(int x, int y) {
|
|
|
|
return SkTAbs(x - y) <= 1;
|
|
|
|
}
|
|
|
|
|
2016-07-18 18:16:14 +00:00
|
|
|
static void test_identity_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& gammas) {
|
2016-06-06 19:02:31 +00:00
|
|
|
// Arbitrary set of 10 pixels
|
|
|
|
constexpr int width = 10;
|
|
|
|
constexpr uint32_t srcPixels[width] = {
|
|
|
|
0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271,
|
2016-06-22 21:07:48 +00:00
|
|
|
0xFF32AB52, 0xFF0383BC, 0xFF000102, 0xFFFFFFFF, 0xFFDDEEFF, };
|
2016-06-06 19:02:31 +00:00
|
|
|
uint32_t dstPixels[width];
|
|
|
|
|
2016-07-18 18:16:14 +00:00
|
|
|
// Create and perform an identity xform.
|
|
|
|
std::unique_ptr<SkColorSpaceXform> xform = ColorSpaceXformTest::CreateIdentityXform(gammas);
|
2016-09-16 18:45:58 +00:00
|
|
|
xform->apply(dstPixels, srcPixels, width, select_xform_format(kN32_SkColorType),
|
|
|
|
kOpaque_SkAlphaType);
|
2016-06-06 19:02:31 +00:00
|
|
|
|
2016-07-18 18:16:14 +00:00
|
|
|
// Since the src->dst matrix is the identity, and the gamma curves match,
|
|
|
|
// the pixels should be unchanged.
|
2016-06-06 19:02:31 +00:00
|
|
|
for (int i = 0; i < width; i++) {
|
2016-06-22 21:07:48 +00:00
|
|
|
REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 0) & 0xFF),
|
2016-08-24 00:53:06 +00:00
|
|
|
SkGetPackedR32(dstPixels[i])));
|
2016-06-22 21:07:48 +00:00
|
|
|
REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 8) & 0xFF),
|
2016-08-24 00:53:06 +00:00
|
|
|
SkGetPackedG32(dstPixels[i])));
|
2016-06-22 21:07:48 +00:00
|
|
|
REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 16) & 0xFF),
|
2016-08-24 00:53:06 +00:00
|
|
|
SkGetPackedB32(dstPixels[i])));
|
2016-06-22 21:07:48 +00:00
|
|
|
REPORTER_ASSERT(r, almost_equal(((srcPixels[i] >> 24) & 0xFF),
|
2016-08-24 00:53:06 +00:00
|
|
|
SkGetPackedA32(dstPixels[i])));
|
2016-06-06 19:02:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ColorSpaceXform_TableGamma, r) {
|
|
|
|
// Lookup-table based gamma curves
|
|
|
|
constexpr size_t tableSize = 10;
|
2016-07-21 14:11:26 +00:00
|
|
|
void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize);
|
|
|
|
sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
|
|
|
|
gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kTable_Type;
|
|
|
|
gammas->fRedData.fTable.fSize = gammas->fGreenData.fTable.fSize =
|
|
|
|
gammas->fBlueData.fTable.fSize = tableSize;
|
|
|
|
gammas->fRedData.fTable.fOffset = gammas->fGreenData.fTable.fOffset =
|
|
|
|
gammas->fBlueData.fTable.fOffset = 0;
|
|
|
|
float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
|
|
|
|
|
|
|
|
table[0] = 0.00f;
|
|
|
|
table[1] = 0.05f;
|
|
|
|
table[2] = 0.10f;
|
|
|
|
table[3] = 0.15f;
|
|
|
|
table[4] = 0.25f;
|
|
|
|
table[5] = 0.35f;
|
|
|
|
table[6] = 0.45f;
|
|
|
|
table[7] = 0.60f;
|
|
|
|
table[8] = 0.75f;
|
|
|
|
table[9] = 1.00f;
|
2016-07-18 18:16:14 +00:00
|
|
|
test_identity_xform(r, gammas);
|
2016-06-06 19:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ColorSpaceXform_ParametricGamma, r) {
|
|
|
|
// Parametric gamma curves
|
2016-07-21 14:11:26 +00:00
|
|
|
void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkGammas::Params));
|
|
|
|
sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
|
|
|
|
gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kParam_Type;
|
|
|
|
gammas->fRedData.fParamOffset = gammas->fGreenData.fParamOffset =
|
|
|
|
gammas->fBlueData.fParamOffset = 0;
|
|
|
|
SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas));
|
2016-06-06 19:02:31 +00:00
|
|
|
|
2016-06-22 21:07:48 +00:00
|
|
|
// Interval, switch xforms at 0.0031308f
|
2016-07-21 14:11:26 +00:00
|
|
|
params->fD = 0.04045f;
|
2016-06-06 19:02:31 +00:00
|
|
|
|
2016-06-22 21:07:48 +00:00
|
|
|
// First equation:
|
2016-07-21 14:11:26 +00:00
|
|
|
params->fE = 1.0f / 12.92f;
|
|
|
|
params->fF = 0.0f;
|
2016-06-06 19:02:31 +00:00
|
|
|
|
2016-06-22 21:07:48 +00:00
|
|
|
// Second equation:
|
|
|
|
// Note that the function is continuous (it's actually sRGB).
|
2016-07-21 14:11:26 +00:00
|
|
|
params->fA = 1.0f / 1.055f;
|
|
|
|
params->fB = 0.055f / 1.055f;
|
|
|
|
params->fC = 0.0f;
|
|
|
|
params->fG = 2.4f;
|
2016-07-18 18:16:14 +00:00
|
|
|
test_identity_xform(r, gammas);
|
2016-06-06 19:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ColorSpaceXform_ExponentialGamma, r) {
|
|
|
|
// Exponential gamma curves
|
2016-07-21 14:11:26 +00:00
|
|
|
sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
|
|
|
|
gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kValue_Type;
|
|
|
|
gammas->fRedData.fValue = gammas->fGreenData.fValue = gammas->fBlueData.fValue = 1.4f;
|
|
|
|
test_identity_xform(r, gammas);
|
|
|
|
}
|
|
|
|
|
2016-09-09 14:48:05 +00:00
|
|
|
DEF_TEST(ColorSpaceXform_NamedGamma, r) {
|
|
|
|
sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new SkGammas());
|
|
|
|
gammas->fRedType = gammas->fGreenType = gammas->fBlueType = SkGammas::Type::kNamed_Type;
|
|
|
|
gammas->fRedData.fNamed = kSRGB_SkGammaNamed;
|
|
|
|
gammas->fGreenData.fNamed = k2Dot2Curve_SkGammaNamed;
|
|
|
|
gammas->fBlueData.fNamed = kLinear_SkGammaNamed;
|
|
|
|
test_identity_xform(r, gammas);
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:11:26 +00:00
|
|
|
DEF_TEST(ColorSpaceXform_NonMatchingGamma, r) {
|
|
|
|
constexpr size_t tableSize = 10;
|
|
|
|
void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize +
|
|
|
|
sizeof(SkGammas::Params));
|
|
|
|
sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
|
|
|
|
|
|
|
|
float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
|
|
|
|
table[0] = 0.00f;
|
|
|
|
table[1] = 0.15f;
|
|
|
|
table[2] = 0.20f;
|
|
|
|
table[3] = 0.25f;
|
|
|
|
table[4] = 0.35f;
|
|
|
|
table[5] = 0.45f;
|
|
|
|
table[6] = 0.55f;
|
|
|
|
table[7] = 0.70f;
|
|
|
|
table[8] = 0.85f;
|
|
|
|
table[9] = 1.00f;
|
|
|
|
|
|
|
|
SkGammas::Params* params = SkTAddOffset<SkGammas::Params>(memory, sizeof(SkGammas) +
|
|
|
|
sizeof(float) * tableSize);
|
|
|
|
params->fA = 1.0f / 1.055f;
|
|
|
|
params->fB = 0.055f / 1.055f;
|
|
|
|
params->fC = 0.0f;
|
|
|
|
params->fD = 0.04045f;
|
|
|
|
params->fE = 1.0f / 12.92f;
|
|
|
|
params->fF = 0.0f;
|
|
|
|
params->fG = 2.4f;
|
|
|
|
|
|
|
|
gammas->fRedType = SkGammas::Type::kValue_Type;
|
|
|
|
gammas->fRedData.fValue = 1.2f;
|
|
|
|
|
|
|
|
gammas->fGreenType = SkGammas::Type::kTable_Type;
|
|
|
|
gammas->fGreenData.fTable.fSize = tableSize;
|
|
|
|
gammas->fGreenData.fTable.fOffset = 0;
|
|
|
|
|
|
|
|
gammas->fBlueType = SkGammas::Type::kParam_Type;
|
|
|
|
gammas->fBlueData.fParamOffset = sizeof(float) * tableSize;
|
|
|
|
|
2016-07-18 18:16:14 +00:00
|
|
|
test_identity_xform(r, gammas);
|
2016-06-06 19:02:31 +00:00
|
|
|
}
|