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 "SkHalf.h" | 13 #include "SkHalf.h" |
14 #include "SkSRGB.h" | 14 #include "SkSRGB.h" |
15 #include "SkTemplates.h" | 15 #include "SkTemplates.h" |
16 | 16 |
17 namespace SK_OPTS_NS { | 17 namespace SK_OPTS_NS { |
18 | 18 |
19 static Sk4f clamp_0_1(const Sk4f& x) { | |
20 // The order of the arguments is important here. We want to make sure that
NaN | |
21 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. | |
22 return Sk4f::Min(Sk4f::Max(x, 0.0f), 1.0f); | |
23 } | |
24 | |
25 static Sk4i linear_to_2dot2(const Sk4f& x) { | 19 static Sk4i linear_to_2dot2(const Sk4f& x) { |
26 // 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). |
27 auto x2 = x.rsqrt(), // x^(-1/2) | 21 auto x2 = x.rsqrt(), // x^(-1/2) |
28 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) | 22 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) |
29 x64 = x32.rsqrt(); // x^(+1/64) | 23 x64 = x32.rsqrt(); // x^(+1/64) |
30 | 24 |
31 // 29 = 32 - 2 - 1 | 25 // 29 = 32 - 2 - 1 |
32 return Sk4f_round(255.0f * x2.invert() * x32 * x64.invert()); | 26 return Sk4f_round(sk_clamp_0_255(255.0f * x2.invert() * x32 * x64.invert()))
; |
33 } | 27 } |
34 | 28 |
35 enum DstGamma { | 29 enum DstGamma { |
36 // 8888 | 30 // 8888 |
37 kSRGB_DstGamma, | 31 kSRGB_DstGamma, |
38 k2Dot2_DstGamma, | 32 k2Dot2_DstGamma, |
39 kTable_DstGamma, | 33 kTable_DstGamma, |
40 | 34 |
41 // F16 | 35 // F16 |
42 kLinear_DstGamma, | 36 kLinear_DstGamma, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues; | 69 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues; |
76 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues; | 70 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues; |
77 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues; | 71 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues; |
78 }; | 72 }; |
79 | 73 |
80 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables] { | 74 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables] { |
81 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { | 75 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { |
82 Sk4i (*linear_to_curve)(const Sk4f&) = | 76 Sk4i (*linear_to_curve)(const Sk4f&) = |
83 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : line
ar_to_2dot2; | 77 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : line
ar_to_2dot2; |
84 | 78 |
85 auto reds = linear_to_curve(clamp_0_1(dstReds)); | 79 auto reds = linear_to_curve(dstReds); |
86 auto greens = linear_to_curve(clamp_0_1(dstGreens)); | 80 auto greens = linear_to_curve(dstGreens); |
87 auto blues = linear_to_curve(clamp_0_1(dstBlues)); | 81 auto blues = linear_to_curve(dstBlues); |
88 | |
89 | 82 |
90 auto rgba = (reds << SK_R32_SHIFT) | 83 auto rgba = (reds << SK_R32_SHIFT) |
91 | (greens << SK_G32_SHIFT) | 84 | (greens << SK_G32_SHIFT) |
92 | (blues << SK_B32_SHIFT) | 85 | (blues << SK_B32_SHIFT) |
93 | (Sk4i{0xFF} << SK_A32_SHIFT); | 86 | (Sk4i{0xFF} << SK_A32_SHIFT); |
94 rgba.store((uint32_t*) dst); | 87 rgba.store((uint32_t*) dst); |
95 | 88 |
96 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t)); | 89 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t)); |
97 } else if (kTable_DstGamma == kDstGamma) { | 90 } else if (kTable_DstGamma == kDstGamma) { |
98 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0
f), 1023.0f); | 91 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0
f), 1023.0f); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, | 141 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, |
149 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, | 142 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, |
150 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; | 143 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; |
151 | 144 |
152 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; | 145 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; |
153 | 146 |
154 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { | 147 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { |
155 Sk4i (*linear_to_curve)(const Sk4f&) = | 148 Sk4i (*linear_to_curve)(const Sk4f&) = |
156 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_t
o_2dot2; | 149 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_t
o_2dot2; |
157 | 150 |
158 auto pixel = linear_to_curve(clamp_0_1(dstPixel)); | 151 auto pixel = linear_to_curve(dstPixel); |
159 | 152 |
160 uint32_t rgba; | 153 uint32_t rgba; |
161 SkNx_cast<uint8_t>(pixel).store(&rgba); | 154 SkNx_cast<uint8_t>(pixel).store(&rgba); |
162 rgba |= 0xFF000000; | 155 rgba |= 0xFF000000; |
163 *((uint32_t*) dst) = SkSwizzle_RGBA_to_PMColor(rgba); | 156 *((uint32_t*) dst) = SkSwizzle_RGBA_to_PMColor(rgba); |
164 dst = SkTAddOffset<void>(dst, sizeof(uint32_t)); | 157 dst = SkTAddOffset<void>(dst, sizeof(uint32_t)); |
165 } else if (kTable_DstGamma == kDstGamma) { | 158 } else if (kTable_DstGamma == kDstGamma) { |
166 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10
23.0f); | 159 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10
23.0f); |
167 | 160 |
168 Sk4i indices = Sk4f_round(scaledPixel); | 161 Sk4i indices = Sk4f_round(scaledPixel); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 } | 196 } |
204 | 197 |
205 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l
en, | 198 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l
en, |
206 const float* const srcTables[3], const fl
oat matrix[16]) { | 199 const float* const srcTables[3], const fl
oat matrix[16]) { |
207 color_xform_RGB1<kLinear_DstGamma>(dst, src, len, srcTables, matrix, nullptr
); | 200 color_xform_RGB1<kLinear_DstGamma>(dst, src, len, srcTables, matrix, nullptr
); |
208 } | 201 } |
209 | 202 |
210 } // namespace SK_OPTS_NS | 203 } // namespace SK_OPTS_NS |
211 | 204 |
212 #endif // SkColorXform_opts_DEFINED | 205 #endif // SkColorXform_opts_DEFINED |
OLD | NEW |