effects_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. for name, sigma := range map[string]float64{
  90. "out_blur_0.5.png": 0.5,
  91. "out_blur_1.5.png": 1.5,
  92. } {
  93. got := Blur(testdataFlowersSmallPNG, sigma)
  94. want, err := Open("testdata/" + name)
  95. if err != nil {
  96. t.Errorf("Open: %v", err)
  97. }
  98. if !compareNRGBA(got, toNRGBA(want), 0) {
  99. t.Errorf("resulting image differs from golden: %s", name)
  100. }
  101. }
  102. }
  103. func BenchmarkBlur(b *testing.B) {
  104. b.ReportAllocs()
  105. for i := 0; i < b.N; i++ {
  106. Blur(testdataBranchesJPG, 3)
  107. }
  108. }
  109. func TestSharpen(t *testing.T) {
  110. td := []struct {
  111. desc string
  112. src image.Image
  113. sigma float64
  114. want *image.NRGBA
  115. }{
  116. {
  117. "Sharpen 3x3 0",
  118. &image.NRGBA{
  119. Rect: image.Rect(-1, -1, 2, 2),
  120. Stride: 3 * 4,
  121. Pix: []uint8{
  122. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  123. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  124. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  125. },
  126. },
  127. 0,
  128. &image.NRGBA{
  129. Rect: image.Rect(0, 0, 3, 3),
  130. Stride: 3 * 4,
  131. Pix: []uint8{
  132. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  133. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  134. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  135. },
  136. },
  137. },
  138. {
  139. "Sharpen 3x3 0.5",
  140. &image.NRGBA{
  141. Rect: image.Rect(-1, -1, 2, 2),
  142. Stride: 3 * 4,
  143. Pix: []uint8{
  144. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  145. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  146. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  147. },
  148. },
  149. 0.5,
  150. &image.NRGBA{
  151. Rect: image.Rect(0, 0, 3, 3),
  152. Stride: 3 * 4,
  153. Pix: []uint8{
  154. 0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66,
  155. 0x64, 0x64, 0x64, 0x64, 0x7d, 0x7d, 0x7d, 0x7e, 0x64, 0x64, 0x64, 0x64,
  156. 0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66,
  157. },
  158. },
  159. },
  160. {
  161. "Sharpen 3x3 100",
  162. &image.NRGBA{
  163. Rect: image.Rect(-1, -1, 2, 2),
  164. Stride: 3 * 4,
  165. Pix: []uint8{
  166. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  167. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  168. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  169. },
  170. },
  171. 100,
  172. &image.NRGBA{
  173. Rect: image.Rect(0, 0, 3, 3),
  174. Stride: 3 * 4,
  175. Pix: []uint8{
  176. 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
  177. 0x64, 0x64, 0x64, 0x64, 0x86, 0x86, 0x86, 0x86, 0x64, 0x64, 0x64, 0x64,
  178. 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
  179. },
  180. },
  181. },
  182. {
  183. "Sharpen 3x1 10",
  184. &image.NRGBA{
  185. Rect: image.Rect(-1, -1, 2, 0),
  186. Stride: 3 * 4,
  187. Pix: []uint8{
  188. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  189. },
  190. },
  191. 10,
  192. &image.NRGBA{
  193. Rect: image.Rect(0, 0, 3, 1),
  194. Stride: 3 * 4,
  195. Pix: []uint8{
  196. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  197. },
  198. },
  199. },
  200. }
  201. for _, d := range td {
  202. got := Sharpen(d.src, d.sigma)
  203. want := d.want
  204. if !compareNRGBA(got, want, 0) {
  205. t.Errorf("test [%s] failed: %#v", d.desc, got)
  206. }
  207. }
  208. }
  209. func TestSharpenGolden(t *testing.T) {
  210. for name, sigma := range map[string]float64{
  211. "out_sharpen_0.5.png": 0.5,
  212. "out_sharpen_1.5.png": 1.5,
  213. } {
  214. got := Sharpen(testdataFlowersSmallPNG, sigma)
  215. want, err := Open("testdata/" + name)
  216. if err != nil {
  217. t.Errorf("Open: %v", err)
  218. }
  219. if !compareNRGBA(got, toNRGBA(want), 0) {
  220. t.Errorf("resulting image differs from golden: %s", name)
  221. }
  222. }
  223. }
  224. func BenchmarkSharpen(b *testing.B) {
  225. b.ReportAllocs()
  226. for i := 0; i < b.N; i++ {
  227. Sharpen(testdataBranchesJPG, 3)
  228. }
  229. }