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.h"
9 #include "sk_tool_utils.h"
10 #include "SkImageSource.h"
11 #include "SkMagnifierImageFilter.h"
12 #include "SkPixelRef.h"
13 #include "SkRandom.h"
14 #include "SkSurface.h"
15 
16 #define WIDTH 500
17 #define HEIGHT 500
18 
DEF_SIMPLE_GM_BG(imagemagnifier,canvas,WIDTH,HEIGHT,SK_ColorBLACK)19 DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
20         SkPaint filterPaint;
21         filterPaint.setImageFilter(
22             SkMagnifierImageFilter::Make(
23                 SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
24                                  SkIntToScalar(WIDTH / 2),
25                                  SkIntToScalar(HEIGHT / 2)),
26                 100, nullptr));
27         canvas->saveLayer(nullptr, &filterPaint);
28         const char* str = "The quick brown fox jumped over the lazy dog.";
29         SkRandom rand;
30         SkFont font(sk_tool_utils::create_portable_typeface());
31         for (int i = 0; i < 25; ++i) {
32             int x = rand.nextULessThan(WIDTH);
33             int y = rand.nextULessThan(HEIGHT);
34             SkPaint paint;
35             paint.setColor(sk_tool_utils::color_to_565(rand.nextBits(24) | 0xFF000000));
36             font.setSize(rand.nextRangeScalar(0, 300));
37             canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint);
38         }
39         canvas->restore();
40 }
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 #define WIDTH_HEIGHT 256
44 
make_img()45 static sk_sp<SkImage> make_img() {
46     SkBitmap bitmap;
47     bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
48     SkCanvas canvas(bitmap);
49 
50     canvas.clear(0x0);
51 
52     SkPaint paint;
53     paint.setColor(SK_ColorBLUE);
54 
55     for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
56         canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
57         canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
58     }
59 
60     SkBitmap result;
61     result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType));
62     result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
63 
64     return SkImage::MakeFromBitmap(result);
65 }
66 
DEF_SIMPLE_GM_BG(imagemagnifier_cropped,canvas,WIDTH_HEIGHT,WIDTH_HEIGHT,SK_ColorBLACK)67 DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
68 
69     sk_sp<SkImage> image(make_img());
70 
71     sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image)));
72 
73     SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
74                                     SkIntToScalar(WIDTH_HEIGHT-32));
75     srcRect.inset(64.0f, 64.0f);
76 
77     constexpr SkScalar kInset = 64.0f;
78 
79     // Crop out a 16 pixel ring around the result
80     const SkRect rect = SkRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
81     SkImageFilter::CropRect cropRect(rect);
82 
83     SkPaint filterPaint;
84     filterPaint.setImageFilter(SkMagnifierImageFilter::Make(srcRect, kInset,
85                                                             std::move(imageSource),
86                                                             &cropRect));
87 
88     canvas->saveLayer(nullptr, &filterPaint);
89     canvas->restore();
90 }
91