No Description

resize_test.go 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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_SameColorWithRGBA(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 := out.At(x, y).(color.RGBA)
  50. if color.R != 0x80 || color.G != 0x80 || color.B != 0x80 || color.A != 0xFF {
  51. t.Errorf("%+v", color)
  52. }
  53. }
  54. }
  55. }
  56. func Test_SameColorWithNRGBA(t *testing.T) {
  57. img := image.NewNRGBA(image.Rect(0, 0, 20, 20))
  58. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  59. for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
  60. img.SetNRGBA(x, y, color.NRGBA{0x80, 0x80, 0x80, 0xFF})
  61. }
  62. }
  63. out := Resize(10, 10, img, Lanczos3)
  64. for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
  65. for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
  66. color := out.At(x, y).(color.RGBA)
  67. if color.R != 0x80 || color.G != 0x80 || color.B != 0x80 || color.A != 0xFF {
  68. t.Errorf("%+v", color)
  69. }
  70. }
  71. }
  72. }
  73. func Test_SameColorWithRGBA64(t *testing.T) {
  74. img := image.NewRGBA64(image.Rect(0, 0, 20, 20))
  75. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  76. for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
  77. img.SetRGBA64(x, y, color.RGBA64{0x8000, 0x8000, 0x8000, 0xFFFF})
  78. }
  79. }
  80. out := Resize(10, 10, img, Lanczos3)
  81. for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
  82. for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
  83. color := out.At(x, y).(color.RGBA64)
  84. if color.R != 0x8000 || color.G != 0x8000 || color.B != 0x8000 || color.A != 0xFFFF {
  85. t.Errorf("%+v", color)
  86. }
  87. }
  88. }
  89. }
  90. func Test_SameColorWithNRGBA64(t *testing.T) {
  91. img := image.NewNRGBA64(image.Rect(0, 0, 20, 20))
  92. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  93. for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
  94. img.SetNRGBA64(x, y, color.NRGBA64{0x8000, 0x8000, 0x8000, 0xFFFF})
  95. }
  96. }
  97. out := Resize(10, 10, img, Lanczos3)
  98. for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
  99. for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
  100. color := out.At(x, y).(color.RGBA64)
  101. if color.R != 0x8000 || color.G != 0x8000 || color.B != 0x8000 || color.A != 0xFFFF {
  102. t.Errorf("%+v", color)
  103. }
  104. }
  105. }
  106. }
  107. func Test_SameColorWithGray(t *testing.T) {
  108. img := image.NewGray(image.Rect(0, 0, 20, 20))
  109. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  110. for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
  111. img.SetGray(x, y, color.Gray{0x80})
  112. }
  113. }
  114. out := Resize(10, 10, img, Lanczos3)
  115. for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
  116. for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
  117. color := out.At(x, y).(color.Gray)
  118. if color.Y != 0x80 {
  119. t.Errorf("%+v", color)
  120. }
  121. }
  122. }
  123. }
  124. func Test_SameColorWithGray16(t *testing.T) {
  125. img := image.NewGray16(image.Rect(0, 0, 20, 20))
  126. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  127. for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
  128. img.SetGray16(x, y, color.Gray16{0x8000})
  129. }
  130. }
  131. out := Resize(10, 10, img, Lanczos3)
  132. for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
  133. for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
  134. color := out.At(x, y).(color.Gray16)
  135. if color.Y != 0x8000 {
  136. t.Errorf("%+v", color)
  137. }
  138. }
  139. }
  140. }
  141. func Test_Bounds(t *testing.T) {
  142. img := image.NewRGBA(image.Rect(20, 10, 200, 99))
  143. out := Resize(80, 80, img, Lanczos2)
  144. out.At(0, 0)
  145. }
  146. func Test_SameSizeReturnsOriginal(t *testing.T) {
  147. img := image.NewRGBA(image.Rect(0, 0, 10, 10))
  148. out := Resize(0, 0, img, Lanczos2)
  149. if img != out {
  150. t.Fail()
  151. }
  152. out = Resize(10, 10, img, Lanczos2)
  153. if img != out {
  154. t.Fail()
  155. }
  156. }
  157. func Test_PixelCoordinates(t *testing.T) {
  158. checkers := image.NewGray(image.Rect(0, 0, 4, 4))
  159. checkers.Pix = []uint8{
  160. 255, 0, 255, 0,
  161. 0, 255, 0, 255,
  162. 255, 0, 255, 0,
  163. 0, 255, 0, 255,
  164. }
  165. resized := Resize(12, 12, checkers, NearestNeighbor).(*image.Gray)
  166. if resized.Pix[0] != 255 || resized.Pix[1] != 255 || resized.Pix[2] != 255 {
  167. t.Fail()
  168. }
  169. if resized.Pix[3] != 0 || resized.Pix[4] != 0 || resized.Pix[5] != 0 {
  170. t.Fail()
  171. }
  172. }
  173. func Test_ResizeWithPremultipliedAlpha(t *testing.T) {
  174. img := image.NewRGBA(image.Rect(0, 0, 1, 4))
  175. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  176. // 0x80 = 0.5 * 0xFF.
  177. img.SetRGBA(0, y, color.RGBA{0x80, 0x80, 0x80, 0x80})
  178. }
  179. out := Resize(1, 2, img, MitchellNetravali)
  180. outputColor := out.At(0, 0).(color.RGBA)
  181. if outputColor.R != 0x80 {
  182. t.Fail()
  183. }
  184. }
  185. func Test_ResizeWithTranslucentColor(t *testing.T) {
  186. img := image.NewNRGBA(image.Rect(0, 0, 1, 2))
  187. // Set the pixel colors to an "invisible green" and white.
  188. // After resizing, the green shouldn't be visible.
  189. img.SetNRGBA(0, 0, color.NRGBA{0x00, 0xFF, 0x00, 0x00})
  190. img.SetNRGBA(0, 1, color.NRGBA{0x00, 0x00, 0x00, 0xFF})
  191. out := Resize(1, 1, img, Bilinear)
  192. _, g, _, _ := out.At(0, 0).RGBA()
  193. if g != 0x00 {
  194. t.Errorf("%+v", g)
  195. }
  196. }
  197. const (
  198. // Use a small image size for benchmarks. We don't want memory performance
  199. // to affect the benchmark results.
  200. benchMaxX = 250
  201. benchMaxY = 250
  202. // Resize values near the original size require increase the amount of time
  203. // resize spends converting the image.
  204. benchWidth = 200
  205. benchHeight = 200
  206. )
  207. func benchRGBA(b *testing.B, interp InterpolationFunction) {
  208. m := image.NewRGBA(image.Rect(0, 0, benchMaxX, benchMaxY))
  209. // Initialize m's pixels to create a non-uniform image.
  210. for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
  211. for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
  212. i := m.PixOffset(x, y)
  213. m.Pix[i+0] = uint8(y + 4*x)
  214. m.Pix[i+1] = uint8(y + 4*x)
  215. m.Pix[i+2] = uint8(y + 4*x)
  216. m.Pix[i+3] = uint8(4*y + x)
  217. }
  218. }
  219. var out image.Image
  220. b.ResetTimer()
  221. for i := 0; i < b.N; i++ {
  222. out = Resize(benchWidth, benchHeight, m, interp)
  223. }
  224. out.At(0, 0)
  225. }
  226. // The names of some interpolation functions are truncated so that the columns
  227. // of 'go test -bench' line up.
  228. func Benchmark_Nearest_RGBA(b *testing.B) {
  229. benchRGBA(b, NearestNeighbor)
  230. }
  231. func Benchmark_Bilinear_RGBA(b *testing.B) {
  232. benchRGBA(b, Bilinear)
  233. }
  234. func Benchmark_Bicubic_RGBA(b *testing.B) {
  235. benchRGBA(b, Bicubic)
  236. }
  237. func Benchmark_Mitchell_RGBA(b *testing.B) {
  238. benchRGBA(b, MitchellNetravali)
  239. }
  240. func Benchmark_Lanczos2_RGBA(b *testing.B) {
  241. benchRGBA(b, Lanczos2)
  242. }
  243. func Benchmark_Lanczos3_RGBA(b *testing.B) {
  244. benchRGBA(b, Lanczos3)
  245. }
  246. func benchYCbCr(b *testing.B, interp InterpolationFunction) {
  247. m := image.NewYCbCr(image.Rect(0, 0, benchMaxX, benchMaxY), image.YCbCrSubsampleRatio422)
  248. // Initialize m's pixels to create a non-uniform image.
  249. for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
  250. for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
  251. yi := m.YOffset(x, y)
  252. ci := m.COffset(x, y)
  253. m.Y[yi] = uint8(16*y + x)
  254. m.Cb[ci] = uint8(y + 16*x)
  255. m.Cr[ci] = uint8(y + 16*x)
  256. }
  257. }
  258. var out image.Image
  259. b.ResetTimer()
  260. for i := 0; i < b.N; i++ {
  261. out = Resize(benchWidth, benchHeight, m, interp)
  262. }
  263. out.At(0, 0)
  264. }
  265. func Benchmark_Nearest_YCC(b *testing.B) {
  266. benchYCbCr(b, NearestNeighbor)
  267. }
  268. func Benchmark_Bilinear_YCC(b *testing.B) {
  269. benchYCbCr(b, Bilinear)
  270. }
  271. func Benchmark_Bicubic_YCC(b *testing.B) {
  272. benchYCbCr(b, Bicubic)
  273. }
  274. func Benchmark_Mitchell_YCC(b *testing.B) {
  275. benchYCbCr(b, MitchellNetravali)
  276. }
  277. func Benchmark_Lanczos2_YCC(b *testing.B) {
  278. benchYCbCr(b, Lanczos2)
  279. }
  280. func Benchmark_Lanczos3_YCC(b *testing.B) {
  281. benchYCbCr(b, Lanczos3)
  282. }