Chromium Code Reviews| Index: src/utils/SkPathUtils.cpp |
| diff --git a/src/utils/SkPathUtils.cpp b/src/utils/SkPathUtils.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..555802452d6ed6c32320b5aaca60ba45d63ad421 |
| --- /dev/null |
| +++ b/src/utils/SkPathUtils.cpp |
| @@ -0,0 +1,148 @@ |
| +/* |
| + * CAUTION: EXPERIMENTAL CODE |
| + * |
| + * This code is not to be used and will not be supported |
| + * if it fails on you. DO NOT USE! |
| + * |
| + * |
| + * |
| + */ |
| + |
| +#include "SkPathUtils.h" |
| + |
| +#include "SkPath.h" |
| +//#include "SkPathOps.h" // this can't be found |
| +#include "SkRandom.h" |
| +#include "SkRegion.h" |
| +#include "SkTime.h" |
| + |
| + |
| +// assumes stride is in bytes |
| +void SkPathUtils::fillRandomBits( int chars, char* bits ){ |
| + SkTime time; |
| + SkMWCRandom rand = SkMWCRandom( time.GetMSecs() ); |
| + |
| + for (int i = 0; i < chars; ++i){ |
| + bits[i] = rand.nextU(); |
| + } |
| +} |
| + |
| +int SkPathUtils::getBit( const char* buffer, int x ) { |
| + int byte = x >> 3; |
| + int bit = x & 7; |
| + |
| + return buffer[byte] & (1 << bit); |
| +} |
| + |
| +void SkPathUtils::line2path_pixel(SkPath* path, const char* line, |
| + int lineIdx, int width) { |
| + for (int i = 0; i < width; ++i) { |
| + // simply makes every ON pixel into a rect path |
| + if (getBit(line,i)) { |
| + path->addRect(SkRect::MakeXYWH(i, lineIdx, 1, 1), |
| + SkPath::kCW_Direction); |
| + } |
| + } |
| +} |
| + |
| +void SkPathUtils::line2path_pixelCircle(SkPath* path, const char* line, |
| + int lineIdx, int width) { |
| + for (int i = 0; i < width; ++i) { |
| + // simply makes every ON pixel into a circle path |
| + if (getBit(line,i)) { |
| + path->addCircle(i + SK_ScalarHalf, |
| + lineIdx + SK_ScalarHalf, |
| + SkFloatToScalar(SQRT_2 / 2.0f)); |
| + } |
| + } |
| +} |
| + |
| +void SkPathUtils::line2path_span(SkPath* path, const char* line, |
| + int lineIdx, int width) { |
| + bool inRun = 0; |
| + int start = 1; |
| + |
| + for (int i = 0; i < width; ++i) { |
| + int curPixel = getBit(line,i); |
| + |
| + if ( (curPixel!=0) != inRun ) { // if transition |
| + if (curPixel) { // if transition on |
| + inRun = 1; |
| + start = i; // mark beginning of span |
| + }else { // if transition off add the span as a path |
| + inRun = 0; |
| + path->addRect(SkRect::MakeXYWH(start, lineIdx, i-start, 1), |
| + SkPath::kCW_Direction); |
| + } |
| + } |
| + } |
| + |
| + if (inRun==1) { // close any open spans |
| + int end = 0; |
| + if ( getBit(line,width-1) ) ++end; |
| + path->addRect(SkRect::MakeXYWH(start, lineIdx, |
| + width - 1 + end - start, 1), |
| + SkPath::kCW_Direction); |
| + } else if ( getBit(line,width-1) ) { // if last pixel on add rect |
| + path->addRect(SkRect::MakeXYWH(width-1, lineIdx, 1, 1), |
| + SkPath::kCW_Direction); |
| + } |
| +} |
| + |
| +void SkPathUtils::bitmap2path(SkPath* path, |
| + const char* bitmap, |
| + const line2path l2p_fn, |
| + int h, int w, int stride) { |
| + // loop for every line in bitmap |
| + for (int i = 0; i < h; ++i) { |
| + // fn ptr handles each line separately |
| + l2p_fn(path, &bitmap[i*stride], i, w); |
| + } |
| +// Simplify(*path, path); // simplify resulting bitmap |
| +} |
| + |
| +void SkPathUtils::bitmap2path_region(SkPath* path, |
| + const char* bitmap, |
| + int h, int w, int stride) { |
| + SkRegion region; |
| + |
| + // loop for each line |
| + for (int y = 0; y < h; ++y){ |
| + bool inRun = 0; |
| + int start = 1; |
| + const char* line = &bitmap[y * stride]; |
| + |
| + // loop for each pixel |
| + for (int i = 0; i < w; ++i) { |
| + int curPixel = getBit(line,i); |
| + |
| + if ( (curPixel!=0) != inRun ) { // if transition |
| + if (curPixel) { // if transition on |
| + inRun = 1; |
| + start = i; // mark beginning of span |
| + }else { // if transition off add the span as a path |
| + inRun = 0; |
| + //add here |
| + region.op(SkIRect::MakeXYWH(start, y, i-start, 1), |
| + SkRegion::kUnion_Op ); |
| + } |
| + } |
| + } |
| + if (inRun==1) { // close any open spans |
| + int end = 0; |
| + if ( getBit(line,w-1) ) ++end; |
| + // add the thing here |
| + region.op(SkIRect::MakeXYWH(start, y, w-1-start+end, 1), |
| + SkRegion::kUnion_Op ); |
| + |
| + } else if ( getBit(line,w-1) ) { // if last pixel on add rect |
| + // add the thing here |
| + region.op(SkIRect::MakeXYWH(w-1, y, 1, 1), |
| + SkRegion::kUnion_Op ); |
| + } |
| + } |
| + |
| + // convert region to path |
| + region.getBoundaryPath(path); |
| + //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.
|
| +} |