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 Sk4i linear_to_2dot2(const Sk4f& x) { | 19 // Strange that we need a wrapper on SkNx_cast to use as a function ptr. |
| 20 static Sk4i Sk4f_trunc(const Sk4f& x) { |
| 21 return SkNx_cast<int>(x); |
| 22 } |
| 23 |
| 24 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). | 25 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). |
21 auto x2 = x.rsqrt(), // x^(-1/2) | 26 auto x2 = x.rsqrt(), // x^(-1/2) |
22 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) | 27 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) |
23 x64 = x32.rsqrt(); // x^(+1/64) | 28 x64 = x32.rsqrt(); // x^(+1/64) |
24 | 29 |
25 // 29 = 32 - 2 - 1 | 30 // 29 = 32 - 2 - 1 |
26 return Sk4f_round(sk_clamp_0_255(255.0f * x2.invert() * x32 * x64.invert()))
; | 31 return 255.0f * x2.invert() * x32 * x64.invert(); |
27 } | 32 } |
28 | 33 |
29 enum DstGamma { | 34 enum DstGamma { |
30 // 8888 | 35 // 8888 |
31 kSRGB_DstGamma, | 36 kSRGB_DstGamma, |
32 k2Dot2_DstGamma, | 37 k2Dot2_DstGamma, |
33 kTable_DstGamma, | 38 kTable_DstGamma, |
34 | 39 |
35 // F16 | 40 // F16 |
36 kLinear_DstGamma, | 41 kLinear_DstGamma, |
(...skipping 30 matching lines...) Expand all Loading... |
67 Sk4f dstReds, dstGreens, dstBlues; | 72 Sk4f dstReds, dstGreens, dstBlues; |
68 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl
ues, &rXgXbX, | 73 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl
ues, &rXgXbX, |
69 &rYgYbY, &rZgZbZ, &rTgTbT] { | 74 &rYgYbY, &rZgZbZ, &rTgTbT] { |
70 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues + rT
gTbT[0]; | 75 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues + rT
gTbT[0]; |
71 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues + rT
gTbT[1]; | 76 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues + rT
gTbT[1]; |
72 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues + rT
gTbT[2]; | 77 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues + rT
gTbT[2]; |
73 }; | 78 }; |
74 | 79 |
75 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables] { | 80 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables] { |
76 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { | 81 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { |
77 Sk4i (*linear_to_curve)(const Sk4f&) = | 82 Sk4f (*linear_to_curve)(const Sk4f&) = (kSRGB_DstGamma == kDstGa
mma) ? |
78 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : line
ar_to_2dot2; | 83 sk_linear_to_srgb_needs_trunc : linear_to_2dot2; |
| 84 Sk4i (*float_to_int)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma
) ? |
| 85 Sk4f_trunc : Sk4f_round; |
79 | 86 |
80 auto reds = linear_to_curve(dstReds); | 87 dstReds = linear_to_curve(dstReds); |
81 auto greens = linear_to_curve(dstGreens); | 88 dstGreens = linear_to_curve(dstGreens); |
82 auto blues = linear_to_curve(dstBlues); | 89 dstBlues = linear_to_curve(dstBlues); |
83 | 90 |
84 auto rgba = (reds << SK_R32_SHIFT) | 91 dstReds = sk_clamp_0_255(dstReds); |
85 | (greens << SK_G32_SHIFT) | 92 dstGreens = sk_clamp_0_255(dstGreens); |
86 | (blues << SK_B32_SHIFT) | 93 dstBlues = sk_clamp_0_255(dstBlues); |
87 | (Sk4i{0xFF} << SK_A32_SHIFT); | 94 |
| 95 auto rgba = (float_to_int(dstReds) << SK_R32_SHIFT) |
| 96 | (float_to_int(dstGreens) << SK_G32_SHIFT) |
| 97 | (float_to_int(dstBlues) << SK_B32_SHIFT) |
| 98 | (Sk4i{0xFF} << SK_A32_SHIFT); |
88 rgba.store((uint32_t*) dst); | 99 rgba.store((uint32_t*) dst); |
89 | 100 |
90 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t)); | 101 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t)); |
91 } else if (kTable_DstGamma == kDstGamma) { | 102 } else if (kTable_DstGamma == kDstGamma) { |
92 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0
f), 1023.0f); | 103 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0
f), 1023.0f); |
93 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0
f), 1023.0f); | 104 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0
f), 1023.0f); |
94 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0
f), 1023.0f); | 105 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0
f), 1023.0f); |
95 | 106 |
96 Sk4i indicesReds = Sk4f_round(scaledReds); | 107 Sk4i indicesReds = Sk4f_round(scaledReds); |
97 Sk4i indicesGreens = Sk4f_round(scaledGreens); | 108 Sk4i indicesGreens = Sk4f_round(scaledGreens); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 | 150 |
140 while (len > 0) { | 151 while (len > 0) { |
141 // Splat r,g,b across a register each. | 152 // Splat r,g,b across a register each. |
142 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, | 153 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, |
143 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, | 154 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, |
144 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; | 155 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; |
145 | 156 |
146 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b + rTgTbT; | 157 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b + rTgTbT; |
147 | 158 |
148 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { | 159 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { |
149 Sk4i (*linear_to_curve)(const Sk4f&) = | 160 Sk4f (*linear_to_curve)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma)
? |
150 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_t
o_2dot2; | 161 sk_linear_to_srgb_needs_trunc : linear_to_2dot2; |
| 162 Sk4i (*float_to_int)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma) ? |
| 163 Sk4f_trunc : Sk4f_round; |
151 | 164 |
152 auto pixel = linear_to_curve(dstPixel); | 165 dstPixel = sk_clamp_0_255(linear_to_curve(dstPixel)); |
153 | 166 |
154 uint32_t rgba; | 167 uint32_t rgba; |
155 SkNx_cast<uint8_t>(pixel).store(&rgba); | 168 SkNx_cast<uint8_t>(float_to_int(dstPixel)).store(&rgba); |
156 rgba |= 0xFF000000; | 169 rgba |= 0xFF000000; |
157 *((uint32_t*) dst) = SkSwizzle_RGBA_to_PMColor(rgba); | 170 *((uint32_t*) dst) = SkSwizzle_RGBA_to_PMColor(rgba); |
158 dst = SkTAddOffset<void>(dst, sizeof(uint32_t)); | 171 dst = SkTAddOffset<void>(dst, sizeof(uint32_t)); |
159 } else if (kTable_DstGamma == kDstGamma) { | 172 } else if (kTable_DstGamma == kDstGamma) { |
160 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10
23.0f); | 173 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10
23.0f); |
161 | 174 |
162 Sk4i indices = Sk4f_round(scaledPixel); | 175 Sk4i indices = Sk4f_round(scaledPixel); |
163 | 176 |
164 *((uint32_t*) dst) = dstTables[0][indices[0]] << SK_R32_SHIFT | 177 *((uint32_t*) dst) = dstTables[0][indices[0]] << SK_R32_SHIFT |
165 | dstTables[1][indices[1]] << SK_G32_SHIFT | 178 | dstTables[1][indices[1]] << SK_G32_SHIFT |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 } | 210 } |
198 | 211 |
199 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l
en, | 212 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l
en, |
200 const float* const srcTables[3], const fl
oat matrix[16]) { | 213 const float* const srcTables[3], const fl
oat matrix[16]) { |
201 color_xform_RGB1<kLinear_DstGamma>(dst, src, len, srcTables, matrix, nullptr
); | 214 color_xform_RGB1<kLinear_DstGamma>(dst, src, len, srcTables, matrix, nullptr
); |
202 } | 215 } |
203 | 216 |
204 } // namespace SK_OPTS_NS | 217 } // namespace SK_OPTS_NS |
205 | 218 |
206 #endif // SkColorXform_opts_DEFINED | 219 #endif // SkColorXform_opts_DEFINED |
OLD | NEW |