OLD | NEW |
| (Empty) |
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 SkColorXform_opts_DEFINED | |
9 #define SkColorXform_opts_DEFINED | |
10 | |
11 #include "SkNx.h" | |
12 #include "SkColorPriv.h" | |
13 #include "SkHalf.h" | |
14 #include "SkSRGB.h" | |
15 #include "SkTemplates.h" | |
16 | |
17 namespace SK_OPTS_NS { | |
18 | |
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) { | |
25 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). | |
26 auto x2 = x.rsqrt(), // x^(-1/2) | |
27 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) | |
28 x64 = x32.rsqrt(); // x^(+1/64) | |
29 | |
30 // 29 = 32 - 2 - 1 | |
31 return 255.0f * x2.invert() * x32 * x64.invert(); | |
32 } | |
33 | |
34 enum DstGamma { | |
35 // 8888 | |
36 kSRGB_DstGamma, | |
37 k2Dot2_DstGamma, | |
38 kTable_DstGamma, | |
39 | |
40 // F16 | |
41 kLinear_DstGamma, | |
42 }; | |
43 | |
44 template <DstGamma kDstGamma, bool kSwapRB> | |
45 static void color_xform_RGB1(void* dst, const uint32_t* src, int len, | |
46 const float* const srcTables[3], const float matrix
[16], | |
47 const uint8_t* const dstTables[3]) { | |
48 int kRShift = 0; | |
49 int kGShift = 8; | |
50 int kBShift = 16; | |
51 int kAShift = 24; | |
52 if (kSwapRB) { | |
53 kBShift = 0; | |
54 kRShift = 16; | |
55 } | |
56 | |
57 Sk4f rXgXbX = Sk4f::Load(matrix + 0), | |
58 rYgYbY = Sk4f::Load(matrix + 4), | |
59 rZgZbZ = Sk4f::Load(matrix + 8), | |
60 rTgTbT = Sk4f::Load(matrix + 12); | |
61 | |
62 if (len >= 4) { | |
63 Sk4f reds, greens, blues; | |
64 auto load_next_4 = [&reds, &greens, &blues, &src, &len, &srcTables] { | |
65 reds = Sk4f{srcTables[0][(src[0] >> 0) & 0xFF], | |
66 srcTables[0][(src[1] >> 0) & 0xFF], | |
67 srcTables[0][(src[2] >> 0) & 0xFF], | |
68 srcTables[0][(src[3] >> 0) & 0xFF]}; | |
69 greens = Sk4f{srcTables[1][(src[0] >> 8) & 0xFF], | |
70 srcTables[1][(src[1] >> 8) & 0xFF], | |
71 srcTables[1][(src[2] >> 8) & 0xFF], | |
72 srcTables[1][(src[3] >> 8) & 0xFF]}; | |
73 blues = Sk4f{srcTables[2][(src[0] >> 16) & 0xFF], | |
74 srcTables[2][(src[1] >> 16) & 0xFF], | |
75 srcTables[2][(src[2] >> 16) & 0xFF], | |
76 srcTables[2][(src[3] >> 16) & 0xFF]}; | |
77 src += 4; | |
78 len -= 4; | |
79 }; | |
80 | |
81 Sk4f dstReds, dstGreens, dstBlues; | |
82 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl
ues, &rXgXbX, | |
83 &rYgYbY, &rZgZbZ, &rTgTbT] { | |
84 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues + rT
gTbT[0]; | |
85 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues + rT
gTbT[1]; | |
86 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues + rT
gTbT[2]; | |
87 }; | |
88 | |
89 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables, kRShi
ft, kGShift, | |
90 kBShift, kAShift] { | |
91 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { | |
92 Sk4f (*linear_to_curve)(const Sk4f&) = (kSRGB_DstGamma == kDstGa
mma) ? | |
93 sk_linear_to_srgb_needs_trunc : linear_to_2dot2; | |
94 Sk4i (*float_to_int)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma
) ? | |
95 Sk4f_trunc : Sk4f_round; | |
96 | |
97 dstReds = linear_to_curve(dstReds); | |
98 dstGreens = linear_to_curve(dstGreens); | |
99 dstBlues = linear_to_curve(dstBlues); | |
100 | |
101 dstReds = sk_clamp_0_255(dstReds); | |
102 dstGreens = sk_clamp_0_255(dstGreens); | |
103 dstBlues = sk_clamp_0_255(dstBlues); | |
104 | |
105 auto rgba = (float_to_int(dstReds) << kRShift) | |
106 | (float_to_int(dstGreens) << kGShift) | |
107 | (float_to_int(dstBlues) << kBShift) | |
108 | (Sk4i{0xFF} << kAShift); | |
109 rgba.store((uint32_t*) dst); | |
110 | |
111 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t)); | |
112 } else if (kTable_DstGamma == kDstGamma) { | |
113 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0
f), 1023.0f); | |
114 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0
f), 1023.0f); | |
115 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0
f), 1023.0f); | |
116 | |
117 Sk4i indicesReds = Sk4f_round(scaledReds); | |
118 Sk4i indicesGreens = Sk4f_round(scaledGreens); | |
119 Sk4i indicesBlues = Sk4f_round(scaledBlues); | |
120 | |
121 uint32_t* dst32 = (uint32_t*) dst; | |
122 dst32[0] = dstTables[0][indicesReds [0]] << kRShift | |
123 | dstTables[1][indicesGreens[0]] << kGShift | |
124 | dstTables[2][indicesBlues [0]] << kBShift | |
125 | 0xFF << kAShift; | |
126 dst32[1] = dstTables[0][indicesReds [1]] << kRShift | |
127 | dstTables[1][indicesGreens[1]] << kGShift | |
128 | dstTables[2][indicesBlues [1]] << kBShift | |
129 | 0xFF << kAShift; | |
130 dst32[2] = dstTables[0][indicesReds [2]] << kRShift | |
131 | dstTables[1][indicesGreens[2]] << kGShift | |
132 | dstTables[2][indicesBlues [2]] << kBShift | |
133 | 0xFF << kAShift; | |
134 dst32[3] = dstTables[0][indicesReds [3]] << kRShift | |
135 | dstTables[1][indicesGreens[3]] << kGShift | |
136 | dstTables[2][indicesBlues [3]] << kBShift | |
137 | 0xFF << kAShift; | |
138 | |
139 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t)); | |
140 } else { | |
141 Sk4h_store4(dst, SkFloatToHalf_finite(dstReds), | |
142 SkFloatToHalf_finite(dstGreens), | |
143 SkFloatToHalf_finite(dstBlues), | |
144 SK_Half1); | |
145 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint64_t)); | |
146 } | |
147 }; | |
148 | |
149 load_next_4(); | |
150 | |
151 while (len >= 4) { | |
152 transform_4(); | |
153 load_next_4(); | |
154 store_4(); | |
155 } | |
156 | |
157 transform_4(); | |
158 store_4(); | |
159 } | |
160 | |
161 while (len > 0) { | |
162 // Splat r,g,b across a register each. | |
163 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, | |
164 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, | |
165 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; | |
166 | |
167 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b + rTgTbT; | |
168 | |
169 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { | |
170 Sk4f (*linear_to_curve)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma)
? | |
171 sk_linear_to_srgb_needs_trunc : linear_to_2dot2; | |
172 Sk4i (*float_to_int)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma) ? | |
173 Sk4f_trunc : Sk4f_round; | |
174 | |
175 dstPixel = sk_clamp_0_255(linear_to_curve(dstPixel)); | |
176 | |
177 uint32_t rgba; | |
178 SkNx_cast<uint8_t>(float_to_int(dstPixel)).store(&rgba); | |
179 rgba |= 0xFF000000; | |
180 if (kSwapRB) { | |
181 *((uint32_t*) dst) = SkSwizzle_RB(rgba); | |
182 } else { | |
183 *((uint32_t*) dst) = rgba; | |
184 } | |
185 dst = SkTAddOffset<void>(dst, sizeof(uint32_t)); | |
186 } else if (kTable_DstGamma == kDstGamma) { | |
187 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10
23.0f); | |
188 | |
189 Sk4i indices = Sk4f_round(scaledPixel); | |
190 | |
191 *((uint32_t*) dst) = dstTables[0][indices[0]] << kRShift | |
192 | dstTables[1][indices[1]] << kGShift | |
193 | dstTables[2][indices[2]] << kBShift | |
194 | 0xFF << kAShift; | |
195 | |
196 dst = SkTAddOffset<void>(dst, sizeof(uint32_t)); | |
197 } else { | |
198 uint64_t rgba; | |
199 SkFloatToHalf_finite(dstPixel).store(&rgba); | |
200 rgba |= static_cast<uint64_t>(SK_Half1) << 48; | |
201 *((uint64_t*) dst) = rgba; | |
202 dst = SkTAddOffset<void>(dst, sizeof(uint64_t)); | |
203 } | |
204 | |
205 src += 1; | |
206 len -= 1; | |
207 } | |
208 } | |
209 | |
210 static void color_xform_RGB1_to_2dot2(uint32_t* dst, const uint32_t* src, int le
n, | |
211 const float* const srcTables[3], const flo
at matrix[16]) { | |
212 color_xform_RGB1<k2Dot2_DstGamma, false>(dst, src, len, srcTables, matrix, n
ullptr); | |
213 } | |
214 | |
215 static void color_xform_RGB1_to_srgb(uint32_t* dst, const uint32_t* src, int len
, | |
216 const float* const srcTables[3], const floa
t matrix[16]) { | |
217 color_xform_RGB1<kSRGB_DstGamma, false>(dst, src, len, srcTables, matrix, nu
llptr); | |
218 } | |
219 | |
220 static void color_xform_RGB1_to_table(uint32_t* dst, const uint32_t* src, int le
n, | |
221 const float* const srcTables[3], const flo
at matrix[16], | |
222 const uint8_t* const dstTables[3]) { | |
223 color_xform_RGB1<kTable_DstGamma, false>(dst, src, len, srcTables, matrix, d
stTables); | |
224 } | |
225 | |
226 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l
en, | |
227 const float* const srcTables[3], const fl
oat matrix[16]) { | |
228 color_xform_RGB1<kLinear_DstGamma, false>(dst, src, len, srcTables, matrix,
nullptr); | |
229 } | |
230 | |
231 static void color_xform_RGB1_to_2dot2_swaprb(uint32_t* dst, const uint32_t* src,
int len, | |
232 const float* const srcTables[3], | |
233 const float matrix[16]) { | |
234 color_xform_RGB1<k2Dot2_DstGamma, true>(dst, src, len, srcTables, matrix, nu
llptr); | |
235 } | |
236 | |
237 static void color_xform_RGB1_to_srgb_swaprb(uint32_t* dst, const uint32_t* src,
int len, | |
238 const float* const srcTables[3], | |
239 const float matrix[16]) { | |
240 color_xform_RGB1<kSRGB_DstGamma, true>(dst, src, len, srcTables, matrix, nul
lptr); | |
241 } | |
242 | |
243 static void color_xform_RGB1_to_table_swaprb(uint32_t* dst, const uint32_t* src,
int len, | |
244 const float* const srcTables[3], | |
245 const float matrix[16], | |
246 const uint8_t* const dstTables[3])
{ | |
247 color_xform_RGB1<kTable_DstGamma, true>(dst, src, len, srcTables, matrix, ds
tTables); | |
248 } | |
249 | |
250 } // namespace SK_OPTS_NS | |
251 | |
252 #endif // SkColorXform_opts_DEFINED | |
OLD | NEW |