2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
10 #include "SkImageSource.h"
11 #include "SkMagnifierImageFilter.h"
12 #include "SkPixelRef.h"
14 #include "SkSurface.h"
19 DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
21 filterPaint.setImageFilter(
22 SkMagnifierImageFilter::Make(
23 SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
24 SkIntToScalar(WIDTH / 2),
25 SkIntToScalar(HEIGHT / 2)),
27 canvas->saveLayer(nullptr, &filterPaint);
28 const char* str = "The quick brown fox jumped over the lazy dog.";
30 for (int i = 0; i < 25; ++i) {
31 int x = rand.nextULessThan(WIDTH);
32 int y = rand.nextULessThan(HEIGHT);
34 sk_tool_utils::set_portable_typeface(&paint);
35 paint.setColor(sk_tool_utils::color_to_565(rand.nextBits(24) | 0xFF000000));
36 paint.setTextSize(rand.nextRangeScalar(0, 300));
37 paint.setAntiAlias(true);
38 canvas->drawString(str, SkIntToScalar(x),
39 SkIntToScalar(y), paint);
44 ////////////////////////////////////////////////////////////////////////////////
45 #define WIDTH_HEIGHT 256
47 static sk_sp<SkImage> make_img() {
49 bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
50 SkCanvas canvas(bitmap);
55 paint.setColor(SK_ColorBLUE);
57 for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
58 canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
59 canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
63 result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType));
64 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
66 return SkImage::MakeFromBitmap(result);
69 DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
71 sk_sp<SkImage> image(make_img());
73 sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image)));
75 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
76 SkIntToScalar(WIDTH_HEIGHT-32));
77 srcRect.inset(64.0f, 64.0f);
79 constexpr SkScalar kInset = 64.0f;
81 // Crop out a 16 pixel ring around the result
82 const SkRect rect = SkRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
83 SkImageFilter::CropRect cropRect(rect);
86 filterPaint.setImageFilter(SkMagnifierImageFilter::Make(srcRect, kInset,
87 std::move(imageSource),
90 canvas->saveLayer(nullptr, &filterPaint);