|
@@ -33,36 +33,41 @@ import (
|
33
|
33
|
// An InterpolationFunction provides the parameters that describe an
|
34
|
34
|
// interpolation kernel. It returns the number of samples to take
|
35
|
35
|
// and the kernel function to use for sampling.
|
36
|
|
-type InterpolationFunction func() (int, func(float64) float64)
|
|
36
|
+type InterpolationFunction int
|
37
|
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
|
|
-}
|
|
38
|
+// InterpolationFunction constants
|
|
39
|
+const (
|
|
40
|
+ // Nearest-neighbor interpolation
|
|
41
|
+ NearestNeighbor InterpolationFunction = iota
|
|
42
|
+ // Bilinear interpolation
|
|
43
|
+ Bilinear
|
|
44
|
+ // Bicubic interpolation (with cubic hermite spline)
|
|
45
|
+ Bicubic
|
|
46
|
+ // Mitchell-Netravali interpolation
|
|
47
|
+ MitchellNetravali
|
|
48
|
+ // Lanczos interpolation (a=2)
|
|
49
|
+ Lanczos2
|
|
50
|
+ // Lanczos interpolation (a=3)
|
|
51
|
+ Lanczos3
|
|
52
|
+)
|
62
|
53
|
|
63
|
|
-// Lanczos interpolation (a=3)
|
64
|
|
-func Lanczos3() (int, func(float64) float64) {
|
65
|
|
- return 6, lanczos3
|
|
54
|
+// kernal, returns an InterpolationFunctions taps and kernel.
|
|
55
|
+func (i InterpolationFunction) kernel() (int, func(float64) float64) {
|
|
56
|
+ switch i {
|
|
57
|
+ case Bilinear:
|
|
58
|
+ return 2, linear
|
|
59
|
+ case Bicubic:
|
|
60
|
+ return 4, cubic
|
|
61
|
+ case MitchellNetravali:
|
|
62
|
+ return 4, mitchellnetravali
|
|
63
|
+ case Lanczos2:
|
|
64
|
+ return 4, lanczos2
|
|
65
|
+ case Lanczos3:
|
|
66
|
+ return 6, lanczos3
|
|
67
|
+ default:
|
|
68
|
+ // Default to NearestNeighbor.
|
|
69
|
+ return 2, nearest
|
|
70
|
+ }
|
66
|
71
|
}
|
67
|
72
|
|
68
|
73
|
// values <1 will sharpen the image
|
|
@@ -81,8 +86,11 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
|
81
|
86
|
if height == 0 {
|
82
|
87
|
height = uint(0.7 + float64(img.Bounds().Dy())/scaleY)
|
83
|
88
|
}
|
|
89
|
+ if interp == NearestNeighbor {
|
|
90
|
+ return resizeNearest(width, height, scaleX, scaleY, img, interp)
|
|
91
|
+ }
|
84
|
92
|
|
85
|
|
- taps, kernel := interp()
|
|
93
|
+ taps, kernel := interp.kernel()
|
86
|
94
|
cpus := runtime.NumCPU()
|
87
|
95
|
wg := sync.WaitGroup{}
|
88
|
96
|
|
|
@@ -269,6 +277,193 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
|
269
|
277
|
}
|
270
|
278
|
}
|
271
|
279
|
|
|
280
|
+func resizeNearest(width, height uint, scaleX, scaleY float64, img image.Image, interp InterpolationFunction) image.Image {
|
|
281
|
+ taps, _ := interp.kernel()
|
|
282
|
+ cpus := runtime.NumCPU()
|
|
283
|
+ wg := sync.WaitGroup{}
|
|
284
|
+
|
|
285
|
+ switch input := img.(type) {
|
|
286
|
+ case *image.RGBA:
|
|
287
|
+ // 8-bit precision
|
|
288
|
+ temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
289
|
+ result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
|
|
290
|
+
|
|
291
|
+ // horizontal filter, results in transposed temporary image
|
|
292
|
+ coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
|
|
293
|
+ wg.Add(cpus)
|
|
294
|
+ for i := 0; i < cpus; i++ {
|
|
295
|
+ slice := makeSlice(temp, i, cpus).(*image.RGBA)
|
|
296
|
+ go func() {
|
|
297
|
+ defer wg.Done()
|
|
298
|
+ nearestRGBA(input, slice, scaleX, coeffs, offset, filterLength)
|
|
299
|
+ }()
|
|
300
|
+ }
|
|
301
|
+ wg.Wait()
|
|
302
|
+
|
|
303
|
+ // horizontal filter on transposed image, result is not transposed
|
|
304
|
+ coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
|
|
305
|
+ wg.Add(cpus)
|
|
306
|
+ for i := 0; i < cpus; i++ {
|
|
307
|
+ slice := makeSlice(result, i, cpus).(*image.RGBA)
|
|
308
|
+ go func() {
|
|
309
|
+ defer wg.Done()
|
|
310
|
+ nearestRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
|
|
311
|
+ }()
|
|
312
|
+ }
|
|
313
|
+ wg.Wait()
|
|
314
|
+ return result
|
|
315
|
+ case *image.YCbCr:
|
|
316
|
+ // 8-bit precision
|
|
317
|
+ // accessing the YCbCr arrays in a tight loop is slow.
|
|
318
|
+ // converting the image before filtering will improve performance.
|
|
319
|
+ inputAsRGBA := convertYCbCrToRGBA(input)
|
|
320
|
+ temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
321
|
+ result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
|
|
322
|
+
|
|
323
|
+ // horizontal filter, results in transposed temporary image
|
|
324
|
+ coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
|
|
325
|
+ wg.Add(cpus)
|
|
326
|
+ for i := 0; i < cpus; i++ {
|
|
327
|
+ slice := makeSlice(temp, i, cpus).(*image.RGBA)
|
|
328
|
+ go func() {
|
|
329
|
+ defer wg.Done()
|
|
330
|
+ nearestRGBA(inputAsRGBA, slice, scaleX, coeffs, offset, filterLength)
|
|
331
|
+ }()
|
|
332
|
+ }
|
|
333
|
+ wg.Wait()
|
|
334
|
+
|
|
335
|
+ // horizontal filter on transposed image, result is not transposed
|
|
336
|
+ coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
|
|
337
|
+ wg.Add(cpus)
|
|
338
|
+ for i := 0; i < cpus; i++ {
|
|
339
|
+ slice := makeSlice(result, i, cpus).(*image.RGBA)
|
|
340
|
+ go func() {
|
|
341
|
+ defer wg.Done()
|
|
342
|
+ nearestRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
|
|
343
|
+ }()
|
|
344
|
+ }
|
|
345
|
+ wg.Wait()
|
|
346
|
+ return result
|
|
347
|
+ case *image.RGBA64:
|
|
348
|
+ // 16-bit precision
|
|
349
|
+ temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
350
|
+ result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
|
|
351
|
+
|
|
352
|
+ // horizontal filter, results in transposed temporary image
|
|
353
|
+ coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
|
|
354
|
+ wg.Add(cpus)
|
|
355
|
+ for i := 0; i < cpus; i++ {
|
|
356
|
+ slice := makeSlice(temp, i, cpus).(*image.RGBA64)
|
|
357
|
+ go func() {
|
|
358
|
+ defer wg.Done()
|
|
359
|
+ nearestRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
|
|
360
|
+ }()
|
|
361
|
+ }
|
|
362
|
+ wg.Wait()
|
|
363
|
+
|
|
364
|
+ // horizontal filter on transposed image, result is not transposed
|
|
365
|
+ coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
|
|
366
|
+ wg.Add(cpus)
|
|
367
|
+ for i := 0; i < cpus; i++ {
|
|
368
|
+ slice := makeSlice(result, i, cpus).(*image.RGBA64)
|
|
369
|
+ go func() {
|
|
370
|
+ defer wg.Done()
|
|
371
|
+ nearestGeneric(temp, slice, scaleY, coeffs, offset, filterLength)
|
|
372
|
+ }()
|
|
373
|
+ }
|
|
374
|
+ wg.Wait()
|
|
375
|
+ return result
|
|
376
|
+ case *image.Gray:
|
|
377
|
+ // 8-bit precision
|
|
378
|
+ temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
379
|
+ result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
|
|
380
|
+
|
|
381
|
+ // horizontal filter, results in transposed temporary image
|
|
382
|
+ coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
|
|
383
|
+ wg.Add(cpus)
|
|
384
|
+ for i := 0; i < cpus; i++ {
|
|
385
|
+ slice := makeSlice(temp, i, cpus).(*image.Gray)
|
|
386
|
+ go func() {
|
|
387
|
+ defer wg.Done()
|
|
388
|
+ nearestGray(input, slice, scaleX, coeffs, offset, filterLength)
|
|
389
|
+ }()
|
|
390
|
+ }
|
|
391
|
+ wg.Wait()
|
|
392
|
+
|
|
393
|
+ // horizontal filter on transposed image, result is not transposed
|
|
394
|
+ coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
|
|
395
|
+ wg.Add(cpus)
|
|
396
|
+ for i := 0; i < cpus; i++ {
|
|
397
|
+ slice := makeSlice(result, i, cpus).(*image.Gray)
|
|
398
|
+ go func() {
|
|
399
|
+ defer wg.Done()
|
|
400
|
+ nearestGray(temp, slice, scaleY, coeffs, offset, filterLength)
|
|
401
|
+ }()
|
|
402
|
+ }
|
|
403
|
+ wg.Wait()
|
|
404
|
+ return result
|
|
405
|
+ case *image.Gray16:
|
|
406
|
+ // 16-bit precision
|
|
407
|
+ temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
|
408
|
+ result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
|
|
409
|
+
|
|
410
|
+ // horizontal filter, results in transposed temporary image
|
|
411
|
+ coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
|
|
412
|
+ wg.Add(cpus)
|
|
413
|
+ for i := 0; i < cpus; i++ {
|
|
414
|
+ slice := makeSlice(temp, i, cpus).(*image.Gray16)
|
|
415
|
+ go func() {
|
|
416
|
+ defer wg.Done()
|
|
417
|
+ nearestGray16(input, slice, scaleX, coeffs, offset, filterLength)
|
|
418
|
+ }()
|
|
419
|
+ }
|
|
420
|
+ wg.Wait()
|
|
421
|
+
|
|
422
|
+ // horizontal filter on transposed image, result is not transposed
|
|
423
|
+ coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
|
|
424
|
+ wg.Add(cpus)
|
|
425
|
+ for i := 0; i < cpus; i++ {
|
|
426
|
+ slice := makeSlice(result, i, cpus).(*image.Gray16)
|
|
427
|
+ go func() {
|
|
428
|
+ defer wg.Done()
|
|
429
|
+ nearestGray16(temp, slice, scaleY, coeffs, offset, filterLength)
|
|
430
|
+ }()
|
|
431
|
+ }
|
|
432
|
+ wg.Wait()
|
|
433
|
+ return result
|
|
434
|
+ default:
|
|
435
|
+ // 16-bit precision
|
|
436
|
+ temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
|
|
437
|
+ result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
|
|
438
|
+
|
|
439
|
+ // horizontal filter, results in transposed temporary image
|
|
440
|
+ coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), img.Bounds().Min.X, taps, blur, scaleX)
|
|
441
|
+ wg.Add(cpus)
|
|
442
|
+ for i := 0; i < cpus; i++ {
|
|
443
|
+ slice := makeSlice(temp, i, cpus).(*image.RGBA64)
|
|
444
|
+ go func() {
|
|
445
|
+ defer wg.Done()
|
|
446
|
+ nearestGeneric(img, slice, scaleX, coeffs, offset, filterLength)
|
|
447
|
+ }()
|
|
448
|
+ }
|
|
449
|
+ wg.Wait()
|
|
450
|
+
|
|
451
|
+ // horizontal filter on transposed image, result is not transposed
|
|
452
|
+ coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
|
|
453
|
+ wg.Add(cpus)
|
|
454
|
+ for i := 0; i < cpus; i++ {
|
|
455
|
+ slice := makeSlice(result, i, cpus).(*image.RGBA64)
|
|
456
|
+ go func() {
|
|
457
|
+ defer wg.Done()
|
|
458
|
+ nearestRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
|
|
459
|
+ }()
|
|
460
|
+ }
|
|
461
|
+ wg.Wait()
|
|
462
|
+ return result
|
|
463
|
+ }
|
|
464
|
+
|
|
465
|
+}
|
|
466
|
+
|
272
|
467
|
// Calculates scaling factors using old and new image dimensions.
|
273
|
468
|
func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
|
274
|
469
|
if width == 0 {
|