Split out encode code from decode, same for tests

This commit is contained in:
Samuel Hawksby-Robinson 2020-10-09 12:08:55 +01:00 committed by Andrea Maria Piana
parent e635869dd5
commit 36105aaac0
2 changed files with 0 additions and 117 deletions

View File

@ -104,33 +104,3 @@ func isWebp(buf []byte) bool {
buf[8] == 0x57 && buf[9] == 0x45 &&
buf[10] == 0x42 && buf[11] == 0x50
}
func RenderAndMakeFile(img image.Image, imgDetail *Details) error {
out, err := os.Create(imgDetail.FileName)
if err != nil {
return err
}
defer out.Close()
err = Render(out, img, imgDetail)
if err != nil {
return err
}
fi, _ := out.Stat()
imgDetail.SizeFile = fi.Size()
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
return jpeg.Encode(w, m, o)
}

View File

@ -1,9 +1,7 @@
package images
import (
"bytes"
"image"
"os"
"testing"
"github.com/stretchr/testify/require"
@ -84,88 +82,3 @@ func TestGet(t *testing.T) {
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 = RenderAndMakeFile(img, options)
require.NoError(t, err)
require.Exactly(t, c.OutputSize, options.SizeFile)
// tidy up
err = os.Remove(options.FileName)
require.NoError(t, err)
}
}