Skip to content

Commit

Permalink
Add first test for encoder code.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Aug 27, 2024
1 parent 7cf4ee9 commit 2e8cc8c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/output/
81 changes: 81 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Go interface to libheif
*
* Copyright (c) 2018-2024 struktur AG, Joachim Bauch <[email protected]>
*
* libheif is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libheif is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libheif. If not, see <http://www.gnu.org/licenses/>.
*/
package libheif

import (
"bytes"
"image"
_ "image/jpeg"
_ "image/png"
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func loadImage(t *testing.T, filename string) image.Image {
t.Helper()
require := require.New(t)
data, err := os.ReadFile(filename)
require.NoError(err)

img, _, err := image.Decode(bytes.NewBuffer(data))
require.NoError(err)
return img
}

func TestEncoder(t *testing.T) {
codecs := map[CompressionFormat]string{
CompressionHEVC: "heif",
CompressionAV1: "avif",
}

outdir := "testdata/output"
err := os.MkdirAll(outdir, 0755)
require.NotErrorIs(t, err, os.ErrExist)

for codec, ext := range codecs {
codec := codec
ext := ext
t.Run(ext, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
require.True(HaveEncoderForFormat(codec))

img := loadImage(t, "testdata/example-1.jpg")
ctx, err := EncodeFromImage(img, codec, 75, LosslessModeDisabled, LoggingLevelFull)
require.NoError(err)

output := path.Join(outdir, "example-1."+ext)
assert.NoError(ctx.WriteToFile(output))

var out bytes.Buffer
assert.NoError(ctx.Write(&out))

if assert.FileExists(output) {
if data, err := os.ReadFile(output); assert.NoError(err) {
assert.Equal(len(data), out.Len())
}
}
})
}

}
Binary file added testdata/example-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/example-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2e8cc8c

Please sign in to comment.