Skip to content

Commit

Permalink
Apply fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynsimon committed May 10, 2020
1 parent c531101 commit 00f2a4c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 99 deletions.
20 changes: 10 additions & 10 deletions blur/blur.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ func Gaussian(src image.Image, radius float64) *image.RGBA {
return clone.AsRGBA(src)
}

// Create the 1-d gaussian kernel
// Create the 1-d gaussian kernel
length := int(math.Ceil(2*radius + 1))
k := convolution.NewKernel(length, 1)
for i, x := 0, -radius; i < length; i, x = i+1, x+1 {
k.Matrix[i] = math.Exp(-(x*x/4/radius))
}
normK := k.Normalized()
for i, x := 0, -radius; i < length; i, x = i+1, x+1 {
k.Matrix[i] = math.Exp(-(x * x / 4 / radius))
}
normK := k.Normalized()

// Perform separable convolution
options := convolution.Options{Bias: 0, Wrap: false, KeepAlpha: false}
result := convolution.Convolve(src, normK, &options)
result = convolution.Convolve(result, normK.Transposed(), &options)
// Perform separable convolution
options := convolution.Options{Bias: 0, Wrap: false, KeepAlpha: false}
result := convolution.Convolve(src, normK, &options)
result = convolution.Convolve(result, normK.Transposed(), &options)

return result
return result
}
6 changes: 3 additions & 3 deletions blur/blur_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ func TestGaussianBlur(t *testing.T) {
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Pix: []uint8{
0xb1, 0xb1, 0xb1, 0xff, 0xbc, 0xbc, 0xbc, 0xff, 0xcc, 0xcc, 0xcc, 0xff,
0x4d, 0x4d, 0x4d, 0xff, 0x68, 0x68, 0x68, 0xff, 0x8b, 0x8b, 0x8b, 0xff,
0x0, 0x0, 0x0, 0xff, 0x25, 0x25, 0x25, 0xff, 0x58, 0x58, 0x58, 0xff,
0xb1, 0xb1, 0xb1, 0xff, 0xbc, 0xbc, 0xbc, 0xff, 0xcc, 0xcc, 0xcc, 0xff,
0x4d, 0x4d, 0x4d, 0xff, 0x68, 0x68, 0x68, 0xff, 0x8b, 0x8b, 0x8b, 0xff,
0x0, 0x0, 0x0, 0xff, 0x25, 0x25, 0x25, 0xff, 0x58, 0x58, 0x58, 0xff,
},
},
},
Expand Down
20 changes: 10 additions & 10 deletions convolution/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Matrix interface {
Normalized() Matrix
MaxX() int
MaxY() int
Transposed() Matrix
Transposed() Matrix
}

// NewKernel returns a kernel of the provided length.
Expand Down Expand Up @@ -66,17 +66,17 @@ func (k *Kernel) At(x, y int) float64 {

// Transposed returns a new Kernel that has the columns as rows and vice versa
func (k *Kernel) Transposed() Matrix {
w := k.Width;
h := k.Height;
nk := NewKernel(h, w)
w := k.Width
h := k.Height
nk := NewKernel(h, w)

for x := 0; x<w; x++ {
for y := 0; y<h; y++ {
nk.Matrix[x*h + y] = k.Matrix[y*w + x];
}
}
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
nk.Matrix[x*h+y] = k.Matrix[y*w+x]
}
}

return nk
return nk
}

// String returns the string representation of the matrix.
Expand Down
152 changes: 76 additions & 76 deletions convolution/kernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,83 +262,83 @@ func TestKernelNormalized(t *testing.T) {
}

func TestKernelTransposed(t *testing.T) {
cases := []struct {
desc string
kernel *Kernel
expected *Kernel
}{
{
desc: "all zero",
kernel: &Kernel{[]float64{
0, 0, 0,
0, 0, 0,
0, 0, 0,
}, 3, 3},
expected: &Kernel{[]float64{
0, 0, 0,
0, 0, 0,
0, 0, 0,
}, 3, 3},
},
{
desc: "one element",
kernel: &Kernel{[]float64{
0, 0, 0,
0, 1, 0,
0, 0, 0,
}, 3, 3},
expected: &Kernel{[]float64{
0, 0, 0,
0, 1, 0,
0, 0, 0,
}, 3, 3},
},
{
desc: "diagonal",
kernel: &Kernel{[]float64{
1, 0, 0,
0, 1, 0,
0, 0, 1,
}, 3, 3},
expected: &Kernel{[]float64{
1, 0, 0,
0, 1, 0,
0, 0, 1,
}, 3, 3},
},
{
desc: "3x2",
kernel: &Kernel{[]float64{
1, 1, 1,
0, 1, 0,
}, 3, 2},
expected: &Kernel{[]float64{
1, 0,
1, 1,
1, 0,
}, 2, 3},
},
{
desc: "5x1",
kernel: &Kernel{[]float64{
1, 1, 1, 0, 1,
}, 5, 1},
expected: &Kernel{[]float64{
1,
1,
1,
0,
1,
}, 1, 5},
},
}
cases := []struct {
desc string
kernel *Kernel
expected *Kernel
}{
{
desc: "all zero",
kernel: &Kernel{[]float64{
0, 0, 0,
0, 0, 0,
0, 0, 0,
}, 3, 3},
expected: &Kernel{[]float64{
0, 0, 0,
0, 0, 0,
0, 0, 0,
}, 3, 3},
},
{
desc: "one element",
kernel: &Kernel{[]float64{
0, 0, 0,
0, 1, 0,
0, 0, 0,
}, 3, 3},
expected: &Kernel{[]float64{
0, 0, 0,
0, 1, 0,
0, 0, 0,
}, 3, 3},
},
{
desc: "diagonal",
kernel: &Kernel{[]float64{
1, 0, 0,
0, 1, 0,
0, 0, 1,
}, 3, 3},
expected: &Kernel{[]float64{
1, 0, 0,
0, 1, 0,
0, 0, 1,
}, 3, 3},
},
{
desc: "3x2",
kernel: &Kernel{[]float64{
1, 1, 1,
0, 1, 0,
}, 3, 2},
expected: &Kernel{[]float64{
1, 0,
1, 1,
1, 0,
}, 2, 3},
},
{
desc: "5x1",
kernel: &Kernel{[]float64{
1, 1, 1, 0, 1,
}, 5, 1},
expected: &Kernel{[]float64{
1,
1,
1,
0,
1,
}, 1, 5},
},
}

for i, c := range cases {
actual := c.kernel.Transposed()
if !kernelEqual(actual.(*Kernel), c.expected) {
t.Errorf("%s case #%d: expected: %#v, actual: %#v", "KernelTransposed "+c.desc, i, c.expected, actual)
}
}
for i, c := range cases {
actual := c.kernel.Transposed()
if !kernelEqual(actual.(*Kernel), c.expected) {
t.Errorf("%s case #%d: expected: %#v, actual: %#v", "KernelTransposed "+c.desc, i, c.expected, actual)
}
}
}

func TestKernelString(t *testing.T) {
Expand Down

0 comments on commit 00f2a4c

Please sign in to comment.