1
0

effects_test.go 6.1 KB

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