Browse Source

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.
nfnt 8 years ago
parent
commit
891127d8d1
1 changed files with 24 additions and 24 deletions
  1. 24
    24
      converter.go

+ 24
- 24
converter.go View File

@@ -158,20 +158,20 @@ func resizeNRGBA(in *image.NRGBA, out *image.RGBA, scale float64, coeffs []int16
158 158
 						xi = 0
159 159
 					}
160 160
 
161
-					rgba[0] += int32(coeff) * int32(row[xi+0])
162
-					rgba[1] += int32(coeff) * int32(row[xi+1])
163
-					rgba[2] += int32(coeff) * int32(row[xi+2])
164
-					rgba[3] += int32(coeff) * int32(row[xi+3])
165
-					sum += int32(coeff)
166
-
167 161
 					// Forward alpha-premultiplication
168 162
 					a := int32(row[xi+3])
169
-					rgba[0] *= a
170
-					rgba[0] /= 0xff
171
-					rgba[1] *= a
172
-					rgba[1] /= 0xff
173
-					rgba[2] *= a
174
-					rgba[2] /= 0xff
163
+					r := int32(row[xi+0]) * a
164
+					r /= 0xff
165
+					g := int32(row[xi+1]) * a
166
+					g /= 0xff
167
+					b := int32(row[xi+2]) * a
168
+					b /= 0xff
169
+
170
+					rgba[0] += int32(coeff) * r
171
+					rgba[1] += int32(coeff) * g
172
+					rgba[2] += int32(coeff) * b
173
+					rgba[3] += int32(coeff) * a
174
+					sum += int32(coeff)
175 175
 				}
176 176
 			}
177 177
 
@@ -259,20 +259,20 @@ func resizeNRGBA64(in *image.NRGBA64, out *image.RGBA64, scale float64, coeffs [
259 259
 						xi = 0
260 260
 					}
261 261
 
262
-					rgba[0] += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1]))
263
-					rgba[1] += int64(coeff) * int64(uint16(row[xi+2])<<8|uint16(row[xi+3]))
264
-					rgba[2] += int64(coeff) * int64(uint16(row[xi+4])<<8|uint16(row[xi+5]))
265
-					rgba[3] += int64(coeff) * int64(uint16(row[xi+6])<<8|uint16(row[xi+7]))
266
-					sum += int64(coeff)
267
-
268 262
 					// Forward alpha-premultiplication
269 263
 					a := int64(uint16(row[xi+6])<<8 | uint16(row[xi+7]))
270
-					rgba[0] *= a
271
-					rgba[0] /= 0xffff
272
-					rgba[1] *= a
273
-					rgba[1] /= 0xffff
274
-					rgba[2] *= a
275
-					rgba[2] /= 0xffff
264
+					r := int64(uint16(row[xi+0])<<8|uint16(row[xi+1])) * a
265
+					r /= 0xffff
266
+					g := int64(uint16(row[xi+2])<<8|uint16(row[xi+3])) * a
267
+					g /= 0xffff
268
+					b := int64(uint16(row[xi+4])<<8|uint16(row[xi+5])) * a
269
+					b /= 0xffff
270
+
271
+					rgba[0] += int64(coeff) * r
272
+					rgba[1] += int64(coeff) * g
273
+					rgba[2] += int64(coeff) * b
274
+					rgba[3] += int64(coeff) * a
275
+					sum += int64(coeff)
276 276
 				}
277 277
 			}
278 278