-
Notifications
You must be signed in to change notification settings - Fork 10
/
writer.go
66 lines (58 loc) · 1000 Bytes
/
writer.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
package ico
import (
"bytes"
"encoding/binary"
"image"
"image/png"
"io"
"image/draw"
"bufio"
)
func Encode(w io.Writer, im image.Image) error {
b := im.Bounds()
m := image.NewRGBA(b)
draw.Draw(m, b, im, b.Min, draw.Src)
header := head{
0,
1,
1,
}
entry := direntry{
Plane: 1,
Bits: 32,
Offset: 22,
}
pngbuffer := new(bytes.Buffer)
pngwriter := bufio.NewWriter(pngbuffer)
err := png.Encode(pngwriter, m)
if err != nil {
return err
}
err = pngwriter.Flush()
if err != nil {
return err
}
entry.Size = uint32(len(pngbuffer.Bytes()))
bounds := m.Bounds()
entry.Width = uint8(bounds.Dx())
entry.Height = uint8(bounds.Dy())
bb := new(bytes.Buffer)
var e error
e = binary.Write(bb, binary.LittleEndian, header)
if e != nil {
return e
}
e = binary.Write(bb, binary.LittleEndian, entry)
if e != nil {
return e
}
_, e = w.Write(bb.Bytes())
if e != nil {
return e
}
_, e = w.Write(pngbuffer.Bytes())
if e != nil {
return e
}
return e
}