xref: /aosp_15_r20/external/skia/docs/examples/Bitmap_extractAlpha_2.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "tools/fiddle/examples.h"
4 REG_FIDDLE(Bitmap_extractAlpha_2, 256, 160, false, 0) {
draw(SkCanvas * canvas)5 void draw(SkCanvas* canvas) {
6     auto radiusToSigma = [](SkScalar radius) -> SkScalar {
7          static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f;
8          return radius > 0 ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
9     };
10     SkBitmap alpha, bitmap;
11     bitmap.allocN32Pixels(100, 100);
12     SkCanvas offscreen(bitmap);
13     offscreen.clear(0);
14     SkPaint paint;
15     paint.setAntiAlias(true);
16     paint.setColor(SK_ColorBLUE);
17     paint.setStyle(SkPaint::kStroke_Style);
18     paint.setStrokeWidth(20);
19     offscreen.drawCircle(50, 50, 39, paint);
20     paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, radiusToSigma(25)));
21     SkIPoint offset;
22     bitmap.extractAlpha(&alpha, &paint, &offset);
23     paint.setColor(SK_ColorRED);
24     canvas->drawImage(bitmap.asImage(), 0, -offset.fY, SkSamplingOptions(), &paint);
25     canvas->drawImage(alpha.asImage(), 100 + offset.fX, 0, SkSamplingOptions(), &paint);
26 }
27 }  // END FIDDLE
28