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