mirror of
https://github.com/esimov/caire.git
synced 2025-10-05 16:47:15 +08:00
Unit testing
This commit is contained in:
32
grayscale_test.go
Normal file
32
grayscale_test.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package caire
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const ImgWidth = 10
|
||||||
|
const ImgHeight = 10
|
||||||
|
|
||||||
|
func TestGrayscale(t *testing.T) {
|
||||||
|
img := image.NewRGBA(image.Rect(0, 0, ImgWidth, ImgHeight))
|
||||||
|
for i := 0; i < img.Bounds().Dx(); i++ {
|
||||||
|
for j := 0; j < img.Bounds().Dy(); j++ {
|
||||||
|
img.Set(i, j, color.RGBA{177, 177, 177, 255})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < img.Bounds().Dx(); i++ {
|
||||||
|
for j := 0; j < img.Bounds().Dy(); j++ {
|
||||||
|
r, g, b, _ := img.At(i, j).RGBA()
|
||||||
|
r = r >> 8
|
||||||
|
g = g >> 8
|
||||||
|
b = b >> 8
|
||||||
|
|
||||||
|
if r != g || r != b || g != b {
|
||||||
|
t.Errorf("R, G, B value expected to be equal. Got %v, %v, %v", r, g, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
71
process_test.go
Normal file
71
process_test.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package caire
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProcessor_Resize(t *testing.T) {
|
||||||
|
reduceImageH(t)
|
||||||
|
reduceImageV(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reduceImageH(t *testing.T) {
|
||||||
|
img := image.NewNRGBA(image.Rect(0, 0, ImgWidth, ImgHeight))
|
||||||
|
var c = NewCarver(img.Bounds().Dx(), img.Bounds().Dy())
|
||||||
|
newWidth := ImgWidth / 2
|
||||||
|
p := &Processor{
|
||||||
|
BlurRadius: 2,
|
||||||
|
SobelThreshold: 10,
|
||||||
|
NewWidth: newWidth,
|
||||||
|
NewHeight: ImgHeight,
|
||||||
|
Percentage: false,
|
||||||
|
Square: false,
|
||||||
|
Debug: false,
|
||||||
|
Scale: false,
|
||||||
|
}
|
||||||
|
// Reduce image size horizontally
|
||||||
|
for x := 0; x < newWidth; x++ {
|
||||||
|
width, height := img.Bounds().Max.X, img.Bounds().Max.Y
|
||||||
|
c = NewCarver(width, height)
|
||||||
|
c.ComputeSeams(img, p)
|
||||||
|
seams := c.FindLowestEnergySeams()
|
||||||
|
img = c.RemoveSeam(img, seams, p.Debug)
|
||||||
|
}
|
||||||
|
imgWidth := img.Bounds().Max.X
|
||||||
|
|
||||||
|
if imgWidth != newWidth {
|
||||||
|
t.Errorf("Resulted image width expected to be %v. Got %v", newWidth, imgWidth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func reduceImageV(t *testing.T) {
|
||||||
|
img := image.NewNRGBA(image.Rect(0, 0, ImgWidth, ImgHeight))
|
||||||
|
var c = NewCarver(img.Bounds().Dx(), img.Bounds().Dy())
|
||||||
|
newHeight := ImgHeight / 2
|
||||||
|
p := &Processor{
|
||||||
|
BlurRadius: 2,
|
||||||
|
SobelThreshold: 10,
|
||||||
|
NewWidth: ImgWidth,
|
||||||
|
NewHeight: newHeight,
|
||||||
|
Percentage: false,
|
||||||
|
Square: false,
|
||||||
|
Debug: false,
|
||||||
|
Scale: false,
|
||||||
|
}
|
||||||
|
// Reduce image size horizontally
|
||||||
|
img = c.RotateImage90(img)
|
||||||
|
for x := 0; x < newHeight; x++ {
|
||||||
|
width, height := img.Bounds().Max.X, img.Bounds().Max.Y
|
||||||
|
c = NewCarver(width, height)
|
||||||
|
c.ComputeSeams(img, p)
|
||||||
|
seams := c.FindLowestEnergySeams()
|
||||||
|
img = c.RemoveSeam(img, seams, p.Debug)
|
||||||
|
}
|
||||||
|
img = c.RotateImage270(img)
|
||||||
|
imgHeight := img.Bounds().Max.Y
|
||||||
|
|
||||||
|
if imgHeight != newHeight {
|
||||||
|
t.Errorf("Resulted image height expected to be %v. Got %v", newHeight, imgHeight)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user