epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed | c3b3266 | 2014-06-17 08:38:31 -0700 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkImageInfo.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkScalar.h" |
| 16 | #include "include/core/SkSize.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/core/SkTypeface.h" |
| 19 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "tools/ToolUtils.h" |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 21 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 22 | static void make_bm(SkBitmap* bm) { |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 23 | const SkColor colors[4] = { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 24 | SK_ColorRED, SK_ColorGREEN, |
| 25 | SK_ColorBLUE, SK_ColorWHITE |
| 26 | }; |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 27 | SkPMColor colorsPM[4]; |
| 28 | for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) { |
| 29 | colorsPM[i] = SkPreMultiplyColor(colors[i]); |
| 30 | } |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 31 | bm->allocN32Pixels(2, 2, true); |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 32 | |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 33 | *bm->getAddr32(0, 0) = colorsPM[0]; |
| 34 | *bm->getAddr32(1, 0) = colorsPM[1]; |
| 35 | *bm->getAddr32(0, 1) = colorsPM[2]; |
| 36 | *bm->getAddr32(1, 1) = colorsPM[3]; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 39 | static SkScalar draw_bm(SkCanvas* canvas, sk_sp<SkImage> img, SkScalar x, SkScalar y, |
| 40 | const SkSamplingOptions& sampling, SkPaint* paint) { |
| 41 | canvas->drawImage(img, x, y, sampling, paint); |
| 42 | return SkIntToScalar(img->width()) * 5/4; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 45 | static SkScalar draw_set(SkCanvas* c, sk_sp<SkImage> img, SkScalar x, SkPaint* p) { |
| 46 | x += draw_bm(c, img, x, 0, SkSamplingOptions(), p); |
| 47 | x += draw_bm(c, img, x, 0, SkSamplingOptions(SkFilterMode::kLinear), p); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 48 | p->setDither(true); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 49 | return x + draw_bm(c, img, x, 0, SkSamplingOptions(SkFilterMode::kLinear), p); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 52 | static SkScalar draw_row(SkCanvas* canvas, sk_sp<SkImage> img) { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 53 | SkAutoCanvasRestore acr(canvas, true); |
| 54 | |
| 55 | SkPaint paint; |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 56 | paint.setAntiAlias(true); |
| 57 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 58 | SkScalar x = 0; |
| 59 | const int scale = 32; |
| 60 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 61 | SkFont font(ToolUtils::create_portable_typeface()); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 62 | const char* name = ToolUtils::colortype_name(img->colorType()); |
| 63 | canvas->drawString(name, x, SkIntToScalar(img->height())*scale*5/8, font, paint); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 64 | canvas->translate(SkIntToScalar(48), 0); |
| 65 | |
| 66 | canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 67 | |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 68 | x += draw_set(canvas, img, 0, &paint); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 69 | paint.reset(); |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 70 | paint.setAlphaf(0.5f); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 71 | draw_set(canvas, img, x, &paint); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 72 | return x * scale / 3; |
| 73 | } |
| 74 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 75 | class FilterGM : public skiagm::GM { |
| 76 | void onOnceBeforeDraw() override { |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 77 | SkBitmap bm32, bm4444, bm565; |
| 78 | make_bm(&bm32); |
| 79 | ToolUtils::copy_to(&bm4444, kARGB_4444_SkColorType, bm32); |
| 80 | ToolUtils::copy_to(&bm565, kRGB_565_SkColorType, bm32); |
| 81 | |
| 82 | fImg32 = bm32.asImage(); |
| 83 | fImg4444 = bm4444.asImage(); |
| 84 | fImg565 = bm565.asImage(); |
reed@google.com | 26b73d3 | 2012-03-08 22:13:04 +0000 | [diff] [blame] | 85 | } |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 86 | |
reed@google.com | 26b73d3 | 2012-03-08 22:13:04 +0000 | [diff] [blame] | 87 | public: |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 88 | sk_sp<SkImage> fImg32, fImg4444, fImg565; |
reed@google.com | 26b73d3 | 2012-03-08 22:13:04 +0000 | [diff] [blame] | 89 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 90 | FilterGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 91 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | protected: |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 95 | SkString onShortName() override { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 96 | return SkString("bitmapfilters"); |
| 97 | } |
| 98 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 99 | SkISize onISize() override { |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 100 | return SkISize::Make(540, 250); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 101 | } |
| 102 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 103 | void onDraw(SkCanvas* canvas) override { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 104 | SkScalar x = SkIntToScalar(10); |
| 105 | SkScalar y = SkIntToScalar(10); |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 106 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 107 | canvas->translate(x, y); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 108 | y = draw_row(canvas, fImg4444); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 109 | canvas->translate(0, y); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 110 | y = draw_row(canvas, fImg565); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 111 | canvas->translate(0, y); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 112 | draw_row(canvas, fImg32); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 113 | } |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 114 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 115 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 116 | using INHERITED = skiagm::GM; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 117 | }; |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 118 | DEF_GM( return new FilterGM; ) |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 119 | |
| 120 | ////////////////////////////////////////////////////////////////////////////// |
| 121 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 122 | class TestExtractAlphaGM : public skiagm::GM { |
| 123 | void onOnceBeforeDraw() override { |
| 124 | // Make a bitmap with per-pixels alpha (stroked circle) |
| 125 | fBitmap.allocN32Pixels(100, 100); |
| 126 | SkCanvas canvas(fBitmap); |
| 127 | canvas.clear(0); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 128 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 129 | SkPaint paint; |
| 130 | paint.setAntiAlias(true); |
| 131 | paint.setColor(SK_ColorBLUE); |
| 132 | paint.setStyle(SkPaint::kStroke_Style); |
| 133 | paint.setStrokeWidth(20); |
| 134 | |
| 135 | canvas.drawCircle(50, 50, 39, paint); |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 136 | |
| 137 | fBitmap.extractAlpha(&fAlpha); |
| 138 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 139 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 140 | public: |
| 141 | SkBitmap fBitmap, fAlpha; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 142 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 143 | protected: |
| 144 | SkString onShortName() override { |
| 145 | return SkString("extractalpha"); |
| 146 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 147 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 148 | SkISize onISize() override { |
| 149 | return SkISize::Make(540, 330); |
| 150 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 151 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 152 | void onDraw(SkCanvas* canvas) override { |
| 153 | SkPaint paint; |
| 154 | paint.setAntiAlias(true); |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 155 | paint.setColor(SK_ColorRED); |
| 156 | |
Mike Reed | 568f0ae | 2021-01-24 08:57:23 -0500 | [diff] [blame] | 157 | SkSamplingOptions sampling(SkFilterMode::kLinear); |
| 158 | |
| 159 | // should stay blue (ignore paint's color) |
| 160 | canvas->drawImage(fBitmap.asImage(), 10, 10, sampling, &paint); |
| 161 | // should draw red |
| 162 | canvas->drawImage(fAlpha.asImage(), 120, 10, sampling, &paint); |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 163 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 164 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 165 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 166 | using INHERITED = skiagm::GM; |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 167 | }; |
| 168 | DEF_GM( return new TestExtractAlphaGM; ) |