blob: c5e6d416bf31bece1bc7d29f4ed9163815ec190b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reedc3b32662014-06-17 08:38:31 -07007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040012#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 Kleinc0bd9f92019-04-23 12:05:21 -050020#include "tools/ToolUtils.h"
reed@android.com048522d2009-06-23 12:19:41 +000021
reed@android.com048522d2009-06-23 12:19:41 +000022static void make_bm(SkBitmap* bm) {
bungeman@google.com2ed67e82011-05-18 18:54:23 +000023 const SkColor colors[4] = {
reed@android.com048522d2009-06-23 12:19:41 +000024 SK_ColorRED, SK_ColorGREEN,
25 SK_ColorBLUE, SK_ColorWHITE
26 };
bungeman@google.com2ed67e82011-05-18 18:54:23 +000027 SkPMColor colorsPM[4];
28 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
29 colorsPM[i] = SkPreMultiplyColor(colors[i]);
30 }
Mike Reed304a07c2017-07-12 15:10:28 -040031 bm->allocN32Pixels(2, 2, true);
bungeman@google.com2ed67e82011-05-18 18:54:23 +000032
Mike Reed304a07c2017-07-12 15:10:28 -040033 *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.com048522d2009-06-23 12:19:41 +000037}
38
Mike Reedfa582c82021-01-23 22:07:05 -050039static 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.com048522d2009-06-23 12:19:41 +000043}
44
Mike Reedfa582c82021-01-23 22:07:05 -050045static 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.com048522d2009-06-23 12:19:41 +000048 p->setDither(true);
Mike Reedfa582c82021-01-23 22:07:05 -050049 return x + draw_bm(c, img, x, 0, SkSamplingOptions(SkFilterMode::kLinear), p);
reed@android.com048522d2009-06-23 12:19:41 +000050}
51
Mike Reedfa582c82021-01-23 22:07:05 -050052static SkScalar draw_row(SkCanvas* canvas, sk_sp<SkImage> img) {
reed@android.com048522d2009-06-23 12:19:41 +000053 SkAutoCanvasRestore acr(canvas, true);
54
55 SkPaint paint;
Mike Reed4de2f1f2019-01-05 16:35:13 -050056 paint.setAntiAlias(true);
57
reed@android.com048522d2009-06-23 12:19:41 +000058 SkScalar x = 0;
59 const int scale = 32;
60
Mike Kleinea3f0142019-03-20 11:12:10 -050061 SkFont font(ToolUtils::create_portable_typeface());
Mike Reedfa582c82021-01-23 22:07:05 -050062 const char* name = ToolUtils::colortype_name(img->colorType());
63 canvas->drawString(name, x, SkIntToScalar(img->height())*scale*5/8, font, paint);
reed@android.com048522d2009-06-23 12:19:41 +000064 canvas->translate(SkIntToScalar(48), 0);
65
66 canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
bungeman@google.com2ed67e82011-05-18 18:54:23 +000067
Mike Reedfa582c82021-01-23 22:07:05 -050068 x += draw_set(canvas, img, 0, &paint);
reed@android.com048522d2009-06-23 12:19:41 +000069 paint.reset();
Mike Reed9407e242019-02-15 16:13:57 -050070 paint.setAlphaf(0.5f);
Mike Reedfa582c82021-01-23 22:07:05 -050071 draw_set(canvas, img, x, &paint);
reed@android.com048522d2009-06-23 12:19:41 +000072 return x * scale / 3;
73}
74
reed2ba21a02015-05-18 12:57:56 -070075class FilterGM : public skiagm::GM {
76 void onOnceBeforeDraw() override {
Mike Reedfa582c82021-01-23 22:07:05 -050077 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.com26b73d32012-03-08 22:13:04 +000085 }
reed2ba21a02015-05-18 12:57:56 -070086
reed@google.com26b73d32012-03-08 22:13:04 +000087public:
Mike Reedfa582c82021-01-23 22:07:05 -050088 sk_sp<SkImage> fImg32, fImg4444, fImg565;
reed@google.com26b73d32012-03-08 22:13:04 +000089
reed2ba21a02015-05-18 12:57:56 -070090 FilterGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040091 this->setBGColor(0xFFDDDDDD);
reed@android.com048522d2009-06-23 12:19:41 +000092 }
93
94protected:
reed2ba21a02015-05-18 12:57:56 -070095 SkString onShortName() override {
reed@android.com048522d2009-06-23 12:19:41 +000096 return SkString("bitmapfilters");
97 }
98
reed2ba21a02015-05-18 12:57:56 -070099 SkISize onISize() override {
Mike Reed304a07c2017-07-12 15:10:28 -0400100 return SkISize::Make(540, 250);
reed@android.com048522d2009-06-23 12:19:41 +0000101 }
102
reed2ba21a02015-05-18 12:57:56 -0700103 void onDraw(SkCanvas* canvas) override {
reed@android.com048522d2009-06-23 12:19:41 +0000104 SkScalar x = SkIntToScalar(10);
105 SkScalar y = SkIntToScalar(10);
bungeman@google.com2ed67e82011-05-18 18:54:23 +0000106
reed@android.com048522d2009-06-23 12:19:41 +0000107 canvas->translate(x, y);
Mike Reedfa582c82021-01-23 22:07:05 -0500108 y = draw_row(canvas, fImg4444);
reed@android.com048522d2009-06-23 12:19:41 +0000109 canvas->translate(0, y);
Mike Reedfa582c82021-01-23 22:07:05 -0500110 y = draw_row(canvas, fImg565);
reed@android.com048522d2009-06-23 12:19:41 +0000111 canvas->translate(0, y);
Mike Reedfa582c82021-01-23 22:07:05 -0500112 draw_row(canvas, fImg32);
reed@android.com048522d2009-06-23 12:19:41 +0000113 }
bungeman@google.com2ed67e82011-05-18 18:54:23 +0000114
reed@android.com048522d2009-06-23 12:19:41 +0000115private:
John Stiles7571f9e2020-09-02 22:42:33 -0400116 using INHERITED = skiagm::GM;
reed@android.com048522d2009-06-23 12:19:41 +0000117};
reed2ba21a02015-05-18 12:57:56 -0700118DEF_GM( return new FilterGM; )
reed@android.com048522d2009-06-23 12:19:41 +0000119
120//////////////////////////////////////////////////////////////////////////////
121
reed2ba21a02015-05-18 12:57:56 -0700122class 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.com048522d2009-06-23 12:19:41 +0000128
reed2ba21a02015-05-18 12:57:56 -0700129 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);
reed2ba21a02015-05-18 12:57:56 -0700136
137 fBitmap.extractAlpha(&fAlpha);
138 }
halcanary9d524f22016-03-29 09:03:52 -0700139
reed2ba21a02015-05-18 12:57:56 -0700140public:
141 SkBitmap fBitmap, fAlpha;
halcanary9d524f22016-03-29 09:03:52 -0700142
reed2ba21a02015-05-18 12:57:56 -0700143protected:
144 SkString onShortName() override {
145 return SkString("extractalpha");
146 }
halcanary9d524f22016-03-29 09:03:52 -0700147
reed2ba21a02015-05-18 12:57:56 -0700148 SkISize onISize() override {
149 return SkISize::Make(540, 330);
150 }
halcanary9d524f22016-03-29 09:03:52 -0700151
reed2ba21a02015-05-18 12:57:56 -0700152 void onDraw(SkCanvas* canvas) override {
153 SkPaint paint;
154 paint.setAntiAlias(true);
reed2ba21a02015-05-18 12:57:56 -0700155 paint.setColor(SK_ColorRED);
156
Mike Reed568f0ae2021-01-24 08:57:23 -0500157 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);
reed2ba21a02015-05-18 12:57:56 -0700163 }
halcanary9d524f22016-03-29 09:03:52 -0700164
reed2ba21a02015-05-18 12:57:56 -0700165private:
John Stiles7571f9e2020-09-02 22:42:33 -0400166 using INHERITED = skiagm::GM;
reed2ba21a02015-05-18 12:57:56 -0700167};
168DEF_GM( return new TestExtractAlphaGM; )