• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "SampleCode.h"
9 #include "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkPath.h"
13 #include "SkRegion.h"
14 #include "SkShader.h"
15 #include "SkUtils.h"
16 #include "Sk1DPathEffect.h"
17 #include "SkCornerPathEffect.h"
18 #include "SkPathMeasure.h"
19 #include "SkRandom.h"
20 #include "SkColorPriv.h"
21 #include "SkColorFilter.h"
22 #include "SkDither.h"
23 
draw_sweep(SkCanvas * c,int width,int height,SkScalar angle)24 static void draw_sweep(SkCanvas* c, int width, int height, SkScalar angle) {
25     SkRect  r;
26     SkPaint p;
27 
28     p.setAntiAlias(true);
29 //    p.setDither(true);
30     p.setStrokeWidth(SkIntToScalar(width/10));
31     p.setStyle(SkPaint::kStroke_Style);
32 
33     r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
34 
35     //    SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorCYAN };
36     SkColor colors[] = { 0x4c737373, 0x4c737373, 0xffffd300 };
37     SkShader* s = SkGradientShader::CreateSweep(r.centerX(), r.centerY(),
38                                                 colors, nullptr, SK_ARRAY_COUNT(colors));
39     p.setShader(s)->unref();
40 
41     SkAutoCanvasRestore acr(c, true);
42 
43     c->translate(r.centerX(), r.centerY());
44     c->rotate(angle);
45     c->translate(-r.centerX(), -r.centerY());
46 
47     SkRect bounds = r;
48     r.inset(p.getStrokeWidth(), p.getStrokeWidth());
49     SkRect innerBounds = r;
50 
51     if (true) {
52         c->drawOval(r, p);
53     } else {
54         SkScalar x = r.centerX();
55         SkScalar y = r.centerY();
56         SkScalar radius = r.width() / 2;
57         SkScalar thickness = p.getStrokeWidth();
58         SkScalar sweep = 360.0f;
59         SkPath path;
60 
61         path.moveTo(x + radius, y);
62         // outer top
63         path.lineTo(x + radius + thickness, y);
64         // outer arc
65         path.arcTo(bounds, 0, sweep, false);
66         // inner arc
67         path.arcTo(innerBounds, sweep, -sweep, false);
68         path.close();
69     }
70 }
71 
make_bm(SkBitmap * bm)72 static void make_bm(SkBitmap* bm) {
73     bm->allocN32Pixels(100, 100);
74     bm->eraseColor(SK_ColorTRANSPARENT);
75 
76     SkCanvas c(*bm);
77     draw_sweep(&c, bm->width(), bm->height(), 0);
78 }
79 
pre_dither(const SkBitmap & bm)80 static void pre_dither(const SkBitmap& bm) {
81     SkAutoLockPixels alp(bm);
82 
83     for (int y = 0; y < bm.height(); y++) {
84         DITHER_4444_SCAN(y);
85 
86         SkPMColor* p = bm.getAddr32(0, y);
87         for (int x = 0; x < bm.width(); x++) {
88             SkPMColor c = *p;
89 
90             unsigned a = SkGetPackedA32(c);
91             unsigned r = SkGetPackedR32(c);
92             unsigned g = SkGetPackedG32(c);
93             unsigned b = SkGetPackedB32(c);
94 
95             unsigned d = DITHER_VALUE(x);
96 
97             a = SkDITHER_A32To4444(a, d);
98             r = SkDITHER_R32To4444(r, d);
99             g = SkDITHER_G32To4444(g, d);
100             b = SkDITHER_B32To4444(b, d);
101 
102             a = SkA4444ToA32(a);
103             r = SkR4444ToR32(r);
104             g = SkG4444ToG32(g);
105             b = SkB4444ToB32(b);
106 
107             *p++ = SkPackARGB32(a, r, g, b);
108         }
109     }
110 }
111 
112 class DitherView : public SampleView {
113 public:
114     SkBitmap    fBM, fBMPreDither, fBM16;
115     SkScalar fAngle;
116 
DitherView()117     DitherView() {
118         make_bm(&fBM);
119         make_bm(&fBMPreDither);
120         pre_dither(fBMPreDither);
121         fBM.copyTo(&fBM16, kARGB_4444_SkColorType);
122 
123         fAngle = 0;
124 
125         this->setBGColor(0xFF181818);
126     }
127 
128 protected:
129     // overrides from SkEventSink
onQuery(SkEvent * evt)130     virtual bool onQuery(SkEvent* evt) {
131         if (SampleCode::TitleQ(*evt)) {
132             SampleCode::TitleR(evt, "Dither");
133             return true;
134         }
135         return this->INHERITED::onQuery(evt);
136     }
137 
onDrawContent(SkCanvas * canvas)138     virtual void onDrawContent(SkCanvas* canvas) {
139         SkPaint paint;
140         SkScalar x = SkIntToScalar(10);
141         SkScalar y = SkIntToScalar(10);
142         const SkScalar DX = SkIntToScalar(fBM.width() + 10);
143 
144         paint.setAntiAlias(true);
145 
146         if (true) {
147             canvas->drawBitmap(fBM, x, y, &paint);
148             x += DX;
149             paint.setDither(true);
150             canvas->drawBitmap(fBM, x, y, &paint);
151 
152             x += DX;
153             paint.setDither(false);
154             canvas->drawBitmap(fBMPreDither, x, y, &paint);
155 
156             x += DX;
157             canvas->drawBitmap(fBM16, x, y, &paint);
158         }
159 
160         canvas->translate(DX, DX*2);
161         draw_sweep(canvas, fBM.width(), fBM.height(), fAngle);
162         canvas->translate(DX, 0);
163         draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle);
164         canvas->translate(DX, 0);
165         draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle);
166 
167         fAngle += SK_Scalar1/2;
168         this->inval(nullptr);
169     }
170 
171 private:
172     typedef SampleView INHERITED;
173 };
174 
175 //////////////////////////////////////////////////////////////////////////////
176 
MyFactory()177 static SkView* MyFactory() { return new DitherView; }
178 static SkViewRegister reg(MyFactory);
179