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 */
7
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/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"
21 #include "tools/ToolUtils.h"
22
make_bm(SkBitmap * bm)23 static void make_bm(SkBitmap* bm) {
24 const SkColor colors[4] = {
25 SK_ColorRED, SK_ColorGREEN,
26 SK_ColorBLUE, SK_ColorWHITE
27 };
28 SkPMColor colorsPM[4];
29 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
30 colorsPM[i] = SkPreMultiplyColor(colors[i]);
31 }
32 bm->allocN32Pixels(2, 2, true);
33
34 *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];
38 }
39
draw_bm(SkCanvas * canvas,const SkBitmap & bm,SkScalar x,SkScalar y,SkPaint * paint)40 static 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
draw_set(SkCanvas * c,const SkBitmap & bm,SkScalar x,SkPaint * p)46 static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x,
47 SkPaint* p) {
48 x += draw_bm(c, bm, x, 0, p);
49 p->setFilterQuality(kLow_SkFilterQuality);
50 x += draw_bm(c, bm, x, 0, p);
51 p->setDither(true);
52 return x + draw_bm(c, bm, x, 0, p);
53 }
54
draw_row(SkCanvas * canvas,const SkBitmap & bm)55 static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
56 SkAutoCanvasRestore acr(canvas, true);
57
58 SkPaint paint;
59 paint.setAntiAlias(true);
60
61 SkScalar x = 0;
62 const int scale = 32;
63
64 SkFont font(ToolUtils::create_portable_typeface());
65 const char* name = ToolUtils::colortype_name(bm.colorType());
66 canvas->drawString(name, x, SkIntToScalar(bm.height())*scale*5/8,
67 font, paint);
68 canvas->translate(SkIntToScalar(48), 0);
69
70 canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
71
72 x += draw_set(canvas, bm, 0, &paint);
73 paint.reset();
74 paint.setAlphaf(0.5f);
75 draw_set(canvas, bm, x, &paint);
76 return x * scale / 3;
77 }
78
79 class FilterGM : public skiagm::GM {
onOnceBeforeDraw()80 void onOnceBeforeDraw() override {
81 make_bm(&fBM32);
82 ToolUtils::copy_to(&fBM4444, kARGB_4444_SkColorType, fBM32);
83 ToolUtils::copy_to(&fBM16, kRGB_565_SkColorType, fBM32);
84 }
85
86 public:
87 SkBitmap fBM4444, fBM16, fBM32;
88
FilterGM()89 FilterGM() {
90 this->setBGColor(0xFFDDDDDD);
91 }
92
93 protected:
onShortName()94 SkString onShortName() override {
95 return SkString("bitmapfilters");
96 }
97
onISize()98 SkISize onISize() override {
99 return SkISize::Make(540, 250);
100 }
101
onDraw(SkCanvas * canvas)102 void onDraw(SkCanvas* canvas) override {
103 SkScalar x = SkIntToScalar(10);
104 SkScalar y = SkIntToScalar(10);
105
106 canvas->translate(x, y);
107 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 }
113
114 private:
115 typedef skiagm::GM INHERITED;
116 };
117 DEF_GM( return new FilterGM; )
118
119 //////////////////////////////////////////////////////////////////////////////
120
121 class TestExtractAlphaGM : public skiagm::GM {
onOnceBeforeDraw()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);
127
128 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);
135
136 fBitmap.extractAlpha(&fAlpha);
137 }
138
139 public:
140 SkBitmap fBitmap, fAlpha;
141
142 protected:
onShortName()143 SkString onShortName() override {
144 return SkString("extractalpha");
145 }
146
onISize()147 SkISize onISize() override {
148 return SkISize::Make(540, 330);
149 }
150
onDraw(SkCanvas * canvas)151 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 }
160
161 private:
162 typedef skiagm::GM INHERITED;
163 };
164 DEF_GM( return new TestExtractAlphaGM; )
165