aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2016-07-20 12:10:11 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-07-20 12:10:11 -0700
commit566ea9b9fc6746ffad390a4029e56d985eb2aec8 (patch)
treec17bd590020df836e26f044e7d4f0f8f18c79634 /src
parent2ea944c2b710caf29d4795ac953bad14224796f7 (diff)
Tune linear->sRGB constants to round-trip all bytes.
I basically just ran a big 5-deep for-loop over the five constants here. This is the first set of coefficients I found that round trips all bytes. I suspect there are many such sets. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2162063003 CQ_INCLUDE_TRYBOTS=master.client.skia:Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-SKNX_NO_SIMD-Trybot Review-Url: https://codereview.chromium.org/2162063003
Diffstat (limited to 'src')
-rw-r--r--src/core/SkSRGB.h30
-rw-r--r--src/opts/SkColorXform_opts.h43
2 files changed, 32 insertions, 41 deletions
diff --git a/src/core/SkSRGB.h b/src/core/SkSRGB.h
index d567a962d8..d3baa74631 100644
--- a/src/core/SkSRGB.h
+++ b/src/core/SkSRGB.h
@@ -14,37 +14,33 @@
*
* Current best practices:
* - for sRGB -> linear, lookup R,G,B in sk_linear_from_srgb;
- * - for linear -> sRGB, call sk_linear_to_srgb() for R,G,B, and round;
+ * - for linear -> sRGB, call sk_linear_to_srgb() for R,G,B;
* - the alpha channel is linear in both formats, needing at most *(1/255.0f) or *255.0f.
*
- * sk_linear_to_srgb()'s output requires rounding; it does not round for you.
- *
- * Given inputs in [0,1], sk_linear_to_srgb() will not underflow 0 but may overflow 255.
- * The overflow is small enough to be handled by rounding.
- * (But if you don't trust the inputs are in [0,1], you'd better clamp both sides immediately.)
- *
* sk_linear_to_srgb() will run a little faster than usual when compiled with SSE4.1+.
*/
extern const float sk_linear_from_srgb[256];
-static inline Sk4f sk_linear_to_srgb(const Sk4f& x) {
+static inline Sk4i sk_linear_to_srgb(const Sk4f& x) {
// Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixels).
- // For 0.00000f <= x < 0.00349f, 12.92 * x
- // For 0.00349f <= x <= 1.00000f, 0.679*(x.^0.5) + 0.423*x.^(0.25) - 0.101
- // Note that 0.00349 was selected because it is a point where both functions produce the
- // same pixel value when rounded.
+ //
+ // Tuned by brute force to minimize the number of bytes that fail to round trip,
+ // here 0 (of 256), and then to minimize the number of points halfway between bytes
+ // (in linear space) that fail to hit the right byte, here 131 (of 255), and to
+ // minimize the number of monotonicity regressions over the range [0,1], here 0.
+
auto rsqrt = x.rsqrt(),
sqrt = rsqrt.invert(),
ftrt = rsqrt.rsqrt();
- auto lo = (12.92f * 255.0f) * x;
+ auto lo = (13.0471f * 255.0f) * x;
- auto hi = (-0.101115084998961f * 255.0f) +
- (+0.678513029959381f * 255.0f) * sqrt +
- (+0.422602055039580f * 255.0f) * ftrt;
+ auto hi = (-0.0974983f * 255.0f)
+ + (+0.687999f * 255.0f) * sqrt
+ + (+0.412999f * 255.0f) * ftrt;
- return (x < 0.00349f).thenElse(lo, hi);
+ return SkNx_cast<int>( (x < 0.0048f).thenElse(lo, hi) );
}
#endif//SkSRGB_DEFINED
diff --git a/src/opts/SkColorXform_opts.h b/src/opts/SkColorXform_opts.h
index 3bb11f5599..af683e105f 100644
--- a/src/opts/SkColorXform_opts.h
+++ b/src/opts/SkColorXform_opts.h
@@ -16,20 +16,20 @@
namespace SK_OPTS_NS {
-static Sk4f linear_to_2dot2(const Sk4f& x) {
+static Sk4f clamp_0_1(const Sk4f& x) {
+ // The order of the arguments is important here. We want to make sure that NaN
+ // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN.
+ return Sk4f::Min(Sk4f::Max(x, 0.0f), 1.0f);
+}
+
+static Sk4i linear_to_2dot2(const Sk4f& x) {
// x^(29/64) is a very good approximation of the true value, x^(1/2.2).
auto x2 = x.rsqrt(), // x^(-1/2)
x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32)
x64 = x32.rsqrt(); // x^(+1/64)
// 29 = 32 - 2 - 1
- return 255.0f * x2.invert() * x32 * x64.invert();
-}
-
-static Sk4f clamp_0_to_255(const Sk4f& x) {
- // The order of the arguments is important here. We want to make sure that NaN
- // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN.
- return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f);
+ return Sk4f_round(255.0f * x2.invert() * x32 * x64.invert());
}
enum DstGamma {
@@ -79,21 +79,18 @@ static void color_xform_RGB1(void* dst, const uint32_t* src, int len,
auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables] {
if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) {
- Sk4f (*linear_to_curve)(const Sk4f&) =
+ Sk4i (*linear_to_curve)(const Sk4f&) =
(kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_to_2dot2;
- dstReds = linear_to_curve(dstReds);
- dstGreens = linear_to_curve(dstGreens);
- dstBlues = linear_to_curve(dstBlues);
+ auto reds = linear_to_curve(clamp_0_1(dstReds));
+ auto greens = linear_to_curve(clamp_0_1(dstGreens));
+ auto blues = linear_to_curve(clamp_0_1(dstBlues));
- dstReds = clamp_0_to_255(dstReds);
- dstGreens = clamp_0_to_255(dstGreens);
- dstBlues = clamp_0_to_255(dstBlues);
- auto rgba = (Sk4f_round(dstReds) << SK_R32_SHIFT)
- | (Sk4f_round(dstGreens) << SK_G32_SHIFT)
- | (Sk4f_round(dstBlues) << SK_B32_SHIFT)
- | (Sk4i{ 0xFF << SK_A32_SHIFT});
+ auto rgba = (reds << SK_R32_SHIFT)
+ | (greens << SK_G32_SHIFT)
+ | (blues << SK_B32_SHIFT)
+ | (Sk4i{0xFF} << SK_A32_SHIFT);
rgba.store((uint32_t*) dst);
dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t));
@@ -155,15 +152,13 @@ static void color_xform_RGB1(void* dst, const uint32_t* src, int len,
auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b;
if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) {
- Sk4f (*linear_to_curve)(const Sk4f&) =
+ Sk4i (*linear_to_curve)(const Sk4f&) =
(kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_to_2dot2;
- dstPixel = linear_to_curve(dstPixel);
-
- dstPixel = clamp_0_to_255(dstPixel);
+ auto pixel = linear_to_curve(clamp_0_1(dstPixel));
uint32_t rgba;
- SkNx_cast<uint8_t>(Sk4f_round(dstPixel)).store(&rgba);
+ SkNx_cast<uint8_t>(pixel).store(&rgba);
rgba |= 0xFF000000;
*((uint32_t*) dst) = SkSwizzle_RGBA_to_PMColor(rgba);
dst = SkTAddOffset<void>(dst, sizeof(uint32_t));