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 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 | 179 |
180 static Sk4f clamp_0_to_255(const Sk4f& x) { | 180 static Sk4f clamp_0_to_255(const Sk4f& x) { |
181 // The order of the arguments is important here. We want to make sure that NaN | 181 // The order of the arguments is important here. We want to make sure that NaN |
182 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. | 182 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. |
183 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); | 183 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); |
184 } | 184 } |
185 | 185 |
186 template <const float (&linear_from_curve)[256], Sk4f (*linear_to_curve)(const S k4f&)> | 186 template <const float (&linear_from_curve)[256], Sk4f (*linear_to_curve)(const S k4f&)> |
187 static void color_xform_RGB1(uint32_t* dst, const uint32_t* src, int len, | 187 static void color_xform_RGB1(uint32_t* dst, const uint32_t* src, int len, |
188 const float matrix[16]) { | 188 const float matrix[16]) { |
189 // Load transformation matrix. | 189 Sk4f rXgXbX = Sk4f::Load(matrix + 0), |
190 auto rXgXbX = Sk4f::Load(matrix + 0), | |
191 rYgYbY = Sk4f::Load(matrix + 4), | 190 rYgYbY = Sk4f::Load(matrix + 4), |
192 rZgZbZ = Sk4f::Load(matrix + 8); | 191 rZgZbZ = Sk4f::Load(matrix + 8); |
193 | 192 |
194 while (len >= 4) { | 193 if (len >= 4) { |
195 // Convert to linear. The look-up table has perfect accuracy. | 194 Sk4f reds, greens, blues; |
196 auto reds = Sk4f{linear_from_curve[(src[0] >> 0) & 0xFF], | 195 auto load_next_4 = [&reds, &greens, &blues, &src, &len] { |
197 linear_from_curve[(src[1] >> 0) & 0xFF], | 196 reds = Sk4f{linear_from_curve[(src[0] >> 0) & 0xFF], |
198 linear_from_curve[(src[2] >> 0) & 0xFF], | 197 linear_from_curve[(src[1] >> 0) & 0xFF], |
199 linear_from_curve[(src[3] >> 0) & 0xFF]}; | 198 linear_from_curve[(src[2] >> 0) & 0xFF], |
200 auto greens = Sk4f{linear_from_curve[(src[0] >> 8) & 0xFF], | 199 linear_from_curve[(src[3] >> 0) & 0xFF]}; |
201 linear_from_curve[(src[1] >> 8) & 0xFF], | 200 greens = Sk4f{linear_from_curve[(src[0] >> 8) & 0xFF], |
202 linear_from_curve[(src[2] >> 8) & 0xFF], | 201 linear_from_curve[(src[1] >> 8) & 0xFF], |
203 linear_from_curve[(src[3] >> 8) & 0xFF]}; | 202 linear_from_curve[(src[2] >> 8) & 0xFF], |
204 auto blues = Sk4f{linear_from_curve[(src[0] >> 16) & 0xFF], | 203 linear_from_curve[(src[3] >> 8) & 0xFF]}; |
205 linear_from_curve[(src[1] >> 16) & 0xFF], | 204 blues = Sk4f{linear_from_curve[(src[0] >> 16) & 0xFF], |
206 linear_from_curve[(src[2] >> 16) & 0xFF], | 205 linear_from_curve[(src[1] >> 16) & 0xFF], |
207 linear_from_curve[(src[3] >> 16) & 0xFF]}; | 206 linear_from_curve[(src[2] >> 16) & 0xFF], |
207 linear_from_curve[(src[3] >> 16) & 0xFF]}; | |
208 src += 4; | |
209 len -= 4; | |
210 }; | |
208 | 211 |
209 // Apply the transformation matrix to dst gamut. | 212 Sk4f dstReds, dstGreens, dstBlues; |
210 auto dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues, | 213 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl ues, &rXgXbX, |
211 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues, | 214 &rYgYbY, &rZgZbZ] { |
212 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues; | 215 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues; |
216 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues; | |
217 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues; | |
218 }; | |
213 | 219 |
214 // Convert to dst gamma. | 220 Sk4i rgba; |
mtklein
2016/06/22 21:29:29
I think we can move rgba into the lambda. No need
msarett
2016/06/22 21:34:22
Done.
| |
215 dstReds = linear_to_curve(dstReds); | 221 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &rgba, &dst] { |
216 dstGreens = linear_to_curve(dstGreens); | 222 dstReds = linear_to_curve(dstReds); |
217 dstBlues = linear_to_curve(dstBlues); | 223 dstGreens = linear_to_curve(dstGreens); |
224 dstBlues = linear_to_curve(dstBlues); | |
218 | 225 |
219 // Clamp floats to byte range. | 226 dstReds = clamp_0_to_255(dstReds); |
220 dstReds = clamp_0_to_255(dstReds); | 227 dstGreens = clamp_0_to_255(dstGreens); |
221 dstGreens = clamp_0_to_255(dstGreens); | 228 dstBlues = clamp_0_to_255(dstBlues); |
222 dstBlues = clamp_0_to_255(dstBlues); | |
223 | 229 |
224 // Convert to bytes and store to memory. | 230 rgba = (Sk4i{(int)0xFF000000} ) |
225 auto rgba = (Sk4i{(int)0xFF000000} ) | 231 | (SkNx_cast<int>(dstReds) ) |
226 | (SkNx_cast<int>(dstReds) ) | 232 | (SkNx_cast<int>(dstGreens) << 8) |
227 | (SkNx_cast<int>(dstGreens) << 8) | 233 | (SkNx_cast<int>(dstBlues) << 16); |
228 | (SkNx_cast<int>(dstBlues) << 16); | 234 rgba.store(dst); |
229 rgba.store(dst); | 235 dst += 4; |
236 }; | |
230 | 237 |
231 dst += 4; | 238 load_next_4(); |
232 src += 4; | 239 |
233 len -= 4; | 240 while (len >= 4) { |
241 transform_4(); | |
242 load_next_4(); | |
243 store_4(); | |
244 } | |
245 | |
246 transform_4(); | |
247 store_4(); | |
234 } | 248 } |
235 | 249 |
236 while (len > 0) { | 250 while (len > 0) { |
237 // Splat r,g,b across a register each. | 251 // Splat r,g,b across a register each. |
238 auto r = Sk4f{linear_from_curve[(*src >> 0) & 0xFF]}, | 252 auto r = Sk4f{linear_from_curve[(*src >> 0) & 0xFF]}, |
239 g = Sk4f{linear_from_curve[(*src >> 8) & 0xFF]}, | 253 g = Sk4f{linear_from_curve[(*src >> 8) & 0xFF]}, |
240 b = Sk4f{linear_from_curve[(*src >> 16) & 0xFF]}; | 254 b = Sk4f{linear_from_curve[(*src >> 16) & 0xFF]}; |
241 | 255 |
242 // Apply transformation matrix to dst gamut. | 256 // Apply transformation matrix to dst gamut. |
243 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; | 257 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 } | 290 } |
277 | 291 |
278 static void color_xform_RGB1_2dot2_to_srgb(uint32_t* dst, const uint32_t* src, i nt len, | 292 static void color_xform_RGB1_2dot2_to_srgb(uint32_t* dst, const uint32_t* src, i nt len, |
279 const float matrix[16]) { | 293 const float matrix[16]) { |
280 color_xform_RGB1<linear_from_2dot2, linear_to_srgb>(dst, src, len, matrix); | 294 color_xform_RGB1<linear_from_2dot2, linear_to_srgb>(dst, src, len, matrix); |
281 } | 295 } |
282 | 296 |
283 } // namespace SK_OPTS_NS | 297 } // namespace SK_OPTS_NS |
284 | 298 |
285 #endif // SkColorXform_opts_DEFINED | 299 #endif // SkColorXform_opts_DEFINED |
OLD | NEW |