blob: 930bf0c7e1d0dc49f6d8abfa08ee93bccb2dedd7 [file] [log] [blame]
Nigel Tao981bf192018-05-23 12:17:55 +10001// Copyright 2018 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build ignore
16
17package main
18
19// make-artificial.go makes test data in various formats.
20//
21// See test/data/artificial/*.make.txt for examples.
22//
23// Usage: go run make-artificial.go < foo.make.txt
24
25import (
26 "bytes"
27 "compress/lzw"
28 "fmt"
29 "io/ioutil"
30 "os"
31 "strconv"
32 "strings"
33)
34
35type stateFunc func(line string) (stateFunc, error)
36
37type repeat struct {
38 count uint32
39 remaining string
40}
41
42var (
43 state stateFunc
44 repeats []repeat
45 out []byte
46 formats = map[string]stateFunc{}
47)
48
49func main() {
50 if err := main1(); err != nil {
51 os.Stderr.WriteString(err.Error() + "\n")
52 os.Exit(1)
53 }
54}
55
56func main1() error {
57 stdin, err := ioutil.ReadAll(os.Stdin)
58 if err != nil {
59 return err
60 }
61 s := string(stdin)
62 for remaining := ""; len(s) > 0; s, remaining = remaining, "" {
63 if i := strings.IndexByte(s, '\n'); i >= 0 {
64 s, remaining = s[:i], s[i+1:]
65 }
66 s = strings.TrimSpace(s)
67 if s == "" || s[0] == '#' {
68 continue
69 }
70
71 if state == nil {
72 const m = "make "
73 if !strings.HasPrefix(s, m) {
74 return fmt.Errorf(`input must start with "make foo"`)
75 }
76 s = s[len(m):]
77 state = formats[s]
78 if state == nil {
79 return fmt.Errorf("unsupported format %q", s)
80 }
81 continue
82 }
83
84 const rep = "repeat "
85 if strings.HasPrefix(s, rep) {
86 args := s[len(rep):]
87 count, args, ok := parseNum(args)
88 if !ok || count <= 0 || args != "[" {
89 return fmt.Errorf("bad repeat command: %q", s)
90 }
91 repeats = append(repeats, repeat{
92 count: count,
93 remaining: remaining,
94 })
95 continue
96 }
97
98 if s == "]" {
99 if len(repeats) <= 0 {
100 return fmt.Errorf(`unbalanced close-repeat command: "]"`)
101 }
102 i := len(repeats) - 1
103 repeats[i].count--
104 if repeats[i].count == 0 {
105 repeats = repeats[:i]
106 } else {
107 remaining = repeats[i].remaining
108 }
109 continue
110 }
111
112 state, err = state(s)
113 if err != nil {
114 return err
115 }
116 if state == nil {
117 return fmt.Errorf("bad state transition")
118 }
119 }
120
121 _, err = os.Stdout.Write(out)
122 return err
123}
124
125// ----
126
127func appendU16LE(b []byte, u uint16) []byte {
128 return append(b, uint8(u), uint8(u>>8))
129}
130
131func log2(u uint32) (i int32) {
132 for i, pow := uint32(0), uint32(1); i < 32; i, pow = i+1, pow<<1 {
133 if u == pow {
134 return int32(i)
135 }
136 if u < pow {
137 break
138 }
139 }
140 return -1
141}
142
143func parseHex(s string) (num uint32, remaining string, ok bool) {
144 if i := strings.IndexByte(s, ' '); i >= 0 {
145 s, remaining = s[:i], s[i+1:]
146 for len(remaining) > 0 && remaining[0] == ' ' {
147 remaining = remaining[1:]
148 }
149 }
150
151 if len(s) < 2 || s[0] != '0' || s[1] != 'x' {
152 return 0, "", false
153 }
154 s = s[2:]
155
156 u, err := strconv.ParseUint(s, 16, 32)
157 if err != nil {
158 return 0, "", false
159 }
160 return uint32(u), remaining, true
161}
162
163func parseNum(s string) (num uint32, remaining string, ok bool) {
164 if i := strings.IndexByte(s, ' '); i >= 0 {
165 s, remaining = s[:i], s[i+1:]
166 for len(remaining) > 0 && remaining[0] == ' ' {
167 remaining = remaining[1:]
168 }
169 }
170
171 u, err := strconv.ParseUint(s, 10, 32)
172 if err != nil {
173 return 0, "", false
174 }
175 return uint32(u), remaining, true
176}
177
178func reverse(x uint32, n uint32) uint32 {
179 x = uint32(reverse8[0xFF&(x>>0)])<<24 |
180 uint32(reverse8[0xFF&(x>>8)])<<16 |
181 uint32(reverse8[0xFF&(x>>16)])<<8 |
182 uint32(reverse8[0xFF&(x>>24)])<<0
183 return x >> (32 - n)
184}
185
186var reverse8 = [256]byte{
187 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, // 0x00 - 0x07
188 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, // 0x08 - 0x0F
189 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, // 0x10 - 0x17
190 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, // 0x18 - 0x1F
191 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, // 0x20 - 0x27
192 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, // 0x28 - 0x2F
193 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, // 0x30 - 0x37
194 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, // 0x38 - 0x3F
195 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, // 0x40 - 0x47
196 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, // 0x48 - 0x4F
197 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, // 0x50 - 0x57
198 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, // 0x58 - 0x5F
199 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, // 0x60 - 0x67
200 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, // 0x68 - 0x6F
201 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, // 0x70 - 0x77
202 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, // 0x78 - 0x7F
203 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, // 0x80 - 0x87
204 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, // 0x88 - 0x8F
205 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, // 0x90 - 0x97
206 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, // 0x98 - 0x9F
207 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, // 0xA0 - 0xA7
208 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, // 0xA8 - 0xAF
209 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, // 0xB0 - 0xB7
210 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, // 0xB8 - 0xBF
211 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, // 0xC0 - 0xC7
212 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, // 0xC8 - 0xCF
213 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, // 0xD0 - 0xD7
214 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, // 0xD8 - 0xDF
215 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, // 0xE0 - 0xE7
216 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, // 0xE8 - 0xEF
217 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, // 0xF0 - 0xF7
218 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF, // 0xF8 - 0xFF
219}
220
221// ----
222
223func init() {
224 formats["deflate"] = stateDeflate
225}
226
227var deflateGlobals struct {
228 bncData []byte
229 stream deflateBitStream
230}
231
232func stateDeflate(line string) (stateFunc, error) {
233 g := &deflateGlobals
234 const (
Nigel Tao0cc17982019-05-19 23:43:20 +1000235 cmdB = "bytes "
Nigel Tao981bf192018-05-23 12:17:55 +1000236 cmdBNC = "blockNoCompression "
237 cmdBFH = "blockFixedHuffman "
238 )
239 bits := uint32(0)
240 s := ""
241
242 retState := stateFunc(nil)
243 switch {
Nigel Tao0cc17982019-05-19 23:43:20 +1000244 case strings.HasPrefix(line, cmdB):
245 s := line[len(cmdB):]
246 for s != "" {
247 x, ok := uint32(0), false
248 x, s, ok = parseHex(s)
249 if !ok {
250 return nil, fmt.Errorf("bad stateDeflate command: %q", line)
251 }
252 out = append(out, uint8(x))
253 }
254 return stateDeflate, nil
255
Nigel Tao981bf192018-05-23 12:17:55 +1000256 case strings.HasPrefix(line, cmdBNC):
257 s = line[len(cmdBNC):]
258 retState = stateDeflateNoCompression
259 bits |= 0 << 1
260 case strings.HasPrefix(line, cmdBFH):
261 s = line[len(cmdBFH):]
262 retState = stateDeflateFixedHuffman
263 bits |= 1 << 1
264 default:
265 return nil, fmt.Errorf("bad stateDeflate command: %q", line)
266 }
267
268 switch s {
269 case "(final) {":
270 bits |= 1
271 case "(nonFinal) {":
272 // No-op.
273 default:
274 return nil, fmt.Errorf("bad stateDeflate command: %q", line)
275 }
276
277 g.stream.writeBits(bits, 3)
278 return retState, nil
279}
280
281func stateDeflateNoCompression(line string) (stateFunc, error) {
282 g := &deflateGlobals
283 if line == "}" {
284 if len(g.bncData) > 0xFFFF {
285 return nil, fmt.Errorf("bncData is too long")
286 }
287 n := uint32(len(g.bncData))
288 g.stream.flush()
289 g.stream.writeBits(n, 16)
290 g.stream.writeBits(0xFFFF-n, 16)
291 g.stream.writeBytes(g.bncData)
292 g.bncData = g.bncData[:0]
293 return stateDeflate, nil
294 }
295
296 if lit, ok := deflateParseLiteral(line); ok {
297 g.bncData = append(g.bncData, lit...)
298 return stateDeflateNoCompression, nil
299 }
300
301 return nil, fmt.Errorf("bad blockNoCompression command: %q", line)
302}
303
304func stateDeflateFixedHuffman(line string) (stateFunc, error) {
305 g := &deflateGlobals
306 if line == "}" {
307 g.stream.flush()
308 return stateDeflate, nil
309 }
310
311 if line == "endOfBlock" {
312 g.stream.writeBits(0, 7)
313 return stateDeflateFixedHuffman, nil
314 }
315
316 if lit, ok := deflateParseLiteral(line); ok {
317 // TODO: support "\xAB" escape codes in the script?
318 for i := 0; i < len(lit); i++ {
319 g.stream.writeLCode(uint32(lit[i]))
320 }
321 return stateDeflateFixedHuffman, nil
322 }
323
Nigel Tao11a68192019-02-02 13:04:09 +1100324 if line == "len 3 distCode 31" {
325 lCode, lExtra, lNExtra := deflateEncodeLength(3)
326 g.stream.writeLCode(lCode)
327 g.stream.writeBits(lExtra, lNExtra)
328 dCode, dExtra, dNExtra := uint32(31), uint32(0), uint32(0)
329 g.stream.writeBits(reverse(dCode, 5), 5)
330 g.stream.writeBits(dExtra, dNExtra)
331 return stateDeflateFixedHuffman, nil
332 }
333
Nigel Tao981bf192018-05-23 12:17:55 +1000334 if l, d, ok := deflateParseLenDist(line); ok {
335 lCode, lExtra, lNExtra := deflateEncodeLength(l)
336 g.stream.writeLCode(lCode)
337 g.stream.writeBits(lExtra, lNExtra)
338 dCode, dExtra, dNExtra := deflateEncodeDistance(d)
339 g.stream.writeBits(reverse(dCode, 5), 5)
340 g.stream.writeBits(dExtra, dNExtra)
341 return stateDeflateFixedHuffman, nil
342 }
343
344 return nil, fmt.Errorf("bad stateDeflateFixedHuffman command: %q", line)
345}
346
347type deflateBitStream struct {
348 bits uint32
349 nBits uint32 // Always within [0, 7].
350}
351
352// writeBits writes the low n bits of b to z.
353func (z *deflateBitStream) writeBits(b uint32, n uint32) {
354 if n > 24 {
355 panic("writeBits: n is too large")
356 }
357 z.bits |= b << z.nBits
358 z.nBits += n
359 for z.nBits >= 8 {
360 out = append(out, uint8(z.bits))
361 z.bits >>= 8
362 z.nBits -= 8
363 }
364}
365
366func (z *deflateBitStream) writeBytes(b []byte) {
367 z.flush()
368 out = append(out, b...)
369}
370
371func (z *deflateBitStream) writeLCode(lCode uint32) {
372 switch {
373 case lCode < 144: // 0b._0011_0000 through 0b._1011_1111
374 lCode += 0x030
375 z.writeBits(reverse(lCode, 8), 8)
376 case lCode < 256: // 0b1_1001_0000 through 0b1_1111_1111
377 lCode += 0x190 - 144
378 z.writeBits(reverse(lCode, 9), 9)
379 case lCode < 280: // 0b._.000_0000 through 0b._.001_0111
380 lCode -= 256 - 0x000
381 z.writeBits(reverse(lCode, 7), 7)
382 default: // 0b._1100_0000 through 0b._1100_0111
383 lCode -= 280 - 0x0C0
384 z.writeBits(reverse(lCode, 8), 8)
385 }
386}
387
388func (z *deflateBitStream) flush() {
389 if z.nBits > 0 {
390 out = append(out, uint8(z.bits))
391 z.bits = 0
392 z.nBits = 0
393 }
394}
395
396func deflateEncodeLength(l uint32) (code uint32, extra uint32, nExtra uint32) {
397 switch {
398 case l < 3:
399 // No-op.
400 case l < 11:
401 l -= 3
402 return (l >> 0) + 257, l & 0x0000, 0
403 case l < 19:
404 l -= 11
405 return (l >> 1) + 265, l & 0x0001, 1
406 case l < 35:
407 l -= 19
408 return (l >> 2) + 269, l & 0x0003, 2
409 case l < 67:
410 l -= 35
411 return (l >> 3) + 273, l & 0x0007, 3
412 case l < 131:
413 l -= 67
414 return (l >> 4) + 277, l & 0x000F, 4
415 case l < 258:
416 l -= 131
417 return (l >> 5) + 281, l & 0x001F, 5
418 case l == 258:
419 return 285, 0, 0
420 }
421 panic(fmt.Sprintf("deflateEncodeLength: l=%d", l))
422}
423
424func deflateEncodeDistance(d uint32) (code uint32, extra uint32, nExtra uint32) {
425 switch {
426 case d < 1:
427 // No-op.
428 case d < 5:
429 d -= 1
430 return (d >> 0) + 0, d & 0x0000, 0
431 case d < 9:
432 d -= 5
433 return (d >> 1) + 4, d & 0x0001, 1
434 case d < 17:
435 d -= 9
436 return (d >> 2) + 6, d & 0x0003, 2
437 case d < 33:
438 d -= 17
439 return (d >> 3) + 8, d & 0x0007, 3
440 case d < 65:
441 d -= 33
442 return (d >> 4) + 10, d & 0x000F, 4
443 case d < 129:
444 d -= 65
445 return (d >> 5) + 12, d & 0x001F, 5
446 case d < 257:
447 d -= 129
448 return (d >> 6) + 14, d & 0x003F, 6
449 case d < 513:
450 d -= 257
451 return (d >> 7) + 16, d & 0x007F, 7
452 case d < 1025:
453 d -= 513
454 return (d >> 8) + 18, d & 0x00FF, 8
455 case d < 2049:
456 d -= 1025
457 return (d >> 9) + 20, d & 0x01FF, 9
458 case d < 4097:
459 d -= 2049
460 return (d >> 10) + 22, d & 0x03FF, 10
461 case d < 8193:
462 d -= 4097
463 return (d >> 11) + 24, d & 0x07FF, 11
464 case d < 16385:
465 d -= 8193
466 return (d >> 12) + 26, d & 0x0FFF, 12
467 case d < 32769:
468 d -= 16385
469 return (d >> 13) + 28, d & 0x1FFF, 13
470 }
471 panic(fmt.Sprintf("deflateEncodeDistance: d=%d", d))
472}
473
474func deflateParseLiteral(s string) (lit string, ok bool) {
475 const (
476 prefix = `literal "`
477 suffix = `"`
478 )
479 if strings.HasPrefix(s, prefix) {
480 s = s[len(prefix):]
481 if strings.HasSuffix(s, suffix) {
482 return s[:len(s)-len(suffix)], true
483 }
484 }
485 return "", false
486}
487
488func deflateParseLenDist(line string) (l uint32, d uint32, ok bool) {
489 const (
490 lStr = "len "
491 dStr = "dist "
492 )
493 s := line
494 if strings.HasPrefix(s, lStr) {
495 s = s[len(lStr):]
496 if l, s, ok := parseNum(s); ok && 3 <= l && l <= 258 {
497 if strings.HasPrefix(s, dStr) {
498 s = s[len(dStr):]
499 if d, s, ok := parseNum(s); ok && 1 <= d && d <= 32768 && s == "" {
500 return l, d, true
501 }
502 }
503 }
504 }
505 return 0, 0, false
506}
507
508// ----
509
510func init() {
511 formats["gif"] = stateGif
512}
513
514var gifGlobals struct {
Nigel Tao31a2bc92019-05-18 17:32:24 +1000515 imageWidth uint32
516 imageHeight uint32
517 imageBackgroundColorIndex uint32
Nigel Tao981bf192018-05-23 12:17:55 +1000518
519 frameLeft uint32
520 frameTop uint32
521 frameWidth uint32
522 frameHeight uint32
523
524 globalPalette [][4]uint8
Nigel Tao746681e2019-05-04 13:49:22 +1000525 localPalette [][4]uint8
Nigel Tao981bf192018-05-23 12:17:55 +1000526}
527
528func stateGif(line string) (stateFunc, error) {
529 const (
Nigel Tao6700c722019-05-26 09:41:55 +1000530 cmdB = "bytes "
531 cmdGC = "graphicControl "
532 cmdL = "lzw "
533 cmdLC = "loopCount "
Nigel Tao981bf192018-05-23 12:17:55 +1000534 )
535outer:
536 switch {
537 case line == "frame {":
Nigel Tao746681e2019-05-04 13:49:22 +1000538 gifGlobals.localPalette = nil
Nigel Tao981bf192018-05-23 12:17:55 +1000539 out = append(out, 0x2C)
540 return stateGifFrame, nil
541
542 case line == "header":
543 out = append(out, "GIF89a"...)
544 return stateGif, nil
545
546 case line == "image {":
547 return stateGifImage, nil
548
549 case line == "trailer":
550 out = append(out, 0x3B)
551 return stateGif, nil
552
Nigel Tao21f28902019-04-20 16:33:11 +1000553 case strings.HasPrefix(line, cmdB):
554 s := line[len(cmdB):]
555 for s != "" {
556 x, ok := uint32(0), false
557 x, s, ok = parseHex(s)
558 if !ok {
559 break outer
560 }
561 out = append(out, uint8(x))
562 }
563 return stateGif, nil
564
Nigel Tao6700c722019-05-26 09:41:55 +1000565 case strings.HasPrefix(line, cmdGC):
566 s := line[len(cmdGC):]
567
568 flags := uint8(0)
569 if i := strings.IndexByte(s, ' '); i < 0 {
570 break
571 } else {
572 switch s[:i] {
573 case "animationDisposalNone":
574 flags |= 0x00
575 case "animationDisposalRestoreBackground":
576 flags |= 0x08
577 case "animationDisposalRestorePrevious":
578 flags |= 0x0C
579 default:
580 break outer
581 }
582 s = s[i+1:]
583 }
584
Nigel Taob1ff27f2019-05-12 22:00:44 +1000585 if !strings.HasSuffix(s, "ms") {
586 break
587 }
588 s = s[:len(s)-2]
589 duration, s, ok := parseNum(s)
590 if !ok || s != "" {
591 break
592 }
593 duration /= 10 // GIF's unit of time is 10ms.
Nigel Tao6700c722019-05-26 09:41:55 +1000594
595 transparentIndex := uint8(0)
596
Nigel Taob1ff27f2019-05-12 22:00:44 +1000597 out = append(out,
Nigel Tao6700c722019-05-26 09:41:55 +1000598 0x21, 0xF9, 0x04,
599 flags,
Nigel Taob1ff27f2019-05-12 22:00:44 +1000600 uint8(duration>>0),
601 uint8(duration>>8),
Nigel Tao6700c722019-05-26 09:41:55 +1000602 transparentIndex,
603 0x00,
Nigel Taob1ff27f2019-05-12 22:00:44 +1000604 )
605 return stateGif, nil
606
Nigel Tao981bf192018-05-23 12:17:55 +1000607 case strings.HasPrefix(line, cmdL):
608 s := line[len(cmdL):]
609 litWidth, s, ok := parseNum(s)
610 if !ok || litWidth < 2 || 8 < litWidth {
611 break
612 }
613 out = append(out, uint8(litWidth))
614
615 uncompressed := []byte(nil)
616 for s != "" {
617 x := uint32(0)
618 x, s, ok = parseHex(s)
619 if !ok {
620 break outer
621 }
622 uncompressed = append(uncompressed, uint8(x))
623 }
624
625 buf := bytes.NewBuffer(nil)
626 w := lzw.NewWriter(buf, lzw.LSB, int(litWidth))
627 if _, err := w.Write(uncompressed); err != nil {
628 return nil, err
629 }
630 if err := w.Close(); err != nil {
631 return nil, err
632 }
633 compressed := buf.Bytes()
634
635 for len(compressed) > 0 {
636 if len(compressed) <= 0xFF {
637 out = append(out, uint8(len(compressed)))
638 out = append(out, compressed...)
639 compressed = nil
640 } else {
641 out = append(out, 0xFF)
642 out = append(out, compressed[:0xFF]...)
643 compressed = compressed[0xFF:]
644 }
645 }
646 out = append(out, 0x00)
647 return stateGif, nil
Nigel Tao7be9c392018-10-13 17:00:45 +1100648
649 case strings.HasPrefix(line, cmdLC):
650 s := line[len(cmdLC):]
651 loopCount, _, ok := parseNum(s)
652 if !ok || 0xFFFF < loopCount {
653 break
654 }
655 out = append(out,
656 0x21, // Extension Introducer.
657 0xFF, // Application Extension Label.
658 0x0B, // Block Size.
659 )
660 out = append(out, "NETSCAPE2.0"...)
661 out = append(out,
662 0x03, // Block Size.
663 0x01, // Magic Number.
664 byte(loopCount),
665 byte(loopCount>>8),
666 0x00, // Block Terminator.
667 )
668 return stateGif, nil
Nigel Tao981bf192018-05-23 12:17:55 +1000669 }
670
671 return nil, fmt.Errorf("bad stateGif command: %q", line)
672}
673
674func stateGifImage(line string) (stateFunc, error) {
675 g := &gifGlobals
676 if line == "}" {
677 out = appendU16LE(out, uint16(g.imageWidth))
678 out = appendU16LE(out, uint16(g.imageHeight))
679 if g.globalPalette == nil {
680 out = append(out, 0x00)
681 } else if n := log2(uint32(len(g.globalPalette))); n < 2 || 8 < n {
682 return nil, fmt.Errorf("bad len(g.globalPalette): %d", len(g.globalPalette))
683 } else {
684 out = append(out, 0x80|uint8(n-1))
685 }
Nigel Tao31a2bc92019-05-18 17:32:24 +1000686 out = append(out, uint8(g.imageBackgroundColorIndex))
Nigel Tao981bf192018-05-23 12:17:55 +1000687 out = append(out, 0x00)
688 for _, x := range g.globalPalette {
689 out = append(out, x[0], x[1], x[2])
690 }
691 return stateGif, nil
692 }
693
694 const (
Nigel Tao31a2bc92019-05-18 17:32:24 +1000695 cmdBCI = "backgroundColorIndex "
Nigel Tao981bf192018-05-23 12:17:55 +1000696 cmdIWH = "imageWidthHeight "
697 cmdP = "palette {"
698 )
699 switch {
Nigel Tao31a2bc92019-05-18 17:32:24 +1000700 case strings.HasPrefix(line, cmdBCI):
701 s := line[len(cmdBCI):]
702 if i, _, ok := parseNum(s); ok {
703 g.imageBackgroundColorIndex = i
704 }
705 return stateGifImage, nil
706
Nigel Tao981bf192018-05-23 12:17:55 +1000707 case strings.HasPrefix(line, cmdIWH):
708 s := line[len(cmdIWH):]
709 if w, s, ok := parseNum(s); ok {
710 if h, _, ok := parseNum(s); ok {
711 g.imageWidth = w
712 g.imageHeight = h
713 return stateGifImage, nil
714 }
715 }
716
717 case strings.HasPrefix(line, cmdP):
718 return stateGifImagePalette, nil
719 }
720
721 return nil, fmt.Errorf("bad stateGifImage command: %q", line)
722}
723
724func stateGifFrame(line string) (stateFunc, error) {
725 g := &gifGlobals
726 if line == "}" {
727 out = appendU16LE(out, uint16(g.frameLeft))
728 out = appendU16LE(out, uint16(g.frameTop))
729 out = appendU16LE(out, uint16(g.frameWidth))
730 out = appendU16LE(out, uint16(g.frameHeight))
Nigel Tao746681e2019-05-04 13:49:22 +1000731 if g.localPalette == nil {
732 out = append(out, 0x00)
733 } else if n := log2(uint32(len(g.localPalette))); n < 2 || 8 < n {
734 return nil, fmt.Errorf("bad len(g.localPalette): %d", len(g.localPalette))
735 } else {
736 out = append(out, 0x80|uint8(n-1))
737 }
738 for _, x := range g.localPalette {
739 out = append(out, x[0], x[1], x[2])
740 }
Nigel Tao981bf192018-05-23 12:17:55 +1000741 return stateGif, nil
742 }
743
744 const (
745 cmdFLTWH = "frameLeftTopWidthHeight "
Nigel Tao746681e2019-05-04 13:49:22 +1000746 cmdP = "palette {"
Nigel Tao981bf192018-05-23 12:17:55 +1000747 )
748 switch {
749 case strings.HasPrefix(line, cmdFLTWH):
750 s := line[len(cmdFLTWH):]
751 if l, s, ok := parseNum(s); ok {
752 if t, s, ok := parseNum(s); ok {
753 if w, s, ok := parseNum(s); ok {
754 if h, _, ok := parseNum(s); ok {
755 g.frameLeft = l
756 g.frameTop = t
757 g.frameWidth = w
758 g.frameHeight = h
759 return stateGifFrame, nil
760 }
761 }
762 }
763 }
Nigel Tao746681e2019-05-04 13:49:22 +1000764
765 case strings.HasPrefix(line, cmdP):
766 return stateGifFramePalette, nil
Nigel Tao981bf192018-05-23 12:17:55 +1000767 }
768
769 return nil, fmt.Errorf("bad stateGifFrame command: %q", line)
770}
771
772func stateGifImagePalette(line string) (stateFunc, error) {
773 g := &gifGlobals
774 if line == "}" {
775 return stateGifImage, nil
776 }
777
778 s := line
779 if rgb0, s, ok := parseHex(s); ok {
780 if rgb1, s, ok := parseHex(s); ok {
781 if rgb2, _, ok := parseHex(s); ok {
782 g.globalPalette = append(g.globalPalette,
783 [4]uint8{uint8(rgb0), uint8(rgb1), uint8(rgb2), 0xFF})
784 return stateGifImagePalette, nil
785 }
786 }
787 }
788
789 return nil, fmt.Errorf("bad stateGifImagePalette command: %q", line)
790}
Nigel Tao746681e2019-05-04 13:49:22 +1000791
792func stateGifFramePalette(line string) (stateFunc, error) {
793 g := &gifGlobals
794 if line == "}" {
795 return stateGifFrame, nil
796 }
797
798 s := line
799 if rgb0, s, ok := parseHex(s); ok {
800 if rgb1, s, ok := parseHex(s); ok {
801 if rgb2, _, ok := parseHex(s); ok {
802 g.localPalette = append(g.localPalette,
803 [4]uint8{uint8(rgb0), uint8(rgb1), uint8(rgb2), 0xFF})
804 return stateGifFramePalette, nil
805 }
806 }
807 }
808
809 return nil, fmt.Errorf("bad stateGifFramePalette command: %q", line)
810}