resize_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_Param1(t *testing.T) {
  14. m := Resize(0, 0, img, NearestNeighbor)
  15. if m.Bounds() != img.Bounds() {
  16. t.Fail()
  17. }
  18. }
  19. func Test_Param2(t *testing.T) {
  20. m := Resize(100, 0, img, NearestNeighbor)
  21. if m.Bounds() != image.Rect(0, 0, 100, 100) {
  22. t.Fail()
  23. }
  24. }
  25. func Test_ZeroImg(t *testing.T) {
  26. zeroImg := image.NewGray16(image.Rect(0, 0, 0, 0))
  27. m := Resize(0, 0, zeroImg, NearestNeighbor)
  28. if m.Bounds() != zeroImg.Bounds() {
  29. t.Fail()
  30. }
  31. }
  32. func Test_CorrectResize(t *testing.T) {
  33. zeroImg := image.NewGray16(image.Rect(0, 0, 256, 256))
  34. m := Resize(60, 0, zeroImg, NearestNeighbor)
  35. if m.Bounds() != image.Rect(0, 0, 60, 60) {
  36. t.Fail()
  37. }
  38. }
  39. func Test_SameColor(t *testing.T) {
  40. img := image.NewRGBA(image.Rect(0, 0, 20, 20))
  41. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  42. for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
  43. img.SetRGBA(x, y, color.RGBA{0x80, 0x80, 0x80, 0xFF})
  44. }
  45. }
  46. out := Resize(10, 10, img, Lanczos3)
  47. for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
  48. for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
  49. color := img.At(x, y).(color.RGBA)
  50. if color.R != 0x80 || color.G != 0x80 || color.B != 0x80 || color.A != 0xFF {
  51. t.Fail()
  52. }
  53. }
  54. }
  55. }
  56. func Benchmark_BigResizeLanczos3(b *testing.B) {
  57. var m image.Image
  58. for i := 0; i < b.N; i++ {
  59. m = Resize(1000, 1000, img, Lanczos3)
  60. }
  61. m.At(0, 0)
  62. }
  63. func Benchmark_Reduction(b *testing.B) {
  64. largeImg := image.NewRGBA(image.Rect(0, 0, 1000, 1000))
  65. var m image.Image
  66. for i := 0; i < b.N; i++ {
  67. m = Resize(300, 300, largeImg, Bicubic)
  68. }
  69. m.At(0, 0)
  70. }
  71. // Benchmark resize of 16 MPix jpeg image to 800px width.
  72. func jpegThumb(b *testing.B, interp InterpolationFunction) {
  73. input := image.NewYCbCr(image.Rect(0, 0, 4896, 3264), image.YCbCrSubsampleRatio422)
  74. var output image.Image
  75. for i := 0; i < b.N; i++ {
  76. output = Resize(800, 0, input, interp)
  77. }
  78. output.At(0, 0)
  79. }
  80. func Benchmark_LargeJpegThumbNearestNeighbor(b *testing.B) {
  81. jpegThumb(b, NearestNeighbor)
  82. }
  83. func Benchmark_LargeJpegThumbBilinear(b *testing.B) {
  84. jpegThumb(b, Bilinear)
  85. }
  86. func Benchmark_LargeJpegThumbBicubic(b *testing.B) {
  87. jpegThumb(b, Bicubic)
  88. }
  89. func Benchmark_LargeJpegThumbMitchellNetravali(b *testing.B) {
  90. jpegThumb(b, MitchellNetravali)
  91. }
  92. func Benchmark_LargeJpegThumbLanczos2(b *testing.B) {
  93. jpegThumb(b, Lanczos2)
  94. }
  95. func Benchmark_LargeJpegThumbLanczos3(b *testing.B) {
  96. jpegThumb(b, Lanczos3)
  97. }