1 /*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPixelRef.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkTypeface.h"
22 #include "include/effects/SkImageFilters.h"
23 #include "include/utils/SkRandom.h"
24 #include "tools/ToolUtils.h"
25
26 #include <utility>
27
28 #define WIDTH 500
29 #define HEIGHT 500
30
DEF_SIMPLE_GM_BG(imagemagnifier,canvas,WIDTH,HEIGHT,SK_ColorBLACK)31 DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
32 SkPaint filterPaint;
33 filterPaint.setImageFilter(
34 SkImageFilters::Magnifier(
35 SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
36 SkIntToScalar(WIDTH / 2),
37 SkIntToScalar(HEIGHT / 2)),
38 100, nullptr));
39 canvas->saveLayer(nullptr, &filterPaint);
40 const char* str = "The quick brown fox jumped over the lazy dog.";
41 SkRandom rand;
42 SkFont font(ToolUtils::create_portable_typeface());
43 for (int i = 0; i < 25; ++i) {
44 int x = rand.nextULessThan(WIDTH);
45 int y = rand.nextULessThan(HEIGHT);
46 SkPaint paint;
47 paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000));
48 font.setSize(rand.nextRangeScalar(0, 300));
49 canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint);
50 }
51 canvas->restore();
52 }
53
54 ////////////////////////////////////////////////////////////////////////////////
55 #define WIDTH_HEIGHT 256
56
make_img()57 static sk_sp<SkImage> make_img() {
58 SkBitmap bitmap;
59 bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
60 SkCanvas canvas(bitmap);
61
62 canvas.clear(0x0);
63
64 SkPaint paint;
65 paint.setColor(SK_ColorBLUE);
66
67 for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
68 canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
69 canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
70 }
71
72 SkBitmap result;
73 result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType));
74 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
75
76 return result.asImage();
77 }
78
DEF_SIMPLE_GM_BG(imagemagnifier_cropped,canvas,WIDTH_HEIGHT,WIDTH_HEIGHT,SK_ColorBLACK)79 DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
80
81 sk_sp<SkImage> image(make_img());
82
83 sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(std::move(image)));
84
85 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
86 SkIntToScalar(WIDTH_HEIGHT-32));
87 srcRect.inset(64.0f, 64.0f);
88
89 constexpr SkScalar kInset = 64.0f;
90
91 // Crop out a 16 pixel ring around the result
92 const SkIRect cropRect = SkIRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
93
94 SkPaint filterPaint;
95 filterPaint.setImageFilter(SkImageFilters::Magnifier(
96 srcRect, kInset, std::move(imageSource), &cropRect));
97
98 canvas->saveLayer(nullptr, &filterPaint);
99 canvas->restore();
100 }
101