blob: b61519bf300b63307ad8242f372fcb7e77816cf6 [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"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040022#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/utils/SkRandom.h"
24#include "tools/ToolUtils.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000025
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include <utility>
27
bsalomon@google.com82aa7482012-08-13 14:22:17 +000028#define WIDTH 500
29#define HEIGHT 500
30
halcanary2a243382015-09-09 08:16:41 -070031DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
senorblancocbf6b6e2014-10-23 15:00:10 -070032 SkPaint filterPaint;
33 filterPaint.setImageFilter(
Michael Ludwig898bbfa2019-08-02 15:21:23 -040034 SkImageFilters::Magnifier(
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000035 SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
bsalomon@google.com82aa7482012-08-13 14:22:17 +000036 SkIntToScalar(WIDTH / 2),
37 SkIntToScalar(HEIGHT / 2)),
robertphillips11171f32016-04-07 07:34:15 -070038 100, nullptr));
halcanary96fcdcc2015-08-27 07:41:13 -070039 canvas->saveLayer(nullptr, &filterPaint);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000040 const char* str = "The quick brown fox jumped over the lazy dog.";
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000041 SkRandom rand;
Mike Kleinea3f0142019-03-20 11:12:10 -050042 SkFont font(ToolUtils::create_portable_typeface());
bsalomon@google.com82aa7482012-08-13 14:22:17 +000043 for (int i = 0; i < 25; ++i) {
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000044 int x = rand.nextULessThan(WIDTH);
45 int y = rand.nextULessThan(HEIGHT);
senorblancocbf6b6e2014-10-23 15:00:10 -070046 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -050047 paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000));
Hal Canary6ac0df82019-01-07 16:01:22 -050048 font.setSize(rand.nextRangeScalar(0, 300));
49 canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000050 }
51 canvas->restore();
bsalomon@google.com82aa7482012-08-13 14:22:17 +000052}
robertphillipsb2a4dc62016-04-14 07:54:04 -070053
54////////////////////////////////////////////////////////////////////////////////
55#define WIDTH_HEIGHT 256
56
57static sk_sp<SkImage> make_img() {
Brian Osmanb44bb312016-12-20 15:54:11 -050058 SkBitmap bitmap;
59 bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
60 SkCanvas canvas(bitmap);
robertphillipsb2a4dc62016-04-14 07:54:04 -070061
Brian Osmanb44bb312016-12-20 15:54:11 -050062 canvas.clear(0x0);
robertphillipsb2a4dc62016-04-14 07:54:04 -070063
64 SkPaint paint;
65 paint.setColor(SK_ColorBLUE);
66
67 for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
Brian Osmanb44bb312016-12-20 15:54:11 -050068 canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
69 canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
robertphillipsb2a4dc62016-04-14 07:54:04 -070070 }
71
Brian Osmanb44bb312016-12-20 15:54:11 -050072 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 SkImage::MakeFromBitmap(result);
robertphillipsb2a4dc62016-04-14 07:54:04 -070077}
78
79DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
80
81 sk_sp<SkImage> image(make_img());
82
Michael Ludwig898bbfa2019-08-02 15:21:23 -040083 sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(std::move(image)));
robertphillipsb2a4dc62016-04-14 07:54:04 -070084
85 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
86 SkIntToScalar(WIDTH_HEIGHT-32));
87 srcRect.inset(64.0f, 64.0f);
88
mtkleindbfd7ab2016-09-01 11:24:54 -070089 constexpr SkScalar kInset = 64.0f;
robertphillipsb2a4dc62016-04-14 07:54:04 -070090
91 // Crop out a 16 pixel ring around the result
Michael Ludwig898bbfa2019-08-02 15:21:23 -040092 const SkIRect cropRect = SkIRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
robertphillipsb2a4dc62016-04-14 07:54:04 -070093
94 SkPaint filterPaint;
Michael Ludwig898bbfa2019-08-02 15:21:23 -040095 filterPaint.setImageFilter(SkImageFilters::Magnifier(
96 srcRect, kInset, std::move(imageSource), &cropRect));
robertphillipsb2a4dc62016-04-14 07:54:04 -070097
98 canvas->saveLayer(nullptr, &filterPaint);
99 canvas->restore();
100}