Add SkDefaultXform as a catch-all to handle color conversions

I'd like to start optimizing the common case for color xforms,
but before doing that, I think it makes sense to have correct
code to support all xforms.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2038823002

Review-Url: https://codereview.chromium.org/2038823002
This commit is contained in:
msarett 2016-06-06 12:02:31 -07:00 committed by Commit bot
parent 53180c9866
commit dc27a648d2
5 changed files with 331 additions and 36 deletions

View File

@ -719,19 +719,21 @@ bool load_matrix(SkMatrix44* toXYZ, const uint8_t* src, size_t len) {
return false;
}
// For this matrix to behave like our "to XYZ D50" matrices, it needs to be scaled.
constexpr float scale = 65535.0 / 32768.0;
float array[16];
array[ 0] = SkFixedToFloat(read_big_endian_int(src));
array[ 1] = SkFixedToFloat(read_big_endian_int(src + 4));
array[ 2] = SkFixedToFloat(read_big_endian_int(src + 8));
array[ 3] = SkFixedToFloat(read_big_endian_int(src + 36)); // translate R
array[ 4] = SkFixedToFloat(read_big_endian_int(src + 12));
array[ 5] = SkFixedToFloat(read_big_endian_int(src + 16));
array[ 6] = SkFixedToFloat(read_big_endian_int(src + 20));
array[ 7] = SkFixedToFloat(read_big_endian_int(src + 40)); // translate G
array[ 8] = SkFixedToFloat(read_big_endian_int(src + 24));
array[ 9] = SkFixedToFloat(read_big_endian_int(src + 28));
array[10] = SkFixedToFloat(read_big_endian_int(src + 32));
array[11] = SkFixedToFloat(read_big_endian_int(src + 44)); // translate B
array[ 0] = scale * SkFixedToFloat(read_big_endian_int(src));
array[ 1] = scale * SkFixedToFloat(read_big_endian_int(src + 4));
array[ 2] = scale * SkFixedToFloat(read_big_endian_int(src + 8));
array[ 3] = scale * SkFixedToFloat(read_big_endian_int(src + 36)); // translate R
array[ 4] = scale * SkFixedToFloat(read_big_endian_int(src + 12));
array[ 5] = scale * SkFixedToFloat(read_big_endian_int(src + 16));
array[ 6] = scale * SkFixedToFloat(read_big_endian_int(src + 20));
array[ 7] = scale * SkFixedToFloat(read_big_endian_int(src + 40)); // translate G
array[ 8] = scale * SkFixedToFloat(read_big_endian_int(src + 24));
array[ 9] = scale * SkFixedToFloat(read_big_endian_int(src + 28));
array[10] = scale * SkFixedToFloat(read_big_endian_int(src + 32));
array[11] = scale * SkFixedToFloat(read_big_endian_int(src + 44)); // translate B
array[12] = 0.0f;
array[13] = 0.0f;
array[14] = 0.0f;

View File

