28 #ifndef NANOSVGRAST_CPLUSPLUS 34 typedef struct NSVGrasterizer NSVGrasterizer;
50 NSVGrasterizer* nsvgCreateRasterizer();
61 void nsvgRasterize(NSVGrasterizer* r,
63 unsigned char* dst,
int w,
int h,
int stride);
66 void nsvgDeleteRasterizer(NSVGrasterizer*);
69 #ifndef NANOSVGRAST_CPLUSPLUS 75 #endif // NANOSVGRAST_H 77 #ifdef NANOSVGRAST_IMPLEMENTATION 81 #define NSVG__SUBSAMPLES 5 82 #define NSVG__FIXSHIFT 10 83 #define NSVG__FIX (1 << NSVG__FIXSHIFT) 84 #define NSVG__FIXMASK (NSVG__FIX-1) 85 #define NSVG__MEMPAGE_SIZE 1024 87 typedef struct NSVGedge {
90 struct NSVGedge* next;
93 typedef struct NSVGpoint {
101 typedef struct NSVGactiveEdge {
105 struct NSVGactiveEdge *next;
108 typedef struct NSVGmemPage {
109 unsigned char mem[NSVG__MEMPAGE_SIZE];
111 struct NSVGmemPage* next;
114 typedef struct NSVGcachedPaint {
118 unsigned int colors[256];
121 struct NSVGrasterizer
140 NSVGactiveEdge* freelist;
142 NSVGmemPage* curpage;
144 unsigned char* scanline;
147 unsigned char* bitmap;
148 int width, height, stride;
151 NSVGrasterizer* nsvgCreateRasterizer()
153 NSVGrasterizer* r = (NSVGrasterizer*)malloc(
sizeof(NSVGrasterizer));
154 if (r == NULL)
goto error;
155 memset(r, 0,
sizeof(NSVGrasterizer));
163 nsvgDeleteRasterizer(r);
167 void nsvgDeleteRasterizer(NSVGrasterizer* r)
171 if (r == NULL)
return;
175 NSVGmemPage* next = p->next;
180 if (r->edges) free(r->edges);
181 if (r->points) free(r->points);
182 if (r->points2) free(r->points2);
183 if (r->scanline) free(r->scanline);
188 static NSVGmemPage* nsvg__nextPage(NSVGrasterizer* r, NSVGmemPage* cur)
193 if (cur != NULL && cur->next != NULL) {
198 newp = (NSVGmemPage*)malloc(
sizeof(NSVGmemPage));
199 if (newp == NULL)
return NULL;
200 memset(newp, 0,
sizeof(NSVGmemPage));
211 static void nsvg__resetPool(NSVGrasterizer* r)
213 NSVGmemPage* p = r->pages;
218 r->curpage = r->pages;
221 static unsigned char* nsvg__alloc(NSVGrasterizer* r,
int size)
224 if (size > NSVG__MEMPAGE_SIZE)
return NULL;
225 if (r->curpage == NULL || r->curpage->size+size > NSVG__MEMPAGE_SIZE) {
226 r->curpage = nsvg__nextPage(r, r->curpage);
228 buf = &r->curpage->mem[r->curpage->size];
229 r->curpage->size += size;
233 static int nsvg__ptEquals(
float x1,
float y1,
float x2,
float y2,
float tol)
237 return dx*dx + dy*dy < tol*tol;
240 static void nsvg__addPathPoint(NSVGrasterizer* r,
float x,
float y,
int flags)
244 if (r->npoints > 0) {
245 pt = &r->points[r->npoints-1];
246 if (nsvg__ptEquals(pt->x,pt->y, x,y, r->distTol)) {
247 pt->flags = (
unsigned char)(pt->flags | flags);
252 if (r->npoints+1 > r->cpoints) {
253 r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64;
254 r->points = (NSVGpoint*)realloc(r->points,
sizeof(NSVGpoint) * r->cpoints);
255 if (r->points == NULL)
return;
258 pt = &r->points[r->npoints];
261 pt->flags = (
unsigned char)flags;
265 static void nsvg__appendPathPoint(NSVGrasterizer* r, NSVGpoint pt)
267 if (r->npoints+1 > r->cpoints) {
268 r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64;
269 r->points = (NSVGpoint*)realloc(r->points,
sizeof(NSVGpoint) * r->cpoints);
270 if (r->points == NULL)
return;
272 r->points[r->npoints] = pt;
276 static void nsvg__duplicatePoints(NSVGrasterizer* r)
278 if (r->npoints > r->cpoints2) {
279 r->cpoints2 = r->npoints;
280 r->points2 = (NSVGpoint*)realloc(r->points2,
sizeof(NSVGpoint) * r->cpoints2);
281 if (r->points2 == NULL)
return;
284 memcpy(r->points2, r->points,
sizeof(NSVGpoint) * r->npoints);
285 r->npoints2 = r->npoints;
288 static void nsvg__addEdge(NSVGrasterizer* r,
float x0,
float y0,
float x1,
float y1)
296 if (r->nedges+1 > r->cedges) {
297 r->cedges = r->cedges > 0 ? r->cedges * 2 : 64;
298 r->edges = (NSVGedge*)realloc(r->edges,
sizeof(NSVGedge) * r->cedges);
299 if (r->edges == NULL)
return;
302 e = &r->edges[r->nedges];
320 static float nsvg__normalize(
float *x,
float* y)
322 float d = sqrtf((*x)*(*x) + (*y)*(*y));
331 static float nsvg__absf(
float x) {
return x < 0 ? -x : x; }
333 static void nsvg__flattenCubicBez(NSVGrasterizer* r,
334 float x1,
float y1,
float x2,
float y2,
335 float x3,
float y3,
float x4,
float y4,
338 float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234;
341 if (level > 10)
return;
349 x123 = (x12+x23)*0.5f;
350 y123 = (y12+y23)*0.5f;
354 d2 = nsvg__absf(((x2 - x4) * dy - (y2 - y4) * dx));
355 d3 = nsvg__absf(((x3 - x4) * dy - (y3 - y4) * dx));
357 if ((d2 + d3)*(d2 + d3) < r->tessTol * (dx*dx + dy*dy)) {
358 nsvg__addPathPoint(r, x4, y4, type);
362 x234 = (x23+x34)*0.5f;
363 y234 = (y23+y34)*0.5f;
364 x1234 = (x123+x234)*0.5f;
365 y1234 = (y123+y234)*0.5f;
367 nsvg__flattenCubicBez(r, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1, 0);
368 nsvg__flattenCubicBez(r, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1, type);
371 static void nsvg__flattenShape(NSVGrasterizer* r,
NSVGshape* shape,
float scale)
376 for (path = shape->paths; path != NULL; path = path->next) {
379 nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, 0);
380 for (i = 0; i < path->npts-1; i += 3) {
381 float* p = &path->pts[i*2];
382 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);
385 nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, 0);
387 for (i = 0, j = r->npoints-1; i < r->npoints; j = i++)
388 nsvg__addEdge(r, r->points[j].x, r->points[j].y, r->points[i].x, r->points[i].y);
394 NSVG_PT_CORNER = 0x01,
395 NSVG_PT_BEVEL = 0x02,
399 static void nsvg__initClosed(NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1,
float lineWidth)
401 float w = lineWidth * 0.5f;
402 float dx = p1->x - p0->x;
403 float dy = p1->y - p0->y;
404 float len = nsvg__normalize(&dx, &dy);
405 float px = p0->x + dx*len*0.5f, py = p0->y + dy*len*0.5f;
406 float dlx = dy, dly = -dx;
407 float lx = px - dlx*w, ly = py - dly*w;
408 float rx = px + dlx*w, ry = py + dly*w;
409 left->x = lx; left->y = ly;
410 right->x = rx; right->y = ry;
413 static void nsvg__buttCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p,
float dx,
float dy,
float lineWidth,
int connect)
415 float w = lineWidth * 0.5f;
416 float px = p->x, py = p->y;
417 float dlx = dy, dly = -dx;
418 float lx = px - dlx*w, ly = py - dly*w;
419 float rx = px + dlx*w, ry = py + dly*w;
421 nsvg__addEdge(r, lx, ly, rx, ry);
424 nsvg__addEdge(r, left->x, left->y, lx, ly);
425 nsvg__addEdge(r, rx, ry, right->x, right->y);
427 left->x = lx; left->y = ly;
428 right->x = rx; right->y = ry;
431 static void nsvg__squareCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p,
float dx,
float dy,
float lineWidth,
int connect)
433 float w = lineWidth * 0.5f;
434 float px = p->x - dx*w, py = p->y - dy*w;
435 float dlx = dy, dly = -dx;
436 float lx = px - dlx*w, ly = py - dly*w;
437 float rx = px + dlx*w, ry = py + dly*w;
439 nsvg__addEdge(r, lx, ly, rx, ry);
442 nsvg__addEdge(r, left->x, left->y, lx, ly);
443 nsvg__addEdge(r, rx, ry, right->x, right->y);
445 left->x = lx; left->y = ly;
446 right->x = rx; right->y = ry;
450 #define NSVG_PI (3.14159265358979323846264338327f) 453 static void nsvg__roundCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p,
float dx,
float dy,
float lineWidth,
int ncap,
int connect)
456 float w = lineWidth * 0.5f;
457 float px = p->x, py = p->y;
458 float dlx = dy, dly = -dx;
459 float lx = 0, ly = 0, rx = 0, ry = 0, prevx = 0, prevy = 0;
461 for (i = 0; i < ncap; i++) {
462 float a = (float)i/(
float)(ncap-1)*NSVG_PI;
463 float ax = cosf(a) * w, ay = sinf(a) * w;
464 float x = px - dlx*ax - dx*ay;
465 float y = py - dly*ax - dy*ay;
468 nsvg__addEdge(r, prevx, prevy, x, y);
475 }
else if (i == ncap-1) {
481 nsvg__addEdge(r, left->x, left->y, lx, ly);
482 nsvg__addEdge(r, rx, ry, right->x, right->y);
485 left->x = lx; left->y = ly;
486 right->x = rx; right->y = ry;
489 static void nsvg__bevelJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1,
float lineWidth)
491 float w = lineWidth * 0.5f;
492 float dlx0 = p0->dy, dly0 = -p0->dx;
493 float dlx1 = p1->dy, dly1 = -p1->dx;
494 float lx0 = p1->x - (dlx0 * w), ly0 = p1->y - (dly0 * w);
495 float rx0 = p1->x + (dlx0 * w), ry0 = p1->y + (dly0 * w);
496 float lx1 = p1->x - (dlx1 * w), ly1 = p1->y - (dly1 * w);
497 float rx1 = p1->x + (dlx1 * w), ry1 = p1->y + (dly1 * w);
499 nsvg__addEdge(r, lx0, ly0, left->x, left->y);
500 nsvg__addEdge(r, lx1, ly1, lx0, ly0);
502 nsvg__addEdge(r, right->x, right->y, rx0, ry0);
503 nsvg__addEdge(r, rx0, ry0, rx1, ry1);
505 left->x = lx1; left->y = ly1;
506 right->x = rx1; right->y = ry1;
509 static void nsvg__miterJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1,
float lineWidth)
511 float w = lineWidth * 0.5f;
512 float dlx0 = p0->dy, dly0 = -p0->dx;
513 float dlx1 = p1->dy, dly1 = -p1->dx;
514 float lx0, rx0, lx1, rx1;
515 float ly0, ry0, ly1, ry1;
517 if (p1->flags & NSVG_PT_LEFT) {
518 lx0 = lx1 = p1->x - p1->dmx * w;
519 ly0 = ly1 = p1->y - p1->dmy * w;
520 nsvg__addEdge(r, lx1, ly1, left->x, left->y);
522 rx0 = p1->x + (dlx0 * w);
523 ry0 = p1->y + (dly0 * w);
524 rx1 = p1->x + (dlx1 * w);
525 ry1 = p1->y + (dly1 * w);
526 nsvg__addEdge(r, right->x, right->y, rx0, ry0);
527 nsvg__addEdge(r, rx0, ry0, rx1, ry1);
529 lx0 = p1->x - (dlx0 * w);
530 ly0 = p1->y - (dly0 * w);
531 lx1 = p1->x - (dlx1 * w);
532 ly1 = p1->y - (dly1 * w);
533 nsvg__addEdge(r, lx0, ly0, left->x, left->y);
534 nsvg__addEdge(r, lx1, ly1, lx0, ly0);
536 rx0 = rx1 = p1->x + p1->dmx * w;
537 ry0 = ry1 = p1->y + p1->dmy * w;
538 nsvg__addEdge(r, right->x, right->y, rx1, ry1);
541 left->x = lx1; left->y = ly1;
542 right->x = rx1; right->y = ry1;
545 static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1,
float lineWidth,
int ncap)
548 float w = lineWidth * 0.5f;
549 float dlx0 = p0->dy, dly0 = -p0->dx;
550 float dlx1 = p1->dy, dly1 = -p1->dx;
551 float a0 = atan2f(dly0, dlx0);
552 float a1 = atan2f(dly1, dlx1);
554 float lx, ly, rx, ry;
556 if (da < NSVG_PI) da += NSVG_PI*2;
557 if (da > NSVG_PI) da -= NSVG_PI*2;
559 n = (int)ceilf((nsvg__absf(da) / NSVG_PI) * (float)ncap);
561 if (n > ncap) n = ncap;
568 for (i = 0; i < n; i++) {
569 float u = (float)i/(
float)(n-1);
571 float ax = cosf(a) * w, ay = sinf(a) * w;
572 float lx1 = p1->x - ax, ly1 = p1->y - ay;
573 float rx1 = p1->x + ax, ry1 = p1->y + ay;
575 nsvg__addEdge(r, lx1, ly1, lx, ly);
576 nsvg__addEdge(r, rx, ry, rx1, ry1);
582 left->x = lx; left->y = ly;
583 right->x = rx; right->y = ry;
586 static void nsvg__straightJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p1,
float lineWidth)
588 float w = lineWidth * 0.5f;
589 float lx = p1->x - (p1->dmx * w), ly = p1->y - (p1->dmy * w);
590 float rx = p1->x + (p1->dmx * w), ry = p1->y + (p1->dmy * w);
592 nsvg__addEdge(r, lx, ly, left->x, left->y);
593 nsvg__addEdge(r, right->x, right->y, rx, ry);
595 left->x = lx; left->y = ly;
596 right->x = rx; right->y = ry;
599 static int nsvg__curveDivs(
float r,
float arc,
float tol)
601 float da = acosf(r / (r + tol)) * 2.0f;
602 int divs = (int)ceilf(arc / da);
603 if (divs < 2) divs = 2;
607 static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points,
int npoints,
int closed,
int lineJoin,
int lineCap,
float lineWidth)
609 int ncap = nsvg__curveDivs(lineWidth*0.5f, NSVG_PI, r->tessTol);
610 NSVGpoint left = {0,0,0,0,0,0,0,0}, right = {0,0,0,0,0,0,0,0}, firstLeft = {0,0,0,0,0,0,0,0}, firstRight = {0,0,0,0,0,0,0,0};
617 p0 = &points[npoints-1];
630 nsvg__initClosed(&left, &right, p0, p1, lineWidth);
635 float dx = p1->x - p0->x;
636 float dy = p1->y - p0->y;
637 nsvg__normalize(&dx, &dy);
638 if (lineCap == NSVG_CAP_BUTT)
639 nsvg__buttCap(r, &left, &right, p0, dx, dy, lineWidth, 0);
640 else if (lineCap == NSVG_CAP_SQUARE)
641 nsvg__squareCap(r, &left, &right, p0, dx, dy, lineWidth, 0);
642 else if (lineCap == NSVG_CAP_ROUND)
643 nsvg__roundCap(r, &left, &right, p0, dx, dy, lineWidth, ncap, 0);
646 for (j = s; j < e; ++j) {
647 if (p1->flags & NSVG_PT_CORNER) {
648 if (lineJoin == NSVG_JOIN_ROUND)
649 nsvg__roundJoin(r, &left, &right, p0, p1, lineWidth, ncap);
650 else if (lineJoin == NSVG_JOIN_BEVEL || (p1->flags & NSVG_PT_BEVEL))
651 nsvg__bevelJoin(r, &left, &right, p0, p1, lineWidth);
653 nsvg__miterJoin(r, &left, &right, p0, p1, lineWidth);
655 nsvg__straightJoin(r, &left, &right, p1, lineWidth);
662 nsvg__addEdge(r, firstLeft.x, firstLeft.y, left.x, left.y);
663 nsvg__addEdge(r, right.x, right.y, firstRight.x, firstRight.y);
666 float dx = p1->x - p0->x;
667 float dy = p1->y - p0->y;
668 nsvg__normalize(&dx, &dy);
669 if (lineCap == NSVG_CAP_BUTT)
670 nsvg__buttCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1);
671 else if (lineCap == NSVG_CAP_SQUARE)
672 nsvg__squareCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1);
673 else if (lineCap == NSVG_CAP_ROUND)
674 nsvg__roundCap(r, &right, &left, p1, -dx, -dy, lineWidth, ncap, 1);
678 static void nsvg__prepareStroke(NSVGrasterizer* r,
float miterLimit,
int lineJoin)
683 p0 = &r->points[r->npoints-1];
685 for (i = 0; i < r->npoints; i++) {
687 p0->dx = p1->x - p0->x;
688 p0->dy = p1->y - p0->y;
689 p0->len = nsvg__normalize(&p0->dx, &p0->dy);
695 p0 = &r->points[r->npoints-1];
697 for (j = 0; j < r->npoints; j++) {
698 float dlx0, dly0, dlx1, dly1, dmr2, cross;
704 p1->dmx = (dlx0 + dlx1) * 0.5f;
705 p1->dmy = (dly0 + dly1) * 0.5f;
706 dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy;
707 if (dmr2 > 0.000001f) {
708 float s2 = 1.0f / dmr2;
717 p1->flags = (p1->flags & NSVG_PT_CORNER) ? NSVG_PT_CORNER : 0;
720 cross = p1->dx * p0->dy - p0->dx * p1->dy;
722 p1->flags |= NSVG_PT_LEFT;
725 if (p1->flags & NSVG_PT_CORNER) {
726 if ((dmr2 * miterLimit*miterLimit) < 1.0f || lineJoin == NSVG_JOIN_BEVEL || lineJoin == NSVG_JOIN_ROUND) {
727 p1->flags |= NSVG_PT_BEVEL;
735 static void nsvg__flattenShapeStroke(NSVGrasterizer* r,
NSVGshape* shape,
float scale)
740 float miterLimit = shape->miterLimit;
741 int lineJoin = shape->strokeLineJoin;
742 int lineCap = shape->strokeLineCap;
743 float lineWidth = shape->strokeWidth *
scale;
745 for (path = shape->paths; path != NULL; path = path->next) {
748 nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, NSVG_PT_CORNER);
749 for (i = 0; i < path->npts-1; i += 3) {
750 float* p = &path->pts[i*2];
751 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);
756 closed = path->closed;
759 p0 = &r->points[r->npoints-1];
761 if (nsvg__ptEquals(p0->x,p0->y, p1->x,p1->y, r->distTol)) {
763 p0 = &r->points[r->npoints-1];
767 if (shape->strokeDashCount > 0) {
768 int idash = 0, dashState = 1;
769 float totalDist = 0, dashLen, allDashLen, dashOffset;
773 nsvg__appendPathPoint(r, r->points[0]);
776 nsvg__duplicatePoints(r);
780 nsvg__appendPathPoint(r, cur);
784 for (j = 0; j < shape->strokeDashCount; j++)
785 allDashLen += shape->strokeDashArray[j];
786 if (shape->strokeDashCount & 1)
789 dashOffset = fmodf(shape->strokeDashOffset, allDashLen);
790 if (dashOffset < 0.0f)
791 dashOffset += allDashLen;
793 while (dashOffset > shape->strokeDashArray[idash]) {
794 dashOffset -= shape->strokeDashArray[idash];
795 idash = (idash + 1) % shape->strokeDashCount;
797 dashLen = (shape->strokeDashArray[idash] - dashOffset) * scale;
799 for (j = 1; j < r->npoints2; ) {
800 float dx = r->points2[j].x - cur.x;
801 float dy = r->points2[j].y - cur.y;
802 float dist = sqrtf(dx*dx + dy*dy);
804 if ((totalDist + dist) > dashLen) {
806 float d = (dashLen - totalDist) / dist;
807 float x = cur.x + dx * d;
808 float y = cur.y + dy * d;
809 nsvg__addPathPoint(r, x, y, NSVG_PT_CORNER);
812 if (r->npoints > 1 && dashState) {
813 nsvg__prepareStroke(r, miterLimit, lineJoin);
814 nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth);
817 dashState = !dashState;
818 idash = (idash+1) % shape->strokeDashCount;
819 dashLen = shape->strokeDashArray[idash] * scale;
823 cur.flags = NSVG_PT_CORNER;
826 nsvg__appendPathPoint(r, cur);
830 nsvg__appendPathPoint(r, cur);
835 if (r->npoints > 1 && dashState)
836 nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth);
838 nsvg__prepareStroke(r, miterLimit, lineJoin);
839 nsvg__expandStroke(r, r->points, r->npoints, closed, lineJoin, lineCap, lineWidth);
844 static int nsvg__cmpEdge(
const void *p,
const void *q)
846 const NSVGedge* a = (
const NSVGedge*)p;
847 const NSVGedge* b = (
const NSVGedge*)q;
849 if (a->y0 < b->y0)
return -1;
850 if (a->y0 > b->y0)
return 1;
855 static NSVGactiveEdge* nsvg__addActive(NSVGrasterizer* r, NSVGedge* e,
float startPoint)
859 if (r->freelist != NULL) {
862 r->freelist = z->next;
865 z = (NSVGactiveEdge*)nsvg__alloc(r,
sizeof(NSVGactiveEdge));
866 if (z == NULL)
return NULL;
869 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
873 z->dx = (int)(-floorf(NSVG__FIX * -dxdy));
875 z->dx = (int)floorf(NSVG__FIX * dxdy);
876 z->x = (int)floorf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)));
885 static void nsvg__freeActive(NSVGrasterizer* r, NSVGactiveEdge* z)
887 z->next = r->freelist;
891 static void nsvg__fillScanline(
unsigned char* scanline,
int len,
int x0,
int x1,
int maxWeight,
int* xmin,
int* xmax)
893 int i = x0 >> NSVG__FIXSHIFT;
894 int j = x1 >> NSVG__FIXSHIFT;
895 if (i < *xmin) *xmin = i;
896 if (j > *xmax) *xmax = j;
897 if (i < len && j >= 0) {
900 scanline[i] = (
unsigned char)(scanline[i] + ((x1 - x0) * maxWeight >> NSVG__FIXSHIFT));
903 scanline[i] = (
unsigned char)(scanline[i] + (((NSVG__FIX - (x0 & NSVG__FIXMASK)) * maxWeight) >> NSVG__FIXSHIFT));
908 scanline[j] = (
unsigned char)(scanline[j] + (((x1 & NSVG__FIXMASK) * maxWeight) >> NSVG__FIXSHIFT));
912 for (++i; i < j; ++i)
913 scanline[i] = (
unsigned char)(scanline[i] + maxWeight);
921 static void nsvg__fillActiveEdges(
unsigned char* scanline,
int len, NSVGactiveEdge* e,
int maxWeight,
int* xmin,
int* xmax,
char fillRule)
926 if (fillRule == NSVG_FILLRULE_NONZERO) {
931 x0 = e->x; w += e->dir;
933 int x1 = e->x; w += e->dir;
936 nsvg__fillScanline(scanline, len, x0, x1, maxWeight, xmin, xmax);
940 }
else if (fillRule == NSVG_FILLRULE_EVENODD) {
947 int x1 = e->x; w = 0;
948 nsvg__fillScanline(scanline, len, x0, x1, maxWeight, xmin, xmax);
955 static float nsvg__clampf(
float a,
float mn,
float mx) {
return a < mn ? mn : (a > mx ? mx : a); }
957 static unsigned int nsvg__RGBA(
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a)
959 return (r) | (g << 8) | (b << 16) | (a << 24);
962 static unsigned int nsvg__lerpRGBA(
unsigned int c0,
unsigned int c1,
float u)
964 int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f);
965 int r = (((c0) & 0xff)*(256-iu) + (((c1) & 0xff)*iu)) >> 8;
966 int g = (((c0>>8) & 0xff)*(256-iu) + (((c1>>8) & 0xff)*iu)) >> 8;
967 int b = (((c0>>16) & 0xff)*(256-iu) + (((c1>>16) & 0xff)*iu)) >> 8;
968 int a = (((c0>>24) & 0xff)*(256-iu) + (((c1>>24) & 0xff)*iu)) >> 8;
969 return nsvg__RGBA((
unsigned char)r, (
unsigned char)g, (
unsigned char)b, (
unsigned char)a);
972 static unsigned int nsvg__applyOpacity(
unsigned int c,
float u)
974 int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f);
976 int g = (c>>8) & 0xff;
977 int b = (c>>16) & 0xff;
978 int a = (((c>>24) & 0xff)*iu) >> 8;
979 return nsvg__RGBA((
unsigned char)r, (
unsigned char)g, (
unsigned char)b, (
unsigned char)a);
982 static inline int nsvg__div255(
int x)
984 return ((x+1) * 257) >> 16;
987 static void nsvg__scanlineSolid(
unsigned char* dst,
int count,
unsigned char* cover,
int x,
int y,
988 float tx,
float ty,
float scale, NSVGcachedPaint* cache)
991 if (cache->type == NSVG_PAINT_COLOR) {
992 int i, cr, cg, cb, ca;
993 cr = cache->colors[0] & 0xff;
994 cg = (cache->colors[0] >> 8) & 0xff;
995 cb = (cache->colors[0] >> 16) & 0xff;
996 ca = (cache->colors[0] >> 24) & 0xff;
998 for (i = 0; i < count; i++) {
1000 int a = nsvg__div255((
int)cover[0] * ca);
1003 r = nsvg__div255(cr * a);
1004 g = nsvg__div255(cg * a);
1005 b = nsvg__div255(cb * a);
1008 r += nsvg__div255(ia * (
int)dst[0]);
1009 g += nsvg__div255(ia * (
int)dst[1]);
1010 b += nsvg__div255(ia * (
int)dst[2]);
1011 a += nsvg__div255(ia * (
int)dst[3]);
1013 dst[0] = (
unsigned char)r;
1014 dst[1] = (
unsigned char)g;
1015 dst[2] = (
unsigned char)b;
1016 dst[3] = (
unsigned char)a;
1021 }
else if (cache->type == NSVG_PAINT_LINEAR_GRADIENT) {
1024 float fx, fy, dx, gy;
1025 float* t = cache->xform;
1026 int i, cr, cg, cb, ca;
1029 fx = ((float)x - tx) /
scale;
1030 fy = ((float)y - ty) /
scale;
1033 for (i = 0; i < count; i++) {
1035 gy = fx*t[1] + fy*t[3] + t[5];
1036 c = cache->colors[(int)nsvg__clampf(gy*255.0f, 0, 255.0f)];
1038 cg = (c >> 8) & 0xff;
1039 cb = (c >> 16) & 0xff;
1040 ca = (c >> 24) & 0xff;
1042 a = nsvg__div255((
int)cover[0] * ca);
1046 r = nsvg__div255(cr * a);
1047 g = nsvg__div255(cg * a);
1048 b = nsvg__div255(cb * a);
1051 r += nsvg__div255(ia * (
int)dst[0]);
1052 g += nsvg__div255(ia * (
int)dst[1]);
1053 b += nsvg__div255(ia * (
int)dst[2]);
1054 a += nsvg__div255(ia * (
int)dst[3]);
1056 dst[0] = (
unsigned char)r;
1057 dst[1] = (
unsigned char)g;
1058 dst[2] = (
unsigned char)b;
1059 dst[3] = (
unsigned char)a;
1065 }
else if (cache->type == NSVG_PAINT_RADIAL_GRADIENT) {
1069 float fx, fy, dx, gx, gy, gd;
1070 float* t = cache->xform;
1071 int i, cr, cg, cb, ca;
1074 fx = ((float)x - tx) /
scale;
1075 fy = ((float)y - ty) /
scale;
1078 for (i = 0; i < count; i++) {
1080 gx = fx*t[0] + fy*t[2] + t[4];
1081 gy = fx*t[1] + fy*t[3] + t[5];
1082 gd = sqrtf(gx*gx + gy*gy);
1083 c = cache->colors[(int)nsvg__clampf(gd*255.0f, 0, 255.0f)];
1085 cg = (c >> 8) & 0xff;
1086 cb = (c >> 16) & 0xff;
1087 ca = (c >> 24) & 0xff;
1089 a = nsvg__div255((
int)cover[0] * ca);
1093 r = nsvg__div255(cr * a);
1094 g = nsvg__div255(cg * a);
1095 b = nsvg__div255(cb * a);
1098 r += nsvg__div255(ia * (
int)dst[0]);
1099 g += nsvg__div255(ia * (
int)dst[1]);
1100 b += nsvg__div255(ia * (
int)dst[2]);
1101 a += nsvg__div255(ia * (
int)dst[3]);
1103 dst[0] = (
unsigned char)r;
1104 dst[1] = (
unsigned char)g;
1105 dst[2] = (
unsigned char)b;
1106 dst[3] = (
unsigned char)a;
1115 static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r,
float tx,
float ty,
float scale, NSVGcachedPaint* cache,
char fillRule)
1117 NSVGactiveEdge *active = NULL;
1120 int maxWeight = (255 / NSVG__SUBSAMPLES);
1123 for (y = 0; y < r->height; y++) {
1124 memset(r->scanline, 0, r->width);
1127 for (s = 0; s < NSVG__SUBSAMPLES; ++s) {
1129 float scany = (float)(y*NSVG__SUBSAMPLES + s) + 0.5f;
1130 NSVGactiveEdge **step = &active;
1135 NSVGactiveEdge *z = *step;
1136 if (z->ey <= scany) {
1139 nsvg__freeActive(r, z);
1142 step = &((*step)->next);
1150 while (*step && (*step)->next) {
1151 if ((*step)->x > (*step)->next->x) {
1152 NSVGactiveEdge* t = *step;
1153 NSVGactiveEdge* q = t->next;
1159 step = &(*step)->next;
1161 if (!changed)
break;
1165 while (e < r->nedges && r->edges[e].y0 <= scany) {
1166 if (r->edges[e].y1 > scany) {
1167 NSVGactiveEdge* z = nsvg__addActive(r, &r->edges[e], scany);
1168 if (z == NULL)
break;
1170 if (active == NULL) {
1172 }
else if (z->x < active->x) {
1178 NSVGactiveEdge* p = active;
1179 while (p->next && p->next->x < z->x)
1191 nsvg__fillActiveEdges(r->scanline, r->width, active, maxWeight, &xmin, &xmax, fillRule);
1194 if (xmin < 0) xmin = 0;
1195 if (xmax > r->width-1) xmax = r->width-1;
1197 nsvg__scanlineSolid(&r->bitmap[y * r->stride] + xmin*4, xmax-xmin+1, &r->scanline[xmin], xmin, y, tx,ty, scale, cache);
1203 static void nsvg__unpremultiplyAlpha(
unsigned char* image,
int w,
int h,
int stride)
1208 for (y = 0; y < h; y++) {
1209 unsigned char *row = &image[y*stride];
1210 for (x = 0; x < w; x++) {
1211 int r = row[0], g = row[1], b = row[2], a = row[3];
1213 row[0] = (
unsigned char)(r*255/a);
1214 row[1] = (
unsigned char)(g*255/a);
1215 row[2] = (
unsigned char)(b*255/a);
1222 for (y = 0; y < h; y++) {
1223 unsigned char *row = &image[y*stride];
1224 for (x = 0; x < w; x++) {
1225 int r = 0, g = 0, b = 0, a = row[3], n = 0;
1227 if (x-1 > 0 && row[-1] != 0) {
1233 if (x+1 < w && row[7] != 0) {
1239 if (y-1 > 0 && row[-stride+3] != 0) {
1241 g += row[-stride+1];
1242 b += row[-stride+2];
1245 if (y+1 < h && row[stride+3] != 0) {
1252 row[0] = (
unsigned char)(r/n);
1253 row[1] = (
unsigned char)(g/n);
1254 row[2] = (
unsigned char)(b/n);
1263 static void nsvg__initPaint(NSVGcachedPaint* cache,
NSVGpaint* paint,
float opacity)
1268 cache->type = paint->type;
1270 if (paint->type == NSVG_PAINT_COLOR) {
1271 cache->colors[0] = nsvg__applyOpacity(paint->color, opacity);
1275 grad = paint->gradient;
1277 cache->spread = grad->spread;
1278 memcpy(cache->xform, grad->xform,
sizeof(
float)*6);
1280 if (grad->nstops == 0) {
1281 for (i = 0; i < 256; i++)
1282 cache->colors[i] = 0;
1283 }
if (grad->nstops == 1) {
1284 for (i = 0; i < 256; i++)
1285 cache->colors[i] = nsvg__applyOpacity(grad->stops[i].color, opacity);
1287 unsigned int ca, cb = 0;
1288 float ua, ub, du, u;
1291 ca = nsvg__applyOpacity(grad->stops[0].color, opacity);
1292 ua = nsvg__clampf(grad->stops[0].offset, 0, 1);
1293 ub = nsvg__clampf(grad->stops[grad->nstops-1].offset, ua, 1);
1294 ia = (int)(ua * 255.0f);
1295 ib = (int)(ub * 255.0f);
1296 for (i = 0; i < ia; i++) {
1297 cache->colors[i] = ca;
1300 for (i = 0; i < grad->nstops-1; i++) {
1301 ca = nsvg__applyOpacity(grad->stops[i].color, opacity);
1302 cb = nsvg__applyOpacity(grad->stops[i+1].color, opacity);
1303 ua = nsvg__clampf(grad->stops[i].offset, 0, 1);
1304 ub = nsvg__clampf(grad->stops[i+1].offset, 0, 1);
1305 ia = (int)(ua * 255.0f);
1306 ib = (int)(ub * 255.0f);
1308 if (count <= 0)
continue;
1310 du = 1.0f / (float)count;
1311 for (j = 0; j < count; j++) {
1312 cache->colors[ia+j] = nsvg__lerpRGBA(ca,cb,u);
1317 for (i = ib; i < 256; i++)
1318 cache->colors[i] = cb;
1365 void nsvgRasterize(NSVGrasterizer* r,
1366 NSVGimage* image,
float tx,
float ty,
float scale,
1367 unsigned char* dst,
int w,
int h,
int stride)
1371 NSVGcachedPaint cache;
1379 if (w > r->cscanline) {
1381 r->scanline = (
unsigned char*)realloc(r->scanline, w);
1382 if (r->scanline == NULL)
return;
1385 for (i = 0; i < h; i++)
1386 memset(&dst[i*stride], 0, w*4);
1388 for (shape = image->shapes; shape != NULL; shape = shape->next) {
1389 if (!(shape->flags & NSVG_FLAGS_VISIBLE))
1392 if (shape->fill.type != NSVG_PAINT_NONE) {
1397 nsvg__flattenShape(r, shape, scale);
1400 for (i = 0; i < r->nedges; i++) {
1403 e->y0 = (ty + e->y0) * NSVG__SUBSAMPLES;
1405 e->y1 = (ty + e->y1) * NSVG__SUBSAMPLES;
1409 qsort(r->edges, r->nedges,
sizeof(NSVGedge), nsvg__cmpEdge);
1412 nsvg__initPaint(&cache, &shape->fill, shape->opacity);
1414 nsvg__rasterizeSortedEdges(r, tx,ty,scale, &cache, shape->fillRule);
1416 if (shape->stroke.type != NSVG_PAINT_NONE && (shape->strokeWidth * scale) > 0.01f) {
1421 nsvg__flattenShapeStroke(r, shape, scale);
1426 for (i = 0; i < r->nedges; i++) {
1429 e->y0 = (ty + e->y0) * NSVG__SUBSAMPLES;
1431 e->y1 = (ty + e->y1) * NSVG__SUBSAMPLES;
1435 qsort(r->edges, r->nedges,
sizeof(NSVGedge), nsvg__cmpEdge);
1438 nsvg__initPaint(&cache, &shape->stroke, shape->opacity);
1440 nsvg__rasterizeSortedEdges(r, tx,ty,scale, &cache, NSVG_FILLRULE_NONZERO);
1444 nsvg__unpremultiplyAlpha(dst, w, h, stride);
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: nanosvg.h:158
Definition: nanosvg.h:129
Definition: nanosvg.h:138
Definition: nanosvg.h:113
Definition: nanosvg.h:121