David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [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 | |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 8 | #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 Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 27 | |
| 28 | #define WIDTH 500 |
| 29 | #define HEIGHT 500 |
| 30 | |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 31 | DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) { |
| 32 | SkPaint filterPaint; |
| 33 | filterPaint.setImageFilter( |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 34 | SkImageFilters::Magnifier( |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 35 | SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100), |
| 36 | SkIntToScalar(WIDTH / 2), |
| 37 | SkIntToScalar(HEIGHT / 2)), |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 38 | 100, nullptr)); |
| 39 | canvas->saveLayer(nullptr, &filterPaint); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 40 | const char* str = "The quick brown fox jumped over the lazy dog."; |
| 41 | SkRandom rand; |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 42 | SkFont font(ToolUtils::create_portable_typeface()); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 43 | for (int i = 0; i < 25; ++i) { |
| 44 | int x = rand.nextULessThan(WIDTH); |
| 45 | int y = rand.nextULessThan(HEIGHT); |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 46 | SkPaint paint; |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 47 | 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 Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 50 | } |
| 51 | canvas->restore(); |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | //////////////////////////////////////////////////////////////////////////////// |
| 55 | #define WIDTH_HEIGHT 256 |
| 56 | |
| 57 | static 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 Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [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); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 75 | |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 76 | return SkImage::MakeFromBitmap(result); |
| 77 | } |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 78 | |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 79 | DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 80 | |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 81 | sk_sp<SkImage> image(make_img()); |
| 82 | |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 83 | sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(std::move(image))); |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [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 | |
| 89 | constexpr SkScalar kInset = 64.0f; |
| 90 | |
| 91 | // Crop out a 16 pixel ring around the result |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 92 | const SkIRect cropRect = SkIRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32); |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 93 | |
| 94 | SkPaint filterPaint; |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 95 | filterPaint.setImageFilter(SkImageFilters::Magnifier( |
| 96 | srcRect, kInset, std::move(imageSource), &cropRect)); |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 97 | |
| 98 | canvas->saveLayer(nullptr, &filterPaint); |
| 99 | canvas->restore(); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 100 | } |