No Description

resize_test.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 Test_Bounds(t *testing.T) {
  57. img := image.NewRGBA(image.Rect(20, 10, 200, 99))
  58. out := Resize(80, 80, img, Lanczos2)
  59. out.At(0, 0)
  60. }
  61. func Test_SameSizeReturnsOriginal(t *testing.T) {
  62. img := image.NewRGBA(image.Rect(0, 0, 10, 10))
  63. out := Resize(0, 0, img, Lanczos2)
  64. if img != out {
  65. t.Fail()
  66. }
  67. out = Resize(10, 10, img, Lanczos2)
  68. if img != out {
  69. t.Fail()
  70. }
  71. }
  72. const (
  73. // Use a small image size for benchmarks. We don't want memory performance
  74. // to affect the benchmark results.
  75. benchMaxX = 250
  76. benchMaxY = 250
  77. // Resize values near the original size require increase the amount of time
  78. // resize spends converting the image.
  79. benchWidth = 200
  80. benchHeight = 200
  81. )
  82. func benchRGBA(b *testing.B, interp InterpolationFunction) {
  83. m := image.NewRGBA(image.Rect(0, 0, benchMaxX, benchMaxY))
  84. // Initialize m's pixels to create a non-uniform image.
  85. for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
  86. for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
  87. i := m.PixOffset(x, y)
  88. m.Pix[i+0] = uint8(y + 4*x)
  89. m.Pix[i+1] = uint8(y + 4*x)
  90. m.Pix[i+2] = uint8(y + 4*x)
  91. m.Pix[i+3] = uint8(4*y + x)
  92. }
  93. }
  94. var out image.Image
  95. b.ResetTimer()
  96. for i := 0; i < b.N; i++ {
  97. out = Resize(benchWidth, benchHeight, m, interp)
  98. }
  99. out.At(0, 0)
  100. }
  101. // The names of some interpolation functions are truncated so that the columns
  102. // of 'go test -bench' line up.
  103. func Benchmark_Nearest_RGBA(b *testing.B) {
  104. benchRGBA(b, NearestNeighbor)
  105. }
  106. func Benchmark_Bilinear_RGBA(b *testing.B) {
  107. benchRGBA(b, Bilinear)
  108. }
  109. func Benchmark_Bicubic_RGBA(b *testing.B) {
  110. benchRGBA(b, Bicubic)
  111. }
  112. func Benchmark_Mitchell_RGBA(b *testing.B) {
  113. benchRGBA(b, MitchellNetravali)
  114. }
  115. func Benchmark_Lanczos2_RGBA(b *testing.B) {
  116. benchRGBA(b, Lanczos2)
  117. }
  118. func Benchmark_Lanczos3_RGBA(b *testing.B) {
  119. benchRGBA(b, Lanczos3)
  120. }
  121. func benchYCbCr(b *testing.B, interp InterpolationFunction) {
  122. m := image.NewYCbCr(image.Rect(0, 0, benchMaxX, benchMaxY), image.YCbCrSubsampleRatio422)
  123. // Initialize m's pixels to create a non-uniform image.
  124. for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
  125. for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
  126. yi := m.YOffset(x, y)
  127. ci := m.COffset(x, y)
  128. m.Y[yi] = uint8(16*y + x)
  129. m.Cb[ci] = uint8(y + 16*x)
  130. m.Cr[ci] = uint8(y + 16*x)
  131. }
  132. }
  133. var out image.Image
  134. b.ResetTimer()
  135. for i := 0; i < b.N; i++ {
  136. out = Resize(benchWidth, benchHeight, m, interp)
  137. }
  138. out.At(0, 0)
  139. }
  140. func Benchmark_Nearest_YCC(b *testing.B) {
  141. benchYCbCr(b, NearestNeighbor)
  142. }
  143. func Benchmark_Bilinear_YCC(b *testing.B) {
  144. benchYCbCr(b, Bilinear)
  145. }
  146. func Benchmark_Bicubic_YCC(b *testing.B) {
  147. benchYCbCr(b, Bicubic)
  148. }
  149. func Benchmark_Mitchell_YCC(b *testing.B) {
  150. benchYCbCr(b, MitchellNetravali)
  151. }
  152. func Benchmark_Lanczos2_YCC(b *testing.B) {
  153. benchYCbCr(b, Lanczos2)
  154. }
  155. func Benchmark_Lanczos3_YCC(b *testing.B) {
  156. benchYCbCr(b, Lanczos3)
  157. }