Improved images.Crop() and implemented testing

This commit is contained in:
Samuel Hawksby-Robinson 2020-10-08 21:24:41 +01:00 committed by Andrea Maria Piana
parent 8ebfe9b1c9
commit bb1e232ffe
2 changed files with 115 additions and 6 deletions

View File

@ -1,20 +1,30 @@
package images
import (
"fmt"
"image"
"github.com/nfnt/resize"
"github.com/oliamb/cutter"
)
func ResizeSquare(size uint, img image.Image) image.Image {
return resize.Resize(size, 0, img, resize.Bilinear)
func Resize(width, height uint, img image.Image) image.Image {
return resize.Resize(width, height, img, resize.Bilinear)
}
func CropSquare(img image.Image, size int, anchor image.Point) (image.Image, error) {
func Crop(img image.Image, rect image.Rectangle) (image.Image, error) {
if img.Bounds().Max.X < rect.Max.X || img.Bounds().Max.Y < rect.Max.Y{
return nil, fmt.Errorf(
"crop dimensions out of bounds of image, image width '%dpx' & height '%dpx'; crop bottom right coordinate at X '%dpx' Y '%dpx'",
img.Bounds().Max.X, img.Bounds().Max.Y,
rect.Max.X, rect.Max.Y,
)
}
return cutter.Crop(img, cutter.Config{
Width: size,
Height: size,
Anchor: anchor,
Width: rect.Dx(),
Height: rect.Dy(),
Anchor: rect.Min,
})
}

View File

@ -0,0 +1,99 @@
package images
import (
"bytes"
"errors"
"github.com/stretchr/testify/require"
"image"
"testing"
)
func TestResize(t *testing.T) {
}
func TestCrop(t *testing.T) {
type params struct{
Rectangle image.Rectangle
OutputBound image.Rectangle
OutputSize int
CropError error
}
topLeftSquare := image.Rectangle{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 80, Y: 80},
}
offsetSquare := image.Rectangle{
Min: image.Point{X: 80, Y: 80},
Max: image.Point{X: 160, Y: 160},
}
outOfBoundsSquare := image.Rectangle{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 1000000, Y: 1000000},
}
rect := image.Rectangle{}
options := &Details{
Quality: 70,
}
cs := []struct{
FileName string
Params []params
}{
{
"elephant.jpg",
[]params{
{topLeftSquare, topLeftSquare, 1447, nil},
{offsetSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '80px' & height '80px'; crop bottom right coordinate at X '160px' Y '160px'")},
{outOfBoundsSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '80px' & height '80px'; crop bottom right coordinate at X '1000000px' Y '1000000px'")},
},
},
{
"rose.webp",
[]params{
{topLeftSquare, topLeftSquare, 1183, nil},
{offsetSquare, offsetSquare, 1251, nil},
{outOfBoundsSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '400px' & height '301px'; crop bottom right coordinate at X '1000000px' Y '1000000px'")},
},
},
{
"spin.gif",
[]params{
{topLeftSquare, topLeftSquare, 693, nil},
{offsetSquare, offsetSquare, 1339, nil},
{outOfBoundsSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '256px' & height '256px'; crop bottom right coordinate at X '1000000px' Y '1000000px'")},
},
},
{
"status.png",
[]params{
{topLeftSquare, topLeftSquare, 1027, nil},
{offsetSquare, offsetSquare, 1157, nil},
{outOfBoundsSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '256px' & height '256px'; crop bottom right coordinate at X '1000000px' Y '1000000px'")},
},
},
}
for _, c := range cs {
img, err := Get(path + c.FileName)
require.NoError(t, err)
for _, p := range c.Params {
cImg, err := Crop(img, p.Rectangle)
if p.CropError != nil {
require.EqualError(t, err, p.CropError.Error())
continue
} else {
require.NoError(t, err)
}
require.Exactly(t, p.OutputBound.Dx(), cImg.Bounds().Dx(), c.FileName)
require.Exactly(t, p.OutputBound.Dy(), cImg.Bounds().Dy(), c.FileName)
bb := bytes.NewBuffer([]byte{})
err = Render(bb, cImg, options)
require.NoError(t, err)
require.Exactly(t, p.OutputSize, bb.Len())
}
}
}