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