Index: src/core/SkSRGB.h |
diff --git a/src/core/SkSRGB.h b/src/core/SkSRGB.h |
index d567a962d84a598ab24fd60994085761bfb6f5a9..98a9b6c930f1a76696c272ce266f17622a932cc7 100644 |
--- a/src/core/SkSRGB.h |
+++ b/src/core/SkSRGB.h |
@@ -14,37 +14,32 @@ |
* |
* 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. |
+ // |
+ // TODO: It was easy enough to find these constants that we should probably further tune |
msarett
2016/07/20 13:37:18
I've got something like this... Minimizes the abs
|
+ // to find one that round trips all bytes correctly while minimizing some overall error. |
+ |
auto rsqrt = x.rsqrt(), |
sqrt = rsqrt.invert(), |
ftrt = rsqrt.rsqrt(); |
- auto lo = (12.92f * 255.0f) * x; |
+ auto lo = (12.9232f * 255.0f) * x; |
- auto hi = (-0.101115084998961f * 255.0f) + |
- (+0.678513029959381f * 255.0f) * sqrt + |
- (+0.422602055039580f * 255.0f) * ftrt; |
+ auto hi = (-0.0958f * 255.0f) |
+ + (+0.6946f * 255.0f) * sqrt |
+ + (+0.4047f * 255.0f) * ftrt; |
- return (x < 0.00349f).thenElse(lo, hi); |
+ return SkNx_cast<int>( (x < 0.005f).thenElse(lo, hi) ); |
} |
#endif//SkSRGB_DEFINED |