bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkPixelRef.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/core/SkRect.h" |
| 19 | #include "include/core/SkRefCnt.h" |
| 20 | #include "include/core/SkScalar.h" |
| 21 | #include "include/core/SkTypeface.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 22 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/utils/SkRandom.h" |
| 24 | #include "tools/ToolUtils.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 25 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include <utility> |
| 27 | |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 28 | #define WIDTH 500 |
| 29 | #define HEIGHT 500 |
| 30 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 31 | DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) { |
senorblanco | cbf6b6e | 2014-10-23 15:00:10 -0700 | [diff] [blame] | 32 | SkPaint filterPaint; |
| 33 | filterPaint.setImageFilter( |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 34 | SkImageFilters::Magnifier( |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 35 | SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100), |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 36 | SkIntToScalar(WIDTH / 2), |
| 37 | SkIntToScalar(HEIGHT / 2)), |
robertphillips | 11171f3 | 2016-04-07 07:34:15 -0700 | [diff] [blame] | 38 | 100, nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | canvas->saveLayer(nullptr, &filterPaint); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 40 | const char* str = "The quick brown fox jumped over the lazy dog."; |
mtklein@google.com | cfa7ba7 | 2013-09-16 18:19:30 +0000 | [diff] [blame] | 41 | SkRandom rand; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 42 | SkFont font(ToolUtils::create_portable_typeface()); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 43 | for (int i = 0; i < 25; ++i) { |
mtklein@google.com | cfa7ba7 | 2013-09-16 18:19:30 +0000 | [diff] [blame] | 44 | int x = rand.nextULessThan(WIDTH); |
| 45 | int y = rand.nextULessThan(HEIGHT); |
senorblanco | cbf6b6e | 2014-10-23 15:00:10 -0700 | [diff] [blame] | 46 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 47 | paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000)); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 48 | font.setSize(rand.nextRangeScalar(0, 300)); |
| 49 | canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 50 | } |
| 51 | canvas->restore(); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 52 | } |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 53 | |
| 54 | //////////////////////////////////////////////////////////////////////////////// |
| 55 | #define WIDTH_HEIGHT 256 |
| 56 | |
| 57 | static sk_sp<SkImage> make_img() { |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 58 | SkBitmap bitmap; |
| 59 | bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT); |
| 60 | SkCanvas canvas(bitmap); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 61 | |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 62 | canvas.clear(0x0); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 63 | |
| 64 | SkPaint paint; |
| 65 | paint.setColor(SK_ColorBLUE); |
| 66 | |
| 67 | for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) { |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 68 | canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint); |
| 69 | canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 72 | 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); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) { |
| 80 | |
| 81 | sk_sp<SkImage> image(make_img()); |
| 82 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 83 | sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(std::move(image))); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 84 | |
| 85 | SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32), |
| 86 | SkIntToScalar(WIDTH_HEIGHT-32)); |
| 87 | srcRect.inset(64.0f, 64.0f); |
| 88 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 89 | constexpr SkScalar kInset = 64.0f; |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 90 | |
| 91 | // Crop out a 16 pixel ring around the result |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 92 | const SkIRect cropRect = SkIRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 93 | |
| 94 | SkPaint filterPaint; |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 95 | filterPaint.setImageFilter(SkImageFilters::Magnifier( |
| 96 | srcRect, kInset, std::move(imageSource), &cropRect)); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 97 | |
| 98 | canvas->saveLayer(nullptr, &filterPaint); |
| 99 | canvas->restore(); |
| 100 | } |