resize_test.go 4.6 KB

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