convolve_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2011 The Graphics-Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package convolve
  5. import (
  6. "code.google.com/p/graphics-go/graphics/graphicstest"
  7. "image"
  8. "reflect"
  9. "testing"
  10. _ "image/png"
  11. )
  12. func TestSeparableWeights(t *testing.T) {
  13. sobelXFull := []float64{
  14. -1, 0, 1,
  15. -2, 0, 2,
  16. -1, 0, 1,
  17. }
  18. sobelXSep := &SeparableKernel{
  19. X: []float64{-1, 0, +1},
  20. Y: []float64{1, 2, 1},
  21. }
  22. w := sobelXSep.Weights()
  23. if !reflect.DeepEqual(w, sobelXFull) {
  24. t.Errorf("got %v want %v", w, sobelXFull)
  25. }
  26. }
  27. func TestConvolve(t *testing.T) {
  28. kernFull, err := NewKernel([]float64{
  29. 0, 0, 0,
  30. 1, 1, 1,
  31. 0, 0, 0,
  32. })
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. kernSep := &SeparableKernel{
  37. X: []float64{1, 1, 1},
  38. Y: []float64{0, 1, 0},
  39. }
  40. src, err := graphicstest.LoadImage("../../testdata/gopher.png")
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. b := src.Bounds()
  45. sep := image.NewRGBA(b)
  46. if err = Convolve(sep, src, kernSep); err != nil {
  47. t.Fatal(err)
  48. }
  49. full := image.NewRGBA(b)
  50. Convolve(full, src, kernFull)
  51. err = graphicstest.ImageWithinTolerance(sep, full, 0x101)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. }
  56. func TestConvolveNil(t *testing.T) {
  57. if err := Convolve(nil, nil, nil); err != nil {
  58. t.Fatal(err)
  59. }
  60. }
  61. func TestConvolveEmpty(t *testing.T) {
  62. empty := image.NewRGBA(image.Rect(0, 0, 0, 0))
  63. if err := Convolve(empty, empty, nil); err != nil {
  64. t.Fatal(err)
  65. }
  66. }