1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkSRGB_DEFINED
9 #define SkSRGB_DEFINED
10
11 #include "SkNx.h"
12
13 /** Components for building our canonical sRGB -> linear and linear -> sRGB transformations.
14 *
15 * Current best practices:
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;
18 * - the alpha channel is linear in both formats, needing at most *(1/255.0f) or *255.0f.
19 *
20 * sk_linear_to_srgb() will run a little faster than usual when compiled with SSE4.1+.
21 */
22
23 extern const float sk_linear_from_srgb[256];
24 extern const uint16_t sk_linear12_from_srgb[256];
25 extern const uint8_t sk_linear12_to_srgb[4096];
26
27 template <typename V>
sk_clamp_0_255(const V & x)28 static inline V sk_clamp_0_255(const V& x) {
29 // The order of the arguments is important here. We want to make sure that NaN
30 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN.
31 return V::Min(V::Max(x, 0.0f), 255.0f);
32 }
33
34 // [0.0f, 1.0f] -> [0.0f, 255.xf], for small x. Correct after truncation.
35 template <typename V>
sk_linear_to_srgb_needs_trunc(const V & x)36 static inline V sk_linear_to_srgb_needs_trunc(const V& x) {
37 // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixels).
38 //
39 // Constants tuned by brute force to minimize (in order of importance) after truncation:
40 // 1) the number of bytes that fail to round trip (0 of 256);
41 // 2) the number of points in [FLT_MIN, 1.0f] that are non-monotonic (0 of ~1 billion);
42 // 3) the number of points halfway between bytes that hit the wrong byte (131 of 255).
43 auto rsqrt = x.rsqrt(),
44 sqrt = rsqrt.invert(),
45 ftrt = rsqrt.rsqrt();
46
47 auto lo = (13.0471f * 255.0f) * x;
48
49 auto hi = SkNx_fma(V{+0.412999f * 255.0f}, ftrt,
50 SkNx_fma(V{+0.687999f * 255.0f}, sqrt,
51 V{-0.0974983f * 255.0f}));
52 return (x < 0.0048f).thenElse(lo, hi);
53 }
54
55 // [0.0f, 1.0f] -> [0.0f, 1.0f]. Correct after rounding.
56 template <typename V>
sk_linear_to_srgb_needs_round(const V & x)57 static inline V sk_linear_to_srgb_needs_round(const V& x) {
58 // Tuned to round trip each sRGB byte after rounding.
59 auto rsqrt = x.rsqrt(),
60 sqrt = rsqrt.invert(),
61 ftrt = rsqrt.rsqrt();
62
63 auto lo = 12.46f * x;
64
65 auto hi = V::Min(1.0f, SkNx_fma(V{+0.411192f}, ftrt,
66 SkNx_fma(V{+0.689206f}, sqrt,
67 V{-0.0988f})));
68 return (x < 0.0043f).thenElse(lo, hi);
69 }
70
71 template <int N>
sk_linear_to_srgb(const SkNx<N,float> & x)72 static inline SkNx<N,int> sk_linear_to_srgb(const SkNx<N,float>& x) {
73 auto f = sk_linear_to_srgb_needs_trunc(x);
74 return SkNx_cast<int>(sk_clamp_0_255(f));
75 }
76
77
78 // sRGB -> linear, using math instead of table lookups.
79 template <typename V>
sk_linear_from_srgb_math(const V & x)80 static inline V sk_linear_from_srgb_math(const V& x) {
81 // Non-linear segment of sRGB curve approximated by
82 // l = 0.0025 + 0.6975x^2 + 0.3x^3
83 const V k0 = 0.0025f,
84 k2 = 0.6975f,
85 k3 = 0.3000f;
86 auto hi = SkNx_fma(x*x, SkNx_fma(x, k3, k2), k0);
87
88 // Linear segment of sRGB curve: the normal slope, extended a little further than normal.
89 auto lo = x * (1/12.92f);
90
91 return (x < 0.055f).thenElse(lo, hi);
92 }
93
94 // Same as above, starting from ints.
95 template <int N>
sk_linear_from_srgb_math(const SkNx<N,int> & s)96 static inline SkNx<N,float> sk_linear_from_srgb_math(const SkNx<N,int>& s) {
97 auto x = SkNx_cast<float>(s);
98
99 // Same math as above, but working with x in [0,255], so x^n needs scaling by u^n.
100 const float u = 1/255.0f;
101
102 const SkNx<N,float> k0 = 0.0025f,
103 k2 = 0.6975f * u*u,
104 k3 = 0.3000f * u*u*u;
105 auto hi = SkNx_fma(x*x, SkNx_fma(x, k3, k2), k0);
106 auto lo = x * (u/12.92f);
107 return (x < (0.055f/u)).thenElse(lo, hi);
108 }
109
110 #endif//SkSRGB_DEFINED
111