Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: src/utils/SkPathUtils.cpp

Issue 16829003: Adding my Bitmap2Path sample for 1on1 meeting. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Added SkRegion mode to PathUtils and Test. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * CAUTION: EXPERIMENTAL CODE
3 *
4 * This code is not to be used and will not be supported
5 * if it fails on you. DO NOT USE!
6 *
7 *
8 *
9 */
10
11 #include "SkPathUtils.h"
12
13 #include "SkPath.h"
14 //#include "SkPathOps.h" // this can't be found
15 #include "SkRandom.h"
16 #include "SkRegion.h"
17 #include "SkTime.h"
18
19
20 // assumes stride is in bytes
21 void SkPathUtils::fillRandomBits( int chars, char* bits ){
22 SkTime time;
23 SkMWCRandom rand = SkMWCRandom( time.GetMSecs() );
24
25 for (int i = 0; i < chars; ++i){
26 bits[i] = rand.nextU();
27 }
28 }
29
30 int SkPathUtils::getBit( const char* buffer, int x ) {
31 int byte = x >> 3;
32 int bit = x & 7;
33
34 return buffer[byte] & (1 << bit);
35 }
36
37 void SkPathUtils::line2path_pixel(SkPath* path, const char* line,
38 int lineIdx, int width) {
39 for (int i = 0; i < width; ++i) {
40 // simply makes every ON pixel into a rect path
41 if (getBit(line,i)) {
42 path->addRect(SkRect::MakeXYWH(i, lineIdx, 1, 1),
43 SkPath::kCW_Direction);
44 }
45 }
46 }
47
48 void SkPathUtils::line2path_pixelCircle(SkPath* path, const char* line,
49 int lineIdx, int width) {
50 for (int i = 0; i < width; ++i) {
51 // simply makes every ON pixel into a circle path
52 if (getBit(line,i)) {
53 path->addCircle(i + SK_ScalarHalf,
54 lineIdx + SK_ScalarHalf,
55 SkFloatToScalar(SQRT_2 / 2.0f));
56 }
57 }
58 }
59
60 void SkPathUtils::line2path_span(SkPath* path, const char* line,
61 int lineIdx, int width) {
62 bool inRun = 0;
63 int start = 1;
64
65 for (int i = 0; i < width; ++i) {
66 int curPixel = getBit(line,i);
67
68 if ( (curPixel!=0) != inRun ) { // if transition
69 if (curPixel) { // if transition on
70 inRun = 1;
71 start = i; // mark beginning of span
72 }else { // if transition off add the span as a path
73 inRun = 0;
74 path->addRect(SkRect::MakeXYWH(start, lineIdx, i-start, 1),
75 SkPath::kCW_Direction);
76 }
77 }
78 }
79
80 if (inRun==1) { // close any open spans
81 int end = 0;
82 if ( getBit(line,width-1) ) ++end;
83 path->addRect(SkRect::MakeXYWH(start, lineIdx,
84 width - 1 + end - start, 1),
85 SkPath::kCW_Direction);
86 } else if ( getBit(line,width-1) ) { // if last pixel on add rect
87 path->addRect(SkRect::MakeXYWH(width-1, lineIdx, 1, 1),
88 SkPath::kCW_Direction);
89 }
90 }
91
92 void SkPathUtils::bitmap2path(SkPath* path,
93 const char* bitmap,
94 const line2path l2p_fn,
95 int h, int w, int stride) {
96 // loop for every line in bitmap
97 for (int i = 0; i < h; ++i) {
98 // fn ptr handles each line separately
99 l2p_fn(path, &bitmap[i*stride], i, w);
100 }
101 // Simplify(*path, path); // simplify resulting bitmap
102 }
103
104 void SkPathUtils::bitmap2path_region(SkPath* path,
105 const char* bitmap,
106 int h, int w, int stride) {
107 SkRegion region;
108
109 // loop for each line
110 for (int y = 0; y < h; ++y){
111 bool inRun = 0;
112 int start = 1;
113 const char* line = &bitmap[y * stride];
114
115 // loop for each pixel
116 for (int i = 0; i < w; ++i) {
117 int curPixel = getBit(line,i);
118
119 if ( (curPixel!=0) != inRun ) { // if transition
120 if (curPixel) { // if transition on
121 inRun = 1;
122 start = i; // mark beginning of span
123 }else { // if transition off add the span as a path
124 inRun = 0;
125 //add here
126 region.op(SkIRect::MakeXYWH(start, y, i-start, 1),
127 SkRegion::kUnion_Op );
128 }
129 }
130 }
131 if (inRun==1) { // close any open spans
132 int end = 0;
133 if ( getBit(line,w-1) ) ++end;
134 // add the thing here
135 region.op(SkIRect::MakeXYWH(start, y, w-1-start+end, 1),
136 SkRegion::kUnion_Op );
137
138 } else if ( getBit(line,w-1) ) { // if last pixel on add rect
139 // add the thing here
140 region.op(SkIRect::MakeXYWH(w-1, y, 1, 1),
141 SkRegion::kUnion_Op );
142 }
143 }
144
145 // convert region to path
146 region.getBoundaryPath(path);
147 //Simplify(*path, path); // simplify the path? may be uneccessary
reed1 2013/06/26 16:54:24 Could test, but I think this is never necessary.
148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698