From 891127d8d1b52734debe1b3c3d7e747502b6c366 Mon Sep 17 00:00:00 2001 From: nfnt Date: Sun, 24 Jul 2016 22:39:20 +0200 Subject: [PATCH] Fix alpha pre-multiplication. The alpha pre-multiplication was applied to an aggregate value an not to the color value. This could cause wrong colors for images with an alpha channel. Fixes #47. --- converter.go | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/converter.go b/converter.go index 20ac593..f9c520d 100644 --- a/converter.go +++ b/converter.go @@ -158,20 +158,20 @@ func resizeNRGBA(in *image.NRGBA, out *image.RGBA, scale float64, coeffs []int16 xi = 0 } - rgba[0] += int32(coeff) * int32(row[xi+0]) - rgba[1] += int32(coeff) * int32(row[xi+1]) - rgba[2] += int32(coeff) * int32(row[xi+2]) - rgba[3] += int32(coeff) * int32(row[xi+3]) - sum += int32(coeff) - // Forward alpha-premultiplication a := int32(row[xi+3]) - rgba[0] *= a - rgba[0] /= 0xff - rgba[1] *= a - rgba[1] /= 0xff - rgba[2] *= a - rgba[2] /= 0xff + r := int32(row[xi+0]) * a + r /= 0xff + g := int32(row[xi+1]) * a + g /= 0xff + b := int32(row[xi+2]) * a + b /= 0xff + + rgba[0] += int32(coeff) * r + rgba[1] += int32(coeff) * g + rgba[2] += int32(coeff) * b + rgba[3] += int32(coeff) * a + sum += int32(coeff) } } @@ -259,20 +259,20 @@ func resizeNRGBA64(in *image.NRGBA64, out *image.RGBA64, scale float64, coeffs [ xi = 0 } - rgba[0] += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1])) - rgba[1] += int64(coeff) * int64(uint16(row[xi+2])<<8|uint16(row[xi+3])) - rgba[2] += int64(coeff) * int64(uint16(row[xi+4])<<8|uint16(row[xi+5])) - rgba[3] += int64(coeff) * int64(uint16(row[xi+6])<<8|uint16(row[xi+7])) - sum += int64(coeff) - // Forward alpha-premultiplication a := int64(uint16(row[xi+6])<<8 | uint16(row[xi+7])) - rgba[0] *= a - rgba[0] /= 0xffff - rgba[1] *= a - rgba[1] /= 0xffff - rgba[2] *= a - rgba[2] /= 0xffff + r := int64(uint16(row[xi+0])<<8|uint16(row[xi+1])) * a + r /= 0xffff + g := int64(uint16(row[xi+2])<<8|uint16(row[xi+3])) * a + g /= 0xffff + b := int64(uint16(row[xi+4])<<8|uint16(row[xi+5])) * a + b /= 0xffff + + rgba[0] += int64(coeff) * r + rgba[1] += int64(coeff) * g + rgba[2] += int64(coeff) * b + rgba[3] += int64(coeff) * a + sum += int64(coeff) } }