| 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 // |
| 34 // For 0.00349f <= x <= 1.00000f, 0.679*(x.^0.5) + 0.423*x.^(0.25) - 0.10
1 | 28 // Tuned by brute force to minimize the number of bytes that fail to round t
rip, |
| 35 // Note that 0.00349 was selected because it is a point where both functions
produce the | 29 // here 0 (of 256), and then to minimize the number of points halfway betwee
n bytes |
| 36 // same pixel value when rounded. | 30 // (in linear space) that fail to hit the right byte, here 131 (of 255), and
to |
| 31 // minimize the number of monotonicity regressions over the range [0,1], her
e 0. |
| 32 |
| 37 auto rsqrt = x.rsqrt(), | 33 auto rsqrt = x.rsqrt(), |
| 38 sqrt = rsqrt.invert(), | 34 sqrt = rsqrt.invert(), |
| 39 ftrt = rsqrt.rsqrt(); | 35 ftrt = rsqrt.rsqrt(); |
| 40 | 36 |
| 41 auto lo = (12.92f * 255.0f) * x; | 37 auto lo = (13.0471f * 255.0f) * x; |
| 42 | 38 |
| 43 auto hi = (-0.101115084998961f * 255.0f) + | 39 auto hi = (-0.0974983f * 255.0f) |
| 44 (+0.678513029959381f * 255.0f) * sqrt + | 40 + (+0.687999f * 255.0f) * sqrt |
| 45 (+0.422602055039580f * 255.0f) * ftrt; | 41 + (+0.412999f * 255.0f) * ftrt; |
| 46 | 42 |
| 47 return (x < 0.00349f).thenElse(lo, hi); | 43 return SkNx_cast<int>( (x < 0.0048f).thenElse(lo, hi) ); |
| 48 } | 44 } |
| 49 | 45 |
| 50 #endif//SkSRGB_DEFINED | 46 #endif//SkSRGB_DEFINED |
| OLD | NEW |