/
ico.go
261 lines (220 loc) · 5.3 KB
/
ico.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Package ico registers image.Decode and DecodeConfig support
// for the icon (container) format.
package ico
import (
"bytes"
"encoding/binary"
"errors"
"image"
"io"
"io/ioutil"
"image/png"
"golang.org/x/image/bmp"
)
type icondir struct {
Reserved uint16
Type uint16
Count uint16
Entries []icondirEntry
}
type icondirEntry struct {
Width byte
Height byte
PaletteCount byte
Reserved byte
ColorPlanes uint16
BitsPerPixel uint16
Size uint32
Offset uint32
}
func (dir *icondir) FindBestIcon() *icondirEntry {
if len(dir.Entries) == 0 {
return nil
}
best := dir.Entries[0]
for _, e := range dir.Entries {
if (e.width() > best.width()) && (e.height() > best.height()) {
best = e
}
}
return &best
}
// ParseIco parses the icon and returns meta information for the icons as icondir.
func ParseIco(r io.Reader) (*icondir, error) {
dir := icondir{}
var err error
err = binary.Read(r, binary.LittleEndian, &dir.Reserved)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &dir.Type)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &dir.Count)
if err != nil {
return nil, err
}
for i := uint16(0); i < dir.Count; i++ {
entry := icondirEntry{}
e := parseIcondirEntry(r, &entry)
if e != nil {
return nil, e
}
dir.Entries = append(dir.Entries, entry)
}
return &dir, err
}
func parseIcondirEntry(r io.Reader, e *icondirEntry) error {
err := binary.Read(r, binary.LittleEndian, e)
if err != nil {
return err
}
return nil
}
type dibHeader struct {
dibHeaderSize uint32
width uint32
height uint32
}
func (e *icondirEntry) ColorCount() int {
if e.PaletteCount == 0 {
return 256
}
return int(e.PaletteCount)
}
func (e *icondirEntry) width() int {
if e.Width == 0 {
return 256
}
return int(e.Width)
}
func (e *icondirEntry) height() int {
if e.Height == 0 {
return 256
}
return int(e.Height)
}
// DecodeConfig returns just the dimensions of the largest image
// contained in the icon withou decoding the entire icon file.
func DecodeConfig(r io.Reader) (image.Config, error) {
dir, err := ParseIco(r)
if err != nil {
return image.Config{}, err
}
best := dir.FindBestIcon()
if best == nil {
return image.Config{}, errInvalid
}
return image.Config{Width: best.width(), Height: best.height()}, nil
}
// The bitmap header structure we read from an icondirEntry
type bitmapHeaderRead struct {
Size uint32
Width uint32
Height uint32
Planes uint16
BitCount uint16
Compression uint32
ImageSize uint32
XPixelsPerMeter uint32
YPixelsPerMeter uint32
ColorsUsed uint32
ColorsImportant uint32
}
// The bitmap header structure we need to generate for bmp.Decode()
type bitmapHeaderWrite struct {
sigBM [2]byte
fileSize uint32
resverved [2]uint16
pixOffset uint32
Size uint32
Width uint32
Height uint32
Planes uint16
BitCount uint16
Compression uint32
ImageSize uint32
XPixelsPerMeter uint32
YPixelsPerMeter uint32
ColorsUsed uint32
ColorsImportant uint32
}
var errInvalid = errors.New("ico: invalid ICO image")
// Decode returns the largest image contained in the icon
// which might be a bmp or png
func Decode(r io.Reader) (image.Image, error) {
icoBytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
r = bytes.NewReader(icoBytes)
dir, err := ParseIco(r)
if err != nil {
return nil, errInvalid
}
best := dir.FindBestIcon()
if best == nil {
return nil, errInvalid
}
return parseImage(best, icoBytes)
}
func parseImage(entry *icondirEntry, icoBytes []byte) (image.Image, error) {
r := bytes.NewReader(icoBytes)
r.Seek(int64(entry.Offset), 0)
// Try PNG first then BMP
img, err := png.Decode(r)
if err != nil {
return parseBMP(entry, icoBytes)
}
return img, nil
}
func parseBMP(entry *icondirEntry, icoBytes []byte) (image.Image, error) {
bmpBytes, err := makeFullBMPBytes(entry, icoBytes)
if err != nil {
return nil, err
}
return bmp.Decode(bmpBytes)
}
func makeFullBMPBytes(entry *icondirEntry, icoBytes []byte) (*bytes.Buffer, error) {
r := bytes.NewReader(icoBytes)
r.Seek(int64(entry.Offset), 0)
var err error
h := bitmapHeaderRead{}
err = binary.Read(r, binary.LittleEndian, &h)
if err != nil {
return nil, err
}
if h.Size != 40 || h.Planes != 1 {
return nil, errInvalid
}
var pixOffset uint32
if h.ColorsUsed == 0 && h.BitCount <= 8 {
pixOffset = 14 + 40 + 4*(1<<h.BitCount)
} else {
pixOffset = 14 + 40 + 4*h.ColorsUsed
}
writeHeader := &bitmapHeaderWrite{
sigBM: [2]byte{'B', 'M'},
fileSize: 14 + 40 + uint32(len(icoBytes)), // correct? important?
pixOffset: pixOffset,
Size: 40,
Width: uint32(h.Width),
Height: uint32(h.Height / 2),
Planes: h.Planes,
BitCount: h.BitCount,
Compression: h.Compression,
ColorsUsed: h.ColorsUsed,
ColorsImportant: h.ColorsImportant,
}
buf := new(bytes.Buffer)
if err = binary.Write(buf, binary.LittleEndian, writeHeader); err != nil {
return nil, err
}
io.CopyN(buf, r, int64(entry.Size))
return buf, nil
}
const icoHeader = "\x00\x00\x01\x00"
func init() {
image.RegisterFormat("ico", icoHeader, Decode, DecodeConfig)
}