-
Notifications
You must be signed in to change notification settings - Fork 0
/
heif_test.go
96 lines (90 loc) · 2.24 KB
/
heif_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package heif
import (
"bytes"
_ "embed"
"encoding/base64"
"image/png"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)
func TestVersion(t *testing.T) {
ver := Version()
if v, exp := cleanString(ver), cleanString(string(versionTxt)); v != exp {
t.Fatalf("expected %s, got: %s", exp, v)
}
t.Logf("libheif: %s", ver)
}
func TestDecoder(t *testing.T) {
t.Skip()
var files []string
err := filepath.Walk("testdata", func(name string, info fs.FileInfo, err error) error {
switch {
case err != nil:
return err
case info.IsDir() || strings.ToLower(filepath.Ext(name)) != ".heic":
return nil
}
files = append(files, name)
return nil
})
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
for _, nn := range files {
name := nn
t.Run(strings.TrimSuffix(filepath.Base(name), ".heic"), func(t *testing.T) {
testDecoder(t, name)
})
}
}
func testDecoder(t *testing.T, name string) {
t.Helper()
f, err := os.OpenFile(name, os.O_RDONLY, 0)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
defer f.Close()
img, err := Decode(f)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
size := img.Bounds().Size()
t.Logf("size: %d / %d", size.X, size.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
b := buf.Bytes()
out := name + ".png"
t.Logf("writing to: %s", out)
if err := os.WriteFile(out, b, 0o644); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
orig := name + ".orig.png"
exp, err := os.ReadFile(orig)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
switch equal := bytes.Equal(b, exp); {
case equal:
t.Logf("%s and %s match!", orig, out)
case os.Getenv("CI") != "":
expEncoded := base64.StdEncoding.EncodeToString(exp)
bEncoded := base64.StdEncoding.EncodeToString(b)
t.Logf("WARNING: expected %s and %s to be equal!", orig, out)
t.Logf("%s (expected):\n%s", orig, expEncoded)
t.Logf("%s:\n%s", out, bEncoded)
default:
t.Errorf("expected %s and %s to be equal!", orig, out)
}
}
func cleanString(s string) string {
return strings.TrimPrefix(strings.TrimSpace(s), "v")
}
// versionTxt is the embedded libheif version.
//
//go:embed version.txt
var versionTxt []byte