| 1 |
From: AlbrechtS <[email protected]> |
| 2 |
Date: Sun, 4 Feb 2018 23:47:38 +0100 |
| 3 |
Subject: Modify rasterizer to support non-square X,Y axes scaling. |
| 4 |
|
| 5 |
Add new function nsvgRasterizeXY() similar to nsvgRasterize() but with |
| 6 |
separate scaling factors for x-axis and y-axis. |
| 7 |
|
| 8 |
Origin: https://github.com/fltk/nanosvg/commit/abcd277.patch |
| 9 |
Bug: https://github.com/memononen/nanosvg/pull/274 |
| 10 |
Forwarded: yes |
| 11 |
Reviewed-By: Chow Loong Jin <[email protected]> |
| 12 |
--- |
| 13 |
src/nanosvgrast.h | 79 ++++++++++++++++++++++++++++++++++++------------------- |
| 14 |
1 file changed, 52 insertions(+), 27 deletions(-) |
| 15 |
|
| 16 |
diff --git a/src/nanosvgrast.h b/src/nanosvgrast.h |
| 17 |
index ba512c7..1804405 100644 |
| 18 |
--- a/src/nanosvgrast.h |
| 19 |
+++ b/src/nanosvgrast.h |
| 20 |
@@ -22,6 +22,12 @@ |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
+/* Modified by FLTK to support non-square X,Y axes scaling. |
| 25 |
+ * |
| 26 |
+ * Added: nsvgRasterizeXY() |
| 27 |
+*/ |
| 28 |
+ |
| 29 |
+ |
| 30 |
#ifndef NANOSVGRAST_H |
| 31 |
#define NANOSVGRAST_H |
| 32 |
|
| 33 |
@@ -46,6 +52,9 @@ typedef struct NSVGrasterizer NSVGrasterizer; |
| 34 |
unsigned char* img = malloc(w*h*4); |
| 35 |
// Rasterize |
| 36 |
nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4); |
| 37 |
+ |
| 38 |
+ // For non-square X,Y scaling, use |
| 39 |
+ nsvgRasterizeXY(rast, image, 0,0,1,1, img, w, h, w*4); |
| 40 |
*/ |
| 41 |
|
| 42 |
// Allocated rasterizer context. |
| 43 |
@@ -55,7 +64,7 @@ NSVGrasterizer* nsvgCreateRasterizer(void); |
| 44 |
// r - pointer to rasterizer context |
| 45 |
// image - pointer to image to rasterize |
| 46 |
// tx,ty - image offset (applied after scaling) |
| 47 |
-// scale - image scale |
| 48 |
+// scale - image scale (assumes square aspect ratio) |
| 49 |
// dst - pointer to destination image data, 4 bytes per pixel (RGBA) |
| 50 |
// w - width of the image to render |
| 51 |
// h - height of the image to render |
| 52 |
@@ -64,6 +73,12 @@ void nsvgRasterize(NSVGrasterizer* r, |
| 53 |
NSVGimage* image, float tx, float ty, float scale, |
| 54 |
unsigned char* dst, int w, int h, int stride); |
| 55 |
|
| 56 |
+// As above, but allow X and Y axes to scale independently for non-square aspects |
| 57 |
+void nsvgRasterizeXY(NSVGrasterizer* r, |
| 58 |
+ NSVGimage* image, float tx, float ty, |
| 59 |
+ float sx, float sy, |
| 60 |
+ unsigned char* dst, int w, int h, int stride); |
| 61 |
+ |
| 62 |
// Deletes rasterizer context. |
| 63 |
void nsvgDeleteRasterizer(NSVGrasterizer*); |
| 64 |
|
| 65 |
@@ -371,7 +386,7 @@ static void nsvg__flattenCubicBez(NSVGrasterizer* r, |
| 66 |
nsvg__flattenCubicBez(r, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1, type); |
| 67 |
} |
| 68 |
|
| 69 |
-static void nsvg__flattenShape(NSVGrasterizer* r, NSVGshape* shape, float scale) |
| 70 |
+static void nsvg__flattenShape(NSVGrasterizer* r, NSVGshape* shape, float sx, float sy) |
| 71 |
{ |
| 72 |
int i, j; |
| 73 |
NSVGpath* path; |
| 74 |
@@ -379,13 +394,13 @@ static void nsvg__flattenShape(NSVGrasterizer* r, NSVGshape* shape, float scale) |
| 75 |
for (path = shape->paths; path != NULL; path = path->next) { |
| 76 |
r->npoints = 0; |
| 77 |
// Flatten path |
| 78 |
- nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, 0); |
| 79 |
+ nsvg__addPathPoint(r, path->pts[0]*sx, path->pts[1]*sy, 0); |
| 80 |
for (i = 0; i < path->npts-1; i += 3) { |
| 81 |
float* p = &path->pts[i*2]; |
| 82 |
- nsvg__flattenCubicBez(r, p[0]*scale,p[1]*scale, p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, 0); |
| 83 |
+ nsvg__flattenCubicBez(r, p[0]*sx,p[1]*sy, p[2]*sx,p[3]*sy, p[4]*sx,p[5]*sy, p[6]*sx,p[7]*sy, 0, 0); |
| 84 |
} |
| 85 |
// Close path |
| 86 |
- nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, 0); |
| 87 |
+ nsvg__addPathPoint(r, path->pts[0]*sx, path->pts[1]*sy, 0); |
| 88 |
// Build edges |
| 89 |
for (i = 0, j = r->npoints-1; i < r->npoints; j = i++) |
| 90 |
nsvg__addEdge(r, r->points[j].x, r->points[j].y, r->points[i].x, r->points[i].y); |
| 91 |
@@ -735,7 +750,7 @@ static void nsvg__prepareStroke(NSVGrasterizer* r, float miterLimit, int lineJoi |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
-static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float scale) |
| 96 |
+static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float sx, float sy) |
| 97 |
{ |
| 98 |
int i, j, closed; |
| 99 |
NSVGpath* path; |
| 100 |
@@ -743,15 +758,16 @@ static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float |
| 101 |
float miterLimit = shape->miterLimit; |
| 102 |
int lineJoin = shape->strokeLineJoin; |
| 103 |
int lineCap = shape->strokeLineCap; |
| 104 |
- float lineWidth = shape->strokeWidth * scale; |
| 105 |
+ const float sw = (sx + sy) / 2; // average scaling factor |
| 106 |
+ const float lineWidth = shape->strokeWidth * sw; // FIXME (?) |
| 107 |
|
| 108 |
for (path = shape->paths; path != NULL; path = path->next) { |
| 109 |
// Flatten path |
| 110 |
r->npoints = 0; |
| 111 |
- nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, NSVG_PT_CORNER); |
| 112 |
+ nsvg__addPathPoint(r, path->pts[0]*sx, path->pts[1]*sy, NSVG_PT_CORNER); |
| 113 |
for (i = 0; i < path->npts-1; i += 3) { |
| 114 |
float* p = &path->pts[i*2]; |
| 115 |
- nsvg__flattenCubicBez(r, p[0]*scale,p[1]*scale, p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, NSVG_PT_CORNER); |
| 116 |
+ nsvg__flattenCubicBez(r, p[0]*sx,p[1]*sy, p[2]*sx,p[3]*sy, p[4]*sx,p[5]*sy, p[6]*sx,p[7]*sy, 0, NSVG_PT_CORNER); |
| 117 |
} |
| 118 |
if (r->npoints < 2) |
| 119 |
continue; |
| 120 |
@@ -797,7 +813,7 @@ static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float |
| 121 |
dashOffset -= shape->strokeDashArray[idash]; |
| 122 |
idash = (idash + 1) % shape->strokeDashCount; |
| 123 |
} |
| 124 |
- dashLen = (shape->strokeDashArray[idash] - dashOffset) * scale; |
| 125 |
+ dashLen = (shape->strokeDashArray[idash] - dashOffset) * sw; |
| 126 |
|
| 127 |
for (j = 1; j < r->npoints2; ) { |
| 128 |
float dx = r->points2[j].x - cur.x; |
| 129 |
@@ -819,7 +835,7 @@ static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float |
| 130 |
// Advance dash pattern |
| 131 |
dashState = !dashState; |
| 132 |
idash = (idash+1) % shape->strokeDashCount; |
| 133 |
- dashLen = shape->strokeDashArray[idash] * scale; |
| 134 |
+ dashLen = shape->strokeDashArray[idash] * sw; |
| 135 |
// Restart |
| 136 |
cur.x = x; |
| 137 |
cur.y = y; |
| 138 |
@@ -994,7 +1010,7 @@ static inline int nsvg__div255(int x) |
| 139 |
} |
| 140 |
|
| 141 |
static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* cover, int x, int y, |
| 142 |
- float tx, float ty, float scale, NSVGcachedPaint* cache) |
| 143 |
+ float tx, float ty, float sx, float sy, NSVGcachedPaint* cache) |
| 144 |
{ |
| 145 |
|
| 146 |
if (cache->type == NSVG_PAINT_COLOR) { |
| 147 |
@@ -1035,9 +1051,9 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co |
| 148 |
int i, cr, cg, cb, ca; |
| 149 |
unsigned int c; |
| 150 |
|
| 151 |
- fx = ((float)x - tx) / scale; |
| 152 |
- fy = ((float)y - ty) / scale; |
| 153 |
- dx = 1.0f / scale; |
| 154 |
+ fx = ((float)x - tx) / sx; |
| 155 |
+ fy = ((float)y - ty) / sy; |
| 156 |
+ dx = 1.0f / sx; |
| 157 |
|
| 158 |
for (i = 0; i < count; i++) { |
| 159 |
int r,g,b,a,ia; |
| 160 |
@@ -1080,9 +1096,9 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co |
| 161 |
int i, cr, cg, cb, ca; |
| 162 |
unsigned int c; |
| 163 |
|
| 164 |
- fx = ((float)x - tx) / scale; |
| 165 |
- fy = ((float)y - ty) / scale; |
| 166 |
- dx = 1.0f / scale; |
| 167 |
+ fx = ((float)x - tx) / sx; |
| 168 |
+ fy = ((float)y - ty) / sy; |
| 169 |
+ dx = 1.0f / sx; |
| 170 |
|
| 171 |
for (i = 0; i < count; i++) { |
| 172 |
int r,g,b,a,ia; |
| 173 |
@@ -1121,7 +1137,7 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
-static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, float scale, NSVGcachedPaint* cache, char fillRule) |
| 178 |
+static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, float sx, float sy, NSVGcachedPaint* cache, char fillRule) |
| 179 |
{ |
| 180 |
NSVGactiveEdge *active = NULL; |
| 181 |
int y, s; |
| 182 |
@@ -1203,7 +1219,7 @@ static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, fl |
| 183 |
if (xmin < 0) xmin = 0; |
| 184 |
if (xmax > r->width-1) xmax = r->width-1; |
| 185 |
if (xmin <= xmax) { |
| 186 |
- nsvg__scanlineSolid(&r->bitmap[y * r->stride] + xmin*4, xmax-xmin+1, &r->scanline[xmin], xmin, y, tx,ty, scale, cache); |
| 187 |
+ nsvg__scanlineSolid(&r->bitmap[y * r->stride] + xmin*4, xmax-xmin+1, &r->scanline[xmin], xmin, y, tx,ty, sx, sy, cache); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
@@ -1372,8 +1388,9 @@ static void dumpEdges(NSVGrasterizer* r, const char* name) |
| 192 |
} |
| 193 |
*/ |
| 194 |
|
| 195 |
-void nsvgRasterize(NSVGrasterizer* r, |
| 196 |
- NSVGimage* image, float tx, float ty, float scale, |
| 197 |
+void nsvgRasterizeXY(NSVGrasterizer* r, |
| 198 |
+ NSVGimage* image, float tx, float ty, |
| 199 |
+ float sx, float sy, |
| 200 |
unsigned char* dst, int w, int h, int stride) |
| 201 |
{ |
| 202 |
NSVGshape *shape = NULL; |
| 203 |
@@ -1382,6 +1399,7 @@ void nsvgRasterize(NSVGrasterizer* r, |
| 204 |
int i; |
| 205 |
int j; |
| 206 |
unsigned char paintOrder; |
| 207 |
+ const float sw = (sx + sy) / 2; // average scaling factor |
| 208 |
|
| 209 |
r->bitmap = dst; |
| 210 |
r->width = w; |
| 211 |
@@ -1409,7 +1427,7 @@ void nsvgRasterize(NSVGrasterizer* r, |
| 212 |
r->freelist = NULL; |
| 213 |
r->nedges = 0; |
| 214 |
|
| 215 |
- nsvg__flattenShape(r, shape, scale); |
| 216 |
+ nsvg__flattenShape(r, shape, sx, sy); |
| 217 |
|
| 218 |
// Scale and translate edges |
| 219 |
for (i = 0; i < r->nedges; i++) { |
| 220 |
@@ -1427,14 +1445,14 @@ void nsvgRasterize(NSVGrasterizer* r, |
| 221 |
// now, traverse the scanlines and find the intersections on each scanline, use non-zero rule |
| 222 |
nsvg__initPaint(&cache, &shape->fill, shape->opacity); |
| 223 |
|
| 224 |
- nsvg__rasterizeSortedEdges(r, tx,ty,scale, &cache, shape->fillRule); |
| 225 |
+ nsvg__rasterizeSortedEdges(r, tx, ty, sx, sy, &cache, shape->fillRule); |
| 226 |
} |
| 227 |
- if (paintOrder == NSVG_PAINT_STROKE && shape->stroke.type != NSVG_PAINT_NONE && (shape->strokeWidth * scale) > 0.01f) { |
| 228 |
+ if (paintOrder == NSVG_PAINT_STROKE && shape->stroke.type != NSVG_PAINT_NONE && (shape->strokeWidth * sw) > 0.01f) { |
| 229 |
nsvg__resetPool(r); |
| 230 |
r->freelist = NULL; |
| 231 |
r->nedges = 0; |
| 232 |
|
| 233 |
- nsvg__flattenShapeStroke(r, shape, scale); |
| 234 |
+ nsvg__flattenShapeStroke(r, shape, sx, sy); |
| 235 |
|
| 236 |
// dumpEdges(r, "edge.svg"); |
| 237 |
|
| 238 |
@@ -1454,7 +1472,7 @@ void nsvgRasterize(NSVGrasterizer* r, |
| 239 |
// now, traverse the scanlines and find the intersections on each scanline, use non-zero rule |
| 240 |
nsvg__initPaint(&cache, &shape->stroke, shape->opacity); |
| 241 |
|
| 242 |
- nsvg__rasterizeSortedEdges(r, tx,ty,scale, &cache, NSVG_FILLRULE_NONZERO); |
| 243 |
+ nsvg__rasterizeSortedEdges(r, tx, ty, sx, sy, &cache, NSVG_FILLRULE_NONZERO); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
@@ -1467,6 +1485,13 @@ void nsvgRasterize(NSVGrasterizer* r, |
| 248 |
r->stride = 0; |
| 249 |
} |
| 250 |
|
| 251 |
+void nsvgRasterize(NSVGrasterizer* r, |
| 252 |
+ NSVGimage* image, float tx, float ty, float scale, |
| 253 |
+ unsigned char* dst, int w, int h, int stride) |
| 254 |
+{ |
| 255 |
+ nsvgRasterizeXY(r,image, tx, ty, scale, scale, dst, w, h, stride); |
| 256 |
+} |
| 257 |
+ |
| 258 |
#endif // NANOSVGRAST_IMPLEMENTATION |
| 259 |
|
| 260 |
#endif // NANOSVGRAST_H |