OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 #include "SkColorMatrixFilter.h" | 8 #include "SkColorMatrixFilter.h" |
9 #include "SkColorMatrix.h" | 9 #include "SkColorMatrix.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
11 #include "SkPMFloat.h" | 11 #include "SkNx.h" |
12 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
13 #include "SkWriteBuffer.h" | 13 #include "SkWriteBuffer.h" |
14 #include "SkUnPreMultiply.h" | 14 #include "SkUnPreMultiply.h" |
15 #include "SkString.h" | 15 #include "SkString.h" |
16 | 16 |
17 #define SK_PMORDER_INDEX_A (SK_A32_SHIFT / 8) | 17 #define SK_PMORDER_INDEX_A (SK_A32_SHIFT / 8) |
18 #define SK_PMORDER_INDEX_R (SK_R32_SHIFT / 8) | 18 #define SK_PMORDER_INDEX_R (SK_R32_SHIFT / 8) |
19 #define SK_PMORDER_INDEX_G (SK_G32_SHIFT / 8) | 19 #define SK_PMORDER_INDEX_G (SK_G32_SHIFT / 8) |
20 #define SK_PMORDER_INDEX_B (SK_B32_SHIFT / 8) | 20 #define SK_PMORDER_INDEX_B (SK_B32_SHIFT / 8) |
21 | 21 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 232 |
233 SkColorMatrixFilter::SkColorMatrixFilter(const SkScalar array[20]) { | 233 SkColorMatrixFilter::SkColorMatrixFilter(const SkScalar array[20]) { |
234 memcpy(fMatrix.fMat, array, 20 * sizeof(SkScalar)); | 234 memcpy(fMatrix.fMat, array, 20 * sizeof(SkScalar)); |
235 this->initState(array); | 235 this->initState(array); |
236 } | 236 } |
237 | 237 |
238 uint32_t SkColorMatrixFilter::getFlags() const { | 238 uint32_t SkColorMatrixFilter::getFlags() const { |
239 return this->INHERITED::getFlags() | fFlags; | 239 return this->INHERITED::getFlags() | fFlags; |
240 } | 240 } |
241 | 241 |
242 static Sk4f premul(const Sk4f& x) { | 242 static Sk4f scale_rgb(float scale) { |
243 float scale = SkPMFloat(x).a(); | 243 static_assert(SK_A32_SHIFT == 24, "Alpha is lane 3"); |
244 Sk4f pm = x * SkPMFloat(1, scale, scale, scale); | 244 return Sk4f(scale, scale, scale, 1); |
245 | |
246 #ifdef SK_DEBUG | |
247 SkPMFloat pmf(pm); | |
248 SkASSERT(pmf.isValid()); | |
249 #endif | |
250 | |
251 return pm; | |
252 } | 245 } |
253 | 246 |
254 static Sk4f unpremul(const SkPMFloat& pm) { | 247 static Sk4f premul(const Sk4f& x) { |
255 float scale = 1 / pm.a(); // candidate for fast/approx invert? | 248 return x * scale_rgb(x.kth<SK_A32_SHIFT/8>()); |
256 return pm * SkPMFloat(1, scale, scale, scale); | |
257 } | 249 } |
258 | 250 |
259 static Sk4f clamp_0_1(const Sk4f& value) { | 251 static Sk4f unpremul(const Sk4f& x) { |
260 return Sk4f::Max(Sk4f::Min(value, Sk4f(1)), Sk4f(0)); | 252 return x * scale_rgb(1 / x.kth<SK_A32_SHIFT/8>()); // TODO: fast/approx inv
ert? |
| 253 } |
| 254 |
| 255 static Sk4f clamp_0_1(const Sk4f& x) { |
| 256 return Sk4f::Max(Sk4f::Min(x, Sk4f(1)), Sk4f(0)); |
| 257 } |
| 258 |
| 259 static SkPMColor round(const Sk4f& x) { |
| 260 SkPMColor c; |
| 261 (x * Sk4f(255) + Sk4f(0.5f)).toBytes((uint8_t*)&c); |
| 262 return c; |
261 } | 263 } |
262 | 264 |
263 void SkColorMatrixFilter::filterSpan(const SkPMColor src[], int count, SkPMColor
dst[]) const { | 265 void SkColorMatrixFilter::filterSpan(const SkPMColor src[], int count, SkPMColor
dst[]) const { |
264 Proc proc = fProc; | 266 Proc proc = fProc; |
265 if (nullptr == proc) { | 267 if (nullptr == proc) { |
266 if (src != dst) { | 268 if (src != dst) { |
267 memcpy(dst, src, count * sizeof(SkPMColor)); | 269 memcpy(dst, src, count * sizeof(SkPMColor)); |
268 } | 270 } |
269 return; | 271 return; |
270 } | 272 } |
271 | 273 |
272 #ifdef SK_SUPPORT_LEGACY_INT_COLORMATRIX | 274 #ifdef SK_SUPPORT_LEGACY_INT_COLORMATRIX |
273 const bool use_floats = false; | 275 const bool use_floats = false; |
274 #else | 276 #else |
275 const bool use_floats = true; | 277 const bool use_floats = true; |
276 #endif | 278 #endif |
277 | 279 |
278 if (use_floats) { | 280 if (use_floats) { |
279 // c0-c3 are already in [0,1]. | 281 // c0-c3 are already in [0,1]. |
280 const Sk4f c0 = Sk4f::Load(fTranspose + 0); | 282 const Sk4f c0 = Sk4f::Load(fTranspose + 0); |
281 const Sk4f c1 = Sk4f::Load(fTranspose + 4); | 283 const Sk4f c1 = Sk4f::Load(fTranspose + 4); |
282 const Sk4f c2 = Sk4f::Load(fTranspose + 8); | 284 const Sk4f c2 = Sk4f::Load(fTranspose + 8); |
283 const Sk4f c3 = Sk4f::Load(fTranspose + 12); | 285 const Sk4f c3 = Sk4f::Load(fTranspose + 12); |
284 // c4 (the translate vector) is in [0, 255]. Bring it back to [0,1]. | 286 // c4 (the translate vector) is in [0, 255]. Bring it back to [0,1]. |
285 const Sk4f c4 = Sk4f::Load(fTranspose + 16)*Sk4f(1.0f/255); | 287 const Sk4f c4 = Sk4f::Load(fTranspose + 16)*Sk4f(1.0f/255); |
286 | 288 |
287 // todo: we could cache this in the constructor... | 289 // todo: we could cache this in the constructor... |
288 SkPMColor matrix_translate_pmcolor = SkPMFloat(premul(clamp_0_1(c4))).ro
und(); | 290 SkPMColor matrix_translate_pmcolor = round(premul(clamp_0_1(c4))); |
289 | 291 |
290 for (int i = 0; i < count; i++) { | 292 for (int i = 0; i < count; i++) { |
291 const SkPMColor src_c = src[i]; | 293 const SkPMColor src_c = src[i]; |
292 if (0 == src_c) { | 294 if (0 == src_c) { |
293 dst[i] = matrix_translate_pmcolor; | 295 dst[i] = matrix_translate_pmcolor; |
294 continue; | 296 continue; |
295 } | 297 } |
296 | 298 |
297 SkPMFloat srcf(src_c); | 299 Sk4f srcf = Sk4f::FromBytes((const uint8_t*)&src_c) * Sk4f(1.0f/255)
; |
298 | 300 |
299 if (0xFF != SkGetPackedA32(src_c)) { | 301 if (0xFF != SkGetPackedA32(src_c)) { |
300 srcf = unpremul(srcf); | 302 srcf = unpremul(srcf); |
301 } | 303 } |
302 | 304 |
303 Sk4f r4 = Sk4f(srcf.r()); | 305 Sk4f r4 = Sk4f(srcf.kth<SK_R32_SHIFT/8>()); |
304 Sk4f g4 = Sk4f(srcf.g()); | 306 Sk4f g4 = Sk4f(srcf.kth<SK_G32_SHIFT/8>()); |
305 Sk4f b4 = Sk4f(srcf.b()); | 307 Sk4f b4 = Sk4f(srcf.kth<SK_B32_SHIFT/8>()); |
306 Sk4f a4 = Sk4f(srcf.a()); | 308 Sk4f a4 = Sk4f(srcf.kth<SK_A32_SHIFT/8>()); |
307 | 309 |
308 // apply matrix | 310 // apply matrix |
309 Sk4f dst4 = c0 * r4 + c1 * g4 + c2 * b4 + c3 * a4 + c4; | 311 Sk4f dst4 = c0 * r4 + c1 * g4 + c2 * b4 + c3 * a4 + c4; |
310 | 312 |
311 // clamp, re-premul, and write | 313 // clamp, re-premul, and write |
312 dst[i] = SkPMFloat(premul(clamp_0_1(dst4))).round(); | 314 dst[i] = round(premul(clamp_0_1(dst4))); |
313 } | 315 } |
314 } else { | 316 } else { |
315 const State& state = fState; | 317 const State& state = fState; |
316 int32_t result[4]; | 318 int32_t result[4]; |
317 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); | 319 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); |
318 | 320 |
319 for (int i = 0; i < count; i++) { | 321 for (int i = 0; i < count; i++) { |
320 SkPMColor c = src[i]; | 322 SkPMColor c = src[i]; |
321 | 323 |
322 unsigned r = SkGetPackedR32(c); | 324 unsigned r = SkGetPackedR32(c); |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 str->append("matrix: ("); | 560 str->append("matrix: ("); |
559 for (int i = 0; i < 20; ++i) { | 561 for (int i = 0; i < 20; ++i) { |
560 str->appendScalar(fMatrix.fMat[i]); | 562 str->appendScalar(fMatrix.fMat[i]); |
561 if (i < 19) { | 563 if (i < 19) { |
562 str->append(", "); | 564 str->append(", "); |
563 } | 565 } |
564 } | 566 } |
565 str->append(")"); | 567 str->append(")"); |
566 } | 568 } |
567 #endif | 569 #endif |
OLD | NEW |