effects_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.5",
  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.5,
  25. &image.NRGBA{
  26. Rect: image.Rect(0, 0, 3, 3),
  27. Stride: 3 * 4,
  28. Pix: []uint8{
  29. 0x01, 0x02, 0x04, 0x04, 0x0a, 0x10, 0x18, 0x18, 0x01, 0x02, 0x04, 0x04,
  30. 0x09, 0x10, 0x18, 0x18, 0x3f, 0x69, 0x9e, 0x9e, 0x09, 0x10, 0x18, 0x18,
  31. 0x01, 0x02, 0x04, 0x04, 0x0a, 0x10, 0x18, 0x18, 0x01, 0x02, 0x04, 0x04,
  32. },
  33. },
  34. },
  35. {
  36. "Blur 3x3 10",
  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. 10,
  47. &image.NRGBA{
  48. Rect: image.Rect(0, 0, 3, 3),
  49. Stride: 3 * 4,
  50. Pix: []uint8{
  51. 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c,
  52. 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c,
  53. 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c,
  54. },
  55. },
  56. },
  57. }
  58. for _, d := range td {
  59. got := Blur(d.src, d.sigma)
  60. want := d.want
  61. if !compareNRGBA(got, want, 0) {
  62. t.Errorf("test [%s] failed: %#v", d.desc, got)
  63. }
  64. }
  65. }
  66. func TestSharpen(t *testing.T) {
  67. td := []struct {
  68. desc string
  69. src image.Image
  70. sigma float64
  71. want *image.NRGBA
  72. }{
  73. {
  74. "Sharpen 3x3 0.5",
  75. &image.NRGBA{
  76. Rect: image.Rect(-1, -1, 2, 2),
  77. Stride: 3 * 4,
  78. Pix: []uint8{
  79. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  80. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  81. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  82. },
  83. },
  84. 0.5,
  85. &image.NRGBA{
  86. Rect: image.Rect(0, 0, 3, 3),
  87. Stride: 3 * 4,
  88. Pix: []uint8{
  89. 0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66,
  90. 0x64, 0x64, 0x64, 0x64, 0x7e, 0x7e, 0x7e, 0x7e, 0x64, 0x64, 0x64, 0x64,
  91. 0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66},
  92. },
  93. },
  94. {
  95. "Sharpen 3x3 10",
  96. &image.NRGBA{
  97. Rect: image.Rect(-1, -1, 2, 2),
  98. Stride: 3 * 4,
  99. Pix: []uint8{
  100. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  101. 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
  102. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66},
  103. },
  104. 100,
  105. &image.NRGBA{
  106. Rect: image.Rect(0, 0, 3, 3),
  107. Stride: 3 * 4,
  108. Pix: []uint8{
  109. 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
  110. 0x64, 0x64, 0x64, 0x64, 0x86, 0x86, 0x86, 0x86, 0x64, 0x64, 0x64, 0x64,
  111. 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
  112. },
  113. },
  114. },
  115. }
  116. for _, d := range td {
  117. got := Sharpen(d.src, d.sigma)
  118. want := d.want
  119. if !compareNRGBA(got, want, 0) {
  120. t.Errorf("test [%s] failed: %#v", d.desc, got)
  121. }
  122. }
  123. }