blur_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2011 The Graphics-Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package graphics
  5. import (
  6. "code.google.com/p/graphics-go/graphics/graphicstest"
  7. "image"
  8. "image/color"
  9. "testing"
  10. _ "image/png"
  11. )
  12. var blurOneColorTests = []transformOneColorTest{
  13. {
  14. "1x1-blank", 1, 1, 1, 1,
  15. &BlurOptions{0.83, 1},
  16. []uint8{0xff},
  17. []uint8{0xff},
  18. },
  19. {
  20. "1x1-spreadblank", 1, 1, 1, 1,
  21. &BlurOptions{0.83, 2},
  22. []uint8{0xff},
  23. []uint8{0xff},
  24. },
  25. {
  26. "3x3-blank", 3, 3, 3, 3,
  27. &BlurOptions{0.83, 2},
  28. []uint8{
  29. 0xff, 0xff, 0xff,
  30. 0xff, 0xff, 0xff,
  31. 0xff, 0xff, 0xff,
  32. },
  33. []uint8{
  34. 0xff, 0xff, 0xff,
  35. 0xff, 0xff, 0xff,
  36. 0xff, 0xff, 0xff,
  37. },
  38. },
  39. {
  40. "3x3-dot", 3, 3, 3, 3,
  41. &BlurOptions{0.34, 1},
  42. []uint8{
  43. 0x00, 0x00, 0x00,
  44. 0x00, 0xff, 0x00,
  45. 0x00, 0x00, 0x00,
  46. },
  47. []uint8{
  48. 0x00, 0x03, 0x00,
  49. 0x03, 0xf2, 0x03,
  50. 0x00, 0x03, 0x00,
  51. },
  52. },
  53. {
  54. "5x5-dot", 5, 5, 5, 5,
  55. &BlurOptions{0.34, 1},
  56. []uint8{
  57. 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0xff, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00,
  62. },
  63. []uint8{
  64. 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x03, 0x00, 0x00,
  66. 0x00, 0x03, 0xf2, 0x03, 0x00,
  67. 0x00, 0x00, 0x03, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00,
  69. },
  70. },
  71. {
  72. "5x5-dot-spread", 5, 5, 5, 5,
  73. &BlurOptions{0.85, 1},
  74. []uint8{
  75. 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0xff, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00,
  80. },
  81. []uint8{
  82. 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x10, 0x20, 0x10, 0x00,
  84. 0x00, 0x20, 0x40, 0x20, 0x00,
  85. 0x00, 0x10, 0x20, 0x10, 0x00,
  86. 0x00, 0x00, 0x00, 0x00, 0x00,
  87. },
  88. },
  89. {
  90. "4x4-box", 4, 4, 4, 4,
  91. &BlurOptions{0.34, 1},
  92. []uint8{
  93. 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0xff, 0xff, 0x00,
  95. 0x00, 0xff, 0xff, 0x00,
  96. 0x00, 0x00, 0x00, 0x00,
  97. },
  98. []uint8{
  99. 0x00, 0x03, 0x03, 0x00,
  100. 0x03, 0xf8, 0xf8, 0x03,
  101. 0x03, 0xf8, 0xf8, 0x03,
  102. 0x00, 0x03, 0x03, 0x00,
  103. },
  104. },
  105. {
  106. "5x5-twodots", 5, 5, 5, 5,
  107. &BlurOptions{0.34, 1},
  108. []uint8{
  109. 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, 0x96, 0x00, 0x96, 0x00,
  112. 0x00, 0x00, 0x00, 0x00, 0x00,
  113. 0x00, 0x00, 0x00, 0x00, 0x00,
  114. },
  115. []uint8{
  116. 0x00, 0x00, 0x00, 0x00, 0x00,
  117. 0x00, 0x02, 0x00, 0x02, 0x00,
  118. 0x02, 0x8e, 0x04, 0x8e, 0x02,
  119. 0x00, 0x02, 0x00, 0x02, 0x00,
  120. 0x00, 0x00, 0x00, 0x00, 0x00,
  121. },
  122. },
  123. }
  124. func TestBlurOneColor(t *testing.T) {
  125. for _, oc := range blurOneColorTests {
  126. dst := oc.newDst()
  127. src := oc.newSrc()
  128. opt := oc.opt.(*BlurOptions)
  129. if err := Blur(dst, src, opt); err != nil {
  130. t.Fatal(err)
  131. }
  132. if !checkTransformTest(t, &oc, dst) {
  133. continue
  134. }
  135. }
  136. }
  137. func TestBlurEmpty(t *testing.T) {
  138. empty := image.NewRGBA(image.Rect(0, 0, 0, 0))
  139. if err := Blur(empty, empty, nil); err != nil {
  140. t.Fatal(err)
  141. }
  142. }
  143. func TestBlurGopher(t *testing.T) {
  144. src, err := graphicstest.LoadImage("../testdata/gopher.png")
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. dst := image.NewRGBA(src.Bounds())
  149. if err = Blur(dst, src, &BlurOptions{StdDev: 1.1}); err != nil {
  150. t.Fatal(err)
  151. }
  152. cmp, err := graphicstest.LoadImage("../testdata/gopher-blur.png")
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. err = graphicstest.ImageWithinTolerance(dst, cmp, 0x101)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. }
  161. func benchBlur(b *testing.B, bounds image.Rectangle) {
  162. b.StopTimer()
  163. // Construct a fuzzy image.
  164. src := image.NewRGBA(bounds)
  165. for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  166. for x := bounds.Min.X; x < bounds.Max.X; x++ {
  167. src.SetRGBA(x, y, color.RGBA{
  168. uint8(5 * x % 0x100),
  169. uint8(7 * y % 0x100),
  170. uint8((7*x + 5*y) % 0x100),
  171. 0xff,
  172. })
  173. }
  174. }
  175. dst := image.NewRGBA(bounds)
  176. b.StartTimer()
  177. for i := 0; i < b.N; i++ {
  178. Blur(dst, src, &BlurOptions{0.84, 3})
  179. }
  180. }
  181. func BenchmarkBlur400x400x3(b *testing.B) {
  182. benchBlur(b, image.Rect(0, 0, 400, 400))
  183. }
  184. // Exactly twice the pixel count of 400x400.
  185. func BenchmarkBlur400x800x3(b *testing.B) {
  186. benchBlur(b, image.Rect(0, 0, 400, 800))
  187. }
  188. // Exactly twice the pixel count of 400x800
  189. func BenchmarkBlur400x1600x3(b *testing.B) {
  190. benchBlur(b, image.Rect(0, 0, 400, 1600))
  191. }