-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqoi_test.go
More file actions
126 lines (107 loc) · 2.71 KB
/
Copy pathqoi_test.go
File metadata and controls
126 lines (107 loc) · 2.71 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package qoi
import (
"bufio"
"image/color"
"image/png"
"os"
"path/filepath"
"strings"
"testing"
)
type VirtualWriter struct {
d []byte
}
func (w *VirtualWriter) Write(p []byte) (n int, err error) {
w.d = append(w.d, p...)
return len(p), nil
}
func TestDecoder(t *testing.T) {
files, err := filepath.Glob("testdata/qoi_test_images/*.qoi")
if err != nil {
t.Fatalf("%v", err)
}
for _, path := range files {
t.Logf("Now decoding: %v\n", path)
f, err := os.Open(path)
if err != nil {
t.Fatalf("%v", err)
}
defer f.Close()
given, err := Decode(f)
if err != nil {
t.Fatalf("%v", err)
}
f_, err := os.Open(strings.TrimSuffix(path, filepath.Ext(path)) + ".png")
if err != nil {
t.Fatalf("%v", err)
}
defer f_.Close()
wanted, err := png.Decode(f_)
if err != nil {
t.Fatalf("%v", err)
}
if given.Bounds() != wanted.Bounds() {
t.Fatalf("Image bounds don't match up!\n")
}
for x := 0; x < given.Bounds().Max.X; x++ {
for y := 0; y < given.Bounds().Max.Y; y++ {
if color.NRGBAModel.Convert(wanted.At(x, y)) != given.At(x, y) {
t.Errorf("Here: (%v, %v). Value QOI: %v, Value PNG: %v\n", x, y, given.At(x, y), wanted.At(x, y))
}
}
}
}
}
func TestEncoder(t *testing.T) {
files, err := filepath.Glob("testdata/qoi_test_images/*.png")
if err != nil {
t.Fatalf("%v", err)
}
for _, path := range files {
t.Logf("Now encoding: %v\n", path)
f, err := os.Open(path)
if err != nil {
t.Fatalf("%v", err)
}
defer f.Close()
im_png, err := png.Decode(f)
if err != nil {
t.Fatalf("%v", err)
}
given := &VirtualWriter{make([]byte, 0)}
Encode(given, im_png)
f_, err := os.Open(strings.TrimSuffix(path, filepath.Ext(path)) + ".qoi")
if err != nil {
t.Fatalf("%v", err)
}
defer f_.Close()
wanted := bufio.NewReader(f_)
for k, val := range given.d[:12] {
wanted, err := wanted.ReadByte()
if err != nil {
t.Fatalf("%v", err)
}
if val != wanted {
t.Fatalf("Incorrect value in header at position %v. Got: %v. Wanted: %v.\n", k, val, wanted)
}
}
wanted.ReadByte()
wanted.ReadByte()
// Considering the official specification, and other implementations, the channel and colorspace values of the header don't need to match exactly.
if given.d[12] != 3 && given.d[12] != 4 {
t.Fatalf("Incorrect channel number in header. Got: %v. Wanted: 3 or 4.\n", given.d[12])
}
if given.d[13] > 1 {
t.Fatalf("Incorrect colorspace value in header. Got: %v. Wanted: 0 or 1.\n", given.d[13])
}
for k, val := range given.d[14:] {
ref, err := wanted.ReadByte()
if err != nil {
t.Fatalf("%v", err)
}
if val != ref {
t.Errorf("Incorrect value at position %v. Got: %v. Wanted: %v.\n", k, val, wanted)
}
}
}
}