From 8d51b88261075250a6e29fcd726e3c30cd2db34e Mon Sep 17 00:00:00 2001 From: Christoph Neuhauser Date: Sat, 6 Jun 2015 16:43:40 +0200 Subject: [PATCH] Fix for "nsvg__scanlineSolid" Updated fix for https://github.com/memononen/nanosvg/issues/37 (code now using nsvg__div255). Fix for "nsvg__scanlineSolid" #2 Updated fix for https://github.com/memononen/nanosvg/issues/37 (code now using nsvg__div255). --- src/nanosvgrast.h | 53 ++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/nanosvgrast.h b/src/nanosvgrast.h index 6ce0557..981ec2e 100644 --- a/src/nanosvgrast.h +++ b/src/nanosvgrast.h @@ -859,6 +859,11 @@ static unsigned int nsvg__applyOpacity(unsigned int c, float u) return nsvg__RGBA(r,g,b,a); } +inline int nsvg__div255(int x) +{ + return ((x+1) * 257) >> 16; +} + static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* cover, int x, int y, float tx, float ty, float scale, NSVGcachedPaint* cache) { @@ -872,18 +877,18 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co for (i = 0; i < count; i++) { int r,g,b; - int a = ((int)cover[0] * ca) >> 8; + int a = nsvg__div255((int)cover[0] * ca); int ia = 255 - a; // Premultiply - r = (cr * a) >> 8; - g = (cg * a) >> 8; - b = (cb * a) >> 8; + r = nsvg__div255(cr * a); + g = nsvg__div255(cg * a); + b = nsvg__div255(cb * a); // Blend over - r += ((ia * (int)dst[0]) >> 8); - g += ((ia * (int)dst[1]) >> 8); - b += ((ia * (int)dst[2]) >> 8); - a += ((ia * (int)dst[3]) >> 8); + r += nsvg__div255(ia * (int)dst[0]); + g += nsvg__div255(ia * (int)dst[1]); + b += nsvg__div255(ia * (int)dst[2]); + a += nsvg__div255(ia * (int)dst[3]); dst[0] = (unsigned char)r; dst[1] = (unsigned char)g; @@ -914,19 +919,19 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co cb = (c >> 16) & 0xff; ca = (c >> 24) & 0xff; - a = ((int)cover[0] * ca) >> 8; + a = nsvg__div255((int)cover[0] * ca); ia = 255 - a; // Premultiply - r = (cr * a) >> 8; - g = (cg * a) >> 8; - b = (cb * a) >> 8; + r = nsvg__div255(cr * a); + g = nsvg__div255(cg * a); + b = nsvg__div255(cb * a); // Blend over - r += ((ia * (int)dst[0]) >> 8); - g += ((ia * (int)dst[1]) >> 8); - b += ((ia * (int)dst[2]) >> 8); - a += ((ia * (int)dst[3]) >> 8); + r += nsvg__div255(ia * (int)dst[0]); + g += nsvg__div255(ia * (int)dst[1]); + b += nsvg__div255(ia * (int)dst[2]); + a += nsvg__div255(ia * (int)dst[3]); dst[0] = (unsigned char)r; dst[1] = (unsigned char)g; @@ -961,19 +966,19 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co cb = (c >> 16) & 0xff; ca = (c >> 24) & 0xff; - a = ((int)cover[0] * ca) >> 8; + a = nsvg__div255((int)cover[0] * ca); ia = 255 - a; // Premultiply - r = (cr * a) >> 8; - g = (cg * a) >> 8; - b = (cb * a) >> 8; + r = nsvg__div255(cr * a); + g = nsvg__div255(cg * a); + b = nsvg__div255(cb * a); // Blend over - r += ((ia * (int)dst[0]) >> 8); - g += ((ia * (int)dst[1]) >> 8); - b += ((ia * (int)dst[2]) >> 8); - a += ((ia * (int)dst[3]) >> 8); + r += nsvg__div255(ia * (int)dst[0]); + g += nsvg__div255(ia * (int)dst[1]); + b += nsvg__div255(ia * (int)dst[2]); + a += nsvg__div255(ia * (int)dst[3]); dst[0] = (unsigned char)r; dst[1] = (unsigned char)g;