|
@@ -26,124 +26,275 @@ package resize
|
26
|
26
|
|
27
|
27
|
import (
|
28
|
28
|
"image"
|
29
|
|
- "image/color"
|
30
|
29
|
"runtime"
|
|
30
|
+ "sync"
|
31
|
31
|
)
|
32
|
32
|
|
33
|
|
-// Filter can interpolate at points (x,y)
|
34
|
|
-type Filter interface {
|
35
|
|
- SetKernelWeights(u float32)
|
36
|
|
- Interpolate(u float32, y int) color.RGBA64
|
|
33
|
+// An InterpolationFunction provides the parameters that describe an
|
|
34
|
+// interpolation kernel. It returns the number of samples to take
|
|
35
|
+// and the kernel function to use for sampling.
|
|
36
|
+type InterpolationFunction func() (int, func(float64) float64)
|
|
37
|
+
|
|
38
|
+// Nearest-neighbor interpolation
|
|
39
|
+func NearestNeighbor() (int, func(float64) float64) {
|
|
40
|
+ return 2, nearest
|
|
41
|
+}
|
|
42
|
+
|
|
43
|
+// Bilinear interpolation
|
|
44
|
+func Bilinear() (int, func(float64) float64) {
|
|
45
|
+ return 2, linear
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+// Bicubic interpolation (with cubic hermite spline)
|
|
49
|
+func Bicubic() (int, func(float64) float64) {
|
|
50
|
+ return 4, cubic
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+// Mitchell-Netravali interpolation
|
|
54
|
+func MitchellNetravali() (int, func(float64) float64) {
|
|
55
|
+ return 4, mitchellnetravali
|
|
56
|
+}
|
|
57
|
+
|
|
58
|
+// Lanczos interpolation (a=2)
|
|
59
|
+func Lanczos2() (int, func(float64) float64) {
|
|
60
|
+ return 4, lanczos2
|
|
61
|
+}
|
|
62
|
+
|
|
63
|
+// Lanczos interpolation (a=3)
|
|
64
|
+func Lanczos3() (int, func(float64) float64) {
|
|
65
|
+ return 6, lanczos3
|
37
|
66
|
}
|
38
|
67
|
|
39
|
|
-// InterpolationFunction return a Filter implementation
|
40
|
|
-// that operates on an image. Two factors
|
41
|
|
-// allow to scale the filter kernels in x- and y-direction
|
42
|
|
-// to prevent moire patterns.
|
43
|
|
-type InterpolationFunction func(image.Image, float32) Filter
|
|
68
|
+// values <1 will sharpen the image
|
|
69
|
+var blur = 1.0
|
44
|
70
|
|
45
|
|
-// Resize an image to new width and height using the interpolation function interp.
|
|
71
|
+// Resize scales an image to new width and height using the interpolation function interp.
|
46
|
72
|
// A new image with the given dimensions will be returned.
|
47
|
73
|
// If one of the parameters width or height is set to 0, its size will be calculated so that
|
48
|
74
|
// the aspect ratio is that of the originating image.
|
49
|
75
|
// The resizing algorithm uses channels for parallel computation.
|
50
|
76
|
func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
|
51
|
|
- oldBounds := img.Bounds()
|
52
|
|
- oldWidth := float32(oldBounds.Dx())
|
53
|
|
- oldHeight := float32(oldBounds.Dy())
|
54
|
|
- scaleX, scaleY := calcFactors(width, height, oldWidth, oldHeight)
|
55
|
|
-
|
56
|
|
- tempImg := image.NewRGBA64(image.Rect(0, 0, oldBounds.Dy(), int(0.7+oldWidth/scaleX)))
|
57
|
|
- b := tempImg.Bounds()
|
58
|
|
- adjust := 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dy()-1))
|
59
|
|
-
|
60
|
|
- n := numJobs(b.Dy())
|
61
|
|
- c := make(chan int, n)
|
62
|
|
- for i := 0; i < n; i++ {
|
63
|
|
- slice := image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n)
|
64
|
|
- go resizeSlice(img, tempImg, interp, scaleX, adjust, float32(oldBounds.Min.X), oldBounds.Min.Y, slice, c)
|
|
77
|
+ scaleX, scaleY := calcFactors(width, height, float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))
|
|
78
|
+ if width == 0 {
|
|
79
|
+ width = uint(0.7 + float64(img.Bounds().Dx())/scaleX)
|
65
|
80
|
}
|
66
|
|
- for i := 0; i < n; i++ {
|
67
|
|
- <-c
|
|
81
|
+ if height == 0 {
|
|
82
|
+ height = uint(0.7 + float64(img.Bounds().Dy())/scaleY)
|
68
|
83
|
}
|
69
|
84
|
|
70
|
|
- resultImg := image.NewRGBA64(image.Rect(0, 0, int(0.7+oldWidth/scaleX), int(0.7+oldHeight/scaleY)))
|
71
|
|
- b = resultImg.Bounds()
|
72
|
|
- adjust = 0.5 * ((oldHeight-1.0)/scaleY - float32(b.Dy()-1))
|
|
85
|
+ taps, kernel := interp()
|
|
86
|
+ cpus := runtime.NumCPU()
|
|
87
|
+ wg := sync.WaitGroup{}
|
73
|
88
|
|
74
|
|
- for i := 0; i < n; i++ {
|
75
|
|
- slice := image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n)
|
76
|
|
- go resizeSlice(tempImg, resultImg, interp, scaleY, adjust, 0, 0, slice, c)
|
77
|
|
- }
|
78
|
|
- for i := 0; i < n; i++ {
|
79
|
|
- <-c
|
80
|
|
- }
|
|
89
|
+ // Generic access to image.Image is slow in tight loops.
|
|
90
|
+ // The optimal access has to be determined from the concrete image type.
|
|
91
|
+ switch input := img.(type) {
|
|
92
|
+ case *image.RGBA:
|
|
93
|
+ // 8-bit precision
|
|
94
|
+ temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
95
|
+ result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
|
81
|
96
|
|
82
|
|
- return resultImg
|
83
|
|
-}
|
|
97
|
+ // horizontal filter, results in transposed temporary image
|
|
98
|
+ coeffs, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
|
|
99
|
+ wg.Add(cpus)
|
|
100
|
+ for i := 0; i < cpus; i++ {
|
|
101
|
+ slice := makeSlice(temp, i, cpus).(*image.RGBA)
|
|
102
|
+ go func() {
|
|
103
|
+ defer wg.Done()
|
|
104
|
+ resizeRGBA(input, slice, scaleX, coeffs, filterLength)
|
|
105
|
+ }()
|
|
106
|
+ }
|
|
107
|
+ wg.Wait()
|
84
|
108
|
|
85
|
|
-// Resize a rectangle image slice
|
86
|
|
-func resizeSlice(input image.Image, output *image.RGBA64, interp InterpolationFunction, scale, adjust, xoffset float32, yoffset int, slice image.Rectangle, c chan int) {
|
87
|
|
- filter := interp(input, float32(clampFactor(scale)))
|
88
|
|
- var u float32
|
89
|
|
- var color color.RGBA64
|
90
|
|
- for y := slice.Min.Y; y < slice.Max.Y; y++ {
|
91
|
|
- u = scale*(float32(y)+adjust) + xoffset
|
92
|
|
- filter.SetKernelWeights(u)
|
93
|
|
- for x := slice.Min.X; x < slice.Max.X; x++ {
|
94
|
|
- color = filter.Interpolate(u, x+yoffset)
|
95
|
|
- i := output.PixOffset(x, y)
|
96
|
|
- output.Pix[i+0] = uint8(color.R >> 8)
|
97
|
|
- output.Pix[i+1] = uint8(color.R)
|
98
|
|
- output.Pix[i+2] = uint8(color.G >> 8)
|
99
|
|
- output.Pix[i+3] = uint8(color.G)
|
100
|
|
- output.Pix[i+4] = uint8(color.B >> 8)
|
101
|
|
- output.Pix[i+5] = uint8(color.B)
|
102
|
|
- output.Pix[i+6] = uint8(color.A >> 8)
|
103
|
|
- output.Pix[i+7] = uint8(color.A)
|
|
109
|
+ // horizontal filter on transposed image, result is not transposed
|
|
110
|
+ coeffs, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
|
|
111
|
+ wg.Add(cpus)
|
|
112
|
+ for i := 0; i < cpus; i++ {
|
|
113
|
+ slice := makeSlice(result, i, cpus).(*image.RGBA)
|
|
114
|
+ go func() {
|
|
115
|
+ defer wg.Done()
|
|
116
|
+ resizeRGBA(temp, slice, scaleY, coeffs, filterLength)
|
|
117
|
+ }()
|
104
|
118
|
}
|
105
|
|
- }
|
|
119
|
+ wg.Wait()
|
|
120
|
+ return result
|
|
121
|
+ case *image.YCbCr:
|
|
122
|
+ // 8-bit precision
|
|
123
|
+ // accessing the YCbCr arrays in a tight loop is slow.
|
|
124
|
+ // converting the image before filtering will improve performance.
|
|
125
|
+ inputAsRGBA := convertYCbCrToRGBA(input)
|
|
126
|
+ temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
127
|
+ result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
|
|
128
|
+
|
|
129
|
+ // horizontal filter, results in transposed temporary image
|
|
130
|
+ coeffs, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
|
|
131
|
+ wg.Add(cpus)
|
|
132
|
+ for i := 0; i < cpus; i++ {
|
|
133
|
+ slice := makeSlice(temp, i, cpus).(*image.RGBA)
|
|
134
|
+ go func() {
|
|
135
|
+ defer wg.Done()
|
|
136
|
+ resizeRGBA(inputAsRGBA, slice, scaleX, coeffs, filterLength)
|
|
137
|
+ }()
|
|
138
|
+ }
|
|
139
|
+ wg.Wait()
|
|
140
|
+
|
|
141
|
+ // horizontal filter on transposed image, result is not transposed
|
|
142
|
+ coeffs, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
|
|
143
|
+ wg.Add(cpus)
|
|
144
|
+ for i := 0; i < cpus; i++ {
|
|
145
|
+ slice := makeSlice(result, i, cpus).(*image.RGBA)
|
|
146
|
+ go func() {
|
|
147
|
+ defer wg.Done()
|
|
148
|
+ resizeRGBA(temp, slice, scaleY, coeffs, filterLength)
|
|
149
|
+ }()
|
|
150
|
+ }
|
|
151
|
+ wg.Wait()
|
|
152
|
+ return result
|
|
153
|
+ case *image.RGBA64:
|
|
154
|
+ // 16-bit precision
|
|
155
|
+ temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
156
|
+ result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
|
|
157
|
+
|
|
158
|
+ // horizontal filter, results in transposed temporary image
|
|
159
|
+ coeffs, filterLength := createWeights16(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
|
|
160
|
+ wg.Add(cpus)
|
|
161
|
+ for i := 0; i < cpus; i++ {
|
|
162
|
+ slice := makeSlice(temp, i, cpus).(*image.RGBA64)
|
|
163
|
+ go func() {
|
|
164
|
+ defer wg.Done()
|
|
165
|
+ resizeRGBA64(input, slice, scaleX, coeffs, filterLength)
|
|
166
|
+ }()
|
|
167
|
+ }
|
|
168
|
+ wg.Wait()
|
|
169
|
+
|
|
170
|
+ // horizontal filter on transposed image, result is not transposed
|
|
171
|
+ coeffs, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
|
|
172
|
+ wg.Add(cpus)
|
|
173
|
+ for i := 0; i < cpus; i++ {
|
|
174
|
+ slice := makeSlice(result, i, cpus).(*image.RGBA64)
|
|
175
|
+ go func() {
|
|
176
|
+ defer wg.Done()
|
|
177
|
+ resizeGeneric(temp, slice, scaleY, coeffs, filterLength)
|
|
178
|
+ }()
|
|
179
|
+ }
|
|
180
|
+ wg.Wait()
|
|
181
|
+ return result
|
|
182
|
+ case *image.Gray:
|
|
183
|
+ // 8-bit precision
|
|
184
|
+ temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
185
|
+ result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
|
|
186
|
+
|
|
187
|
+ // horizontal filter, results in transposed temporary image
|
|
188
|
+ coeffs, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
|
|
189
|
+ wg.Add(cpus)
|
|
190
|
+ for i := 0; i < cpus; i++ {
|
|
191
|
+ slice := makeSlice(temp, i, cpus).(*image.Gray)
|
|
192
|
+ go func() {
|
|
193
|
+ defer wg.Done()
|
|
194
|
+ resizeGray(input, slice, scaleX, coeffs, filterLength)
|
|
195
|
+ }()
|
|
196
|
+ }
|
|
197
|
+ wg.Wait()
|
|
198
|
+
|
|
199
|
+ // horizontal filter on transposed image, result is not transposed
|
|
200
|
+ coeffs, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
|
|
201
|
+ wg.Add(cpus)
|
|
202
|
+ for i := 0; i < cpus; i++ {
|
|
203
|
+ slice := makeSlice(result, i, cpus).(*image.Gray)
|
|
204
|
+ go func() {
|
|
205
|
+ defer wg.Done()
|
|
206
|
+ resizeGray(temp, slice, scaleY, coeffs, filterLength)
|
|
207
|
+ }()
|
|
208
|
+ }
|
|
209
|
+ wg.Wait()
|
|
210
|
+ return result
|
|
211
|
+ case *image.Gray16:
|
|
212
|
+ // 16-bit precision
|
|
213
|
+ temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
214
|
+ result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
|
|
215
|
+
|
|
216
|
+ // horizontal filter, results in transposed temporary image
|
|
217
|
+ coeffs, filterLength := createWeights16(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
|
|
218
|
+ wg.Add(cpus)
|
|
219
|
+ for i := 0; i < cpus; i++ {
|
|
220
|
+ slice := makeSlice(temp, i, cpus).(*image.Gray16)
|
|
221
|
+ go func() {
|
|
222
|
+ defer wg.Done()
|
|
223
|
+ resizeGray16(input, slice, scaleX, coeffs, filterLength)
|
|
224
|
+ }()
|
|
225
|
+ }
|
|
226
|
+ wg.Wait()
|
|
227
|
+
|
|
228
|
+ // horizontal filter on transposed image, result is not transposed
|
|
229
|
+ coeffs, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
|
|
230
|
+ wg.Add(cpus)
|
|
231
|
+ for i := 0; i < cpus; i++ {
|
|
232
|
+ slice := makeSlice(result, i, cpus).(*image.Gray16)
|
|
233
|
+ go func() {
|
|
234
|
+ defer wg.Done()
|
|
235
|
+ resizeGray16(temp, slice, scaleY, coeffs, filterLength)
|
|
236
|
+ }()
|
|
237
|
+ }
|
|
238
|
+ wg.Wait()
|
|
239
|
+ return result
|
|
240
|
+ default:
|
|
241
|
+ // 16-bit precision
|
|
242
|
+ temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
|
|
243
|
+ result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
|
|
244
|
+
|
|
245
|
+ // horizontal filter, results in transposed temporary image
|
|
246
|
+ coeffs, filterLength := createWeights16(temp.Bounds().Dy(), img.Bounds().Min.X, taps, blur, scaleX, kernel)
|
|
247
|
+ wg.Add(cpus)
|
|
248
|
+ for i := 0; i < cpus; i++ {
|
|
249
|
+ slice := makeSlice(temp, i, cpus).(*image.RGBA64)
|
|
250
|
+ go func() {
|
|
251
|
+ defer wg.Done()
|
|
252
|
+ resizeGeneric(img, slice, scaleX, coeffs, filterLength)
|
|
253
|
+ }()
|
|
254
|
+ }
|
|
255
|
+ wg.Wait()
|
106
|
256
|
|
107
|
|
- c <- 1
|
|
257
|
+ // horizontal filter on transposed image, result is not transposed
|
|
258
|
+ coeffs, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
|
|
259
|
+ wg.Add(cpus)
|
|
260
|
+ for i := 0; i < cpus; i++ {
|
|
261
|
+ slice := makeSlice(result, i, cpus).(*image.RGBA64)
|
|
262
|
+ go func() {
|
|
263
|
+ defer wg.Done()
|
|
264
|
+ resizeRGBA64(temp, slice, scaleY, coeffs, filterLength)
|
|
265
|
+ }()
|
|
266
|
+ }
|
|
267
|
+ wg.Wait()
|
|
268
|
+ return result
|
|
269
|
+ }
|
108
|
270
|
}
|
109
|
271
|
|
110
|
|
-// Calculate scaling factors using old and new image dimensions.
|
111
|
|
-func calcFactors(width, height uint, oldWidth, oldHeight float32) (scaleX, scaleY float32) {
|
|
272
|
+// Calculates scaling factors using old and new image dimensions.
|
|
273
|
+func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
|
112
|
274
|
if width == 0 {
|
113
|
275
|
if height == 0 {
|
114
|
276
|
scaleX = 1.0
|
115
|
277
|
scaleY = 1.0
|
116
|
278
|
} else {
|
117
|
|
- scaleY = oldHeight / float32(height)
|
|
279
|
+ scaleY = oldHeight / float64(height)
|
118
|
280
|
scaleX = scaleY
|
119
|
281
|
}
|
120
|
282
|
} else {
|
121
|
|
- scaleX = oldWidth / float32(width)
|
|
283
|
+ scaleX = oldWidth / float64(width)
|
122
|
284
|
if height == 0 {
|
123
|
285
|
scaleY = scaleX
|
124
|
286
|
} else {
|
125
|
|
- scaleY = oldHeight / float32(height)
|
|
287
|
+ scaleY = oldHeight / float64(height)
|
126
|
288
|
}
|
127
|
289
|
}
|
128
|
290
|
return
|
129
|
291
|
}
|
130
|
292
|
|
131
|
|
-// Set filter scaling factor to avoid moire patterns.
|
132
|
|
-// This is only useful in case of downscaling (factor>1).
|
133
|
|
-func clampFactor(factor float32) float32 {
|
134
|
|
- if factor < 1 {
|
135
|
|
- factor = 1
|
136
|
|
- }
|
137
|
|
- return factor
|
|
293
|
+type imageWithSubImage interface {
|
|
294
|
+ image.Image
|
|
295
|
+ SubImage(image.Rectangle) image.Image
|
138
|
296
|
}
|
139
|
297
|
|
140
|
|
-// Set number of parallel jobs
|
141
|
|
-// but prevent resize from doing too much work
|
142
|
|
-// if #CPUs > width
|
143
|
|
-func numJobs(d int) (n int) {
|
144
|
|
- n = runtime.NumCPU()
|
145
|
|
- if n > d {
|
146
|
|
- n = d
|
147
|
|
- }
|
148
|
|
- return
|
|
298
|
+func makeSlice(img imageWithSubImage, i, n int) image.Image {
|
|
299
|
+ return img.SubImage(image.Rect(img.Bounds().Min.X, img.Bounds().Min.Y+i*img.Bounds().Dy()/n, img.Bounds().Max.X, img.Bounds().Min.Y+(i+1)*img.Bounds().Dy()/n))
|
149
|
300
|
}
|