resize_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package resize
  2. import (
  3. "image"
  4. "image/color"
  5. "image/png"
  6. "os"
  7. //"runtime"
  8. "testing"
  9. )
  10. var img = image.NewGray16(image.Rect(0, 0, 3, 3))
  11. func init() {
  12. //runtime.GOMAXPROCS(runtime.NumCPU())
  13. img.Set(1, 1, color.White)
  14. }
  15. func Test_Param1(t *testing.T) {
  16. m := Resize(0, 0, img, NearestNeighbor)
  17. if m.Bounds() != img.Bounds() {
  18. t.Fail()
  19. }
  20. }
  21. func Test_Param2(t *testing.T) {
  22. m := Resize(100, 0, img, NearestNeighbor)
  23. if m.Bounds() != image.Rect(0, 0, 100, 100) {
  24. t.Fail()
  25. }
  26. }
  27. func Test_ZeroImg(t *testing.T) {
  28. zeroImg := image.NewGray16(image.Rect(0, 0, 0, 0))
  29. m := Resize(0, 0, zeroImg, NearestNeighbor)
  30. if m.Bounds() != zeroImg.Bounds() {
  31. t.Fail()
  32. }
  33. }
  34. func Test_CorrectResize(t *testing.T) {
  35. zeroImg := image.NewGray16(image.Rect(0, 0, 256, 256))
  36. m := Resize(60, 0, zeroImg, NearestNeighbor)
  37. if m.Bounds() != image.Rect(0, 0, 60, 60) {
  38. t.Fail()
  39. }
  40. }
  41. func Test_SameColor(t *testing.T) {
  42. img := image.NewRGBA(image.Rect(0, 0, 20, 20))
  43. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  44. for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
  45. img.SetRGBA(x, y, color.RGBA{0x80, 0x80, 0x80, 0xFF})
  46. }
  47. }
  48. out := Resize(10, 10, img, Lanczos3)
  49. for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
  50. for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
  51. color := img.At(x, y).(color.RGBA)
  52. if color.R != 0x80 || color.G != 0x80 || color.B != 0x80 || color.A != 0xFF {
  53. t.Fail()
  54. }
  55. }
  56. }
  57. }
  58. func Test_Bounds(t *testing.T) {
  59. img := image.NewRGBA(image.Rect(20, 10, 200, 99))
  60. out := Resize(80, 80, img, Lanczos2)
  61. out.At(0, 0)
  62. }
  63. func Benchmark_BigResizeLanczos3(b *testing.B) {
  64. var m image.Image
  65. for i := 0; i < b.N; i++ {
  66. m = Resize(1000, 1000, img, Lanczos3)
  67. }
  68. m.At(0, 0)
  69. }
  70. func Benchmark_Reduction(b *testing.B) {
  71. largeImg := image.NewRGBA(image.Rect(0, 0, 1000, 1000))
  72. var m image.Image
  73. for i := 0; i < b.N; i++ {
  74. m = Resize(300, 300, largeImg, Bicubic)
  75. }
  76. m.At(0, 0)
  77. }
  78. // Benchmark resize of 16 MPix jpeg image to 800px width.
  79. func jpegThumb(b *testing.B, interp InterpolationFunction) {
  80. input := image.NewYCbCr(image.Rect(0, 0, 4896, 3264), image.YCbCrSubsampleRatio422)
  81. var output image.Image
  82. for i := 0; i < b.N; i++ {
  83. output = Resize(800, 0, input, interp)
  84. }
  85. output.At(0, 0)
  86. }
  87. func Benchmark_LargeJpegThumbNearestNeighbor(b *testing.B) {
  88. jpegThumb(b, NearestNeighbor)
  89. }
  90. func Benchmark_LargeJpegThumbBilinear(b *testing.B) {
  91. jpegThumb(b, Bilinear)
  92. }
  93. func Benchmark_LargeJpegThumbBicubic(b *testing.B) {
  94. jpegThumb(b, Bicubic)
  95. }
  96. func Benchmark_LargeJpegThumbMitchellNetravali(b *testing.B) {
  97. jpegThumb(b, MitchellNetravali)
  98. }
  99. func Benchmark_LargeJpegThumbLanczos2(b *testing.B) {
  100. jpegThumb(b, Lanczos2)
  101. }
  102. func Benchmark_LargeJpegThumbLanczos3(b *testing.B) {
  103. jpegThumb(b, Lanczos3)
  104. }
  105. func Benchmark_LargeN(b *testing.B) {
  106. file, _ := os.Open("M94A0467.png")
  107. defer file.Close()
  108. img, _ := png.Decode(file)
  109. var output image.Image
  110. b.ResetTimer()
  111. for i := 0; i < b.N; i++ {
  112. output = Resize(900, 0, img, Bicubic)
  113. }
  114. b.StopTimer()
  115. output.At(0, 0)
  116. outPng, _ := os.Create("out.png")
  117. defer outPng.Close()
  118. png.Encode(outPng, output)
  119. }