Index: src/core/SkColorSpaceXform.cpp |
diff --git a/src/core/SkColorSpaceXform.cpp b/src/core/SkColorSpaceXform.cpp |
index 473e715353f3337b644388b406b3e6a87902d58a..c8a470da3cee0e86cc6217d383d545129b622cd5 100644 |
--- a/src/core/SkColorSpaceXform.cpp |
+++ b/src/core/SkColorSpaceXform.cpp |
@@ -21,16 +21,16 @@ 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) { |
+ if (!srcSpace || !dstSpace || as_CSB(srcSpace)->colorLUT() || as_CSB(dstSpace)->colorLUT()) { |
scroggo
2016/06/06 14:06:44
Just making sure I understand - colorLUT is not ye
msarett
2016/06/06 17:33:44
Yes, this is a mix of unimplemented and invalid.
|
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; |
- } |
+ 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()) { |
float srcGammas[3]; |
float dstGammas[3]; |
srcGammas[0] = as_CSB(srcSpace)->gammas()->fRed.fValue; |
@@ -44,18 +44,14 @@ 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())); |
} |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
-SkGammaByValueXform::SkGammaByValueXform(float srcGammas[3], const SkMatrix44& srcToDst, |
- float dstGammas[3]) |
- : fSrcToDst(srcToDst) |
-{ |
- memcpy(fSrcGammas, srcGammas, 3 * sizeof(float)); |
- memcpy(fDstGammas, dstGammas, 3 * sizeof(float)); |
+static float byte_to_float(uint8_t v) { |
scroggo
2016/06/06 14:06:44
Should this be inline?
msarett
2016/06/06 17:33:44
Yeah I think so. "inline" is just a suggestion to
|
+ return ((float) v) * (1.0f / 255.0f); |
} |
static uint8_t clamp_float_to_byte(float v) { |
@@ -69,12 +65,22 @@ static uint8_t clamp_float_to_byte(float v) { |
} |
} |
+/////////////////////////////////////////////////////////////////////////////////////////////////// |
+ |
+SkGammaByValueXform::SkGammaByValueXform(float srcGammas[3], const SkMatrix44& srcToDst, |
+ float dstGammas[3]) |
+ : fSrcToDst(srcToDst) |
+{ |
+ memcpy(fSrcGammas, srcGammas, 3 * sizeof(float)); |
+ memcpy(fDstGammas, dstGammas, 3 * sizeof(float)); |
+} |
+ |
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 +113,130 @@ void SkGammaByValueXform::xform_RGBA_8888(uint32_t* dst, const uint32_t* src, ui |
src++; |
} |
} |
+ |
+/////////////////////////////////////////////////////////////////////////////////////////////////// |
+ |
+// Interpolating lookup in a variably sized table. |
+float interp_lut(uint8_t byte, float* table, size_t tableSize) { |
+ float index = byte * (1.0f / 255.0f) * (tableSize - 1); |
scroggo
2016/06/06 14:06:44
nit:
byte * (1.0f / 255.0f)
Won't "byte" get
msarett
2016/06/06 17:33:43
Yes it is, thanks!
|
+ float diff = index - floor(index); |
+ return table[(int) floor(index)] * (1.0f - diff) + table[(int) ceil(index)] * diff; |
scroggo
2016/06/06 14:06:44
Note: We have macros for sk_float_floor2int and sk
msarett
2016/06/06 17:33:43
Agreed, done.
|
+} |
+ |
+// 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 |
scroggo
2016/06/06 14:06:44
Have you seen any insane gamma functions? In that
msarett
2016/06/06 17:33:44
I have not seen any insane gammas. If we did enco
scroggo
2016/06/06 17:40:50
FIXME sounds good to me.
|
+// function will be increasing. |
+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++) { |
scroggo
2016/06/06 14:06:44
Would it be worth it to use something faster than
msarett
2016/06/06 17:33:44
If we verify that this function is increasing befo
scroggo
2016/06/06 17:40:50
sgtm
|
+ 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()); |
+ // 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; |
+ |
+ // FIXME (msarett): |
+ // Is this math safe? Are we at risk of dividing by zero? |
scroggo
2016/06/06 14:06:44
The risk is dividing by E or G. Those were read fr
msarett
2016/06/06 17:33:44
You're right. I added some checks and removed the
|
+ if (dstFloats[i] < interval) { |
+ // X = (Y - F) / E |
+ dstFloats[i] = (dstFloats[i] - gamma.fF) / gamma.fE; |
+ } else { |
+ // X = (Y - C)^(1 / G) - B |
+ dstFloats[i] = pow(dstFloats[i] - gamma.fC, 1.0f / gamma.fG) - gamma.fB; |
+ } |
+ } |
+ } |
+ |
+ *dst = SkPackARGB32NoCheck(((*src >> 24) & 0xFF), |
scroggo
2016/06/06 14:06:44
Why is it safe to use the NoCheck version? Is this
msarett
2016/06/06 17:33:44
This function is (currently) opaque->opaque or unp
|
+ clamp_float_to_byte(dstFloats[0]), |
+ clamp_float_to_byte(dstFloats[1]), |
+ clamp_float_to_byte(dstFloats[2])); |
+ |
+ dst++; |
+ src++; |
+ } |
+} |