Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | #include "fiddle/examples.h" |
| 4 | // HASH=092739b4cd5d732a27c07ced8ef45f01 |
Hal Canary | a7181e7c | 2019-03-18 16:06:34 -0400 | [diff] [blame] | 5 | REG_FIDDLE(Bitmap_extractAlpha_2, 256, 160, false, 0) { |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 6 | void draw(SkCanvas* canvas) { |
| 7 | auto radiusToSigma = [](SkScalar radius) -> SkScalar { |
| 8 | static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f; |
| 9 | return radius > 0 ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f; |
| 10 | }; |
| 11 | SkBitmap alpha, bitmap; |
| 12 | bitmap.allocN32Pixels(100, 100); |
| 13 | SkCanvas offscreen(bitmap); |
| 14 | offscreen.clear(0); |
| 15 | SkPaint paint; |
| 16 | paint.setAntiAlias(true); |
| 17 | paint.setColor(SK_ColorBLUE); |
| 18 | paint.setStyle(SkPaint::kStroke_Style); |
| 19 | paint.setStrokeWidth(20); |
| 20 | offscreen.drawCircle(50, 50, 39, paint); |
| 21 | offscreen.flush(); |
| 22 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, radiusToSigma(25))); |
| 23 | SkIPoint offset; |
| 24 | bitmap.extractAlpha(&alpha, &paint, &offset); |
| 25 | paint.setColor(SK_ColorRED); |
| 26 | canvas->drawBitmap(bitmap, 0, -offset.fY, &paint); |
| 27 | canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint); |
| 28 | } |
| 29 | } // END FIDDLE |