blob: 73b58873263c58ea6d5400f5852a7c3ae3fb4d4e [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"
12#include "include/core/SkFilterQuality.h"
13#include "include/core/SkFont.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTypeface.h"
20#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/ToolUtils.h"
reed@android.com048522d2009-06-23 12:19:41 +000022
reed@android.com048522d2009-06-23 12:19:41 +000023static void make_bm(SkBitmap* bm) {
bungeman@google.com2ed67e82011-05-18 18:54:23 +000024 const SkColor colors[4] = {
reed@android.com048522d2009-06-23 12:19:41 +000025 SK_ColorRED, SK_ColorGREEN,
26 SK_ColorBLUE, SK_ColorWHITE
27 };
bungeman@google.com2ed67e82011-05-18 18:54:23 +000028 SkPMColor colorsPM[4];
29 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
30 colorsPM[i] = SkPreMultiplyColor(colors[i]);
31 }
Mike Reed304a07c2017-07-12 15:10:28 -040032 bm->allocN32Pixels(2, 2, true);
bungeman@google.com2ed67e82011-05-18 18:54:23 +000033
Mike Reed304a07c2017-07-12 15:10:28 -040034 *bm->getAddr32(0, 0) = colorsPM[0];
35 *bm->getAddr32(1, 0) = colorsPM[1];
36 *bm->getAddr32(0, 1) = colorsPM[2];
37 *bm->getAddr32(1, 1) = colorsPM[3];
reed@android.com048522d2009-06-23 12:19:41 +000038}
39
40static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
41 SkScalar x, SkScalar y, SkPaint* paint) {
42 canvas->drawBitmap(bm, x, y, paint);
43 return SkIntToScalar(bm.width()) * 5/4;
44}
45
46static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x,
47 SkPaint* p) {
48 x += draw_bm(c, bm, x, 0, p);
reed93a12152015-03-16 10:08:34 -070049 p->setFilterQuality(kLow_SkFilterQuality);
reed@android.com048522d2009-06-23 12:19:41 +000050 x += draw_bm(c, bm, x, 0, p);
51 p->setDither(true);
52 return x + draw_bm(c, bm, x, 0, p);
53}
54
reed@android.com048522d2009-06-23 12:19:41 +000055static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
56 SkAutoCanvasRestore acr(canvas, true);
57
58 SkPaint paint;
Mike Reed4de2f1f2019-01-05 16:35:13 -050059 paint.setAntiAlias(true);
60
reed@android.com048522d2009-06-23 12:19:41 +000061 SkScalar x = 0;
62 const int scale = 32;
63
Mike Kleinea3f0142019-03-20 11:12:10 -050064 SkFont font(ToolUtils::create_portable_typeface());
65 const char* name = ToolUtils::colortype_name(bm.colorType());
Cary Clark2a475ea2017-04-28 15:35:12 -040066 canvas->drawString(name, x, SkIntToScalar(bm.height())*scale*5/8,
Mike Reed4de2f1f2019-01-05 16:35:13 -050067 font, paint);
reed@android.com048522d2009-06-23 12:19:41 +000068 canvas->translate(SkIntToScalar(48), 0);
69
70 canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
bungeman@google.com2ed67e82011-05-18 18:54:23 +000071
reed@android.com048522d2009-06-23 12:19:41 +000072 x += draw_set(canvas, bm, 0, &paint);
73 paint.reset();
Mike Reed9407e242019-02-15 16:13:57 -050074 paint.setAlphaf(0.5f);
reed@android.com048522d2009-06-23 12:19:41 +000075 draw_set(canvas, bm, x, &paint);
76 return x * scale / 3;
77}
78
reed2ba21a02015-05-18 12:57:56 -070079class FilterGM : public skiagm::GM {
80 void onOnceBeforeDraw() override {
Mike Reed304a07c2017-07-12 15:10:28 -040081 make_bm(&fBM32);
Mike Kleinea3f0142019-03-20 11:12:10 -050082 ToolUtils::copy_to(&fBM4444, kARGB_4444_SkColorType, fBM32);
83 ToolUtils::copy_to(&fBM16, kRGB_565_SkColorType, fBM32);
reed@google.com26b73d32012-03-08 22:13:04 +000084 }
reed2ba21a02015-05-18 12:57:56 -070085
reed@google.com26b73d32012-03-08 22:13:04 +000086public:
Mike Reed304a07c2017-07-12 15:10:28 -040087 SkBitmap fBM4444, fBM16, fBM32;
reed@google.com26b73d32012-03-08 22:13:04 +000088
reed2ba21a02015-05-18 12:57:56 -070089 FilterGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040090 this->setBGColor(0xFFDDDDDD);
reed@android.com048522d2009-06-23 12:19:41 +000091 }
92
93protected:
reed2ba21a02015-05-18 12:57:56 -070094 SkString onShortName() override {
reed@android.com048522d2009-06-23 12:19:41 +000095 return SkString("bitmapfilters");
96 }
97
reed2ba21a02015-05-18 12:57:56 -070098 SkISize onISize() override {
Mike Reed304a07c2017-07-12 15:10:28 -040099 return SkISize::Make(540, 250);
reed@android.com048522d2009-06-23 12:19:41 +0000100 }
101
reed2ba21a02015-05-18 12:57:56 -0700102 void onDraw(SkCanvas* canvas) override {
reed@android.com048522d2009-06-23 12:19:41 +0000103 SkScalar x = SkIntToScalar(10);
104 SkScalar y = SkIntToScalar(10);
bungeman@google.com2ed67e82011-05-18 18:54:23 +0000105
reed@android.com048522d2009-06-23 12:19:41 +0000106 canvas->translate(x, y);
reed@android.com048522d2009-06-23 12:19:41 +0000107 y = draw_row(canvas, fBM4444);
108 canvas->translate(0, y);
109 y = draw_row(canvas, fBM16);
110 canvas->translate(0, y);
111 draw_row(canvas, fBM32);
112 }
bungeman@google.com2ed67e82011-05-18 18:54:23 +0000113
reed@android.com048522d2009-06-23 12:19:41 +0000114private:
reed2ba21a02015-05-18 12:57:56 -0700115 typedef skiagm::GM INHERITED;
reed@android.com048522d2009-06-23 12:19:41 +0000116};
reed2ba21a02015-05-18 12:57:56 -0700117DEF_GM( return new FilterGM; )
reed@android.com048522d2009-06-23 12:19:41 +0000118
119//////////////////////////////////////////////////////////////////////////////
120
reed2ba21a02015-05-18 12:57:56 -0700121class TestExtractAlphaGM : public skiagm::GM {
122 void onOnceBeforeDraw() override {
123 // Make a bitmap with per-pixels alpha (stroked circle)
124 fBitmap.allocN32Pixels(100, 100);
125 SkCanvas canvas(fBitmap);
126 canvas.clear(0);
reed@android.com048522d2009-06-23 12:19:41 +0000127
reed2ba21a02015-05-18 12:57:56 -0700128 SkPaint paint;
129 paint.setAntiAlias(true);
130 paint.setColor(SK_ColorBLUE);
131 paint.setStyle(SkPaint::kStroke_Style);
132 paint.setStrokeWidth(20);
133
134 canvas.drawCircle(50, 50, 39, paint);
reed2ba21a02015-05-18 12:57:56 -0700135
136 fBitmap.extractAlpha(&fAlpha);
137 }
halcanary9d524f22016-03-29 09:03:52 -0700138
reed2ba21a02015-05-18 12:57:56 -0700139public:
140 SkBitmap fBitmap, fAlpha;
halcanary9d524f22016-03-29 09:03:52 -0700141
reed2ba21a02015-05-18 12:57:56 -0700142protected:
143 SkString onShortName() override {
144 return SkString("extractalpha");
145 }
halcanary9d524f22016-03-29 09:03:52 -0700146
reed2ba21a02015-05-18 12:57:56 -0700147 SkISize onISize() override {
148 return SkISize::Make(540, 330);
149 }
halcanary9d524f22016-03-29 09:03:52 -0700150
reed2ba21a02015-05-18 12:57:56 -0700151 void onDraw(SkCanvas* canvas) override {
152 SkPaint paint;
153 paint.setAntiAlias(true);
154 paint.setFilterQuality(kLow_SkFilterQuality);
155 paint.setColor(SK_ColorRED);
156
157 canvas->drawBitmap(fBitmap, 10, 10, &paint); // should stay blue (ignore paint's color)
158 canvas->drawBitmap(fAlpha, 120, 10, &paint); // should draw red
159 }
halcanary9d524f22016-03-29 09:03:52 -0700160
reed2ba21a02015-05-18 12:57:56 -0700161private:
162 typedef skiagm::GM INHERITED;
163};
164DEF_GM( return new TestExtractAlphaGM; )