michael@0: /* michael@0: * CAUTION: EXPERIMENTAL CODE michael@0: * michael@0: * This code is not to be used and will not be supported michael@0: * if it fails on you. DO NOT USE! michael@0: * michael@0: */ michael@0: michael@0: #include "SkPathUtils.h" michael@0: michael@0: #include "SkPath.h" michael@0: #include "SkPathOps.h" // this can't be found, how do I link it? michael@0: #include "SkRegion.h" michael@0: michael@0: typedef void (*line2path)(SkPath*, const char*, int, int); michael@0: #define SQRT_2 1.41421356237f michael@0: #define ON 0xFF000000 // black pixel michael@0: #define OFF 0x00000000 // transparent pixel michael@0: michael@0: // assumes stride is in bytes michael@0: /* michael@0: static void FillRandomBits( int chars, char* bits ){ michael@0: SkTime time; michael@0: SkRandom rand = SkRandom( time.GetMSecs() ); michael@0: michael@0: for (int i = 0; i < chars; ++i){ michael@0: bits[i] = rand.nextU(); michael@0: } michael@0: }OA michael@0: */ michael@0: michael@0: static int GetBit( const char* buffer, int x ) { michael@0: int byte = x >> 3; michael@0: int bit = x & 7; michael@0: michael@0: return buffer[byte] & (128 >> bit); michael@0: } michael@0: michael@0: /* michael@0: static void Line2path_pixel(SkPath* path, const char* line, michael@0: int lineIdx, int width) { michael@0: for (int i = 0; i < width; ++i) { michael@0: // simply makes every ON pixel into a rect path michael@0: if (GetBit(line,i)) { michael@0: path->addRect(SkRect::MakeXYWH(i, lineIdx, 1, 1), michael@0: SkPath::kCW_Direction); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void Line2path_pixelCircle(SkPath* path, const char* line, michael@0: int lineIdx, int width) { michael@0: for (int i = 0; i < width; ++i) { michael@0: // simply makes every ON pixel into a circle path michael@0: if (GetBit(line,i)) { michael@0: path->addCircle(i + SK_ScalarHalf, michael@0: lineIdx + SK_ScalarHalf, michael@0: SQRT_2 / 2.0f); michael@0: } michael@0: } michael@0: } michael@0: */ michael@0: michael@0: static void Line2path_span(SkPath* path, const char* line, michael@0: int lineIdx, int width) { michael@0: bool inRun = 0; michael@0: int start = 1; michael@0: michael@0: for (int i = 0; i < width; ++i) { michael@0: int curPixel = GetBit(line,i); michael@0: michael@0: if ( (curPixel!=0) != inRun ) { // if transition michael@0: if (curPixel) { // if transition on michael@0: inRun = 1; michael@0: start = i; // mark beginning of span michael@0: }else { // if transition off add the span as a path michael@0: inRun = 0; michael@0: path->addRect(SkRect::MakeXYWH(SkIntToScalar(start), SkIntToScalar(lineIdx), michael@0: SkIntToScalar(i-start), SK_Scalar1), michael@0: SkPath::kCW_Direction); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (inRun==1) { // close any open spans michael@0: int end = 0; michael@0: if ( GetBit(line,width-1) ) ++end; michael@0: path->addRect(SkRect::MakeXYWH(SkIntToScalar(start), SkIntToScalar(lineIdx), michael@0: SkIntToScalar(width - 1 + end - start), SK_Scalar1), michael@0: SkPath::kCW_Direction); michael@0: } else if ( GetBit(line, width - 1) ) { // if last pixel on add michael@0: path->addRect(SkRect::MakeXYWH(width - SK_Scalar1, SkIntToScalar(lineIdx), michael@0: SK_Scalar1, SK_Scalar1), michael@0: SkPath::kCW_Direction); michael@0: } michael@0: } michael@0: michael@0: void SkPathUtils::BitsToPath_Path(SkPath* path, michael@0: const char* bitmap, michael@0: int w, int h, int stride) { michael@0: // loop for every line in bitmap michael@0: for (int i = 0; i < h; ++i) { michael@0: // fn ptr handles each line separately michael@0: //l2p_fn(path, &bitmap[i*stride], i, w); michael@0: Line2path_span(path, &bitmap[i*stride], i, w); michael@0: } michael@0: Simplify(*path, path); // simplify resulting path. michael@0: } michael@0: michael@0: void SkPathUtils::BitsToPath_Region(SkPath* path, michael@0: const char* bitmap, michael@0: int w, int h, int stride) { michael@0: SkRegion region; michael@0: michael@0: // loop for each line michael@0: for (int y = 0; y < h; ++y){ michael@0: bool inRun = 0; michael@0: int start = 1; michael@0: const char* line = &bitmap[y * stride]; michael@0: michael@0: // loop for each pixel michael@0: for (int i = 0; i < w; ++i) { michael@0: int curPixel = GetBit(line,i); michael@0: michael@0: if ( (curPixel!=0) != inRun ) { // if transition michael@0: if (curPixel) { // if transition on michael@0: inRun = 1; michael@0: start = i; // mark beginning of span michael@0: }else { // if transition off add the span as a path michael@0: inRun = 0; michael@0: //add here michael@0: region.op(SkIRect::MakeXYWH(start, y, i-start, 1), michael@0: SkRegion::kUnion_Op ); michael@0: } michael@0: } michael@0: } michael@0: if (inRun==1) { // close any open spans michael@0: int end = 0; michael@0: if ( GetBit(line,w-1) ) ++end; michael@0: // add the thing here michael@0: region.op(SkIRect::MakeXYWH(start, y, w-1-start+end, 1), michael@0: SkRegion::kUnion_Op ); michael@0: michael@0: } else if ( GetBit(line,w-1) ) { // if last pixel on add rect michael@0: // add the thing here michael@0: region.op(SkIRect::MakeXYWH(w-1, y, 1, 1), michael@0: SkRegion::kUnion_Op ); michael@0: } michael@0: } michael@0: // convert region to path michael@0: region.getBoundaryPath(path); michael@0: }