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.h"
9 #include "sk_tool_utils.h"
10 
make_bm(SkBitmap * bm)11 static void make_bm(SkBitmap* bm) {
12     const SkColor colors[4] = {
13         SK_ColorRED, SK_ColorGREEN,
14         SK_ColorBLUE, SK_ColorWHITE
15     };
16     SkPMColor colorsPM[4];
17     for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
18         colorsPM[i] = SkPreMultiplyColor(colors[i]);
19     }
20     SkColorTable* ctable = new SkColorTable(colorsPM, 4);
21 
22     bm->allocPixels(SkImageInfo::Make(2, 2, kIndex_8_SkColorType,
23                                       kPremul_SkAlphaType),
24                     nullptr, ctable);
25     ctable->unref();
26 
27     *bm->getAddr8(0, 0) = 0;
28     *bm->getAddr8(1, 0) = 1;
29     *bm->getAddr8(0, 1) = 2;
30     *bm->getAddr8(1, 1) = 3;
31 }
32 
draw_bm(SkCanvas * canvas,const SkBitmap & bm,SkScalar x,SkScalar y,SkPaint * paint)33 static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
34                         SkScalar x, SkScalar y, SkPaint* paint) {
35     canvas->drawBitmap(bm, x, y, paint);
36     return SkIntToScalar(bm.width()) * 5/4;
37 }
38 
draw_set(SkCanvas * c,const SkBitmap & bm,SkScalar x,SkPaint * p)39 static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x,
40                          SkPaint* p) {
41     x += draw_bm(c, bm, x, 0, p);
42     p->setFilterQuality(kLow_SkFilterQuality);
43     x += draw_bm(c, bm, x, 0, p);
44     p->setDither(true);
45     return x + draw_bm(c, bm, x, 0, p);
46 }
47 
draw_row(SkCanvas * canvas,const SkBitmap & bm)48 static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
49     SkAutoCanvasRestore acr(canvas, true);
50 
51     SkPaint paint;
52     SkScalar x = 0;
53     const int scale = 32;
54 
55     paint.setAntiAlias(true);
56     sk_tool_utils::set_portable_typeface(&paint);
57     const char* name = sk_tool_utils::colortype_name(bm.colorType());
58     canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8,
59                      paint);
60     canvas->translate(SkIntToScalar(48), 0);
61 
62     canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
63 
64     x += draw_set(canvas, bm, 0, &paint);
65     paint.reset();
66     paint.setAlpha(0x80);
67     draw_set(canvas, bm, x, &paint);
68     return x * scale / 3;
69 }
70 
71 class FilterGM : public skiagm::GM {
onOnceBeforeDraw()72     void onOnceBeforeDraw() override {
73         make_bm(&fBM8);
74         fBM8.copyTo(&fBM4444, kARGB_4444_SkColorType);
75         fBM8.copyTo(&fBM16, kRGB_565_SkColorType);
76         fBM8.copyTo(&fBM32, kN32_SkColorType);
77     }
78 
79 public:
80     SkBitmap    fBM8, fBM4444, fBM16, fBM32;
81 
FilterGM()82     FilterGM() {
83         this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
84     }
85 
86 protected:
onShortName()87     SkString onShortName() override {
88         return SkString("bitmapfilters");
89     }
90 
onISize()91     SkISize onISize() override {
92         return SkISize::Make(540, 330);
93     }
94 
onDraw(SkCanvas * canvas)95     void onDraw(SkCanvas* canvas) override {
96         SkScalar x = SkIntToScalar(10);
97         SkScalar y = SkIntToScalar(10);
98 
99         canvas->translate(x, y);
100         y = draw_row(canvas, fBM8);
101         canvas->translate(0, y);
102         y = draw_row(canvas, fBM4444);
103         canvas->translate(0, y);
104         y = draw_row(canvas, fBM16);
105         canvas->translate(0, y);
106         draw_row(canvas, fBM32);
107     }
108 
109 private:
110     typedef skiagm::GM INHERITED;
111 };
112 DEF_GM( return new FilterGM; )
113 
114 //////////////////////////////////////////////////////////////////////////////
115 
116 class TestExtractAlphaGM : public skiagm::GM {
onOnceBeforeDraw()117     void onOnceBeforeDraw() override {
118         // Make a bitmap with per-pixels alpha (stroked circle)
119         fBitmap.allocN32Pixels(100, 100);
120         SkCanvas canvas(fBitmap);
121         canvas.clear(0);
122 
123         SkPaint paint;
124         paint.setAntiAlias(true);
125         paint.setColor(SK_ColorBLUE);
126         paint.setStyle(SkPaint::kStroke_Style);
127         paint.setStrokeWidth(20);
128 
129         canvas.drawCircle(50, 50, 39, paint);
130         canvas.flush();
131 
132         fBitmap.extractAlpha(&fAlpha);
133     }
134 
135 public:
136     SkBitmap fBitmap, fAlpha;
137 
138 protected:
onShortName()139     SkString onShortName() override {
140         return SkString("extractalpha");
141     }
142 
onISize()143     SkISize onISize() override {
144         return SkISize::Make(540, 330);
145     }
146 
onDraw(SkCanvas * canvas)147     void onDraw(SkCanvas* canvas) override {
148         SkPaint paint;
149         paint.setAntiAlias(true);
150         paint.setFilterQuality(kLow_SkFilterQuality);
151         paint.setColor(SK_ColorRED);
152 
153         canvas->drawBitmap(fBitmap, 10, 10, &paint);    // should stay blue (ignore paint's color)
154         canvas->drawBitmap(fAlpha, 120, 10, &paint);    // should draw red
155     }
156 
157 private:
158     typedef skiagm::GM INHERITED;
159 };
160 DEF_GM( return new TestExtractAlphaGM; )
161 
162