github.com/aloncn/graphics-go@v0.0.1/graphics/convolve/convolve_test.go (about)

     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  
     5  package convolve
     6  
     7  import (
     8  	"code.google.com/p/graphics-go/graphics/graphicstest"
     9  	"image"
    10  	"reflect"
    11  	"testing"
    12  
    13  	_ "image/png"
    14  )
    15  
    16  func TestSeparableWeights(t *testing.T) {
    17  	sobelXFull := []float64{
    18  		-1, 0, 1,
    19  		-2, 0, 2,
    20  		-1, 0, 1,
    21  	}
    22  	sobelXSep := &SeparableKernel{
    23  		X: []float64{-1, 0, +1},
    24  		Y: []float64{1, 2, 1},
    25  	}
    26  	w := sobelXSep.Weights()
    27  	if !reflect.DeepEqual(w, sobelXFull) {
    28  		t.Errorf("got %v want %v", w, sobelXFull)
    29  	}
    30  }
    31  
    32  func TestConvolve(t *testing.T) {
    33  	kernFull, err := NewKernel([]float64{
    34  		0, 0, 0,
    35  		1, 1, 1,
    36  		0, 0, 0,
    37  	})
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	kernSep := &SeparableKernel{
    43  		X: []float64{1, 1, 1},
    44  		Y: []float64{0, 1, 0},
    45  	}
    46  
    47  	src, err := graphicstest.LoadImage("../../testdata/gopher.png")
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	b := src.Bounds()
    52  
    53  	sep := image.NewRGBA(b)
    54  	if err = Convolve(sep, src, kernSep); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	full := image.NewRGBA(b)
    59  	Convolve(full, src, kernFull)
    60  
    61  	err = graphicstest.ImageWithinTolerance(sep, full, 0x101)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  }
    66  
    67  func TestConvolveNil(t *testing.T) {
    68  	if err := Convolve(nil, nil, nil); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  }
    72  
    73  func TestConvolveEmpty(t *testing.T) {
    74  	empty := image.NewRGBA(image.Rect(0, 0, 0, 0))
    75  	if err := Convolve(empty, empty, nil); err != nil {
    76  		t.Fatal(err)
    77  	}
    78  }