effects_test.go 6.3 KB

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