• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
20 #include "tools/ToolUtils.h"
21 
make_bm(SkBitmap * bm)22 static void make_bm(SkBitmap* bm) {
23     const SkColor colors[4] = {
24         SK_ColorRED, SK_ColorGREEN,
25         SK_ColorBLUE, SK_ColorWHITE
26     };
27     SkPMColor colorsPM[4];
28     for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
29         colorsPM[i] = SkPreMultiplyColor(colors[i]);
30     }
31     bm->allocN32Pixels(2, 2, true);
32 
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];
37 }
38 
draw_bm(SkCanvas * canvas,sk_sp<SkImage> img,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,SkPaint * paint)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;
43 }
44 
draw_set(SkCanvas * c,sk_sp<SkImage> img,SkScalar x,SkPaint * p)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);
48     p->setDither(true);
49     return x + draw_bm(c, img, x, 0, SkSamplingOptions(SkFilterMode::kLinear), p);
50 }
51 
draw_row(SkCanvas * canvas,sk_sp<SkImage> img)52 static SkScalar draw_row(SkCanvas* canvas, sk_sp<SkImage> img) {
53     SkAutoCanvasRestore acr(canvas, true);
54 
55     SkPaint paint;
56     paint.setAntiAlias(true);
57 
58     SkScalar x = 0;
59     const int scale = 32;
60 
61     SkFont      font(ToolUtils::create_portable_typeface());
62     const char* name = ToolUtils::colortype_name(img->colorType());
63     canvas->drawString(name, x, SkIntToScalar(img->height())*scale*5/8, font, paint);
64     canvas->translate(SkIntToScalar(48), 0);
65 
66     canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
67 
68     x += draw_set(canvas, img, 0, &paint);
69     paint.reset();
70     paint.setAlphaf(0.5f);
71     draw_set(canvas, img, x, &paint);
72     return x * scale / 3;
73 }
74 
75 class FilterGM : public skiagm::GM {
onOnceBeforeDraw()76     void onOnceBeforeDraw() override {
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();
85     }
86 
87 public:
88     sk_sp<SkImage> fImg32, fImg4444, fImg565;
89 
FilterGM()90     FilterGM() {
91         this->setBGColor(0xFFDDDDDD);
92     }
93 
94 protected:
onShortName()95     SkString onShortName() override {
96         return SkString("bitmapfilters");
97     }
98 
onISize()99     SkISize onISize() override {
100         return SkISize::Make(540, 250);
101     }
102 
onDraw(SkCanvas * canvas)103     void onDraw(SkCanvas* canvas) override {
104         SkScalar x = SkIntToScalar(10);
105         SkScalar y = SkIntToScalar(10);
106 
107         canvas->translate(x, y);
108         y = draw_row(canvas, fImg4444);
109         canvas->translate(0, y);
110         y = draw_row(canvas, fImg565);
111         canvas->translate(0, y);
112         draw_row(canvas, fImg32);
113     }
114 
115 private:
116     using INHERITED = skiagm::GM;
117 };
118 DEF_GM( return new FilterGM; )
119 
120 //////////////////////////////////////////////////////////////////////////////
121 
122 class TestExtractAlphaGM : public skiagm::GM {
onOnceBeforeDraw()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);
128 
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);
136 
137         fBitmap.extractAlpha(&fAlpha);
138     }
139 
140 public:
141     SkBitmap fBitmap, fAlpha;
142 
143 protected:
onShortName()144     SkString onShortName() override {
145         return SkString("extractalpha");
146     }
147 
onISize()148     SkISize onISize() override {
149         return SkISize::Make(540, 330);
150     }
151 
onDraw(SkCanvas * canvas)152     void onDraw(SkCanvas* canvas) override {
153         SkPaint paint;
154         paint.setAntiAlias(true);
155         paint.setColor(SK_ColorRED);
156 
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);
163     }
164 
165 private:
166     using INHERITED = skiagm::GM;
167 };
168 DEF_GM( return new TestExtractAlphaGM; )
169