-
Notifications
You must be signed in to change notification settings - Fork 10
/
reader_test.go
98 lines (86 loc) · 1.78 KB
/
reader_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 ico
import (
"testing"
"os"
"image/png"
"image"
"fmt"
"math"
)
func sqDiffUInt8(x, y uint8) uint64 {
d := uint64(x) - uint64(y)
return d * d
}
func fastCompare(img1, img2 *image.NRGBA) (int64, error) {
if img1.Bounds() != img2.Bounds() {
return 0, fmt.Errorf("image bounds not equal: %+v, %+v", img1.Bounds(), img2.Bounds())
}
accumError := int64(0)
for i := 0; i < len(img1.Pix); i++ {
accumError += int64(sqDiffUInt8(img1.Pix[i], img2.Pix[i]))
}
return int64(math.Sqrt(float64(accumError))), nil
}
func aTestDecodeConfig(t *testing.T) {
t.Parallel()
file := "testdata/golang.ico"
copyFile := "testdata/golang.png"
reader, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
icoImage, err := DecodeConfig(reader)
reader.Close()
if err != nil {
t.Fatal(err)
}
reader, err = os.Open(copyFile)
if err != nil {
t.Fatal(err)
}
pngImage, err := png.DecodeConfig(reader)
reader.Close()
if err != nil {
t.Fatal(err)
}
if icoImage != pngImage {
t.Errorf("%v - %v", icoImage, pngImage)
}
}
func TestDecode(t *testing.T) {
t.Parallel()
file := "testdata/golang.ico"
copyFile := "testdata/golang.png"
reader, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
icoImage, err := Decode(reader)
if err != nil {
t.Fatal(err)
}
reader.Close()
reader, err = os.Open(copyFile)
if err != nil {
t.Fatal(err)
}
pngImage, err := png.Decode(reader)
if err != nil {
t.Fatal(err)
}
reader.Close()
if icoImage == nil || !icoImage.Bounds().Eq(pngImage.Bounds()) {
t.Fatal("bounds differ")
}
inrgba, ok := icoImage.(*image.NRGBA)
if !ok {
t.Fatal("not nrgba")
}
pnrgba, ok := pngImage.(*image.NRGBA)
if !ok {
t.Fatal("png not nrgba")
}
if b, err := fastCompare(inrgba, pnrgba); err != nil || b != 0 {
t.Fatalf("pix differ %d %v\n", b, err)
}
}