Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * CAUTION: EXPERIMENTAL CODE |
michael@0 | 3 | * |
michael@0 | 4 | * This code is not to be used and will not be supported |
michael@0 | 5 | * if it fails on you. DO NOT USE! |
michael@0 | 6 | * |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | #include "SkPathUtils.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "SkPath.h" |
michael@0 | 12 | #include "SkPathOps.h" // this can't be found, how do I link it? |
michael@0 | 13 | #include "SkRegion.h" |
michael@0 | 14 | |
michael@0 | 15 | typedef void (*line2path)(SkPath*, const char*, int, int); |
michael@0 | 16 | #define SQRT_2 1.41421356237f |
michael@0 | 17 | #define ON 0xFF000000 // black pixel |
michael@0 | 18 | #define OFF 0x00000000 // transparent pixel |
michael@0 | 19 | |
michael@0 | 20 | // assumes stride is in bytes |
michael@0 | 21 | /* |
michael@0 | 22 | static void FillRandomBits( int chars, char* bits ){ |
michael@0 | 23 | SkTime time; |
michael@0 | 24 | SkRandom rand = SkRandom( time.GetMSecs() ); |
michael@0 | 25 | |
michael@0 | 26 | for (int i = 0; i < chars; ++i){ |
michael@0 | 27 | bits[i] = rand.nextU(); |
michael@0 | 28 | } |
michael@0 | 29 | }OA |
michael@0 | 30 | */ |
michael@0 | 31 | |
michael@0 | 32 | static int GetBit( const char* buffer, int x ) { |
michael@0 | 33 | int byte = x >> 3; |
michael@0 | 34 | int bit = x & 7; |
michael@0 | 35 | |
michael@0 | 36 | return buffer[byte] & (128 >> bit); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | /* |
michael@0 | 40 | static void Line2path_pixel(SkPath* path, const char* line, |
michael@0 | 41 | int lineIdx, int width) { |
michael@0 | 42 | for (int i = 0; i < width; ++i) { |
michael@0 | 43 | // simply makes every ON pixel into a rect path |
michael@0 | 44 | if (GetBit(line,i)) { |
michael@0 | 45 | path->addRect(SkRect::MakeXYWH(i, lineIdx, 1, 1), |
michael@0 | 46 | SkPath::kCW_Direction); |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | static void Line2path_pixelCircle(SkPath* path, const char* line, |
michael@0 | 52 | int lineIdx, int width) { |
michael@0 | 53 | for (int i = 0; i < width; ++i) { |
michael@0 | 54 | // simply makes every ON pixel into a circle path |
michael@0 | 55 | if (GetBit(line,i)) { |
michael@0 | 56 | path->addCircle(i + SK_ScalarHalf, |
michael@0 | 57 | lineIdx + SK_ScalarHalf, |
michael@0 | 58 | SQRT_2 / 2.0f); |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | */ |
michael@0 | 63 | |
michael@0 | 64 | static void Line2path_span(SkPath* path, const char* line, |
michael@0 | 65 | int lineIdx, int width) { |
michael@0 | 66 | bool inRun = 0; |
michael@0 | 67 | int start = 1; |
michael@0 | 68 | |
michael@0 | 69 | for (int i = 0; i < width; ++i) { |
michael@0 | 70 | int curPixel = GetBit(line,i); |
michael@0 | 71 | |
michael@0 | 72 | if ( (curPixel!=0) != inRun ) { // if transition |
michael@0 | 73 | if (curPixel) { // if transition on |
michael@0 | 74 | inRun = 1; |
michael@0 | 75 | start = i; // mark beginning of span |
michael@0 | 76 | }else { // if transition off add the span as a path |
michael@0 | 77 | inRun = 0; |
michael@0 | 78 | path->addRect(SkRect::MakeXYWH(SkIntToScalar(start), SkIntToScalar(lineIdx), |
michael@0 | 79 | SkIntToScalar(i-start), SK_Scalar1), |
michael@0 | 80 | SkPath::kCW_Direction); |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | if (inRun==1) { // close any open spans |
michael@0 | 86 | int end = 0; |
michael@0 | 87 | if ( GetBit(line,width-1) ) ++end; |
michael@0 | 88 | path->addRect(SkRect::MakeXYWH(SkIntToScalar(start), SkIntToScalar(lineIdx), |
michael@0 | 89 | SkIntToScalar(width - 1 + end - start), SK_Scalar1), |
michael@0 | 90 | SkPath::kCW_Direction); |
michael@0 | 91 | } else if ( GetBit(line, width - 1) ) { // if last pixel on add |
michael@0 | 92 | path->addRect(SkRect::MakeXYWH(width - SK_Scalar1, SkIntToScalar(lineIdx), |
michael@0 | 93 | SK_Scalar1, SK_Scalar1), |
michael@0 | 94 | SkPath::kCW_Direction); |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | void SkPathUtils::BitsToPath_Path(SkPath* path, |
michael@0 | 99 | const char* bitmap, |
michael@0 | 100 | int w, int h, int stride) { |
michael@0 | 101 | // loop for every line in bitmap |
michael@0 | 102 | for (int i = 0; i < h; ++i) { |
michael@0 | 103 | // fn ptr handles each line separately |
michael@0 | 104 | //l2p_fn(path, &bitmap[i*stride], i, w); |
michael@0 | 105 | Line2path_span(path, &bitmap[i*stride], i, w); |
michael@0 | 106 | } |
michael@0 | 107 | Simplify(*path, path); // simplify resulting path. |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | void SkPathUtils::BitsToPath_Region(SkPath* path, |
michael@0 | 111 | const char* bitmap, |
michael@0 | 112 | int w, int h, int stride) { |
michael@0 | 113 | SkRegion region; |
michael@0 | 114 | |
michael@0 | 115 | // loop for each line |
michael@0 | 116 | for (int y = 0; y < h; ++y){ |
michael@0 | 117 | bool inRun = 0; |
michael@0 | 118 | int start = 1; |
michael@0 | 119 | const char* line = &bitmap[y * stride]; |
michael@0 | 120 | |
michael@0 | 121 | // loop for each pixel |
michael@0 | 122 | for (int i = 0; i < w; ++i) { |
michael@0 | 123 | int curPixel = GetBit(line,i); |
michael@0 | 124 | |
michael@0 | 125 | if ( (curPixel!=0) != inRun ) { // if transition |
michael@0 | 126 | if (curPixel) { // if transition on |
michael@0 | 127 | inRun = 1; |
michael@0 | 128 | start = i; // mark beginning of span |
michael@0 | 129 | }else { // if transition off add the span as a path |
michael@0 | 130 | inRun = 0; |
michael@0 | 131 | //add here |
michael@0 | 132 | region.op(SkIRect::MakeXYWH(start, y, i-start, 1), |
michael@0 | 133 | SkRegion::kUnion_Op ); |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | if (inRun==1) { // close any open spans |
michael@0 | 138 | int end = 0; |
michael@0 | 139 | if ( GetBit(line,w-1) ) ++end; |
michael@0 | 140 | // add the thing here |
michael@0 | 141 | region.op(SkIRect::MakeXYWH(start, y, w-1-start+end, 1), |
michael@0 | 142 | SkRegion::kUnion_Op ); |
michael@0 | 143 | |
michael@0 | 144 | } else if ( GetBit(line,w-1) ) { // if last pixel on add rect |
michael@0 | 145 | // add the thing here |
michael@0 | 146 | region.op(SkIRect::MakeXYWH(w-1, y, 1, 1), |
michael@0 | 147 | SkRegion::kUnion_Op ); |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | // convert region to path |
michael@0 | 151 | region.getBoundaryPath(path); |
michael@0 | 152 | } |