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.
This commit is contained in:
nfnt 2016-07-24 22:39:20 +02:00
parent 4d93a29130
commit 891127d8d1
1 changed files with 24 additions and 24 deletions

View File

@ -158,20 +158,20 @@ func resizeNRGBA(in *image.NRGBA, out *image.RGBA, scale float64, coeffs []int16
xi = 0 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 // Forward alpha-premultiplication
a := int32(row[xi+3]) a := int32(row[xi+3])
rgba[0] *= a r := int32(row[xi+0]) * a
rgba[0] /= 0xff r /= 0xff
rgba[1] *= a g := int32(row[xi+1]) * a
rgba[1] /= 0xff g /= 0xff
rgba[2] *= a b := int32(row[xi+2]) * a
rgba[2] /= 0xff 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 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 // Forward alpha-premultiplication
a := int64(uint16(row[xi+6])<<8 | uint16(row[xi+7])) a := int64(uint16(row[xi+6])<<8 | uint16(row[xi+7]))
rgba[0] *= a r := int64(uint16(row[xi+0])<<8|uint16(row[xi+1])) * a
rgba[0] /= 0xffff r /= 0xffff
rgba[1] *= a g := int64(uint16(row[xi+2])<<8|uint16(row[xi+3])) * a
rgba[1] /= 0xffff g /= 0xffff
rgba[2] *= a b := int64(uint16(row[xi+4])<<8|uint16(row[xi+5])) * a
rgba[2] /= 0xffff 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)
} }
} }