From 2ee9c3b5f31dc39e6dbb1b5f302d2711c1511cef Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Tue, 21 Dec 2010 18:59:51 +0000 Subject: [PATCH] better handling of alpha blending - patch from cdestigter - closes #674 --- include/mapnik/graphics.hpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/include/mapnik/graphics.hpp b/include/mapnik/graphics.hpp index 86e0fa895..be07f31af 100644 --- a/include/mapnik/graphics.hpp +++ b/include/mapnik/graphics.hpp @@ -384,10 +384,14 @@ namespace mapnik unsigned g0 = (rgba0 >> 16 ) & 0xff; unsigned b0 = (rgba0 >> 8) & 0xff; - r0 = byte(((r1 - r0) * a1 + (r0 << 8)) >> 8); - g0 = byte(((g1 - g0) * a1 + (g0 << 8)) >> 8); - b0 = byte(((b1 - b0) * a1 + (b0 << 8)) >> 8); - a0 = byte((a1 + a0) - ((a1 * a0 + 255) >> 8)); + unsigned atmp = a1 + a0 - ((a1 * a0 + 255) >> 8); + if (atmp) + { + r0 = byte((r1 * a1 + (r0 * a0) - ((r0 * a0 * a1 + 255) >> 8)) / atmp); + g0 = byte((g1 * a1 + (g0 * a0) - ((g0 * a0 * a1 + 255) >> 8)) / atmp); + b0 = byte((b1 * a1 + (b0 * a0) - ((b0 * a0 * a1 + 255) >> 8)) / atmp); + } + a0 = byte(atmp); row_to[x] = (a0)| (b0 << 8) | (g0 << 16) | (r0 << 24) ; #else @@ -407,10 +411,14 @@ namespace mapnik unsigned g0 = (rgba0 >> 8 ) & 0xff; unsigned b0 = (rgba0 >> 16) & 0xff; - r0 = byte(((r1 - r0) * a1 + (r0 << 8)) >> 8); - g0 = byte(((g1 - g0) * a1 + (g0 << 8)) >> 8); - b0 = byte(((b1 - b0) * a1 + (b0 << 8)) >> 8); - a0 = byte((a1 + a0) - ((a1 * a0 + 255) >> 8)); + unsigned atmp = a1 + a0 - ((a1 * a0 + 255) >> 8); + if (atmp) + { + r0 = byte((r1 * a1 + (r0 * a0) - ((r0 * a0 * a1 + 255) >> 8)) / atmp); + g0 = byte((g1 * a1 + (g0 * a0) - ((g0 * a0 * a1 + 255) >> 8)) / atmp); + b0 = byte((b1 * a1 + (b0 * a0) - ((b0 * a0 * a1 + 255) >> 8)) / atmp); + } + a0 = byte(atmp); row_to[x] = (a0 << 24)| (b0 << 16) | (g0 << 8) | (r0) ; #endif