-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdecode_test.go
139 lines (127 loc) · 3.01 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
package aseprite
import (
"image"
"image/color"
"image/png"
"os"
"testing"
"github.com/askeladdk/aseprite/internal/require"
)
func equalPalette(a, b color.Palette) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestDecode(t *testing.T) {
for _, tt := range []struct {
Name string
Filename string
Outfile string
Frames int
Tags int
}{
{
Name: "paletted",
Filename: "./testfiles/slime_paletted.aseprite",
Outfile: "slime_paletted.png",
Frames: 10,
Tags: 2,
},
{
Name: "grayscale",
Filename: "./testfiles/slime_grayscale.aseprite",
Outfile: "slime_grayscale.png",
Frames: 10,
Tags: 2,
},
{
Name: "blendtest",
Filename: "./testfiles/blendtest.aseprite",
Outfile: "blendtest.png",
Frames: 1,
Tags: 0,
},
} {
t.Run(tt.Name, func(t *testing.T) {
f, err := os.Open(tt.Filename)
require.NoError(t, err)
defer f.Close()
img, imgformat, err := image.Decode(f)
require.NoError(t, err)
if imgformat != "aseprite" {
t.Fatal(imgformat)
}
aspr, ok := img.(*Aseprite)
require.True(t, ok)
require.True(t, len(aspr.Frames) == tt.Frames, "frames", len(aspr.Frames))
require.True(t, len(aspr.Tags) == tt.Tags, "tags", len(aspr.Tags))
out, err := os.Create(tt.Outfile)
require.NoError(t, err)
require.NoError(t, png.Encode(out, img))
})
}
}
func TestDecodeConfig(t *testing.T) {
for _, tt := range []struct {
Name string
Filename string
ImageConfig image.Config
}{
{
Name: "paletted",
Filename: "./testfiles/slime_paletted.aseprite",
ImageConfig: image.Config{
ColorModel: &color.Palette{
0: color.Alpha16{0},
1: color.NRGBA{247, 231, 198, 255},
2: color.NRGBA{214, 142, 73, 255},
3: color.NRGBA{166, 55, 37, 255},
4: color.NRGBA{51, 30, 80, 255},
},
Width: 128,
Height: 256,
},
},
{
Name: "grayscale",
Filename: "./testfiles/slime_grayscale.aseprite",
ImageConfig: image.Config{
ColorModel: color.Gray16Model,
Width: 128,
Height: 256,
},
},
{
Name: "blendtest",
Filename: "./testfiles/blendtest.aseprite",
ImageConfig: image.Config{
ColorModel: color.RGBAModel,
Width: 640,
Height: 360,
},
},
} {
t.Run(tt.Name, func(t *testing.T) {
f, err := os.Open(tt.Filename)
require.NoError(t, err)
defer f.Close()
conf, imgformat, err := image.DecodeConfig(f)
require.NoError(t, err)
require.True(t, imgformat == "aseprite", "image format", imgformat)
require.True(t, conf.Height == tt.ImageConfig.Height, "height")
require.True(t, conf.Width == tt.ImageConfig.Width, "width")
if pal, ok := conf.ColorModel.(color.Palette); ok {
imgpal := *tt.ImageConfig.ColorModel.(*color.Palette)
require.True(t, equalPalette(pal, imgpal), "palette")
} else {
require.True(t, conf.ColorModel == tt.ImageConfig.ColorModel, "color model")
}
})
}
}