This repository has been archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
decode_test.go
98 lines (82 loc) · 2.35 KB
/
decode_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
97
98
package tga_test
import (
"bufio"
_ "github.com/ftrvxmtrx/tga" // should be the first one, because TGA doesn't have any constant "header"
"image"
"image/color"
"os"
"testing"
_ "image/png"
)
type tgaTest struct {
golden string
source string
}
var tgaTests = []tgaTest{
{"bw.png", "cbw8.tga"},
{"bw.png", "ubw8.tga"},
{"color.png", "ctc32.tga"},
{"color.png", "ctc24.tga"},
{"color.png", "ctc16.tga"},
{"color.png", "ccm8.tga"},
{"color.png", "ucm8.tga"},
{"color.png", "utc32.tga"},
{"color.png", "utc24.tga"},
{"color.png", "utc16.tga"},
{"monochrome16.png", "monochrome16_top_left_rle.tga"},
{"monochrome16.png", "monochrome16_top_left.tga"},
{"monochrome8.png", "monochrome8_bottom_left_rle.tga"},
{"monochrome8.png", "monochrome8_bottom_left.tga"},
{"rgb24.0.png", "rgb24_bottom_left_rle.tga"},
{"rgb24.1.png", "rgb24_top_left_colormap.tga"},
{"rgb24.0.png", "rgb24_top_left.tga"},
{"rgb32.0.png", "rgb32_bottom_left.tga"},
{"rgb32.1.png", "rgb32_top_left_rle_colormap.tga"},
{"rgb32.0.png", "rgb32_top_left_rle.tga"},
}
func decode(filename string) (image.Image, string, error) {
f, err := os.Open("testdata/" + filename)
if err != nil {
return nil, "", err
}
defer f.Close()
return image.Decode(bufio.NewReader(f))
}
func delta(a, b uint32) int {
if a < b {
return int(b) - int(a)
}
return int(a) - int(b)
}
func equal(c0, c1 color.Color) bool {
r0, g0, b0, a0 := c0.RGBA()
r1, g1, b1, a1 := c1.RGBA()
if a0 == 0 && a1 == 0 {
return true
}
return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1
}
func TestDecode(t *testing.T) {
loop:
for _, test := range tgaTests {
if golden, _, err := decode(test.golden); err != nil {
t.Errorf("%s: %v", test.golden, err)
} else if source, format, err := decode(test.source); err != nil {
t.Errorf("%s: %v", test.source, err)
} else if format != "tga" {
t.Errorf("%s: expected tga, got %v", test.source, format)
} else if !golden.Bounds().Eq(source.Bounds()) {
t.Errorf("%s: expected bounds %v, got %v", test.source, golden.Bounds(), source.Bounds())
} else {
gb := golden.Bounds()
for x := gb.Min.X; x < gb.Max.X; x++ {
for y := gb.Min.Y; y < gb.Max.Y; y++ {
if !equal(golden.At(x, y), source.At(x, y)) {
t.Errorf("%s: (%d, %d) -- expected %v, got %v", test.source, x, y, golden.At(x, y), source.At(x, y))
continue loop
}
}
}
}
}
}