OLD | NEW |
(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 #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 |
| 15 typedef 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 /* |
| 22 static void FillRandomBits( int chars, char* bits ){ |
| 23 SkTime time; |
| 24 SkMWCRandom rand = SkMWCRandom( time.GetMSecs() ); |
| 25 |
| 26 for (int i = 0; i < chars; ++i){ |
| 27 bits[i] = rand.nextU(); |
| 28 } |
| 29 }OA |
| 30 */ |
| 31 |
| 32 static 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 /* |
| 40 static 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 |
| 51 static 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, |
| 58 SkFloatToScalar(SQRT_2 / 2.0f)); |
| 59 } |
| 60 } |
| 61 } |
| 62 */ |
| 63 |
| 64 static 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; |
| 78 path->addRect(SkRect::MakeXYWH(start, lineIdx, i-start, 1), |
| 79 SkPath::kCW_Direction); |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 if (inRun==1) { // close any open spans |
| 85 int end = 0; |
| 86 if ( GetBit(line,width-1) ) ++end; |
| 87 path->addRect(SkRect::MakeXYWH(start, lineIdx, |
| 88 width - 1 + end - start, 1), |
| 89 SkPath::kCW_Direction); |
| 90 } else if ( GetBit(line,width-1) ) { // if last pixel on add rect |
| 91 path->addRect(SkRect::MakeXYWH(width-1, lineIdx, 1, 1), |
| 92 SkPath::kCW_Direction); |
| 93 } |
| 94 } |
| 95 |
| 96 void SkPathUtils::BitsToPath_Path(SkPath* path, |
| 97 const char* bitmap, |
| 98 int h, int w, int stride) { |
| 99 // loop for every line in bitmap |
| 100 for (int i = 0; i < h; ++i) { |
| 101 // fn ptr handles each line separately |
| 102 //l2p_fn(path, &bitmap[i*stride], i, w); |
| 103 Line2path_span(path, &bitmap[i*stride], i, w); |
| 104 } |
| 105 Simplify(*path, path); // simplify resulting bitmap |
| 106 } |
| 107 |
| 108 void SkPathUtils::BitsToPath_Region(SkPath* path, |
| 109 const char* bitmap, |
| 110 int h, int w, int stride) { |
| 111 SkRegion region; |
| 112 |
| 113 // loop for each line |
| 114 for (int y = 0; y < h; ++y){ |
| 115 bool inRun = 0; |
| 116 int start = 1; |
| 117 const char* line = &bitmap[y * stride]; |
| 118 |
| 119 // loop for each pixel |
| 120 for (int i = 0; i < w; ++i) { |
| 121 int curPixel = GetBit(line,i); |
| 122 |
| 123 if ( (curPixel!=0) != inRun ) { // if transition |
| 124 if (curPixel) { // if transition on |
| 125 inRun = 1; |
| 126 start = i; // mark beginning of span |
| 127 }else { // if transition off add the span as a path |
| 128 inRun = 0; |
| 129 //add here |
| 130 region.op(SkIRect::MakeXYWH(start, y, i-start, 1), |
| 131 SkRegion::kUnion_Op ); |
| 132 } |
| 133 } |
| 134 } |
| 135 if (inRun==1) { // close any open spans |
| 136 int end = 0; |
| 137 if ( GetBit(line,w-1) ) ++end; |
| 138 // add the thing here |
| 139 region.op(SkIRect::MakeXYWH(start, y, w-1-start+end, 1), |
| 140 SkRegion::kUnion_Op ); |
| 141 |
| 142 } else if ( GetBit(line,w-1) ) { // if last pixel on add rect |
| 143 // add the thing here |
| 144 region.op(SkIRect::MakeXYWH(w-1, y, 1, 1), |
| 145 SkRegion::kUnion_Op ); |
| 146 } |
| 147 } |
| 148 // convert region to path |
| 149 region.getBoundaryPath(path); |
| 150 } |
OLD | NEW |