resize_test.go 4.6 KB

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