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.
master
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
}
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)
}
}