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 SkColorXform_opts_DEFINED | 8 #ifndef SkColorXform_opts_DEFINED |
9 #define SkColorXform_opts_DEFINED | 9 #define SkColorXform_opts_DEFINED |
10 | 10 |
11 #include "SkNx.h" | 11 #include "SkNx.h" |
12 #include "SkColorPriv.h" | 12 #include "SkColorPriv.h" |
13 #include "SkSRGB.h" | |
14 | 13 |
| 14 extern const float sk_linear_from_srgb[256]; |
15 extern const float sk_linear_from_2dot2[256]; | 15 extern const float sk_linear_from_2dot2[256]; |
16 | 16 |
17 namespace SK_OPTS_NS { | 17 namespace SK_OPTS_NS { |
18 | 18 |
19 static Sk4f linear_to_2dot2(const Sk4f& x) { | 19 static Sk4f linear_to_2dot2(const Sk4f& x) { |
20 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). | 20 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). |
21 auto x2 = x.rsqrt(), // x^(-1/2) | 21 auto x2 = x.rsqrt(), // x^(-1/2) |
22 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) | 22 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) |
23 x64 = x32.rsqrt(); // x^(+1/64) | 23 x64 = x32.rsqrt(); // x^(+1/64) |
24 | 24 |
25 // 29 = 32 - 2 - 1 | 25 // 29 = 32 - 2 - 1 |
26 return 255.0f * x2.invert() * x32 * x64.invert(); | 26 return 255.0f * x2.invert() * x32 * x64.invert(); |
27 } | 27 } |
28 | 28 |
| 29 static Sk4f linear_to_srgb(const Sk4f& x) { |
| 30 // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixe
ls). |
| 31 // For 0.00000f <= x < 0.00349f, 12.92 * x |
| 32 // For 0.00349f <= x <= 1.00000f, 0.679*(x.^0.5) + 0.423*x.^(0.25) - 0.10
1 |
| 33 // Note that 0.00349 was selected because it is a point where both functions
produce the |
| 34 // same pixel value when rounded. |
| 35 auto rsqrt = x.rsqrt(), |
| 36 sqrt = rsqrt.invert(), |
| 37 ftrt = rsqrt.rsqrt(); |
| 38 |
| 39 auto hi = (-0.101115084998961f * 255.0f) + |
| 40 (+0.678513029959381f * 255.0f) * sqrt + |
| 41 (+0.422602055039580f * 255.0f) * ftrt; |
| 42 |
| 43 auto lo = (12.92f * 255.0f) * x; |
| 44 |
| 45 auto mask = (x < 0.00349f); |
| 46 return mask.thenElse(lo, hi); |
| 47 } |
| 48 |
29 static Sk4f clamp_0_to_255(const Sk4f& x) { | 49 static Sk4f clamp_0_to_255(const Sk4f& x) { |
30 // The order of the arguments is important here. We want to make sure that
NaN | 50 // The order of the arguments is important here. We want to make sure that
NaN |
31 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. | 51 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. |
32 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); | 52 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); |
33 } | 53 } |
34 | 54 |
35 template <const float (&linear_from_curve)[256], Sk4f (*linear_to_curve)(const S
k4f&)> | 55 template <const float (&linear_from_curve)[256], Sk4f (*linear_to_curve)(const S
k4f&)> |
36 static void color_xform_RGB1(uint32_t* dst, const uint32_t* src, int len, | 56 static void color_xform_RGB1(uint32_t* dst, const uint32_t* src, int len, |
37 const float matrix[16]) { | 57 const float matrix[16]) { |
38 Sk4f rXgXbX = Sk4f::Load(matrix + 0), | 58 Sk4f rXgXbX = Sk4f::Load(matrix + 0), |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 color_xform_RGB1<sk_linear_from_srgb, linear_to_2dot2>(dst, src, len, matrix
); | 147 color_xform_RGB1<sk_linear_from_srgb, linear_to_2dot2>(dst, src, len, matrix
); |
128 } | 148 } |
129 | 149 |
130 static void color_xform_RGB1_2dot2_to_2dot2(uint32_t* dst, const uint32_t* src,
int len, | 150 static void color_xform_RGB1_2dot2_to_2dot2(uint32_t* dst, const uint32_t* src,
int len, |
131 const float matrix[16]) { | 151 const float matrix[16]) { |
132 color_xform_RGB1<sk_linear_from_2dot2, linear_to_2dot2>(dst, src, len, matri
x); | 152 color_xform_RGB1<sk_linear_from_2dot2, linear_to_2dot2>(dst, src, len, matri
x); |
133 } | 153 } |
134 | 154 |
135 static void color_xform_RGB1_srgb_to_srgb(uint32_t* dst, const uint32_t* src, in
t len, | 155 static void color_xform_RGB1_srgb_to_srgb(uint32_t* dst, const uint32_t* src, in
t len, |
136 const float matrix[16]) { | 156 const float matrix[16]) { |
137 color_xform_RGB1<sk_linear_from_srgb, sk_linear_to_srgb>(dst, src, len, matr
ix); | 157 color_xform_RGB1<sk_linear_from_srgb, linear_to_srgb>(dst, src, len, matrix)
; |
138 } | 158 } |
139 | 159 |
140 static void color_xform_RGB1_2dot2_to_srgb(uint32_t* dst, const uint32_t* src, i
nt len, | 160 static void color_xform_RGB1_2dot2_to_srgb(uint32_t* dst, const uint32_t* src, i
nt len, |
141 const float matrix[16]) { | 161 const float matrix[16]) { |
142 color_xform_RGB1<sk_linear_from_2dot2, sk_linear_to_srgb>(dst, src, len, mat
rix); | 162 color_xform_RGB1<sk_linear_from_2dot2, linear_to_srgb>(dst, src, len, matrix
); |
143 } | 163 } |
144 | 164 |
145 } // namespace SK_OPTS_NS | 165 } // namespace SK_OPTS_NS |
146 | 166 |
147 #endif // SkColorXform_opts_DEFINED | 167 #endif // SkColorXform_opts_DEFINED |
OLD | NEW |