blob: 888bb5d9abe1d2ddf5941db5d316bb624f70c48b [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"
Brian Osmanb44bb312016-12-20 15:54:11 -050011#include "SkPixelRef.h"
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000012#include "SkRandom.h"
robertphillipsb2a4dc62016-04-14 07:54:04 -070013#include "SkSurface.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000014
15#define WIDTH 500
16#define HEIGHT 500
17
halcanary2a243382015-09-09 08:16:41 -070018DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
senorblancocbf6b6e2014-10-23 15:00:10 -070019 SkPaint filterPaint;
20 filterPaint.setImageFilter(
robertphillips11171f32016-04-07 07:34:15 -070021 SkMagnifierImageFilter::Make(
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000022 SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
bsalomon@google.com82aa7482012-08-13 14:22:17 +000023 SkIntToScalar(WIDTH / 2),
24 SkIntToScalar(HEIGHT / 2)),
robertphillips11171f32016-04-07 07:34:15 -070025 100, nullptr));
halcanary96fcdcc2015-08-27 07:41:13 -070026 canvas->saveLayer(nullptr, &filterPaint);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000027 const char* str = "The quick brown fox jumped over the lazy dog.";
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000028 SkRandom rand;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000029 for (int i = 0; i < 25; ++i) {
mtklein@google.comcfa7ba72013-09-16 18:19:30 +000030 int x = rand.nextULessThan(WIDTH);
31 int y = rand.nextULessThan(HEIGHT);
senorblancocbf6b6e2014-10-23 15:00:10 -070032 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070033 sk_tool_utils::set_portable_typeface(&paint);
caryclarkd2ce1852015-07-16 12:35:58 -070034 paint.setColor(sk_tool_utils::color_to_565(rand.nextBits(24) | 0xFF000000));
mtklein@google.com26c6d582013-09-16 19:05:44 +000035 paint.setTextSize(rand.nextRangeScalar(0, 300));
senorblancocbf6b6e2014-10-23 15:00:10 -070036 paint.setAntiAlias(true);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000037 canvas->drawText(str, strlen(str), SkIntToScalar(x),
38 SkIntToScalar(y), paint);
39 }
40 canvas->restore();
bsalomon@google.com82aa7482012-08-13 14:22:17 +000041}
robertphillipsb2a4dc62016-04-14 07:54:04 -070042
43////////////////////////////////////////////////////////////////////////////////
44#define WIDTH_HEIGHT 256
45
46static sk_sp<SkImage> make_img() {
Brian Osmanb44bb312016-12-20 15:54:11 -050047 SkBitmap bitmap;
48 bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
49 SkCanvas canvas(bitmap);
robertphillipsb2a4dc62016-04-14 07:54:04 -070050
Brian Osmanb44bb312016-12-20 15:54:11 -050051 canvas.clear(0x0);
robertphillipsb2a4dc62016-04-14 07:54:04 -070052
53 SkPaint paint;
54 paint.setColor(SK_ColorBLUE);
55
56 for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
Brian Osmanb44bb312016-12-20 15:54:11 -050057 canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
58 canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
robertphillipsb2a4dc62016-04-14 07:54:04 -070059 }
60
Brian Osmanb44bb312016-12-20 15:54:11 -050061 SkBitmap result;
62 result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType));
63 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
64
65 return SkImage::MakeFromBitmap(result);
robertphillipsb2a4dc62016-04-14 07:54:04 -070066}
67
68DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
69
70 sk_sp<SkImage> image(make_img());
71
72 sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image)));
73
74 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
75 SkIntToScalar(WIDTH_HEIGHT-32));
76 srcRect.inset(64.0f, 64.0f);
77
mtkleindbfd7ab2016-09-01 11:24:54 -070078 constexpr SkScalar kInset = 64.0f;
robertphillipsb2a4dc62016-04-14 07:54:04 -070079
80 // Crop out a 16 pixel ring around the result
81 const SkRect rect = SkRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
82 SkImageFilter::CropRect cropRect(rect);
83
84 SkPaint filterPaint;
85 filterPaint.setImageFilter(SkMagnifierImageFilter::Make(srcRect, kInset,
86 std::move(imageSource),
87 &cropRect));
88
89 canvas->saveLayer(nullptr, &filterPaint);
90 canvas->restore();
91}