blob: 88a6d10d9639ca9571c49680cf1418ab727cefaa [file] [log] [blame]
bsalomon@google.com82aa7482012-08-13 14:22:17 +00001/*
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"
robertphillipsb2a4dc62016-04-14 07:54:04 -07009#include "SkImageSource.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000010#include "SkMagnifierImageFilter.h"
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000011#include "SkRandom.h"
robertphillipsb2a4dc62016-04-14 07:54:04 -070012#include "SkSurface.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000013
14#define WIDTH 500
15#define HEIGHT 500
16
halcanary2a243382015-09-09 08:16:41 -070017DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
senorblancocbf6b6e2014-10-23 15:00:10 -070018 SkPaint filterPaint;
19 filterPaint.setImageFilter(
robertphillips11171f32016-04-07 07:34:15 -070020 SkMagnifierImageFilter::Make(
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000021 SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
bsalomon@google.com82aa7482012-08-13 14:22:17 +000022 SkIntToScalar(WIDTH / 2),
23 SkIntToScalar(HEIGHT / 2)),
robertphillips11171f32016-04-07 07:34:15 -070024 100, nullptr));
halcanary96fcdcc2015-08-27 07:41:13 -070025 canvas->saveLayer(nullptr, &filterPaint);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000026 const char* str = "The quick brown fox jumped over the lazy dog.";
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000027 SkRandom rand;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000028 for (int i = 0; i < 25; ++i) {
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000029 int x = rand.nextULessThan(WIDTH);
30 int y = rand.nextULessThan(HEIGHT);
senorblancocbf6b6e2014-10-23 15:00:10 -070031 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070032 sk_tool_utils::set_portable_typeface(&paint);
caryclarkd2ce1852015-07-16 12:35:58 -070033 paint.setColor(sk_tool_utils::color_to_565(rand.nextBits(24) | 0xFF000000));
mtklein@google.com26c6d582013-09-16 19:05:44 +000034 paint.setTextSize(rand.nextRangeScalar(0, 300));
senorblancocbf6b6e2014-10-23 15:00:10 -070035 paint.setAntiAlias(true);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000036 canvas->drawText(str, strlen(str), SkIntToScalar(x),
37 SkIntToScalar(y), paint);
38 }
39 canvas->restore();
bsalomon@google.com82aa7482012-08-13 14:22:17 +000040}
robertphillipsb2a4dc62016-04-14 07:54:04 -070041
42////////////////////////////////////////////////////////////////////////////////
43#define WIDTH_HEIGHT 256
44
45static sk_sp<SkImage> make_img() {
46 const SkImageInfo info = SkImageInfo::MakeN32Premul(WIDTH_HEIGHT, WIDTH_HEIGHT);
47
48 sk_sp<SkSurface> surf(SkSurface::MakeRaster(info));
49
50 SkCanvas* canvas = surf->getCanvas();
51
52 canvas->clear(0x0);
53
54 SkPaint paint;
55 paint.setColor(SK_ColorBLUE);
56
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);
60 }
61
62 return surf->makeImageSnapshot();
63}
64
65DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
66
67 sk_sp<SkImage> image(make_img());
68
69 sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image)));
70
71 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
72 SkIntToScalar(WIDTH_HEIGHT-32));
73 srcRect.inset(64.0f, 64.0f);
74
mtkleindbfd7ab2016-09-01 11:24:54 -070075 constexpr SkScalar kInset = 64.0f;
robertphillipsb2a4dc62016-04-14 07:54:04 -070076
77 // Crop out a 16 pixel ring around the result
78 const SkRect rect = SkRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
79 SkImageFilter::CropRect cropRect(rect);
80
81 SkPaint filterPaint;
82 filterPaint.setImageFilter(SkMagnifierImageFilter::Make(srcRect, kInset,
83 std::move(imageSource),
84 &cropRect));
85
86 canvas->saveLayer(nullptr, &filterPaint);
87 canvas->restore();
88}