forked from smirkcat/ico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
230 lines (207 loc) · 5.42 KB
/
reader.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
package ico
import (
"bytes"
"encoding/binary"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"io"
"golang.org/x/image/bmp"
)
func init() {
image.RegisterFormat("ico", "\x00\x00\x01\x00?????\x00", Decode, DecodeConfig)
}
// ---- public ----
func Decode(r io.Reader) (image.Image, error) {
var d decoder
if err := d.decode(r); err != nil {
return nil, err
}
return d.images[0], nil
}
func DecodeAll(r io.Reader) ([]image.Image, error) {
var d decoder
if err := d.decode(r); err != nil {
return nil, err
}
return d.images, nil
}
func DecodeConfig(r io.Reader) (image.Config, error) {
var (
d decoder
cfg image.Config
err error
)
if err = d.decodeHeader(r); err != nil {
return cfg, err
}
if err = d.decodeEntries(r); err != nil {
return cfg, err
}
e := d.entries[0]
buf := make([]byte, e.Size+14)
n, err := io.ReadFull(r, buf[14:])
if err != nil && err != io.ErrUnexpectedEOF {
return cfg, err
}
buf = buf[:14+n]
if n > 8 && bytes.Equal(buf[14:22], pngHeader) {
return png.DecodeConfig(bytes.NewReader(buf[14:]))
}
d.forgeBMPHead(buf, &e)
return bmp.DecodeConfig(bytes.NewReader(buf))
}
// ---- private ----
type direntry struct {
Width byte
Height byte
Palette byte
_ byte
Plane uint16
Bits uint16
Size uint32
Offset uint32
}
type head struct {
Zero uint16
Type uint16
Number uint16
}
type decoder struct {
head head
entries []direntry
images []image.Image
}
// decode multiple images from entries in reader
func (d *decoder) decode(r io.Reader) (err error) {
if err = d.decodeHeader(r); err != nil {
return err
}
if err = d.decodeEntries(r); err != nil {
return err
}
d.images = make([]image.Image, d.head.Number)
for i := range d.entries {
e := &(d.entries[i])
data := make([]byte, e.Size+14)
n, err := io.ReadFull(r, data[14:])
if err != nil && err != io.ErrUnexpectedEOF {
return err
}
data = data[:14+n]
if n > 8 && bytes.Equal(data[14:22], pngHeader) { // decode as PNG
if d.images[i], err = png.Decode(bytes.NewReader(data[14:])); err != nil {
return err
}
} else { // decode as BMP
maskData := d.forgeBMPHead(data, e)
if maskData != nil {
data = data[:n+14-len(maskData)]
}
if d.images[i], err = bmp.Decode(bytes.NewReader(data)); err != nil {
return err
}
if int(e.Size) < len(data)-14 {
continue
}
bounds := d.images[i].Bounds()
mask := image.NewAlpha(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
masked := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
for row := 0; row < int(e.Height); row++ {
for col := 0; col < int(e.Width); col++ {
if maskData != nil {
rowSize := (int(e.Width) + 31) / 32 * 4
if (maskData[row*rowSize+col/8]>>(7-uint(col)%8))&0x01 != 1 {
mask.SetAlpha(col, int(e.Height)-row-1, color.Alpha{255})
}
}
if e.Bits == 32 { // 32 bit bmps do hacky things with an alpha channel, it's included as the 4th byte of the colors
rowSize := (int(e.Width)*32 + 31) / 32 * 4
offset := int(binary.LittleEndian.Uint32(data[10:14]))
alphaPosition := offset + row*rowSize + col*4 + 3
var alpha color.Alpha
if len(data) > alphaPosition { // bounds checking for alpha
alpha = color.Alpha{data[alphaPosition]}
}
mask.SetAlpha(col, int(e.Height)-row-1, alpha)
}
}
}
draw.DrawMask(masked, masked.Bounds(), d.images[i], bounds.Min, mask, bounds.Min, draw.Src)
d.images[i] = masked
}
}
if len(d.images) == 0 {
return fmt.Errorf("images not process during creating a rgba")
}
return nil
}
func (d *decoder) decodeHeader(r io.Reader) error {
binary.Read(r, binary.LittleEndian, &(d.head))
if d.head.Zero != 0 || d.head.Type != 1 {
return fmt.Errorf("corrupted head: [%x,%x]", d.head.Zero, d.head.Type)
}
return nil
}
func (d *decoder) decodeEntries(r io.Reader) error {
n := int(d.head.Number)
d.entries = make([]direntry, n)
for i := 0; i < n; i++ {
if err := binary.Read(r, binary.LittleEndian, &(d.entries[i])); err != nil {
return err
}
}
if len(d.entries) == 0 {
return fmt.Errorf("no entries to images")
}
return nil
}
func (d *decoder) forgeBMPHead(buf []byte, e *direntry) (mask []byte) {
// See en.wikipedia.org/wiki/BMP_file_format
data := buf[14:]
imageSize := len(data)
if e.Bits != 32 {
maskSize := (int(e.Width) + 31) / 32 * 4 * int(e.Height)
imageSize -= maskSize
if imageSize <= 0 {
return
}
mask = data[imageSize:]
}
copy(buf[0:2], "\x42\x4D") // Magic number
dibSize := binary.LittleEndian.Uint32(data[:4])
w := binary.LittleEndian.Uint32(data[4:8])
h := binary.LittleEndian.Uint32(data[8:12])
if h > w {
binary.LittleEndian.PutUint32(data[8:12], h/2)
}
binary.LittleEndian.PutUint32(buf[2:6], uint32(imageSize)) // File size
// Calculate offset into image data
numColors := binary.LittleEndian.Uint32(data[32:36])
bits := binary.LittleEndian.Uint16(data[14:16])
switch bits {
case 1, 2, 4, 8:
x := uint32(1 << bits)
if numColors == 0 || numColors > x {
numColors = x
}
default:
numColors = 0
}
var numColorsSize uint32
switch dibSize {
case 12, 64:
numColorsSize = numColors * 3
default:
numColorsSize = numColors * 4
}
offset := 14 + dibSize + numColorsSize
if dibSize > 40 && int(dibSize-4) <= len(data) {
offset += binary.LittleEndian.Uint32(data[dibSize-8 : dibSize-4])
}
binary.LittleEndian.PutUint32(buf[10:14], offset)
return
}
var pngHeader = []byte{'\x89', 'P', 'N', 'G', '\r', '\n', '\x1a', '\n'}