Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkSRGB_DEFINED | 8 #ifndef SkSRGB_DEFINED |
| 9 #define SkSRGB_DEFINED | 9 #define SkSRGB_DEFINED |
| 10 | 10 |
| 11 #include "SkNx.h" | 11 #include "SkNx.h" |
| 12 | 12 |
| 13 /** Components for building our canonical sRGB -> linear and linear -> sRGB tran sformations. | 13 /** Components for building our canonical sRGB -> linear and linear -> sRGB tran sformations. |
| 14 * | 14 * |
| 15 * Current best practices: | 15 * Current best practices: |
| 16 * - for sRGB -> linear, lookup R,G,B in sk_linear_from_srgb; | 16 * - for sRGB -> linear, lookup R,G,B in sk_linear_from_srgb; |
| 17 * - for linear -> sRGB, call sk_linear_to_srgb() for R,G,B, and round; | 17 * - for linear -> sRGB, call sk_linear_to_srgb() for R,G,B; |
| 18 * - the alpha channel is linear in both formats, needing at most *(1/255.0 f) or *255.0f. | 18 * - the alpha channel is linear in both formats, needing at most *(1/255.0 f) or *255.0f. |
| 19 * | 19 * |
| 20 * sk_linear_to_srgb()'s output requires rounding; it does not round for you. | |
| 21 * | |
| 22 * Given inputs in [0,1], sk_linear_to_srgb() will not underflow 0 but may over flow 255. | |
| 23 * The overflow is small enough to be handled by rounding. | |
| 24 * (But if you don't trust the inputs are in [0,1], you'd better clamp both sid es immediately.) | |
| 25 * | |
| 26 * sk_linear_to_srgb() will run a little faster than usual when compiled with S SE4.1+. | 20 * sk_linear_to_srgb() will run a little faster than usual when compiled with S SE4.1+. |
| 27 */ | 21 */ |
| 28 | 22 |
| 29 extern const float sk_linear_from_srgb[256]; | 23 extern const float sk_linear_from_srgb[256]; |
| 30 | 24 |
| 31 static inline Sk4f sk_linear_to_srgb(const Sk4f& x) { | 25 static inline Sk4i sk_linear_to_srgb(const Sk4f& x) { |
| 32 // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixe ls). | 26 // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixe ls). |
| 33 // For 0.00000f <= x < 0.00349f, 12.92 * x | 27 // Tuned by brute force to minimize the number of bytes that fail to round t rip, here 0. |
| 34 // For 0.00349f <= x <= 1.00000f, 0.679*(x.^0.5) + 0.423*x.^(0.25) - 0.10 1 | 28 // |
| 35 // Note that 0.00349 was selected because it is a point where both functions produce the | 29 // 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
| |
| 36 // same pixel value when rounded. | 30 // to find one that round trips all bytes correctly while minimizing some ov erall error. |
| 31 | |
| 37 auto rsqrt = x.rsqrt(), | 32 auto rsqrt = x.rsqrt(), |
| 38 sqrt = rsqrt.invert(), | 33 sqrt = rsqrt.invert(), |
| 39 ftrt = rsqrt.rsqrt(); | 34 ftrt = rsqrt.rsqrt(); |
| 40 | 35 |
| 41 auto lo = (12.92f * 255.0f) * x; | 36 auto lo = (12.9232f * 255.0f) * x; |
| 42 | 37 |
| 43 auto hi = (-0.101115084998961f * 255.0f) + | 38 auto hi = (-0.0958f * 255.0f) |
| 44 (+0.678513029959381f * 255.0f) * sqrt + | 39 + (+0.6946f * 255.0f) * sqrt |
| 45 (+0.422602055039580f * 255.0f) * ftrt; | 40 + (+0.4047f * 255.0f) * ftrt; |
| 46 | 41 |
| 47 return (x < 0.00349f).thenElse(lo, hi); | 42 return SkNx_cast<int>( (x < 0.005f).thenElse(lo, hi) ); |
| 48 } | 43 } |
| 49 | 44 |
| 50 #endif//SkSRGB_DEFINED | 45 #endif//SkSRGB_DEFINED |
| OLD | NEW |