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 | |
| 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame^] | 9 | #include "sk_tool_utils.h" |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 10 | #include "SkImageSource.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 11 | #include "SkMagnifierImageFilter.h" |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 12 | #include "SkPixelRef.h" |
mtklein@google.com | cfa7ba7 | 2013-09-16 18:19:30 +0000 | [diff] [blame] | 13 | #include "SkRandom.h" |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 14 | #include "SkSurface.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 15 | |
| 16 | #define WIDTH 500 |
| 17 | #define HEIGHT 500 |
| 18 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 19 | DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) { |
senorblanco | cbf6b6e | 2014-10-23 15:00:10 -0700 | [diff] [blame] | 20 | SkPaint filterPaint; |
| 21 | filterPaint.setImageFilter( |
robertphillips | 11171f3 | 2016-04-07 07:34:15 -0700 | [diff] [blame] | 22 | SkMagnifierImageFilter::Make( |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 23 | SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100), |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 24 | SkIntToScalar(WIDTH / 2), |
| 25 | SkIntToScalar(HEIGHT / 2)), |
robertphillips | 11171f3 | 2016-04-07 07:34:15 -0700 | [diff] [blame] | 26 | 100, nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | canvas->saveLayer(nullptr, &filterPaint); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 28 | 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] | 29 | SkRandom rand; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 30 | for (int i = 0; i < 25; ++i) { |
mtklein@google.com | cfa7ba7 | 2013-09-16 18:19:30 +0000 | [diff] [blame] | 31 | int x = rand.nextULessThan(WIDTH); |
| 32 | int y = rand.nextULessThan(HEIGHT); |
senorblanco | cbf6b6e | 2014-10-23 15:00:10 -0700 | [diff] [blame] | 33 | SkPaint paint; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 34 | sk_tool_utils::set_portable_typeface(&paint); |
caryclark | d2ce185 | 2015-07-16 12:35:58 -0700 | [diff] [blame] | 35 | paint.setColor(sk_tool_utils::color_to_565(rand.nextBits(24) | 0xFF000000)); |
mtklein@google.com | 26c6d58 | 2013-09-16 19:05:44 +0000 | [diff] [blame] | 36 | paint.setTextSize(rand.nextRangeScalar(0, 300)); |
senorblanco | cbf6b6e | 2014-10-23 15:00:10 -0700 | [diff] [blame] | 37 | paint.setAntiAlias(true); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 38 | canvas->drawText(str, strlen(str), SkIntToScalar(x), |
| 39 | SkIntToScalar(y), paint); |
| 40 | } |
| 41 | canvas->restore(); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 42 | } |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 43 | |
| 44 | //////////////////////////////////////////////////////////////////////////////// |
| 45 | #define WIDTH_HEIGHT 256 |
| 46 | |
| 47 | static sk_sp<SkImage> make_img() { |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 48 | SkBitmap bitmap; |
| 49 | bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT); |
| 50 | SkCanvas canvas(bitmap); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 51 | |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 52 | canvas.clear(0x0); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 53 | |
| 54 | SkPaint paint; |
| 55 | paint.setColor(SK_ColorBLUE); |
| 56 | |
| 57 | for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) { |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 58 | canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint); |
| 59 | canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 62 | SkBitmap result; |
| 63 | result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType)); |
| 64 | result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0); |
| 65 | |
| 66 | return SkImage::MakeFromBitmap(result); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) { |
| 70 | |
| 71 | sk_sp<SkImage> image(make_img()); |
| 72 | |
| 73 | sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image))); |
| 74 | |
| 75 | SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32), |
| 76 | SkIntToScalar(WIDTH_HEIGHT-32)); |
| 77 | srcRect.inset(64.0f, 64.0f); |
| 78 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 79 | constexpr SkScalar kInset = 64.0f; |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 80 | |
| 81 | // Crop out a 16 pixel ring around the result |
| 82 | const SkRect rect = SkRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32); |
| 83 | SkImageFilter::CropRect cropRect(rect); |
| 84 | |
| 85 | SkPaint filterPaint; |
| 86 | filterPaint.setImageFilter(SkMagnifierImageFilter::Make(srcRect, kInset, |
| 87 | std::move(imageSource), |
| 88 | &cropRect)); |
| 89 | |
| 90 | canvas->saveLayer(nullptr, &filterPaint); |
| 91 | canvas->restore(); |
| 92 | } |