1
0

effects_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package imaging
  2. import (
  3. "image"
  4. "testing"
  5. )
  6. func TestBlur(t *testing.T) {
  7. td := []struct {
  8. desc string
  9. src image.Image
  10. sigma float64
  11. want *image.NRGBA
  12. }{
  13. {
  14. "Blur 3x3 0",
  15. &image.NRGBA{
  16. Rect: image.Rect(-1, -1, 2, 2),
  17. Stride: 3 * 4,
  18. Pix: []uint8{
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  20. 0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  21. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  22. },
  23. },
  24. 0.0,
  25. &image.NRGBA{
  26. Rect: image.Rect(0, 0, 3, 3),
  27. Stride: 3 * 4,
  28. Pix: []uint8{
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. },
  33. },
  34. },
  35. {
  36. "Blur 3x3 0.5",
  37. &image.NRGBA{
  38. Rect: image.Rect(-1, -1, 2, 2),
  39. Stride: 3 * 4,
  40. Pix: []uint8{
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. },
  45. },
  46. 0.5,
  47. &image.NRGBA{
  48. Rect: image.Rect(0, 0, 3, 3),
  49. Stride: 3 * 4,
  50. Pix: []uint8{
  51. 0x66, 0xaa, 0xff, 0x04, 0x66, 0xaa, 0xff, 0x18, 0x66, 0xaa, 0xff, 0x04,
  52. 0x66, 0xaa, 0xff, 0x18, 0x66, 0xaa, 0xff, 0x9e, 0x66, 0xaa, 0xff, 0x18,
  53. 0x66, 0xaa, 0xff, 0x04, 0x66, 0xaa, 0xff, 0x18, 0x66, 0xaa, 0xff, 0x04,
  54. },
  55. },
  56. },
  57. {
  58. "Blur 3x3 10",
  59. &image.NRGBA{
  60. Rect: image.Rect(-1, -1, 2, 2),
  61. Stride: 3 * 4,
  62. Pix: []uint8{
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. },
  67. },
  68. 10,
  69. &image.NRGBA{
  70. Rect: image.Rect(0, 0, 3, 3),
  71. Stride: 3 * 4,
  72. Pix: []uint8{
  73. 0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c,
  74. 0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c,
  75. 0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c,
  76. },
  77. },
  78. },
  79. }
  80. for _, d := range td {
  81. got := Blur(d.src, d.sigma)
  82. want := d.want
  83. if !compareNRGBA(got, want, 0) {
  84. t.Errorf("test [%s] failed: %#v", d.desc, got)
  85. }
  86. }
  87. }
  88. func TestBlurGolden(t *testing.T) {
  89. src, err := Open("testdata/lena_128.png")
  90. if err != nil {
  91. t.Errorf("Open: %v", err)
  92. }
  93. for name, sigma := range map[string]float64{
  94. "out_blur_0.5.png": 0.5,
  95. "out_blur_1.5.png": 1.5,
  96. } {
  97. got := Blur(src, sigma)
  98. want, err := Open("testdata/" + name)
  99. if err != nil {
  100. t.Errorf("Open: %v", err)
  101. }
  102. if !compareNRGBA(got, toNRGBA(want), 0) {
  103. t.Errorf("resulting image differs from golden: %s", name)
  104. }
  105. }
  106. }
  107. func BenchmarkBlur(b *testing.B) {
  108. b.StopTimer()
  109. img, err := Open("testdata/lena_512.png")
  110. if err != nil {
  111. b.Fatalf("Open: %v", err)
  112. }
  113. b.StartTimer()
  114. for i := 0; i < b.N; i++ {
  115. Blur(img, 3)
  116. }
  117. }
  118. func TestSharpen(t *testing.T) {
  119. td := []struct {
  120. desc string
  121. src image.Image
  122. sigma float64
  123. want *image.NRGBA
  124. }{
  125. {
  126. "Sharpen 3x3 0",
  127. &image.NRGBA{
  128. Rect: image.Rect(-1, -1, 2, 2),
  129. Stride: 3 * 4,
  130. Pix: []uint8{
  131. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  132. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  133. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  134. },
  135. },
  136. 0,
  137. &image.NRGBA{
  138. Rect: image.Rect(0, 0, 3, 3),
  139. Stride: 3 * 4,
  140. Pix: []uint8{
  141. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  142. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  143. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  144. },
  145. },
  146. },
  147. {
  148. "Sharpen 3x3 0.5",
  149. &image.NRGBA{
  150. Rect: image.Rect(-1, -1, 2, 2),
  151. Stride: 3 * 4,
  152. Pix: []uint8{
  153. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  154. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  155. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  156. },
  157. },
  158. 0.5,
  159. &image.NRGBA{
  160. Rect: image.Rect(0, 0, 3, 3),
  161. Stride: 3 * 4,
  162. Pix: []uint8{
  163. 0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66,
  164. 0x64, 0x64, 0x64, 0x64, 0x7d, 0x7d, 0x7d, 0x7e, 0x64, 0x64, 0x64, 0x64,
  165. 0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66,
  166. },
  167. },
  168. },
  169. {
  170. "Sharpen 3x3 100",
  171. &image.NRGBA{
  172. Rect: image.Rect(-1, -1, 2, 2),
  173. Stride: 3 * 4,
  174. Pix: []uint8{
  175. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  176. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  177. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  178. },
  179. },
  180. 100,
  181. &image.NRGBA{
  182. Rect: image.Rect(0, 0, 3, 3),
  183. Stride: 3 * 4,
  184. Pix: []uint8{
  185. 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
  186. 0x64, 0x64, 0x64, 0x64, 0x86, 0x86, 0x86, 0x86, 0x64, 0x64, 0x64, 0x64,
  187. 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
  188. },
  189. },
  190. },
  191. {
  192. "Sharpen 3x1 10",
  193. &image.NRGBA{
  194. Rect: image.Rect(-1, -1, 2, 0),
  195. Stride: 3 * 4,
  196. Pix: []uint8{
  197. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  198. },
  199. },
  200. 10,
  201. &image.NRGBA{
  202. Rect: image.Rect(0, 0, 3, 1),
  203. Stride: 3 * 4,
  204. Pix: []uint8{
  205. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  206. },
  207. },
  208. },
  209. }
  210. for _, d := range td {
  211. got := Sharpen(d.src, d.sigma)
  212. want := d.want
  213. if !compareNRGBA(got, want, 0) {
  214. t.Errorf("test [%s] failed: %#v", d.desc, got)
  215. }
  216. }
  217. }
  218. func TestSharpenGolden(t *testing.T) {
  219. src, err := Open("testdata/lena_128.png")
  220. if err != nil {
  221. t.Errorf("Open: %v", err)
  222. }
  223. for name, sigma := range map[string]float64{
  224. "out_sharpen_0.5.png": 0.5,
  225. "out_sharpen_1.5.png": 1.5,
  226. } {
  227. got := Sharpen(src, sigma)
  228. want, err := Open("testdata/" + name)
  229. if err != nil {
  230. t.Errorf("Open: %v", err)
  231. }
  232. if !compareNRGBA(got, toNRGBA(want), 0) {
  233. t.Errorf("resulting image differs from golden: %s", name)
  234. }
  235. }
  236. }
  237. func BenchmarkSharpen(b *testing.B) {
  238. b.StopTimer()
  239. img, err := Open("testdata/lena_512.png")
  240. if err != nil {
  241. b.Fatalf("Open: %v", err)
  242. }
  243. b.StartTimer()
  244. for i := 0; i < b.N; i++ {
  245. Sharpen(img, 3)
  246. }
  247. }