Skip to content

Commit 2e8cc8c

Browse files
committed
Add first test for encoder code.
1 parent 7cf4ee9 commit 2e8cc8c

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testdata/output/

encoder_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Go interface to libheif
3+
*
4+
* Copyright (c) 2018-2024 struktur AG, Joachim Bauch <bauch@struktur.de>
5+
*
6+
* libheif is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as
8+
* published by the Free Software Foundation, either version 3 of
9+
* the License, or (at your option) any later version.
10+
*
11+
* libheif is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with libheif. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package libheif
20+
21+
import (
22+
"bytes"
23+
"image"
24+
_ "image/jpeg"
25+
_ "image/png"
26+
"os"
27+
"path"
28+
"testing"
29+
30+
"github.com/stretchr/testify/assert"
31+
"github.com/stretchr/testify/require"
32+
)
33+
34+
func loadImage(t *testing.T, filename string) image.Image {
35+
t.Helper()
36+
require := require.New(t)
37+
data, err := os.ReadFile(filename)
38+
require.NoError(err)
39+
40+
img, _, err := image.Decode(bytes.NewBuffer(data))
41+
require.NoError(err)
42+
return img
43+
}
44+
45+
func TestEncoder(t *testing.T) {
46+
codecs := map[CompressionFormat]string{
47+
CompressionHEVC: "heif",
48+
CompressionAV1: "avif",
49+
}
50+
51+
outdir := "testdata/output"
52+
err := os.MkdirAll(outdir, 0755)
53+
require.NotErrorIs(t, err, os.ErrExist)
54+
55+
for codec, ext := range codecs {
56+
codec := codec
57+
ext := ext
58+
t.Run(ext, func(t *testing.T) {
59+
assert := assert.New(t)
60+
require := require.New(t)
61+
require.True(HaveEncoderForFormat(codec))
62+
63+
img := loadImage(t, "testdata/example-1.jpg")
64+
ctx, err := EncodeFromImage(img, codec, 75, LosslessModeDisabled, LoggingLevelFull)
65+
require.NoError(err)
66+
67+
output := path.Join(outdir, "example-1."+ext)
68+
assert.NoError(ctx.WriteToFile(output))
69+
70+
var out bytes.Buffer
71+
assert.NoError(ctx.Write(&out))
72+
73+
if assert.FileExists(output) {
74+
if data, err := os.ReadFile(output); assert.NoError(err) {
75+
assert.Equal(len(data), out.Len())
76+
}
77+
}
78+
})
79+
}
80+
81+
}

testdata/example-1.jpg

424 KB
Loading

testdata/example-2.jpg

452 KB
Loading

0 commit comments

Comments
 (0)