@ -9,8 +9,8 @@
#include "SkColorSpace_Base.h"
#include "SkColorSpaceXform.h"
bool compute_gamut_xform(SkMatrix44* srcToDst, const SkMatrix44& srcToXYZ,
const SkMatrix44& dstToXYZ) {
static inline bool compute_gamut_xform(SkMatrix44* srcToDst, const SkMatrix44& srcToXYZ,
const SkMatrix44& dstToXYZ) {
if (!dstToXYZ.invert(srcToDst)) {
return false;
}
@ -22,15 +22,21 @@ bool compute_gamut_xform(SkMatrix44* srcToDst, const SkMatrix44& srcToXYZ,
std::unique_ptr<SkColorSpaceXform> SkColorSpaceXform::New(const sk_sp<SkColorSpace>& srcSpace,
const sk_sp<SkColorSpace>& dstSpace) {
if (!srcSpace || !dstSpace) {
// Invalid input
return nullptr;
}
if (as_CSB(srcSpace)->colorLUT() || as_CSB(dstSpace)->colorLUT()) {
// Unimplemented
return nullptr;
}
SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor);
if (!compute_gamut_xform(&srcToDst, srcSpace->xyz(), dstSpace->xyz())) {
return nullptr;
}
if (as_CSB(srcSpace)->gammas()->isValues() && as_CSB(dstSpace)->gammas()->isValues()) {
SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor);
if (!compute_gamut_xform(&srcToDst, srcSpace->xyz(), dstSpace->xyz())) {
return nullptr;
}
float srcGammas[3];
float dstGammas[3];
srcGammas[0] = as_CSB(srcSpace)->gammas()->fRed.fValue;
@ -44,8 +50,25 @@ std::unique_ptr<SkColorSpaceXform> SkColorSpaceXform::New(const sk_sp<SkColorSpa
new SkGammaByValueXform(srcGammas, srcToDst, dstGammas));
}
// Unimplemeted
return nullptr;
return std::unique_ptr<SkColorSpaceXform>(
new SkDefaultXform(as_CSB(srcSpace)->gammas(), srcToDst, as_CSB(dstSpace)->gammas()));
}
///////////////////////////////////////////////////////////////////////////////////////////////////
static inline float byte_to_float(uint8_t v) {
return ((float) v) * (1.0f / 255.0f);
}
static inline uint8_t clamp_float_to_byte(float v) {
v = v * 255.0f;
if (v > 255.0f) {
return 255;
} else if (v <= 0.0f) {
return 0;
} else {
return (uint8_t) (v + 0.5f);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
@ -58,23 +81,12 @@ SkGammaByValueXform::SkGammaByValueXform(float srcGammas[3], const SkMatrix44& s
memcpy(fDstGammas, dstGammas, 3 * sizeof(float));
}
static uint8_t clamp_float_to_byte(float v) {
v = v * 255.0f;
if (v > 255.0f) {
return 255;
} else if (v <= 0.0f) {
return 0;
} else {
return (uint8_t) (v + 0.5f);
}
}
void SkGammaByValueXform::xform_RGBA_8888(uint32_t* dst, const uint32_t* src, uint32_t len) const {
while (len-- > 0) {
float srcFloats[3];
srcFloats[0] = ((*src >> 0) & 0xFF) * (1.0f / 255.0f);
srcFloats[1] = ((*src >> 8) & 0xFF) * (1.0f / 255.0f);
srcFloats[2] = ((*src >> 16) & 0xFF) * (1.0f / 255.0f);
srcFloats[0] = byte_to_float((*src >> 0) & 0xFF);
srcFloats[1] = byte_to_float((*src >> 8) & 0xFF);
srcFloats[2] = byte_to_float((*src >> 16) & 0xFF);
// Convert to linear.
srcFloats[0] = pow(srcFloats[0], fSrcGammas[0]);
@ -107,3 +119,153 @@ void SkGammaByValueXform::xform_RGBA_8888(uint32_t* dst, const uint32_t* src, ui
src++;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Interpolating lookup in a variably sized table.
static inline float interp_lut(uint8_t byte, float* table, size_t tableSize) {
float index = byte_to_float(byte) * (tableSize - 1);
float diff = index - sk_float_floor2int(index);
return table[(int) sk_float_floor2int(index)] * (1.0f - diff) +
table[(int) sk_float_ceil2int(index)] * diff;
}
// Inverse table lookup. Ex: what index corresponds to the input value? This will
// have strange results when the table is non-increasing. But any sane gamma
// function will be increasing.
// FIXME (msarett):
// This is a placeholder implementation for inverting table gammas. First, I need to
// verify if there are actually destination profiles that require this functionality.
// Next, there are certainly faster and more robust approaches to solving this problem.
// The LUT based approach in QCMS would be a good place to start.
static inline float interp_lut_inv(float input, float* table, size_t tableSize) {
if (input <= table[0]) {
return table[0];
} else if (input >= table[tableSize - 1]) {
return 1.0f;
}
for (uint32_t i = 1; i < tableSize; i++) {
if (table[i] >= input) {
// We are guaranteed that input is greater than table[i - 1].
float diff = input - table[i - 1];
float distance = table[i] - table[i - 1];
float index = (i - 1) + diff / distance;
return index / (tableSize - 1);
}
}
// Should be unreachable, since we'll return before the loop if input is
// larger than the last entry.
SkASSERT(false);
return 0.0f;
}
SkDefaultXform::SkDefaultXform(const sk_sp<SkGammas>& srcGammas, const SkMatrix44& srcToDst,
const sk_sp<SkGammas>& dstGammas)
: fSrcGammas(srcGammas)
, fSrcToDst(srcToDst)
, fDstGammas(dstGammas)
{}
void SkDefaultXform::xform_RGBA_8888(uint32_t* dst, const uint32_t* src, uint32_t len) const {
while (len-- > 0) {
// Convert to linear.
// FIXME (msarett):
// Rather than support three different strategies of transforming gamma, QCMS
// builds a 256 entry float lookup table from the gamma info. This handles
// the gamma transform and the conversion from bytes to floats. This may
// be simpler and faster than our current approach.
float srcFloats[3];
for (int i = 0; i < 3; i++) {
const SkGammaCurve& gamma = (*fSrcGammas)[i];
uint8_t byte = (*src >> (8 * i)) & 0xFF;
if (gamma.isValue()) {
srcFloats[i] = pow(byte_to_float(byte), gamma.fValue);
} else if (gamma.isTable()) {
srcFloats[i] = interp_lut(byte, gamma.fTable.get(), gamma.fTableSize);
} else {
SkASSERT(gamma.isParametric());
float component = byte_to_float(byte);
if (component < gamma.fD) {
// Y = E * X + F
srcFloats[i] = gamma.fE * component + gamma.fF;
} else {
// Y = (A * X + B)^G + C
srcFloats[i] = pow(gamma.fA * component + gamma.fB, gamma.fG) + gamma.fC;
}
}
}
// Convert to dst gamut.
float dstFloats[3];
dstFloats[0] = srcFloats[0] * fSrcToDst.getFloat(0, 0) +
srcFloats[1] * fSrcToDst.getFloat(1, 0) +
srcFloats[2] * fSrcToDst.getFloat(2, 0) + fSrcToDst.getFloat(3, 0);
dstFloats[1] = srcFloats[0] * fSrcToDst.getFloat(0, 1) +
srcFloats[1] * fSrcToDst.getFloat(1, 1) +
srcFloats[2] * fSrcToDst.getFloat(2, 1) + fSrcToDst.getFloat(3, 1);
dstFloats[2] = srcFloats[0] * fSrcToDst.getFloat(0, 2) +
srcFloats[1] * fSrcToDst.getFloat(1, 2) +
srcFloats[2] * fSrcToDst.getFloat(2, 2) + fSrcToDst.getFloat(3, 2);
// Convert to dst gamma.
// FIXME (msarett):
// Rather than support three different strategies of transforming inverse gamma,
// QCMS builds a large float lookup table from the gamma info. Is this faster or
// better than our approach?
for (int i = 0; i < 3; i++) {
const SkGammaCurve& gamma = (*fDstGammas)[i];
if (gamma.isValue()) {
dstFloats[i] = pow(dstFloats[i], 1.0f / gamma.fValue);
} else if (gamma.isTable()) {
// FIXME (msarett):
// An inverse table lookup is particularly strange and non-optimal.
dstFloats[i] = interp_lut_inv(dstFloats[i], gamma.fTable.get(), gamma.fTableSize);
} else {
SkASSERT(gamma.isParametric());
// FIXME (msarett):
// This is a placeholder implementation for inverting parametric gammas.
// First, I need to verify if there are actually destination profiles that
// require this functionality. Next, I need to explore other possibilities
// for this implementation. The LUT based approach in QCMS would be a good
// place to start.
// We need to take the inverse of a piecewise function. Assume that
// the gamma function is continuous, or this won't make much sense
// anyway.
// Plug in |fD| to the first equation to calculate the new piecewise
// interval. Then simply use the inverse of the original functions.
float interval = gamma.fE * gamma.fD + gamma.fF;
if (dstFloats[i] < interval) {
// X = (Y - F) / E
if (0.0f == gamma.fE) {
// The gamma curve for this segment is constant, so the inverse
// is undefined.
dstFloats[i] = 0.0f;
} else {
dstFloats[i] = (dstFloats[i] - gamma.fF) / gamma.fE;
}
} else {
// X = ((Y - C)^(1 / G) - B) / A
if (0.0f == gamma.fA || 0.0f == gamma.fG) {
// The gamma curve for this segment is constant, so the inverse
// is undefined.
dstFloats[i] = 0.0f;
} else {
dstFloats[i] = (pow(dstFloats[i] - gamma.fC, 1.0f / gamma.fG) - gamma.fB)
/ gamma.fA;
}
}
}
}
*dst = SkPackARGB32NoCheck(((*src >> 24) & 0xFF),
clamp_float_to_byte(dstFloats[0]),
clamp_float_to_byte(dstFloats[1]),
clamp_float_to_byte(dstFloats[2]));
dst++;
src++;
}
}

View File

@ -9,8 +9,9 @@
#define SkColorSpaceXform_DEFINED
#include "SkColorSpace.h"
#include "SkColorSpace_Base.h"
class SkColorSpaceXform {
class SkColorSpaceXform : SkNoncopyable {
public:
/**
@ -48,4 +49,24 @@ private:
friend class SkColorSpaceXform;
};
/**
* Works for any valid src and dst profiles.
*/
class SkDefaultXform : public SkColorSpaceXform {
public:
void xform_RGBA_8888(uint32_t* dst, const uint32_t* src, uint32_t len) const override;
private:
SkDefaultXform(const sk_sp<SkGammas>& srcGammas, const SkMatrix44& srcToDst,
const sk_sp<SkGammas>& dstGammas);
sk_sp<SkGammas> fSrcGammas;
const SkMatrix44 fSrcToDst;
sk_sp<SkGammas> fDstGammas;
friend class SkColorSpaceXform;
friend class ColorSpaceXformTest;
};
#endif

View File

@ -78,6 +78,11 @@ public:
return fRed.isValue() && fGreen.isValue() && fBlue.isValue();
}
const SkGammaCurve& operator[](int i) {
SkASSERT(0 <= i && i < 3);
return (&fRed)[i];
}
const SkGammaCurve fRed;
const SkGammaCurve fGreen;
const SkGammaCurve fBlue;
@ -117,6 +122,8 @@ public:
const sk_sp<SkGammas>& gammas() const { return fGammas; }
SkColorLookUpTable* colorLUT() const { return fColorLUT.get(); }
/**
* Writes this object as an ICC profile.
*/

View File

@ -0,0 +1,103 @@
/*
* 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"
#include "SkColorPriv.h"
#include "SkColorSpace.h"
#include "SkColorSpace_Base.h"
#include "SkColorSpaceXform.h"
#include "Test.h"
class ColorSpaceXformTest {
public:
static SkDefaultXform* CreateDefaultXform(const sk_sp<SkGammas>& srcGamma,
const SkMatrix44& srcToDst, const sk_sp<SkGammas>& dstGamma) {
return new SkDefaultXform(srcGamma, srcToDst, dstGamma);
}
};
static void test_xform(skiatest::Reporter* r, const sk_sp<SkGammas>& gammas) {
// Arbitrary set of 10 pixels
constexpr int width = 10;
constexpr uint32_t srcPixels[width] = {
0xFFABCDEF, 0xFF146829, 0xFF382759, 0xFF184968, 0xFFDE8271,
0xFF32AB52, 0xFF0383BC, 0xFF000000, 0xFFFFFFFF, 0xFFDDEEFF, };
uint32_t dstPixels[width];
// Identity matrix
SkMatrix44 srcToDst = SkMatrix44::I();
// Create and perform xform
std::unique_ptr<SkColorSpaceXform> xform(
ColorSpaceXformTest::CreateDefaultXform(gammas, srcToDst, gammas));
xform->xform_RGBA_8888(dstPixels, srcPixels, width);
// Since the matrix is the identity, and the gamma curves match, the pixels
// should be unchanged.
for (int i = 0; i < width; i++) {
// TODO (msarett):
// As the implementation changes, we may want to use a tolerance here.
REPORTER_ASSERT(r, ((srcPixels[i] >> 0) & 0xFF) == SkGetPackedR32(dstPixels[i]));
REPORTER_ASSERT(r, ((srcPixels[i] >> 8) & 0xFF) == SkGetPackedG32(dstPixels[i]));
REPORTER_ASSERT(r, ((srcPixels[i] >> 16) & 0xFF) == SkGetPackedB32(dstPixels[i]));
REPORTER_ASSERT(r, ((srcPixels[i] >> 24) & 0xFF) == SkGetPackedA32(dstPixels[i]));
}
}
DEF_TEST(ColorSpaceXform_TableGamma, r) {
// Lookup-table based gamma curves
SkGammaCurve red, green, blue;
constexpr size_t tableSize = 10;
red.fTable = std::unique_ptr<float[]>(new float[tableSize]);
green.fTable = std::unique_ptr<float[]>(new float[tableSize]);
blue.fTable = std::unique_ptr<float[]>(new float[tableSize]);
red.fTableSize = green.fTableSize = blue.fTableSize = 10;
red.fTable[0] = green.fTable[0] = blue.fTable[0] = 0.00f;
red.fTable[1] = green.fTable[1] = blue.fTable[1] = 0.05f;
red.fTable[2] = green.fTable[2] = blue.fTable[2] = 0.10f;
red.fTable[3] = green.fTable[3] = blue.fTable[3] = 0.15f;
red.fTable[4] = green.fTable[4] = blue.fTable[4] = 0.25f;
red.fTable[5] = green.fTable[5] = blue.fTable[5] = 0.35f;
red.fTable[6] = green.fTable[6] = blue.fTable[6] = 0.45f;
red.fTable[7] = green.fTable[7] = blue.fTable[7] = 0.60f;
red.fTable[8] = green.fTable[8] = blue.fTable[8] = 0.75f;
red.fTable[9] = green.fTable[9] = blue.fTable[9] = 1.00f;
sk_sp<SkGammas> gammas =
sk_make_sp<SkGammas>(std::move(red), std::move(green), std::move(blue));
test_xform(r, gammas);
}
DEF_TEST(ColorSpaceXform_ParametricGamma, r) {
// Parametric gamma curves
SkGammaCurve red, green, blue;
// Interval, switch xforms at 0.5f
red.fD = green.fD = blue.fD = 0.5f;
// First equation, Y = 0.5f * X
red.fE = green.fE = blue.fE = 0.5f;
// Second equation, Y = ((1.0f * X) + 0.0f) ^ 3.0f + 0.125f
// Note that the function is continuous:
// 0.5f * 0.5f = ((1.0f * 0.5f) + 0.0f) ^ 3.0f + 0.125f = 0.25f
red.fA = green.fA = blue.fA = 1.0f;
red.fB = green.fB = blue.fB = 0.0f;
red.fC = green.fC = blue.fC = 0.125f;
red.fG = green.fG = blue.fG = 3.0f;
sk_sp<SkGammas> gammas = sk_make_sp<SkGammas>(std::move(red), std::move(green), std::move(blue));
test_xform(r, gammas);
}
DEF_TEST(ColorSpaceXform_ExponentialGamma, r) {
// Exponential gamma curves
SkGammaCurve red, green, blue;
red.fValue = green.fValue = blue.fValue = 4.0f;
sk_sp<SkGammas> gammas =
sk_make_sp<SkGammas>(std::move(red), std::move(green), std::move(blue));
test_xform(r, gammas);
}