OLD | NEW |
---|---|
(Empty) | |
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 "SampleCode.h" | |
9 #include "SkView.h" | |
10 #include "SkCanvas.h" | |
11 #include "SkPathOps.h" | |
12 | |
13 #define CHAR_BIT 8 | |
14 #define SQRT_2 1.41421356237 | |
15 | |
16 bool getBit( const char *buffer, | |
reed1
2013/06/13 17:58:41
any function local to your file... static bool ...
| |
17 const int x ) { | |
reed1
2013/06/13 17:58:41
for parameters, const on a non-ptr is never meanin
| |
18 | |
19 int byte = x / CHAR_BIT; | |
reed1
2013/06/13 17:58:41
nit: when you know your modulus is a power of two,
| |
20 int bit = x % CHAR_BIT; | |
21 | |
22 return buffer[byte] & (1 << bit); | |
reed1
2013/06/13 17:58:41
this is not a bool, but an int (which is fine), bu
| |
23 } | |
24 | |
25 | |
26 void line2path_pixel( SkPath* path, | |
27 const char* line, | |
28 const int lineIdx, | |
29 const int width ) { | |
30 for (int i=0; i<width; i++) { | |
31 // simply makes every ON pixel into a rect path | |
32 if (getBit(line,i)==1) { | |
33 // add a rect for the pixel | |
34 SkRect r = { i, lineIdx, i+1, lineIdx+1 }; | |
reed1
2013/06/13 17:58:41
alternate:
SkRect::MakeXYWH(i, lineIdx, 1, 1)
co
| |
35 path->addRect(r, SkPath::kCW_Direction); | |
36 } | |
37 } | |
38 } | |
39 | |
40 void line2path_pixelCircle( SkPath* path, | |
reed1
2013/06/13 17:58:41
all these guys can be marked 'static'
| |
41 const char* line, | |
42 const int lineIdx, | |
43 const int width ) { | |
44 for (int i=0; i<width; i++) { | |
reed1
2013/06/13 17:58:41
very nitty: need more space, and pre-increment
fo
| |
45 // simply makes every ON pixel into a circle path | |
46 if (getBit(line,i)==1) { | |
reed1
2013/06/13 17:58:41
if (getBit(...)) {
| |
47 // add a circle for the pixel | |
48 path->addCircle( i+.5, lineIdx+.5, SQRT_2 / 2.0 ); | |
reed1
2013/06/13 17:58:41
all 3 params will generate doubles...
use 0.5f if
| |
49 } | |
50 } | |
51 } | |
52 | |
53 void line2path_span( SkPath* path, | |
54 const char* line, | |
55 const int lineIdx, | |
56 const int width ) { | |
57 bool state = 0; | |
58 int start = 1; | |
59 | |
60 for (int i=0; i<width; i++) { | |
61 if (i==width-1) { // if the end of a scanline, close open paths | |
62 if (state==1) { | |
63 SkRect r = { start, lineIdx, | |
64 i+getBit(line,i), lineIdx+1 }; | |
65 path->addRect(r, SkPath::kCW_Direction); | |
66 } else if (getBit(line,i)==1) { | |
67 SkRect r = { i, lineIdx, | |
68 i+1, lineIdx+1 }; | |
69 path->addRect(r, SkPath::kCW_Direction); | |
70 } | |
71 start = 0; | |
72 state = 0; | |
73 // if a transition point: | |
74 }else if (getBit(line,i)!=state) { | |
75 if (getBit(line,i)==1) { // transition on | |
76 state = 1; //mark beginning of span | |
77 start = i; | |
78 }else { // transition off | |
79 state = 0; | |
80 SkRect r = { start, lineIdx, | |
81 i, lineIdx+1}; | |
82 path->addRect(r, SkPath::kCW_Direction); | |
83 } | |
84 } | |
85 } | |
86 } | |
87 | |
88 void bitmap2path( SkPath* path, | |
89 const char* bitmap, | |
90 void (*line2path)( SkPath*, | |
91 const char*, | |
92 const int, | |
93 const int ), | |
94 const int h, const int w, | |
95 const int stride ) { | |
96 // loop for every line in bitmap | |
97 for (int i=0; i<h; i++) { | |
98 line2path(path, &bitmap[i*stride], i, w); | |
99 } | |
100 } | |
101 | |
102 | |
103 class Bitmap2Path : public SampleView { | |
104 public: | |
105 Bitmap2Path() { | |
106 } | |
107 | |
108 protected: | |
109 // overrides from SkEventSink | |
110 virtual bool onQuery(SkEvent* evt) { | |
111 if (SampleCode::TitleQ(*evt)) { | |
112 SampleCode::TitleR(evt, "Bitmap2Path"); | |
113 return true; | |
114 } | |
115 return this->INHERITED::onQuery(evt); | |
116 } | |
117 | |
118 virtual void onDrawContent(SkCanvas* canvas) { | |
119 const char raw_bits[] = { | |
120 0x99, 0x00, | |
121 0x5a, 0x00, | |
122 0x3c, 0x00, | |
123 0x18, 0x00, | |
124 0x99, 0x00, | |
125 0x5a, 0x00, | |
126 0x3c, 0x00, | |
127 0x18, 0x00, | |
128 }; | |
129 | |
130 // pointer into memory for binary bitmap | |
131 const char* bitmap = (char*) &raw_bits; | |
132 int h=8, w=8, str=2; | |
133 | |
134 // array of funtion pointers for modes (allows looping) | |
135 const int numModes = 3; | |
136 void (*mode[numModes])( SkPath*, const char*, | |
reed1
2013/06/13 17:58:41
const void (*....
maybe typdef this guy to a name
| |
137 const int, const int ) | |
138 = { line2path_pixel, line2path_pixelCircle, line2path_span }; | |
139 | |
140 // select paint for drawing binary pixels -- BLACK | |
141 SkPaint paint; | |
142 paint.setAntiAlias(true); | |
143 paint.setStyle(SkPaint::kFill_Style); | |
144 paint.setColor(SK_ColorBLACK); | |
145 | |
146 // for each mode | |
147 for (int i=0; i<numModes; i++) | |
148 { | |
149 //convert bitmap to path | |
150 SkPath path; | |
151 bitmap2path(&path,bitmap,mode[i],h,w,str); | |
152 | |
153 //simplify path | |
154 SkPath simple; | |
155 Simplify(path, &simple); | |
156 | |
157 //draw to canvas | |
158 canvas->save(); | |
159 canvas->scale(10.0, 10.0); | |
reed1
2013/06/13 17:58:41
more doubles
canvas->scale(10, 10);
| |
160 canvas->translate(10.0*i, 0); | |
161 canvas->drawPath(simple, paint); | |
162 canvas->restore(); | |
163 } | |
164 | |
165 // this->inval(NULL); | |
166 } | |
167 | |
168 private: | |
169 typedef SkView INHERITED; | |
170 }; | |
171 | |
172 ////////////////////////////////////////////////////////////////////////////// | |
173 | |
174 static SkView* MyFactory() { return new Bitmap2Path; } | |
175 static SkViewRegister reg(MyFactory); | |
OLD | NEW |