blob: b61519bf300b63307ad8242f372fcb7e77816cf6 [file] [log] [blame]
David Ghandehari9e5b5872016-07-28 09:50:04 -07001/*
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
Xiaoming Shi73dfa202020-03-12 11:31:35 -07008#include "gm/gm.h"
9#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"
17#include "include/core/SkPixelRef.h"
18#include "include/core/SkRect.h"
19#include "include/core/SkRefCnt.h"
20#include "include/core/SkScalar.h"
21#include "include/core/SkTypeface.h"
22#include "include/effects/SkImageFilters.h"
23#include "include/utils/SkRandom.h"
24#include "tools/ToolUtils.h"
25
26#include <utility>
David Ghandehari9e5b5872016-07-28 09:50:04 -070027
28#define WIDTH 500
29#define HEIGHT 500
30
Andrew Top200ce4b2018-01-29 13:43:50 -080031DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
32 SkPaint filterPaint;
33 filterPaint.setImageFilter(
Xiaoming Shi73dfa202020-03-12 11:31:35 -070034 SkImageFilters::Magnifier(
David Ghandehari9e5b5872016-07-28 09:50:04 -070035 SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
36 SkIntToScalar(WIDTH / 2),
37 SkIntToScalar(HEIGHT / 2)),
Andrew Top200ce4b2018-01-29 13:43:50 -080038 100, nullptr));
39 canvas->saveLayer(nullptr, &filterPaint);
David Ghandehari9e5b5872016-07-28 09:50:04 -070040 const char* str = "The quick brown fox jumped over the lazy dog.";
41 SkRandom rand;
Xiaoming Shi73dfa202020-03-12 11:31:35 -070042 SkFont font(ToolUtils::create_portable_typeface());
David Ghandehari9e5b5872016-07-28 09:50:04 -070043 for (int i = 0; i < 25; ++i) {
44 int x = rand.nextULessThan(WIDTH);
45 int y = rand.nextULessThan(HEIGHT);
Andrew Top200ce4b2018-01-29 13:43:50 -080046 SkPaint paint;
Xiaoming Shi73dfa202020-03-12 11:31:35 -070047 paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000));
48 font.setSize(rand.nextRangeScalar(0, 300));
49 canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint);
David Ghandehari9e5b5872016-07-28 09:50:04 -070050 }
51 canvas->restore();
Andrew Top200ce4b2018-01-29 13:43:50 -080052}
53
54////////////////////////////////////////////////////////////////////////////////
55#define WIDTH_HEIGHT 256
56
57static sk_sp<SkImage> make_img() {
58 SkBitmap bitmap;
59 bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
60 SkCanvas canvas(bitmap);
61
62 canvas.clear(0x0);
63
64 SkPaint paint;
65 paint.setColor(SK_ColorBLUE);
66
67 for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
68 canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
69 canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
David Ghandehari9e5b5872016-07-28 09:50:04 -070070 }
71
Andrew Top200ce4b2018-01-29 13:43:50 -080072 SkBitmap result;
73 result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType));
74 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
David Ghandehari9e5b5872016-07-28 09:50:04 -070075
Andrew Top200ce4b2018-01-29 13:43:50 -080076 return SkImage::MakeFromBitmap(result);
77}
David Ghandehari9e5b5872016-07-28 09:50:04 -070078
Andrew Top200ce4b2018-01-29 13:43:50 -080079DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
David Ghandehari9e5b5872016-07-28 09:50:04 -070080
Andrew Top200ce4b2018-01-29 13:43:50 -080081 sk_sp<SkImage> image(make_img());
82
Xiaoming Shi73dfa202020-03-12 11:31:35 -070083 sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(std::move(image)));
Andrew Top200ce4b2018-01-29 13:43:50 -080084
85 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
86 SkIntToScalar(WIDTH_HEIGHT-32));
87 srcRect.inset(64.0f, 64.0f);
88
89 constexpr SkScalar kInset = 64.0f;
90
91 // Crop out a 16 pixel ring around the result
Xiaoming Shi73dfa202020-03-12 11:31:35 -070092 const SkIRect cropRect = SkIRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
Andrew Top200ce4b2018-01-29 13:43:50 -080093
94 SkPaint filterPaint;
Xiaoming Shi73dfa202020-03-12 11:31:35 -070095 filterPaint.setImageFilter(SkImageFilters::Magnifier(
96 srcRect, kInset, std::move(imageSource), &cropRect));
Andrew Top200ce4b2018-01-29 13:43:50 -080097
98 canvas->saveLayer(nullptr, &filterPaint);
99 canvas->restore();
David Ghandehari9e5b5872016-07-28 09:50:04 -0700100}