resize_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package resize
  2. import (
  3. "image"
  4. "fmt"
  5. "image/color"
  6. "runtime"
  7. "testing"
  8. )
  9. var img = image.NewGray16(image.Rect(0, 0, 3, 3))
  10. func init() {
  11. runtime.GOMAXPROCS(runtime.NumCPU())
  12. img.Set(1, 1, color.White)
  13. }
  14. func Test_Param1(t *testing.T) {
  15. m := Resize(0, 0, img, NearestNeighbor)
  16. if m.Bounds() != img.Bounds() {
  17. t.Fail()
  18. }
  19. }
  20. func Test_Param2(t *testing.T) {
  21. m := Resize(100, 0, img, NearestNeighbor)
  22. if m.Bounds() != image.Rect(0, 0, 100, 100) {
  23. t.Fail()
  24. }
  25. }
  26. func Test_ZeroImg(t *testing.T) {
  27. zeroImg := image.NewGray16(image.Rect(0, 0, 0, 0))
  28. m := Resize(0, 0, zeroImg, NearestNeighbor)
  29. if m.Bounds() != zeroImg.Bounds() {
  30. t.Fail()
  31. }
  32. }
  33. func Test_CorrectResize(t *testing.T) {
  34. zeroImg := image.NewGray16(image.Rect(0, 0, 256, 256))
  35. m := Resize(60, 0, zeroImg, NearestNeighbor)
  36. if m.Bounds() != image.Rect(0, 0, 60, 60) {
  37. t.Fail()
  38. }
  39. }
  40. func Test_SameColor(t *testing.T) {
  41. img := image.NewRGBA(image.Rect(0, 0, 20, 20))
  42. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  43. for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
  44. img.SetRGBA(x, y, color.RGBA{0x80, 0x80, 0x80, 0xFF})
  45. }
  46. }
  47. out := Resize(10, 10, img, Lanczos3)
  48. for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
  49. for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
  50. color := img.At(x, y).(color.RGBA)
  51. if color.R != 0x80 || color.G != 0x80 || color.B != 0x80 || color.A != 0xFF {
  52. t.Fail()
  53. }
  54. }
  55. }
  56. }
  57. func Test_Bounds(t *testing.T) {
  58. img := image.NewRGBA(image.Rect(20, 10, 200, 99))
  59. out := Resize(80, 80, img, Lanczos2)
  60. out.At(0, 0)
  61. }
  62. func Test_SameSizeReturnsOriginal(t *testing.T) {
  63. img := image.NewRGBA(image.Rect(0, 0, 10, 10))
  64. out := Resize(0, 0, img, Lanczos2)
  65. if img != out {
  66. t.Fail()
  67. }
  68. out = Resize(10, 10, img, Lanczos2)
  69. if img != out {
  70. t.Fail()
  71. }
  72. }
  73. func Test_ResizeWithPremultipliedAlpha(t *testing.T) {
  74. img := image.NewRGBA(image.Rect(0, 0, 1, 4))
  75. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  76. // 0x80 = 0.5 * 0xFF.
  77. img.SetRGBA(0, y, color.RGBA{0x80, 0x80, 0x80, 0x80})
  78. }
  79. out := Resize(1, 2, img, MitchellNetravali)
  80. fmt.Println(out)
  81. outputColor := out.At(0,0).(color.NRGBA)
  82. if outputColor.R != 0xFF {
  83. t.Fail()
  84. }
  85. }
  86. const (
  87. // Use a small image size for benchmarks. We don't want memory performance
  88. // to affect the benchmark results.
  89. benchMaxX = 250
  90. benchMaxY = 250
  91. // Resize values near the original size require increase the amount of time
  92. // resize spends converting the image.
  93. benchWidth = 200
  94. benchHeight = 200
  95. )
  96. func benchRGBA(b *testing.B, interp InterpolationFunction) {
  97. m := image.NewRGBA(image.Rect(0, 0, benchMaxX, benchMaxY))
  98. // Initialize m's pixels to create a non-uniform image.
  99. for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
  100. for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
  101. i := m.PixOffset(x, y)
  102. m.Pix[i+0] = uint8(y + 4*x)
  103. m.Pix[i+1] = uint8(y + 4*x)
  104. m.Pix[i+2] = uint8(y + 4*x)
  105. m.Pix[i+3] = uint8(4*y + x)
  106. }
  107. }
  108. var out image.Image
  109. b.ResetTimer()
  110. for i := 0; i < b.N; i++ {
  111. out = Resize(benchWidth, benchHeight, m, interp)
  112. }
  113. out.At(0, 0)
  114. }
  115. // The names of some interpolation functions are truncated so that the columns
  116. // of 'go test -bench' line up.
  117. func Benchmark_Nearest_RGBA(b *testing.B) {
  118. benchRGBA(b, NearestNeighbor)
  119. }
  120. func Benchmark_Bilinear_RGBA(b *testing.B) {
  121. benchRGBA(b, Bilinear)
  122. }
  123. func Benchmark_Bicubic_RGBA(b *testing.B) {
  124. benchRGBA(b, Bicubic)
  125. }
  126. func Benchmark_Mitchell_RGBA(b *testing.B) {
  127. benchRGBA(b, MitchellNetravali)
  128. }
  129. func Benchmark_Lanczos2_RGBA(b *testing.B) {
  130. benchRGBA(b, Lanczos2)
  131. }
  132. func Benchmark_Lanczos3_RGBA(b *testing.B) {
  133. benchRGBA(b, Lanczos3)
  134. }
  135. func benchYCbCr(b *testing.B, interp InterpolationFunction) {
  136. m := image.NewYCbCr(image.Rect(0, 0, benchMaxX, benchMaxY), image.YCbCrSubsampleRatio422)
  137. // Initialize m's pixels to create a non-uniform image.
  138. for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
  139. for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
  140. yi := m.YOffset(x, y)
  141. ci := m.COffset(x, y)
  142. m.Y[yi] = uint8(16*y + x)
  143. m.Cb[ci] = uint8(y + 16*x)
  144. m.Cr[ci] = uint8(y + 16*x)
  145. }
  146. }
  147. var out image.Image
  148. b.ResetTimer()
  149. for i := 0; i < b.N; i++ {
  150. out = Resize(benchWidth, benchHeight, m, interp)
  151. }
  152. out.At(0, 0)
  153. }
  154. func Benchmark_Nearest_YCC(b *testing.B) {
  155. benchYCbCr(b, NearestNeighbor)
  156. }
  157. func Benchmark_Bilinear_YCC(b *testing.B) {
  158. benchYCbCr(b, Bilinear)
  159. }
  160. func Benchmark_Bicubic_YCC(b *testing.B) {
  161. benchYCbCr(b, Bicubic)
  162. }
  163. func Benchmark_Mitchell_YCC(b *testing.B) {
  164. benchYCbCr(b, MitchellNetravali)
  165. }
  166. func Benchmark_Lanczos2_YCC(b *testing.B) {
  167. benchYCbCr(b, Lanczos2)
  168. }
  169. func Benchmark_Lanczos3_YCC(b *testing.B) {
  170. benchYCbCr(b, Lanczos3)
  171. }