Implemented improved image rendering and added test

This commit is contained in:
Samuel Hawksby-Robinson 2020-10-08 16:30:18 +01:00 committed by Andrea Maria Piana
parent b7cd88744c
commit 8ebfe9b1c9
4 changed files with 116 additions and 19 deletions

View File

@ -105,14 +105,14 @@ func isWebp(buf []byte) bool {
buf[10] == 0x42 && buf[11] == 0x50 buf[10] == 0x42 && buf[11] == 0x50
} }
func Render(img image.Image, imgDetail *Details) error { func MakeAndRenderFile(img image.Image, imgDetail *Details) error {
out, err := os.Create(imgDetail.FileName) out, err := os.Create(imgDetail.FileName)
if err != nil { if err != nil {
return err return err
} }
defer out.Close() defer out.Close()
err = renderJpeg(out, img, imgDetail) err = Render(out, img, imgDetail)
if err != nil { if err != nil {
return err return err
} }
@ -123,6 +123,11 @@ func Render(img image.Image, imgDetail *Details) error {
return nil return nil
} }
func Render(w io.Writer, img image.Image, imgDetail *Details) error {
// Currently a wrapper for renderJpeg, but this function is useful if multiple render formats are needed
return renderJpeg(w, img, imgDetail)
}
func renderJpeg(w io.Writer, m image.Image, imgDetail *Details) error { func renderJpeg(w io.Writer, m image.Image, imgDetail *Details) error {
o := new(jpeg.Options) o := new(jpeg.Options)
o.Quality = imgDetail.Quality o.Quality = imgDetail.Quality

View File

@ -1,21 +1,28 @@
package images package images
import ( import (
"github.com/stretchr/testify/require" "bytes"
"image" "image"
"os"
"testing" "testing"
"github.com/stretchr/testify/require"
)
const (
path = "../_assets/tests/"
) )
func TestGet(t *testing.T) { func TestGet(t *testing.T) {
cs := []struct{ cs := []struct {
Filepath string Filepath string
Error bool Error bool
Nil bool Nil bool
Bounds image.Rectangle Bounds image.Rectangle
}{ }{
{ {
"../_assets/tests/elephant.jpg", "elephant.jpg",
false, false,
false, false,
image.Rectangle{ image.Rectangle{
@ -24,7 +31,7 @@ func TestGet(t *testing.T) {
}, },
}, },
{ {
"../_assets/tests/status.png", "status.png",
false, false,
false, false,
image.Rectangle{ image.Rectangle{
@ -33,7 +40,7 @@ func TestGet(t *testing.T) {
}, },
}, },
{ {
"../_assets/tests/spin.gif", "spin.gif",
false, false,
false, false,
image.Rectangle{ image.Rectangle{
@ -42,7 +49,7 @@ func TestGet(t *testing.T) {
}, },
}, },
{ {
"../_assets/tests/rose.webp", "rose.webp",
false, false,
false, false,
image.Rectangle{ image.Rectangle{
@ -51,29 +58,114 @@ func TestGet(t *testing.T) {
}, },
}, },
{ {
"../_assets/tests/test.aac", "test.aac",
true, true,
true, true,
image.Rectangle{}, image.Rectangle{},
}, },
} }
for _, test := range cs { for _, c := range cs {
img, err := Get(test.Filepath) img, err := Get(path + c.Filepath)
if test.Error { if c.Error {
require.Error(t, err) require.Error(t, err)
} else { } else {
require.NoError(t, err) require.NoError(t, err)
} }
if test.Nil { if c.Nil {
require.Nil(t, img) require.Nil(t, img)
continue continue
} else { } else {
require.NotNil(t, img) require.NotNil(t, img)
} }
require.Exactly(t, test.Bounds, img.Bounds()) require.Exactly(t, c.Bounds, img.Bounds())
}
}
func TestRender(t *testing.T) {
cs := []struct {
FileName string
RenderSize int
}{
{
"elephant.jpg",
1447,
},
{
"rose.webp",
11119,
},
{
"spin.gif",
2263,
},
{
"status.png",
5834,
},
}
options := Details{
Quality: 70,
}
for _, c := range cs {
img, err := Get(path + c.FileName)
require.NoError(t, err)
bb := bytes.NewBuffer([]byte{})
err = Render(bb, img, &options)
require.NoError(t, err)
require.Exactly(t, c.RenderSize, bb.Len())
}
}
func TestMakeAndRenderFile(t *testing.T) {
cs := []struct {
FileName string
OutName string
OutputSize int64
}{
{
"elephant.jpg",
"_elephant.jpg",
1447,
},
{
"rose.webp",
"_rose.jpg",
11119,
},
{
"spin.gif",
"_spin.jpg",
2263,
},
{
"status.png",
"_status.jpg",
5834,
},
}
for _, c := range cs {
img, err := Get(path + c.FileName)
require.NoError(t, err)
options := &Details{
FileName: path + c.OutName,
Quality: 70,
}
err = MakeAndRenderFile(img, options)
require.NoError(t, err)
require.Exactly(t, c.OutputSize, options.SizeFile)
// tidy up
err = os.Remove(options.FileName)
require.NoError(t, err)
} }
} }

View File

@ -1 +1 @@
package images package images

View File

@ -25,7 +25,7 @@ type Details struct {
type FileType uint type FileType uint
func MakeDetails(imageName string, size uint, quality int, properties string) Details { func NewDetails(imageName string, size uint, quality int, properties string) Details {
return Details{ return Details{
SizePixel: size, SizePixel: size,
Quality: quality, Quality: quality,