blob: 1c7fad1398b922dfdf6efce79c4e17b784689a0a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkPixelRef.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkRect.h"
19#include "include/core/SkRefCnt.h"
20#include "include/core/SkScalar.h"
21#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/effects/SkImageSource.h"
23#include "include/effects/SkMagnifierImageFilter.h"
24#include "include/utils/SkRandom.h"
25#include "tools/ToolUtils.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000026
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include <utility>
28
bsalomon@google.com82aa7482012-08-13 14:22:17 +000029#define WIDTH 500
30#define HEIGHT 500
31
halcanary2a243382015-09-09 08:16:41 -070032DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
senorblancocbf6b6e2014-10-23 15:00:10 -070033 SkPaint filterPaint;
34 filterPaint.setImageFilter(
robertphillips11171f32016-04-07 07:34:15 -070035 SkMagnifierImageFilter::Make(
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000036 SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
bsalomon@google.com82aa7482012-08-13 14:22:17 +000037 SkIntToScalar(WIDTH / 2),
38 SkIntToScalar(HEIGHT / 2)),
robertphillips11171f32016-04-07 07:34:15 -070039 100, nullptr));
halcanary96fcdcc2015-08-27 07:41:13 -070040 canvas->saveLayer(nullptr, &filterPaint);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000041 const char* str = "The quick brown fox jumped over the lazy dog.";
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000042 SkRandom rand;
Mike Kleinea3f0142019-03-20 11:12:10 -050043 SkFont font(ToolUtils::create_portable_typeface());
bsalomon@google.com82aa7482012-08-13 14:22:17 +000044 for (int i = 0; i < 25; ++i) {
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000045 int x = rand.nextULessThan(WIDTH);
46 int y = rand.nextULessThan(HEIGHT);
senorblancocbf6b6e2014-10-23 15:00:10 -070047 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -050048 paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000));
Hal Canary6ac0df82019-01-07 16:01:22 -050049 font.setSize(rand.nextRangeScalar(0, 300));
50 canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000051 }
52 canvas->restore();
bsalomon@google.com82aa7482012-08-13 14:22:17 +000053}
robertphillipsb2a4dc62016-04-14 07:54:04 -070054
55////////////////////////////////////////////////////////////////////////////////
56#define WIDTH_HEIGHT 256
57
58static sk_sp<SkImage> make_img() {
Brian Osmanb44bb312016-12-20 15:54:11 -050059 SkBitmap bitmap;
60 bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
61 SkCanvas canvas(bitmap);
robertphillipsb2a4dc62016-04-14 07:54:04 -070062
Brian Osmanb44bb312016-12-20 15:54:11 -050063 canvas.clear(0x0);
robertphillipsb2a4dc62016-04-14 07:54:04 -070064
65 SkPaint paint;
66 paint.setColor(SK_ColorBLUE);
67
68 for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
Brian Osmanb44bb312016-12-20 15:54:11 -050069 canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
70 canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
robertphillipsb2a4dc62016-04-14 07:54:04 -070071 }
72
Brian Osmanb44bb312016-12-20 15:54:11 -050073 SkBitmap result;
74 result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType));
75 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
76
77 return SkImage::MakeFromBitmap(result);
robertphillipsb2a4dc62016-04-14 07:54:04 -070078}
79
80DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
81
82 sk_sp<SkImage> image(make_img());
83
84 sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image)));
85
86 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
87 SkIntToScalar(WIDTH_HEIGHT-32));
88 srcRect.inset(64.0f, 64.0f);
89
mtkleindbfd7ab2016-09-01 11:24:54 -070090 constexpr SkScalar kInset = 64.0f;
robertphillipsb2a4dc62016-04-14 07:54:04 -070091
92 // Crop out a 16 pixel ring around the result
93 const SkRect rect = SkRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
94 SkImageFilter::CropRect cropRect(rect);
95
96 SkPaint filterPaint;
97 filterPaint.setImageFilter(SkMagnifierImageFilter::Make(srcRect, kInset,
98 std::move(imageSource),
99 &cropRect));
100
101 canvas->saveLayer(nullptr, &filterPaint);
102 canvas->restore();
103}