forked from ftrvxmtrx/tga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
86 lines (69 loc) · 2.22 KB
/
encode_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
package tga_test
import (
"image"
"image/draw"
"os"
"testing"
"github.com/schwarzlichtbezirk/tga"
)
const dstFilename = "encode_test.tga"
func encode(m image.Image) (err error) {
var f *os.File
if f, err = os.Create("testdata/" + dstFilename); err != nil {
return
}
defer f.Close()
err = tga.Encode(f, m)
return
}
func testImagesEqual(filename string, expected, got image.Image, t *testing.T) bool {
gb := expected.Bounds()
for x := gb.Min.X; x < gb.Max.X; x++ {
for y := gb.Min.Y; y < gb.Max.Y; y++ {
if !equal(expected.At(x, y), got.At(x, y)) {
t.Errorf("%s: (%d, %d) -- expected %v, got %v", filename, x, y, expected.At(x, y), got.At(x, y))
return false
}
}
}
return true
}
func TestEncode(t *testing.T) {
defer os.Remove("testdata/" + dstFilename)
loop:
for _, test := range tgaTests {
typ := "normal"
if m, _, err := decode(test.source); err != nil {
t.Errorf("%s: %v", test.source, err)
} else if err = encode(m); err != nil {
t.Errorf("%s: encode failed (%v)", test.source, err)
} else if second, _, err := decode(dstFilename); err != nil {
t.Errorf("%s: %v", dstFilename, err)
} else if testImagesEqual(test.source, m, second, t) {
// test monochrome
b := second.Bounds()
gray := image.NewGray(b)
draw.Draw(gray, b, second, b.Min, draw.Src)
typ = "gray"
if err = encode(gray); err != nil {
t.Errorf("%s: encode to gray failed (%v)", test.source, err)
} else if third, _, err := decode(dstFilename); err != nil {
t.Errorf("%s: gray decode failed (%v)", dstFilename, err)
} else if testImagesEqual(test.source, gray, third, t) {
premultiplied := image.NewRGBA(b)
draw.Draw(premultiplied, b, second, b.Min, draw.Src)
typ = "premultiplied"
if err = encode(premultiplied); err != nil {
t.Errorf("%s: encode to premultiplied failed (%v)", test.source, err)
} else if fourth, _, err := decode(dstFilename); err != nil {
t.Errorf("%s: premultiplied decode failed (%v)", dstFilename, err)
} else if testImagesEqual(test.source, premultiplied, fourth, t) {
continue loop
} else {
t.Errorf("%T %T", premultiplied, fourth)
}
}
}
t.Errorf("%s: encoded %s image (%s) is different", test.source, typ, dstFilename)
}
}