resize_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package resize
  2. import (
  3. "image"
  4. "image/color"
  5. "runtime"
  6. "testing"
  7. )
  8. var img = image.NewGray16(image.Rect(0, 0, 3, 3))
  9. func init() {
  10. runtime.GOMAXPROCS(runtime.NumCPU())
  11. img.Set(1, 1, color.White)
  12. }
  13. func Test_Nearest(t *testing.T) {
  14. m := Resize(6, 0, img, NearestNeighbor)
  15. if m.At(1, 1) == m.At(2, 2) {
  16. t.Fail()
  17. }
  18. }
  19. func Test_Param1(t *testing.T) {
  20. m := Resize(0, 0, img, NearestNeighbor)
  21. if m.Bounds() != img.Bounds() {
  22. t.Fail()
  23. }
  24. }
  25. func Test_Param2(t *testing.T) {
  26. m := Resize(100, 0, img, NearestNeighbor)
  27. if m.Bounds() != image.Rect(0, 0, 100, 100) {
  28. t.Fail()
  29. }
  30. }
  31. func Test_ZeroImg(t *testing.T) {
  32. zeroImg := image.NewGray16(image.Rect(0, 0, 0, 0))
  33. m := Resize(0, 0, zeroImg, NearestNeighbor)
  34. if m.Bounds() != zeroImg.Bounds() {
  35. t.Fail()
  36. }
  37. }
  38. func Test_CorrectResize(t *testing.T) {
  39. zeroImg := image.NewGray16(image.Rect(0, 0, 256, 256))
  40. m := Resize(60, 0, zeroImg, NearestNeighbor)
  41. if m.Bounds() != image.Rect(0, 0, 60, 60) {
  42. t.Fail()
  43. }
  44. }
  45. func Benchmark_BigResizeLanczos3(b *testing.B) {
  46. var m image.Image
  47. for i := 0; i < b.N; i++ {
  48. m = Resize(1000, 1000, img, Lanczos3)
  49. }
  50. m.At(0, 0)
  51. }
  52. func Benchmark_Reduction(b *testing.B) {
  53. largeImg := image.NewRGBA(image.Rect(0, 0, 1000, 1000))
  54. var m image.Image
  55. for i := 0; i < b.N; i++ {
  56. m = Resize(300, 300, largeImg, Bicubic)
  57. }
  58. m.At(0, 0)
  59. }
  60. // Benchmark resize of 16 MPix jpeg image to 800px width.
  61. func jpegThumb(b *testing.B, interp InterpolationFunction) {
  62. input := image.NewYCbCr(image.Rect(0, 0, 4896, 3264), image.YCbCrSubsampleRatio422)
  63. var output image.Image
  64. for i := 0; i < b.N; i++ {
  65. output = Resize(800, 0, input, interp)
  66. }
  67. output.At(0, 0)
  68. }
  69. func Benchmark_LargeJpegThumbNearestNeighbor(b *testing.B) {
  70. jpegThumb(b, NearestNeighbor)
  71. }
  72. func Benchmark_LargeJpegThumbBilinear(b *testing.B) {
  73. jpegThumb(b, Bilinear)
  74. }
  75. func Benchmark_LargeJpegThumbBicubic(b *testing.B) {
  76. jpegThumb(b, Bicubic)
  77. }
  78. func Benchmark_LargeJpegThumbMitchellNetravali(b *testing.B) {
  79. jpegThumb(b, MitchellNetravali)
  80. }
  81. func Benchmark_LargeJpegThumbLanczos2(b *testing.B) {
  82. jpegThumb(b, Lanczos2)
  83. }
  84. func Benchmark_LargeJpegThumbLanczos3(b *testing.B) {
  85. jpegThumb(b, Lanczos3)
  86. }