blob: fdca09aa6307dab132580206fefbe64518365513 [file] [log] [blame]
commit-bot@chromium.org064779a2013-07-01 17:50:29 +00001/*
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#include "SkPathUtils.h"
10
11#include "SkPath.h"
12#include "SkPathOps.h" // this can't be found, how do I link it?
13#include "SkRegion.h"
14
15typedef void (*line2path)(SkPath*, const char*, int, int);
16#define SQRT_2 1.41421356237f
17#define ON 0xFF000000 // black pixel
18#define OFF 0x00000000 // transparent pixel
19
20// assumes stride is in bytes
21/*
22static void FillRandomBits( int chars, char* bits ){
23 SkTime time;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000024 SkRandom rand = SkRandom( time.GetMSecs() );
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000025
26 for (int i = 0; i < chars; ++i){
27 bits[i] = rand.nextU();
28 }
29}OA
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000030*/
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000031
32static int GetBit( const char* buffer, int x ) {
33 int byte = x >> 3;
34 int bit = x & 7;
35
36 return buffer[byte] & (128 >> bit);
37}
38
39/*
40static void Line2path_pixel(SkPath* path, const char* line,
41 int lineIdx, int width) {
42 for (int i = 0; i < width; ++i) {
43 // simply makes every ON pixel into a rect path
44 if (GetBit(line,i)) {
45 path->addRect(SkRect::MakeXYWH(i, lineIdx, 1, 1),
46 SkPath::kCW_Direction);
47 }
48 }
49}
50
51static void Line2path_pixelCircle(SkPath* path, const char* line,
52 int lineIdx, int width) {
53 for (int i = 0; i < width; ++i) {
54 // simply makes every ON pixel into a circle path
55 if (GetBit(line,i)) {
56 path->addCircle(i + SK_ScalarHalf,
57 lineIdx + SK_ScalarHalf,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000058 SQRT_2 / 2.0f);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000059 }
60 }
61}
62*/
63
64static void Line2path_span(SkPath* path, const char* line,
65 int lineIdx, int width) {
66 bool inRun = 0;
67 int start = 1;
68
69 for (int i = 0; i < width; ++i) {
70 int curPixel = GetBit(line,i);
71
72 if ( (curPixel!=0) != inRun ) { // if transition
73 if (curPixel) { // if transition on
74 inRun = 1;
75 start = i; // mark beginning of span
76 }else { // if transition off add the span as a path
77 inRun = 0;
dierk@google.com6b423122013-07-01 20:50:41 +000078 path->addRect(SkRect::MakeXYWH(SkIntToScalar(start), SkIntToScalar(lineIdx),
dierk@google.com19b84382013-07-01 20:10:56 +000079 SkIntToScalar(i-start), SK_Scalar1),
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000080 SkPath::kCW_Direction);
81 }
82 }
83 }
84
85 if (inRun==1) { // close any open spans
86 int end = 0;
87 if ( GetBit(line,width-1) ) ++end;
dierk@google.com6b423122013-07-01 20:50:41 +000088 path->addRect(SkRect::MakeXYWH(SkIntToScalar(start), SkIntToScalar(lineIdx),
89 SkIntToScalar(width - 1 + end - start), SK_Scalar1),
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000090 SkPath::kCW_Direction);
dierk@google.com6bee4ec2013-07-01 19:52:36 +000091 } else if ( GetBit(line, width - 1) ) { // if last pixel on add
dierk@google.com6b423122013-07-01 20:50:41 +000092 path->addRect(SkRect::MakeXYWH(width - SK_Scalar1, SkIntToScalar(lineIdx),
dierk@google.com74887b62013-07-01 19:30:14 +000093 SK_Scalar1, SK_Scalar1),
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000094 SkPath::kCW_Direction);
95 }
96}
97
98void SkPathUtils::BitsToPath_Path(SkPath* path,
99 const char* bitmap,
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +0000100 int w, int h, int stride) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000101 // loop for every line in bitmap
102 for (int i = 0; i < h; ++i) {
103 // fn ptr handles each line separately
104 //l2p_fn(path, &bitmap[i*stride], i, w);
105 Line2path_span(path, &bitmap[i*stride], i, w);
106 }
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +0000107 Simplify(*path, path); // simplify resulting path.
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000108}
109
110void SkPathUtils::BitsToPath_Region(SkPath* path,
111 const char* bitmap,
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +0000112 int w, int h, int stride) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000113 SkRegion region;
114
115 // loop for each line
116 for (int y = 0; y < h; ++y){
117 bool inRun = 0;
118 int start = 1;
119 const char* line = &bitmap[y * stride];
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000120
121 // loop for each pixel
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000122 for (int i = 0; i < w; ++i) {
123 int curPixel = GetBit(line,i);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000124
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000125 if ( (curPixel!=0) != inRun ) { // if transition
126 if (curPixel) { // if transition on
127 inRun = 1;
128 start = i; // mark beginning of span
129 }else { // if transition off add the span as a path
130 inRun = 0;
131 //add here
132 region.op(SkIRect::MakeXYWH(start, y, i-start, 1),
133 SkRegion::kUnion_Op );
134 }
135 }
136 }
137 if (inRun==1) { // close any open spans
138 int end = 0;
139 if ( GetBit(line,w-1) ) ++end;
140 // add the thing here
141 region.op(SkIRect::MakeXYWH(start, y, w-1-start+end, 1),
142 SkRegion::kUnion_Op );
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000143
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000144 } else if ( GetBit(line,w-1) ) { // if last pixel on add rect
145 // add the thing here
146 region.op(SkIRect::MakeXYWH(w-1, y, 1, 1),
147 SkRegion::kUnion_Op );
148 }
149 }
150 // convert region to path
151 region.getBoundaryPath(path);
152}