OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkAlphaThresholdFilter.h" | 8 #include "SkAlphaThresholdFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkImagePriv.h" |
10 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
11 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
12 #include "SkRegion.h" | 13 #include "SkRegion.h" |
13 | 14 |
14 class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter { | 15 class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter { |
15 public: | 16 public: |
16 SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold, | 17 SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold, |
17 SkScalar outerThreshold, SkImageFilter* input); | 18 SkScalar outerThreshold, SkImageFilter* input); |
18 | 19 |
19 SK_TO_STRING_OVERRIDE() | 20 SK_TO_STRING_OVERRIDE() |
20 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterIm
pl) | 21 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterIm
pl) |
21 | 22 |
22 protected: | 23 protected: |
23 void flatten(SkWriteBuffer&) const SK_OVERRIDE; | 24 void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
24 | 25 |
25 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, | 26 virtual bool onFilterImage(Proxy*, const SkImage* src, const Context&, |
26 SkBitmap* result, SkIPoint* offset) const SK_OVER
RIDE; | 27 SkAutoTUnref<const SkImage>& result, |
| 28 SkIPoint* offset) const SK_OVERRIDE; |
27 #if SK_SUPPORT_GPU | 29 #if SK_SUPPORT_GPU |
28 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const Sk
Matrix&, | 30 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const Sk
Matrix&, |
29 const SkIRect& bounds) const SK_OVERRIDE; | 31 const SkIRect& bounds) const SK_OVERRIDE; |
30 #endif | 32 #endif |
31 | 33 |
32 private: | 34 private: |
33 SkRegion fRegion; | 35 SkRegion fRegion; |
34 SkScalar fInnerThreshold; | 36 SkScalar fInnerThreshold; |
35 SkScalar fOuterThreshold; | 37 SkScalar fOuterThreshold; |
36 typedef SkImageFilter INHERITED; | 38 typedef SkImageFilter INHERITED; |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 } | 308 } |
307 #endif | 309 #endif |
308 | 310 |
309 void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const { | 311 void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const { |
310 this->INHERITED::flatten(buffer); | 312 this->INHERITED::flatten(buffer); |
311 buffer.writeScalar(fInnerThreshold); | 313 buffer.writeScalar(fInnerThreshold); |
312 buffer.writeScalar(fOuterThreshold); | 314 buffer.writeScalar(fOuterThreshold); |
313 buffer.writeRegion(fRegion); | 315 buffer.writeRegion(fRegion); |
314 } | 316 } |
315 | 317 |
316 bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src, | 318 bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkImage* src, |
317 const Context& ctx, SkBitmap* dst
, | 319 const Context& ctx, SkAutoTUnref<
const SkImage>& dst, |
318 SkIPoint* offset) const { | 320 SkIPoint* offset) const { |
319 SkASSERT(src.colorType() == kN32_SkColorType); | |
320 | |
321 if (src.colorType() != kN32_SkColorType) { | |
322 return false; | |
323 } | |
324 | |
325 SkMatrix localInverse; | 321 SkMatrix localInverse; |
326 if (!ctx.ctm().invert(&localInverse)) { | 322 if (!ctx.ctm().invert(&localInverse)) { |
327 return false; | 323 return false; |
328 } | 324 } |
329 | 325 |
330 SkAutoLockPixels alp(src); | 326 SkBitmap srcBitmap; |
331 SkASSERT(src.getPixels()); | 327 SkAutoImageAsN32Bitmap aai(src, &srcBitmap); |
332 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) { | 328 if (!srcBitmap.getPixels() || srcBitmap.width() <= 0 || srcBitmap.height() <
= 0) { |
333 return false; | 329 return false; |
334 } | 330 } |
335 | 331 |
336 if (!dst->tryAllocPixels(src.info())) { | 332 SkBitmap dstBitmap; |
| 333 if (!dstBitmap.tryAllocPixels(srcBitmap.info())) { |
337 return false; | 334 return false; |
338 } | 335 } |
339 | 336 |
340 U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF); | 337 U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF); |
341 U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF); | 338 U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF); |
342 SkColor* sptr = src.getAddr32(0, 0); | 339 SkColor* sptr = srcBitmap.getAddr32(0, 0); |
343 SkColor* dptr = dst->getAddr32(0, 0); | 340 SkColor* dptr = dstBitmap.getAddr32(0, 0); |
344 int width = src.width(), height = src.height(); | 341 int width = srcBitmap.width(), height = srcBitmap.height(); |
345 for (int y = 0; y < height; ++y) { | 342 for (int y = 0; y < height; ++y) { |
346 for (int x = 0; x < width; ++x) { | 343 for (int x = 0; x < width; ++x) { |
347 const SkColor& source = sptr[y * width + x]; | 344 const SkColor& source = sptr[y * width + x]; |
348 SkColor output_color(source); | 345 SkColor output_color(source); |
349 SkPoint position; | 346 SkPoint position; |
350 localInverse.mapXY((SkScalar)x, (SkScalar)y, &position); | 347 localInverse.mapXY((SkScalar)x, (SkScalar)y, &position); |
351 if (fRegion.contains((int32_t)position.x(), (int32_t)position.y()))
{ | 348 if (fRegion.contains((int32_t)position.x(), (int32_t)position.y()))
{ |
352 if (SkColorGetA(source) < innerThreshold) { | 349 if (SkColorGetA(source) < innerThreshold) { |
353 U8CPU alpha = SkColorGetA(source); | 350 U8CPU alpha = SkColorGetA(source); |
354 if (alpha == 0) | 351 if (alpha == 0) |
355 alpha = 1; | 352 alpha = 1; |
356 float scale = (float)innerThreshold / alpha; | 353 float scale = (float)innerThreshold / alpha; |
357 output_color = SkColorSetARGB(innerThreshold, | 354 output_color = SkColorSetARGB(innerThreshold, |
358 (U8CPU)(SkColorGetR(source) *
scale), | 355 (U8CPU)(SkColorGetR(source) *
scale), |
359 (U8CPU)(SkColorGetG(source) *
scale), | 356 (U8CPU)(SkColorGetG(source) *
scale), |
360 (U8CPU)(SkColorGetB(source) *
scale)); | 357 (U8CPU)(SkColorGetB(source) *
scale)); |
361 } | 358 } |
362 } else { | 359 } else { |
363 if (SkColorGetA(source) > outerThreshold) { | 360 if (SkColorGetA(source) > outerThreshold) { |
364 float scale = (float)outerThreshold / SkColorGetA(source); | 361 float scale = (float)outerThreshold / SkColorGetA(source); |
365 output_color = SkColorSetARGB(outerThreshold, | 362 output_color = SkColorSetARGB(outerThreshold, |
366 (U8CPU)(SkColorGetR(source) *
scale), | 363 (U8CPU)(SkColorGetR(source) *
scale), |
367 (U8CPU)(SkColorGetG(source) *
scale), | 364 (U8CPU)(SkColorGetG(source) *
scale), |
368 (U8CPU)(SkColorGetB(source) *
scale)); | 365 (U8CPU)(SkColorGetB(source) *
scale)); |
369 } | 366 } |
370 } | 367 } |
371 dptr[y * dst->width() + x] = output_color; | 368 dptr[y * dstBitmap.width() + x] = output_color; |
372 } | 369 } |
373 } | 370 } |
| 371 srcBitmap = SkBitmap(); |
374 | 372 |
| 373 SkImage* image = SkNewImageFromBitmap(dstBitmap, NULL); |
| 374 if (NULL == image) { |
| 375 return false; |
| 376 } |
| 377 dst.reset(image); |
| 378 offset->fX = 0; |
| 379 offset->fY = 0; |
375 return true; | 380 return true; |
376 } | 381 } |
377 | 382 |
378 #ifndef SK_IGNORE_TO_STRING | 383 #ifndef SK_IGNORE_TO_STRING |
379 void SkAlphaThresholdFilterImpl::toString(SkString* str) const { | 384 void SkAlphaThresholdFilterImpl::toString(SkString* str) const { |
380 str->appendf("SkAlphaThresholdImageFilter: ("); | 385 str->appendf("SkAlphaThresholdImageFilter: ("); |
381 str->appendf("inner: %f outer: %f", fInnerThreshold, fOuterThreshold); | 386 str->appendf("inner: %f outer: %f", fInnerThreshold, fOuterThreshold); |
382 str->append(")"); | 387 str->append(")"); |
383 } | 388 } |
384 #endif | 389 #endif |
385 | 390 |
OLD | NEW |