blob: 2585ea763c388dbf27eeca2c0dc73f86fdfa6ce7 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
robertphillipsb2a4dc62016-04-14 07:54:04 -070010#include "SkImageSource.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000011#include "SkMagnifierImageFilter.h"
Brian Osmanb44bb312016-12-20 15:54:11 -050012#include "SkPixelRef.h"
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000013#include "SkRandom.h"
robertphillipsb2a4dc62016-04-14 07:54:04 -070014#include "SkSurface.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000015
16#define WIDTH 500
17#define HEIGHT 500
18
halcanary2a243382015-09-09 08:16:41 -070019DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
senorblancocbf6b6e2014-10-23 15:00:10 -070020 SkPaint filterPaint;
21 filterPaint.setImageFilter(
robertphillips11171f32016-04-07 07:34:15 -070022 SkMagnifierImageFilter::Make(
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000023 SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
bsalomon@google.com82aa7482012-08-13 14:22:17 +000024 SkIntToScalar(WIDTH / 2),
25 SkIntToScalar(HEIGHT / 2)),
robertphillips11171f32016-04-07 07:34:15 -070026 100, nullptr));
halcanary96fcdcc2015-08-27 07:41:13 -070027 canvas->saveLayer(nullptr, &filterPaint);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000028 const char* str = "The quick brown fox jumped over the lazy dog.";
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000029 SkRandom rand;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000030 for (int i = 0; i < 25; ++i) {
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000031 int x = rand.nextULessThan(WIDTH);
32 int y = rand.nextULessThan(HEIGHT);
senorblancocbf6b6e2014-10-23 15:00:10 -070033 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070034 sk_tool_utils::set_portable_typeface(&paint);
caryclarkd2ce1852015-07-16 12:35:58 -070035 paint.setColor(sk_tool_utils::color_to_565(rand.nextBits(24) | 0xFF000000));
mtklein@google.com26c6d582013-09-16 19:05:44 +000036 paint.setTextSize(rand.nextRangeScalar(0, 300));
senorblancocbf6b6e2014-10-23 15:00:10 -070037 paint.setAntiAlias(true);
Cary Clark2a475ea2017-04-28 15:35:12 -040038 canvas->drawString(str, SkIntToScalar(x),
bsalomon@google.com82aa7482012-08-13 14:22:17 +000039 SkIntToScalar(y), paint);
40 }
41 canvas->restore();
bsalomon@google.com82aa7482012-08-13 14:22:17 +000042}
robertphillipsb2a4dc62016-04-14 07:54:04 -070043
44////////////////////////////////////////////////////////////////////////////////
45#define WIDTH_HEIGHT 256
46
47static sk_sp<SkImage> make_img() {
Brian Osmanb44bb312016-12-20 15:54:11 -050048 SkBitmap bitmap;
49 bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
50 SkCanvas canvas(bitmap);
robertphillipsb2a4dc62016-04-14 07:54:04 -070051
Brian Osmanb44bb312016-12-20 15:54:11 -050052 canvas.clear(0x0);
robertphillipsb2a4dc62016-04-14 07:54:04 -070053
54 SkPaint paint;
55 paint.setColor(SK_ColorBLUE);
56
57 for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
Brian Osmanb44bb312016-12-20 15:54:11 -050058 canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
59 canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
robertphillipsb2a4dc62016-04-14 07:54:04 -070060 }
61
Brian Osmanb44bb312016-12-20 15:54:11 -050062 SkBitmap result;
63 result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType));
64 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
65
66 return SkImage::MakeFromBitmap(result);
robertphillipsb2a4dc62016-04-14 07:54:04 -070067}
68
69DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
70
71 sk_sp<SkImage> image(make_img());
72
73 sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image)));
74
75 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
76 SkIntToScalar(WIDTH_HEIGHT-32));
77 srcRect.inset(64.0f, 64.0f);
78
mtkleindbfd7ab2016-09-01 11:24:54 -070079 constexpr SkScalar kInset = 64.0f;
robertphillipsb2a4dc62016-04-14 07:54:04 -070080
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);
84
85 SkPaint filterPaint;
86 filterPaint.setImageFilter(SkMagnifierImageFilter::Make(srcRect, kInset,
87 std::move(imageSource),
88 &cropRect));
89
90 canvas->saveLayer(nullptr, &filterPaint);
91 canvas->restore();
92}