Eugene Zagidullin 9 years ago
parent
commit
9780a95734
1 changed files with 63 additions and 16 deletions
  1. 63
    16
      converter.go

+ 63
- 16
converter.go View File

@@ -76,14 +76,23 @@ func resizeGeneric(in image.Image, out *image.NRGBA64, scale float64, coeffs []i
76 76
 
77 77
 			offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
78 78
 			// Reverse alpha-premultiplication
79
+			r := rgba[0] / sum
80
+			g := rgba[1] / sum
81
+			b := rgba[2] / sum
79 82
 			a := rgba[3] / sum
80
-			value := clampUint16(rgba[0] * 0xffff / a / sum)
83
+
84
+			if a != 0 {
85
+				r = r * 0xffff / a
86
+				g = g * 0xffff / a
87
+				b = b * 0xffff / a
88
+			}
89
+			value := clampUint16(r)
81 90
 			out.Pix[offset+0] = uint8(value >> 8)
82 91
 			out.Pix[offset+1] = uint8(value)
83
-			value = clampUint16(rgba[1] * 0xffff / a / sum)
92
+			value = clampUint16(g)
84 93
 			out.Pix[offset+2] = uint8(value >> 8)
85 94
 			out.Pix[offset+3] = uint8(value)
86
-			value = clampUint16(rgba[2] * 0xffff / a / sum)
95
+			value = clampUint16(b)
87 96
 			out.Pix[offset+4] = uint8(value >> 8)
88 97
 			out.Pix[offset+5] = uint8(value)
89 98
 			value = clampUint16(a)
@@ -127,10 +136,20 @@ func resizeRGBA(in *image.RGBA, out *image.NRGBA, scale float64, coeffs []int16,
127 136
 
128 137
 			xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
129 138
 			// Reverse alpha-premultiplication
139
+			r := rgba[0] / sum
140
+			g := rgba[1] / sum
141
+			b := rgba[2] / sum
130 142
 			a := rgba[3] / sum
131
-			out.Pix[xo+0] = clampUint8(rgba[0] * 0xff / a / sum)
132
-			out.Pix[xo+1] = clampUint8(rgba[1] * 0xff / a / sum)
133
-			out.Pix[xo+2] = clampUint8(rgba[2] * 0xff / a / sum)
143
+
144
+			if a != 0 {
145
+				r = r * 0xff / a
146
+				g = g * 0xff / a
147
+				b = b * 0xff / a
148
+			}
149
+
150
+			out.Pix[xo+0] = clampUint8(r)
151
+			out.Pix[xo+1] = clampUint8(g)
152
+			out.Pix[xo+2] = clampUint8(b)
134 153
 			out.Pix[xo+3] = clampUint8(a)
135 154
 		}
136 155
 	}
@@ -172,10 +191,20 @@ func resizeNRGBA(in *image.NRGBA, out *image.NRGBA, scale float64, coeffs []int1
172 191
 
173 192
 			xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
174 193
 			// Reverse alpha-premultiplication
194
+			r := rgba[0] / sum
195
+			g := rgba[1] / sum
196
+			b := rgba[2] / sum
175 197
 			a := rgba[3] / sum
176
-			out.Pix[xo+0] = clampUint8(rgba[0] * 0xff / a / sum)
177
-			out.Pix[xo+1] = clampUint8(rgba[1] * 0xff / a / sum)
178
-			out.Pix[xo+2] = clampUint8(rgba[2] * 0xff / a / sum)
198
+
199
+			if a != 0 {
200
+				r = r * 0xff / a
201
+				g = g * 0xff / a
202
+				b = b * 0xff / a
203
+			}
204
+
205
+			out.Pix[xo+0] = clampUint8(r)
206
+			out.Pix[xo+1] = clampUint8(g)
207
+			out.Pix[xo+2] = clampUint8(b)
179 208
 			out.Pix[xo+3] = clampUint8(a)
180 209
 		}
181 210
 	}
@@ -215,14 +244,23 @@ func resizeRGBA64(in *image.RGBA64, out *image.NRGBA64, scale float64, coeffs []
215 244
 
216 245
 			xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
217 246
 			// Reverse alpha-premultiplication
247
+			r := rgba[0] / sum
248
+			g := rgba[1] / sum
249
+			b := rgba[2] / sum
218 250
 			a := rgba[3] / sum
219
-			value := clampUint16(rgba[0] * 0xffff / a / sum)
251
+
252
+			if a != 0 {
253
+				r = r * 0xffff / a
254
+				g = g * 0xffff / a
255
+				b = b * 0xffff / a
256
+			}
257
+			value := clampUint16(r)
220 258
 			out.Pix[xo+0] = uint8(value >> 8)
221 259
 			out.Pix[xo+1] = uint8(value)
222
-			value = clampUint16(rgba[1] * 0xffff / a / sum)
260
+			value = clampUint16(g)
223 261
 			out.Pix[xo+2] = uint8(value >> 8)
224 262
 			out.Pix[xo+3] = uint8(value)
225
-			value = clampUint16(rgba[2] * 0xffff / a / sum)
263
+			value = clampUint16(b)
226 264
 			out.Pix[xo+4] = uint8(value >> 8)
227 265
 			out.Pix[xo+5] = uint8(value)
228 266
 			value = clampUint16(a)
@@ -255,7 +293,7 @@ func resizeNRGBA64(in *image.NRGBA64, out *image.NRGBA64, scale float64, coeffs
255 293
 					default:
256 294
 						xi = 0
257 295
 					}
258
-					
296
+
259 297
 					// Forward alpha-premultiplication
260 298
 					a := int64(row[xi+6])<<8 | int64(row[xi+7])
261 299
 					rgba[0] += int64(coeff) * (int64(row[xi+0])<<8 | int64(row[xi+1])) * a / 0xffff
@@ -268,14 +306,23 @@ func resizeNRGBA64(in *image.NRGBA64, out *image.NRGBA64, scale float64, coeffs
268 306
 
269 307
 			xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
270 308
 			// Reverse alpha-premultiplication
309
+			r := rgba[0] / sum
310
+			g := rgba[1] / sum
311
+			b := rgba[2] / sum
271 312
 			a := rgba[3] / sum
272
-			value := clampUint16(rgba[0] * 0xffff / a / sum)
313
+
314
+			if a != 0 {
315
+				r = r * 0xffff / a
316
+				g = g * 0xffff / a
317
+				b = b * 0xffff / a
318
+			}
319
+			value := clampUint16(r)
273 320
 			out.Pix[xo+0] = uint8(value >> 8)
274 321
 			out.Pix[xo+1] = uint8(value)
275
-			value = clampUint16(rgba[1] * 0xffff / a / sum)
322
+			value = clampUint16(g)
276 323
 			out.Pix[xo+2] = uint8(value >> 8)
277 324
 			out.Pix[xo+3] = uint8(value)
278
-			value = clampUint16(rgba[2] * 0xffff / a / sum)
325
+			value = clampUint16(b)
279 326
 			out.Pix[xo+4] = uint8(value >> 8)
280 327
 			out.Pix[xo+5] = uint8(value)
281 328
 			value = clampUint16(a)