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
}
func Render(img image.Image, imgDetail *Details) error {
func MakeAndRenderFile(img image.Image, imgDetail *Details) error {
out, err := os.Create(imgDetail.FileName)
if err != nil {
return err
}
defer out.Close()
err = renderJpeg(out, img, imgDetail)
err = Render(out, img, imgDetail)
if err != nil {
return err
}
@ -123,6 +123,11 @@ func Render(img image.Image, imgDetail *Details) error {
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 {
o := new(jpeg.Options)
o.Quality = imgDetail.Quality

View File

@ -1,21 +1,28 @@
package images
import (
"github.com/stretchr/testify/require"
"bytes"
"image"
"os"
"testing"
"github.com/stretchr/testify/require"
)
const (
path = "../_assets/tests/"
)
func TestGet(t *testing.T) {
cs := []struct{
cs := []struct {
Filepath string
Error bool
Nil bool
Bounds image.Rectangle
}{
{
"../_assets/tests/elephant.jpg",
"elephant.jpg",
false,
false,
image.Rectangle{
@ -24,7 +31,7 @@ func TestGet(t *testing.T) {
},
},
{
"../_assets/tests/status.png",
"status.png",
false,
false,
image.Rectangle{
@ -33,7 +40,7 @@ func TestGet(t *testing.T) {
},
},
{
"../_assets/tests/spin.gif",
"spin.gif",
false,
false,
image.Rectangle{
@ -42,7 +49,7 @@ func TestGet(t *testing.T) {
},
},
{
"../_assets/tests/rose.webp",
"rose.webp",
false,
false,
image.Rectangle{
@ -51,29 +58,114 @@ func TestGet(t *testing.T) {
},
},
{
"../_assets/tests/test.aac",
"test.aac",
true,
true,
image.Rectangle{},
},
}
for _, test := range cs {
img, err := Get(test.Filepath)
for _, c := range cs {
img, err := Get(path + c.Filepath)
if test.Error {
if c.Error {
require.Error(t, err)
} else {
require.NoError(t, err)
}
if test.Nil {
if c.Nil {
require.Nil(t, img)
continue
} else {
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

@ -25,7 +25,7 @@ type Details struct {
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{
SizePixel: size,
Quality: quality,