forked from pressly/goico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
173 lines (150 loc) · 3.48 KB
/
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
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
package ico
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"image"
"io"
bmp "github.com/jsummers/gobmp"
)
type dib struct {
DibSize uint32
Width uint32
Height uint32
Planes uint16
Bpp uint16
Compression uint32
XPMM uint32
YPMM uint32
Colors uint32
}
type encoder struct {
w io.Writer
m image.Image
entry bytes.Buffer
}
func (e *encoder) writeHeader(num int) (err error) {
// Write the required zero byte
if err = binary.Write(e.w, binary.LittleEndian, uint16(0)); err != nil {
return err
}
// Write the required one byte
if err = binary.Write(e.w, binary.LittleEndian, uint16(1)); err != nil {
return err
}
// Write the number of images
if err = binary.Write(e.w, binary.LittleEndian, uint16(num)); err != nil {
return err
}
return nil
}
func getSize(n []byte) (uint8, error) {
var orig uint32
var size uint8
if err := binary.Read(bytes.NewReader(n), binary.LittleEndian, &orig); err != nil {
return size, err
}
switch {
case orig == 256:
size = uint8(0)
case orig > 256:
return size, fmt.Errorf("width too big")
default:
size = uint8(orig)
}
return size, nil
}
func (e *encoder) writeEntry(b []byte, size, offset int) (err error) {
width, err := getSize(b[4:8])
if err != nil {
return err
}
if err = binary.Write(e.w, binary.LittleEndian, width); err != nil {
return err
}
height, err := getSize(b[8:12])
if err != nil {
return err
}
if err = binary.Write(e.w, binary.LittleEndian, height); err != nil {
return err
}
var palette uint16
if err = binary.Read(bytes.NewReader(b[32:36]), binary.LittleEndian, &palette); err != nil {
return err
}
fmt.Println("palette", palette, uint8(palette))
if err = binary.Write(e.w, binary.LittleEndian, uint8(palette)); err != nil {
return err
}
if err = binary.Write(e.w, binary.LittleEndian, uint8(0)); err != nil {
return err
}
var planes uint16
if err = binary.Read(bytes.NewReader(b[12:14]), binary.LittleEndian, &planes); err != nil {
return err
}
fmt.Println("planes", planes)
if err = binary.Write(e.w, binary.LittleEndian, planes); err != nil {
return err
}
var bpp uint16
if err = binary.Read(bytes.NewReader(b[14:16]), binary.LittleEndian, &bpp); err != nil {
return err
}
fmt.Println("bpp", bpp)
if err = binary.Write(e.w, binary.LittleEndian, bpp); err != nil {
return err
}
if err = binary.Write(e.w, binary.LittleEndian, uint32(size)); err != nil {
return err
}
fmt.Println("size", size, uint32(size))
if err = binary.Write(e.w, binary.LittleEndian, uint32(offset)); err != nil {
return err
}
fmt.Println("offset", offset, uint32(offset))
return nil
}
func (e *encoder) writeImage(m []byte) (err error) {
n, err := e.w.Write(m)
if n != len(m) {
return fmt.Errorf("not enough bytes written")
}
if err != nil {
return err
}
return nil
}
func Encode(w io.Writer, m image.Image) error {
var err error
e := new(encoder)
e.w = w
e.m = m
if err = e.writeHeader(1); err != nil {
return err
}
var buf bytes.Buffer
wr := bufio.NewWriter(&buf)
if err = bmp.Encode(wr, e.m); err != nil {
return err
}
wr.Flush()
if n := buf.Next(14); len(n) != 14 {
return fmt.Errorf("image not proper size")
} else {
fmt.Println(n)
var fSize uint32
binary.Read(bytes.NewReader(n[2:6]), binary.LittleEndian, &fSize)
fmt.Println("fSize", fSize)
}
b := buf.Bytes()
if err = e.writeEntry(b[:40], len(b), 22); err != nil {
return err
}
if err = e.writeImage(b); err != nil {
return err
}
return nil
}