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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 */ | 21 */ |
22 | 22 |
23 extern const float sk_linear_from_srgb[256]; | 23 extern const float sk_linear_from_srgb[256]; |
24 | 24 |
25 static inline Sk4f sk_clamp_0_255(const Sk4f& x) { | 25 static inline Sk4f sk_clamp_0_255(const Sk4f& x) { |
26 // The order of the arguments is important here. We want to make sure that
NaN | 26 // The order of the arguments is important here. We want to make sure that
NaN |
27 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. | 27 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. |
28 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); | 28 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); |
29 } | 29 } |
30 | 30 |
31 static inline Sk4i sk_linear_to_srgb(const Sk4f& x) { | 31 // This should probably only be called from sk_linear_to_srgb() or sk_linear_to_
srgb_noclamp(). |
| 32 // It generally doesn't make sense to work with sRGB floats. |
| 33 static inline Sk4f sk_linear_to_srgb_needs_trunc(const Sk4f& x) { |
32 // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixe
ls). | 34 // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixe
ls). |
33 // | 35 // |
34 // Tuned by brute force to minimize the number of bytes that fail to round t
rip, | 36 // Constants tuned by brute force to minimize (in order of importance) after
truncation: |
35 // here 0 (of 256), and then to minimize the number of points halfway betwee
n bytes | 37 // 1) the number of bytes that fail to round trip (0 of 256); |
36 // (in linear space) that fail to hit the right byte, here 131 (of 255), and
to | 38 // 2) the number of points in [FLT_MIN, 1.0f] that are non-monotonic (0 o
f ~1 billion); |
37 // minimize the number of monotonicity regressions over the range [0,1], her
e 0. | 39 // 3) the number of points halfway between bytes that hit the wrong byte
(131 of 255). |
38 | |
39 auto rsqrt = x.rsqrt(), | 40 auto rsqrt = x.rsqrt(), |
40 sqrt = rsqrt.invert(), | 41 sqrt = rsqrt.invert(), |
41 ftrt = rsqrt.rsqrt(); | 42 ftrt = rsqrt.rsqrt(); |
42 | 43 |
43 auto lo = (13.0471f * 255.0f) * x; | 44 auto lo = (13.0471f * 255.0f) * x; |
44 | 45 |
45 auto hi = (-0.0974983f * 255.0f) | 46 auto hi = (-0.0974983f * 255.0f) |
46 + (+0.687999f * 255.0f) * sqrt | 47 + (+0.687999f * 255.0f) * sqrt |
47 + (+0.412999f * 255.0f) * ftrt; | 48 + (+0.412999f * 255.0f) * ftrt; |
| 49 return (x < 0.0048f).thenElse(lo, hi); |
| 50 } |
48 | 51 |
49 return SkNx_cast<int>(sk_clamp_0_255((x < 0.0048f).thenElse(lo, hi))); | 52 static inline Sk4i sk_linear_to_srgb(const Sk4f& x) { |
| 53 Sk4f f = sk_linear_to_srgb_needs_trunc(x); |
| 54 return SkNx_cast<int>(sk_clamp_0_255(f)); |
| 55 } |
| 56 |
| 57 static inline Sk4i sk_linear_to_srgb_noclamp(const Sk4f& x) { |
| 58 Sk4f f = sk_linear_to_srgb_needs_trunc(x); |
| 59 for (int i = 0; i < 4; i++) { |
| 60 SkASSERTF(0.0f <= f[i] && f[i] < 256.0f, "f[%d] was %g, outside [0,256)\
n", i, f[i]); |
| 61 } |
| 62 return SkNx_cast<int>(f); |
50 } | 63 } |
51 | 64 |
52 #endif//SkSRGB_DEFINED | 65 #endif//SkSRGB_DEFINED |
OLD | NEW |