-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
385 lines (321 loc) · 7.3 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package goapng
import (
"bytes"
"compress/zlib"
"encoding/binary"
"errors"
"hash/crc32"
"image"
"image/png"
"io"
)
type Encoder struct {
CompressionLevel CompressionLevel
}
const (
pngHeader = "\x89PNG\r\n\x1a\n"
filterNum = 5 // none, sub, up, average, paeth
)
type CompressionLevel int
const (
DefaultCompression CompressionLevel = 0
NoCompression CompressionLevel = -1
BestSpeed CompressionLevel = -2
BestCompression CompressionLevel = -3
)
func levelToZlib(l CompressionLevel) int {
switch l {
case DefaultCompression:
return zlib.DefaultCompression
case NoCompression:
return zlib.NoCompression
case BestSpeed:
return zlib.BestSpeed
case BestCompression:
return zlib.BestCompression
default:
return zlib.DefaultCompression
}
}
type idat []byte
func writeUint16(b []uint8, u uint16) {
b[0] = uint8(u >> 8)
b[1] = uint8(u)
}
func writeUint32(b []uint8, u uint32) {
b[0] = uint8(u >> 24)
b[1] = uint8(u >> 16)
b[2] = uint8(u >> 8)
b[3] = uint8(u)
}
type APNG struct {
Images []image.Image // The successive images.
Delays []uint16 // The successive delay times, one per frame, in 100ths of a second.
Disposals []byte // The successive disposal methods, one per frame.
LoopCount uint32 // The loop count. 0 indicates infinite looping.
Config image.Config
}
type encoder struct {
a *APNG
w io.Writer
seqNum uint32 // Sequence number of the animation chunk.
tmpHeader [8]byte
tmp [4 * 256]byte
tmpFooter [4]byte
ihdr []byte
idats []idat
err error
}
func (e *encoder) writeChunk(b []byte, name string) {
if e.err != nil {
return
}
// Write header (length, type).
n := uint32(len(b))
if int(n) != len(b) {
e.err = errors.New("apng: chunk is too large")
return
}
writeUint32(e.tmpHeader[:4], n)
e.tmpHeader[4] = name[0]
e.tmpHeader[5] = name[1]
e.tmpHeader[6] = name[2]
e.tmpHeader[7] = name[3]
_, e.err = e.w.Write(e.tmpHeader[:8])
if e.err != nil {
return
}
// Write data.
_, e.err = e.w.Write(b)
if e.err != nil {
return
}
// Write footer (crc).
crc := crc32.NewIEEE()
crc.Write(e.tmpHeader[4:8])
crc.Write(b)
writeUint32(e.tmpFooter[:4], crc.Sum32())
_, e.err = e.w.Write(e.tmpFooter[:4])
}
func (e *encoder) writeIHDR() {
e.writeChunk(e.ihdr, "IHDR")
}
func (e *encoder) writeacTL() {
writeUint32(e.tmp[0:4], uint32(len(e.a.Images)))
writeUint32(e.tmp[4:8], e.a.LoopCount)
e.writeChunk(e.tmp[:8], "acTL")
}
func (e *encoder) writefcTL(frameIndex int) {
// Write sequence_number.
writeUint32(e.tmp[0:4], e.seqNum)
bounds := (e.a.Images[frameIndex]).Bounds()
// Write width.
writeUint32(e.tmp[4:8], uint32(bounds.Max.X-bounds.Min.X))
// Write height.
writeUint32(e.tmp[8:12], uint32(bounds.Max.Y-bounds.Min.Y))
// Write x_offset.
writeUint32(e.tmp[12:16], uint32(bounds.Min.X))
// Write y_offset.
writeUint32(e.tmp[16:20], uint32(bounds.Min.Y))
// Write delay_num(numerator).
writeUint16(e.tmp[20:22], e.a.Delays[frameIndex])
// Write delay_den(denominator).
writeUint16(e.tmp[22:24], uint16(100))
// Write dispose_op.
//switch d := e.a.Disposals[frameIndex]; d {
//case 0, 1, 2:
// e.tmp[24] = d
//default:
// e.tmp[24] = 0
//}
e.tmp[24] = 0
// Write blend_op.
e.tmp[25] = 0
e.writeChunk(e.tmp[:26], "fcTL")
e.seqNum++
}
func (e *encoder) writeIDATs() {
for _, id := range e.idats {
e.writeChunk(id, "IDAT")
}
}
func (e *encoder) writefdATs() {
for _, id := range e.idats {
writeUint32(e.tmp[0:4], e.seqNum)
fdat := make([]byte, 4, len(id))
copy(fdat, e.tmp[0:4])
fdat = append(fdat, id...)
e.writeChunk(fdat, "fdAT")
e.seqNum++
}
}
func (e *encoder) writeIEND() {
e.writeChunk(nil, "IEND")
}
const (
dsStart = iota
dsSeenIHDR
dsSeenPLTE
dsSeentRNS
dsSeenIDAT
dsSeenIEND
)
type chunkFetcher struct {
bb *bytes.Buffer
tmp [3 * 256]byte
stage int
pc *pngChunk
ac *apngChunk
}
type pngChunk struct {
ihdr []byte
idats []idat
}
type apngChunk struct {
ihdr []byte
}
func (c *chunkFetcher) parseIHDR(length uint32) error {
_, err := io.ReadFull(c.bb, c.tmp[:length])
if err != nil {
return err
}
c.pc.ihdr = make([]byte, length)
copy(c.pc.ihdr, c.tmp[:length])
return nil
}
func (c *chunkFetcher) parseIDAT(length uint32) error {
id := c.bb.Next(int(length))
if len(id) < int(length) {
return io.EOF
}
c.pc.idats = append(c.pc.idats, id)
return nil
}
func (c *chunkFetcher) parseIEND(length uint32) error {
return nil
}
func (c *chunkFetcher) parsePNGChunk() error {
_, err := io.ReadFull(c.bb, c.tmp[:8])
if err != nil {
return err
}
length := binary.BigEndian.Uint32(c.tmp[:4])
switch string(c.tmp[4:8]) {
case "IHDR":
c.stage = dsSeenIHDR
err = c.parseIHDR(length)
case "PLTE":
// todo
case "tRNS":
// todo
case "IDAT":
c.stage = dsSeenIDAT
err = c.parseIDAT(length)
case "IEND":
c.stage = dsSeenIEND
err = c.parseIEND(length)
}
c.bb.Next(4) // Get rid of crc(4 bytes).
return err
}
func fetchPNGChunk(bb *bytes.Buffer) (*pngChunk, error) {
bb.Next(len(pngHeader))
c := &chunkFetcher{
bb: bb,
stage: dsStart,
pc: new(pngChunk),
}
for c.stage != dsSeenIEND {
if err := c.parsePNGChunk(); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, err
}
}
return c.pc, nil
}
func isSameColorModel(img []image.Image) bool {
if len(img) == 0 || img[0] == nil {
return false
}
reference := img[0].ColorModel()
for i := 1; i < len(img); i++ {
if img[i] == nil || img[i].ColorModel() != reference {
return false
}
}
return true
}
func fullfillFrameRegionConstraints(img []image.Image) bool {
if len(img) == 0 || img[0] == nil {
return false
}
reference := img[0].Bounds()
// constraints:
// x_offset >= 0 && y_offset >= 0
if !(reference.Min.X >= 0 && reference.Min.Y >= 0) {
return false
}
for i := 1; i < len(img); i++ {
if img[i] == nil {
return false
}
bounds := img[i].Bounds()
// constrains:
// x_offset >= 0
// && y_offset >= 0
// && x_offset + width = max_x <= first frame width
// && y_offset + height = max_y <= first frame height
if !(bounds.Min.X >= 0 && bounds.Min.Y >= 0 && bounds.Max.X <= reference.Max.X && bounds.Max.Y <= reference.Max.Y) {
return false
}
}
return true
}
func EncodeAll(w io.Writer, a *APNG) error {
if len(a.Images) == 0 {
return errors.New("apng: need at least one image")
}
if len(a.Images) != len(a.Delays) {
return errors.New("apng: mismatched image and delay lengths")
}
if a.Disposals != nil && len(a.Images) != len(a.Disposals) {
return errors.New("apng: mismatch image and disposal lengths")
}
if !isSameColorModel(a.Images) {
return errors.New("apng: must be all the same color model of images")
}
if !fullfillFrameRegionConstraints(a.Images) {
return errors.New("apng: must fullfill frame region constraints.")
}
e := encoder{
a: a,
w: w,
}
_, e.err = io.WriteString(w, pngHeader)
for i, img := range a.Images {
bb := new(bytes.Buffer)
if err := png.Encode(bb, img); err != nil {
return errors.New("apng: png encoding error(" + err.Error() + ")")
}
pc, err := fetchPNGChunk(bb)
if err != nil {
return err
}
e.ihdr = pc.ihdr
e.idats = pc.idats
// First image is defalt image.
if i == 0 {
e.writeIHDR()
e.writeacTL()
e.writefcTL(i)
e.writeIDATs()
} else {
e.writefcTL(i)
e.writefdATs()
}
}
e.writeIEND()
return e.err
}