-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqoi_test.go
88 lines (82 loc) · 1.95 KB
/
qoi_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
package qoi_test
import (
"bytes"
"image/color"
"image/png"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/takeyourhatoff/qoi"
)
func TestDecode(t *testing.T) {
tt := require.New(t)
files, err := filepath.Glob("testdata/qoi_test_images/*.qoi")
tt.NoError(err)
for _, p := range files {
f, err := os.Open(p)
tt.NoError(err)
defer f.Close()
img, err := qoi.Decode(f)
tt.NoError(err, p)
w, err := os.Open(strings.TrimSuffix(p, filepath.Ext(p)) + ".png")
tt.NoError(err)
defer w.Close()
ref, err := png.Decode(w)
tt.NoError(err)
tt.Equal(ref.Bounds(), img.Bounds())
for x := ref.Bounds().Min.X; x < ref.Bounds().Dx(); x++ {
for y := ref.Bounds().Min.X; y < ref.Bounds().Dy(); y++ {
tt.Equal(
color.NRGBAModel.Convert(ref.At(x, y)),
img.At(x, y),
"%q {x: %d, y: %d}", p, x, y,
)
}
}
}
}
func TestEncode(t *testing.T) {
tt := require.New(t)
files, err := filepath.Glob("testdata/qoi_test_images/*.png")
tt.NoError(err)
for _, p := range files {
f, err := os.Open(p)
tt.NoError(err)
defer f.Close()
img, err := png.Decode(f)
tt.NoError(err, p)
var buf bytes.Buffer
err = qoi.Encode(&buf, img)
tt.NoError(err)
ref, err := os.ReadFile(strings.TrimSuffix(p, filepath.Ext(p)) + ".qoi")
buf.Bytes()[12], ref[12] = 0, 0 // ignore channels field in header
tt.NoError(err)
tt.Equal(ref, buf.Bytes())
}
}
func BenchmarkDecode(b *testing.B) {
tt := require.New(b)
buf, err := os.ReadFile("testdata/qoi_test_images/dice.qoi")
tt.NoError(err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := qoi.Decode(bytes.NewReader(buf))
tt.NoError(err)
}
}
func BenchmarkEncode(b *testing.B) {
tt := require.New(b)
f, err := os.Open("testdata/qoi_test_images/dice.png")
tt.NoError(err)
defer f.Close()
img, err := png.Decode(f)
tt.NoError(err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = qoi.Encode(ioutil.Discard, img)
tt.NoError(err)
}
}