blob: a8c74c661bd01ac7d3f9720a79d00ae57cdb5a77 [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 (
235 cmdBNC = "blockNoCompression "
236 cmdBFH = "blockFixedHuffman "
237 )
238 bits := uint32(0)
239 s := ""
240
241 retState := stateFunc(nil)
242 switch {
243 case strings.HasPrefix(line, cmdBNC):
244 s = line[len(cmdBNC):]
245 retState = stateDeflateNoCompression
246 bits |= 0 << 1
247 case strings.HasPrefix(line, cmdBFH):
248 s = line[len(cmdBFH):]
249 retState = stateDeflateFixedHuffman
250 bits |= 1 << 1
251 default:
252 return nil, fmt.Errorf("bad stateDeflate command: %q", line)
253 }
254
255 switch s {
256 case "(final) {":
257 bits |= 1
258 case "(nonFinal) {":
259 // No-op.
260 default:
261 return nil, fmt.Errorf("bad stateDeflate command: %q", line)
262 }
263
264 g.stream.writeBits(bits, 3)
265 return retState, nil
266}
267
268func stateDeflateNoCompression(line string) (stateFunc, error) {
269 g := &deflateGlobals
270 if line == "}" {
271 if len(g.bncData) > 0xFFFF {
272 return nil, fmt.Errorf("bncData is too long")
273 }
274 n := uint32(len(g.bncData))
275 g.stream.flush()
276 g.stream.writeBits(n, 16)
277 g.stream.writeBits(0xFFFF-n, 16)
278 g.stream.writeBytes(g.bncData)
279 g.bncData = g.bncData[:0]
280 return stateDeflate, nil
281 }
282
283 if lit, ok := deflateParseLiteral(line); ok {
284 g.bncData = append(g.bncData, lit...)
285 return stateDeflateNoCompression, nil
286 }
287
288 return nil, fmt.Errorf("bad blockNoCompression command: %q", line)
289}
290
291func stateDeflateFixedHuffman(line string) (stateFunc, error) {
292 g := &deflateGlobals
293 if line == "}" {
294 g.stream.flush()
295 return stateDeflate, nil
296 }
297
298 if line == "endOfBlock" {
299 g.stream.writeBits(0, 7)
300 return stateDeflateFixedHuffman, nil
301 }
302
303 if lit, ok := deflateParseLiteral(line); ok {
304 // TODO: support "\xAB" escape codes in the script?
305 for i := 0; i < len(lit); i++ {
306 g.stream.writeLCode(uint32(lit[i]))
307 }
308 return stateDeflateFixedHuffman, nil
309 }
310
Nigel Tao11a68192019-02-02 13:04:09 +1100311 if line == "len 3 distCode 31" {
312 lCode, lExtra, lNExtra := deflateEncodeLength(3)
313 g.stream.writeLCode(lCode)
314 g.stream.writeBits(lExtra, lNExtra)
315 dCode, dExtra, dNExtra := uint32(31), uint32(0), uint32(0)
316 g.stream.writeBits(reverse(dCode, 5), 5)
317 g.stream.writeBits(dExtra, dNExtra)
318 return stateDeflateFixedHuffman, nil
319 }
320
Nigel Tao981bf192018-05-23 12:17:55 +1000321 if l, d, ok := deflateParseLenDist(line); ok {
322 lCode, lExtra, lNExtra := deflateEncodeLength(l)
323 g.stream.writeLCode(lCode)
324 g.stream.writeBits(lExtra, lNExtra)
325 dCode, dExtra, dNExtra := deflateEncodeDistance(d)
326 g.stream.writeBits(reverse(dCode, 5), 5)
327 g.stream.writeBits(dExtra, dNExtra)
328 return stateDeflateFixedHuffman, nil
329 }
330
331 return nil, fmt.Errorf("bad stateDeflateFixedHuffman command: %q", line)
332}
333
334type deflateBitStream struct {
335 bits uint32
336 nBits uint32 // Always within [0, 7].
337}
338
339// writeBits writes the low n bits of b to z.
340func (z *deflateBitStream) writeBits(b uint32, n uint32) {
341 if n > 24 {
342 panic("writeBits: n is too large")
343 }
344 z.bits |= b << z.nBits
345 z.nBits += n
346 for z.nBits >= 8 {
347 out = append(out, uint8(z.bits))
348 z.bits >>= 8
349 z.nBits -= 8
350 }
351}
352
353func (z *deflateBitStream) writeBytes(b []byte) {
354 z.flush()
355 out = append(out, b...)
356}
357
358func (z *deflateBitStream) writeLCode(lCode uint32) {
359 switch {
360 case lCode < 144: // 0b._0011_0000 through 0b._1011_1111
361 lCode += 0x030
362 z.writeBits(reverse(lCode, 8), 8)
363 case lCode < 256: // 0b1_1001_0000 through 0b1_1111_1111
364 lCode += 0x190 - 144
365 z.writeBits(reverse(lCode, 9), 9)
366 case lCode < 280: // 0b._.000_0000 through 0b._.001_0111
367 lCode -= 256 - 0x000
368 z.writeBits(reverse(lCode, 7), 7)
369 default: // 0b._1100_0000 through 0b._1100_0111
370 lCode -= 280 - 0x0C0
371 z.writeBits(reverse(lCode, 8), 8)
372 }
373}
374
375func (z *deflateBitStream) flush() {
376 if z.nBits > 0 {
377 out = append(out, uint8(z.bits))
378 z.bits = 0
379 z.nBits = 0
380 }
381}
382
383func deflateEncodeLength(l uint32) (code uint32, extra uint32, nExtra uint32) {
384 switch {
385 case l < 3:
386 // No-op.
387 case l < 11:
388 l -= 3
389 return (l >> 0) + 257, l & 0x0000, 0
390 case l < 19:
391 l -= 11
392 return (l >> 1) + 265, l & 0x0001, 1
393 case l < 35:
394 l -= 19
395 return (l >> 2) + 269, l & 0x0003, 2
396 case l < 67:
397 l -= 35
398 return (l >> 3) + 273, l & 0x0007, 3
399 case l < 131:
400 l -= 67
401 return (l >> 4) + 277, l & 0x000F, 4
402 case l < 258:
403 l -= 131
404 return (l >> 5) + 281, l & 0x001F, 5
405 case l == 258:
406 return 285, 0, 0
407 }
408 panic(fmt.Sprintf("deflateEncodeLength: l=%d", l))
409}
410
411func deflateEncodeDistance(d uint32) (code uint32, extra uint32, nExtra uint32) {
412 switch {
413 case d < 1:
414 // No-op.
415 case d < 5:
416 d -= 1
417 return (d >> 0) + 0, d & 0x0000, 0
418 case d < 9:
419 d -= 5
420 return (d >> 1) + 4, d & 0x0001, 1
421 case d < 17:
422 d -= 9
423 return (d >> 2) + 6, d & 0x0003, 2
424 case d < 33:
425 d -= 17
426 return (d >> 3) + 8, d & 0x0007, 3
427 case d < 65:
428 d -= 33
429 return (d >> 4) + 10, d & 0x000F, 4
430 case d < 129:
431 d -= 65
432 return (d >> 5) + 12, d & 0x001F, 5
433 case d < 257:
434 d -= 129
435 return (d >> 6) + 14, d & 0x003F, 6
436 case d < 513:
437 d -= 257
438 return (d >> 7) + 16, d & 0x007F, 7
439 case d < 1025:
440 d -= 513
441 return (d >> 8) + 18, d & 0x00FF, 8
442 case d < 2049:
443 d -= 1025
444 return (d >> 9) + 20, d & 0x01FF, 9
445 case d < 4097:
446 d -= 2049
447 return (d >> 10) + 22, d & 0x03FF, 10
448 case d < 8193:
449 d -= 4097
450 return (d >> 11) + 24, d & 0x07FF, 11
451 case d < 16385:
452 d -= 8193
453 return (d >> 12) + 26, d & 0x0FFF, 12
454 case d < 32769:
455 d -= 16385
456 return (d >> 13) + 28, d & 0x1FFF, 13
457 }
458 panic(fmt.Sprintf("deflateEncodeDistance: d=%d", d))
459}
460
461func deflateParseLiteral(s string) (lit string, ok bool) {
462 const (
463 prefix = `literal "`
464 suffix = `"`
465 )
466 if strings.HasPrefix(s, prefix) {
467 s = s[len(prefix):]
468 if strings.HasSuffix(s, suffix) {
469 return s[:len(s)-len(suffix)], true
470 }
471 }
472 return "", false
473}
474
475func deflateParseLenDist(line string) (l uint32, d uint32, ok bool) {
476 const (
477 lStr = "len "
478 dStr = "dist "
479 )
480 s := line
481 if strings.HasPrefix(s, lStr) {
482 s = s[len(lStr):]
483 if l, s, ok := parseNum(s); ok && 3 <= l && l <= 258 {
484 if strings.HasPrefix(s, dStr) {
485 s = s[len(dStr):]
486 if d, s, ok := parseNum(s); ok && 1 <= d && d <= 32768 && s == "" {
487 return l, d, true
488 }
489 }
490 }
491 }
492 return 0, 0, false
493}
494
495// ----
496
497func init() {
498 formats["gif"] = stateGif
499}
500
501var gifGlobals struct {
Nigel Tao31a2bc92019-05-18 17:32:24 +1000502 imageWidth uint32
503 imageHeight uint32
504 imageBackgroundColorIndex uint32
Nigel Tao981bf192018-05-23 12:17:55 +1000505
506 frameLeft uint32
507 frameTop uint32
508 frameWidth uint32
509 frameHeight uint32
510
511 globalPalette [][4]uint8
Nigel Tao746681e2019-05-04 13:49:22 +1000512 localPalette [][4]uint8
Nigel Tao981bf192018-05-23 12:17:55 +1000513}
514
515func stateGif(line string) (stateFunc, error) {
516 const (
Nigel Taob1ff27f2019-05-12 22:00:44 +1000517 cmdB = "bytes "
518 cmdGCD = "graphicControlDuration "
519 cmdL = "lzw "
520 cmdLC = "loopCount "
Nigel Tao981bf192018-05-23 12:17:55 +1000521 )
522outer:
523 switch {
524 case line == "frame {":
Nigel Tao746681e2019-05-04 13:49:22 +1000525 gifGlobals.localPalette = nil
Nigel Tao981bf192018-05-23 12:17:55 +1000526 out = append(out, 0x2C)
527 return stateGifFrame, nil
528
529 case line == "header":
530 out = append(out, "GIF89a"...)
531 return stateGif, nil
532
533 case line == "image {":
534 return stateGifImage, nil
535
536 case line == "trailer":
537 out = append(out, 0x3B)
538 return stateGif, nil
539
Nigel Tao21f28902019-04-20 16:33:11 +1000540 case strings.HasPrefix(line, cmdB):
541 s := line[len(cmdB):]
542 for s != "" {
543 x, ok := uint32(0), false
544 x, s, ok = parseHex(s)
545 if !ok {
546 break outer
547 }
548 out = append(out, uint8(x))
549 }
550 return stateGif, nil
551
Nigel Taob1ff27f2019-05-12 22:00:44 +1000552 case strings.HasPrefix(line, cmdGCD):
553 s := line[len(cmdGCD):]
554 if !strings.HasSuffix(s, "ms") {
555 break
556 }
557 s = s[:len(s)-2]
558 duration, s, ok := parseNum(s)
559 if !ok || s != "" {
560 break
561 }
562 duration /= 10 // GIF's unit of time is 10ms.
563 out = append(out,
564 0x21, 0xF9, 0x04, 0x00,
565 uint8(duration>>0),
566 uint8(duration>>8),
567 0x00, 0x00,
568 )
569 return stateGif, nil
570
Nigel Tao981bf192018-05-23 12:17:55 +1000571 case strings.HasPrefix(line, cmdL):
572 s := line[len(cmdL):]
573 litWidth, s, ok := parseNum(s)
574 if !ok || litWidth < 2 || 8 < litWidth {
575 break
576 }
577 out = append(out, uint8(litWidth))
578
579 uncompressed := []byte(nil)
580 for s != "" {
581 x := uint32(0)
582 x, s, ok = parseHex(s)
583 if !ok {
584 break outer
585 }
586 uncompressed = append(uncompressed, uint8(x))
587 }
588
589 buf := bytes.NewBuffer(nil)
590 w := lzw.NewWriter(buf, lzw.LSB, int(litWidth))
591 if _, err := w.Write(uncompressed); err != nil {
592 return nil, err
593 }
594 if err := w.Close(); err != nil {
595 return nil, err
596 }
597 compressed := buf.Bytes()
598
599 for len(compressed) > 0 {
600 if len(compressed) <= 0xFF {
601 out = append(out, uint8(len(compressed)))
602 out = append(out, compressed...)
603 compressed = nil
604 } else {
605 out = append(out, 0xFF)
606 out = append(out, compressed[:0xFF]...)
607 compressed = compressed[0xFF:]
608 }
609 }
610 out = append(out, 0x00)
611 return stateGif, nil
Nigel Tao7be9c392018-10-13 17:00:45 +1100612
613 case strings.HasPrefix(line, cmdLC):
614 s := line[len(cmdLC):]
615 loopCount, _, ok := parseNum(s)
616 if !ok || 0xFFFF < loopCount {
617 break
618 }
619 out = append(out,
620 0x21, // Extension Introducer.
621 0xFF, // Application Extension Label.
622 0x0B, // Block Size.
623 )
624 out = append(out, "NETSCAPE2.0"...)
625 out = append(out,
626 0x03, // Block Size.
627 0x01, // Magic Number.
628 byte(loopCount),
629 byte(loopCount>>8),
630 0x00, // Block Terminator.
631 )
632 return stateGif, nil
Nigel Tao981bf192018-05-23 12:17:55 +1000633 }
634
635 return nil, fmt.Errorf("bad stateGif command: %q", line)
636}
637
638func stateGifImage(line string) (stateFunc, error) {
639 g := &gifGlobals
640 if line == "}" {
641 out = appendU16LE(out, uint16(g.imageWidth))
642 out = appendU16LE(out, uint16(g.imageHeight))
643 if g.globalPalette == nil {
644 out = append(out, 0x00)
645 } else if n := log2(uint32(len(g.globalPalette))); n < 2 || 8 < n {
646 return nil, fmt.Errorf("bad len(g.globalPalette): %d", len(g.globalPalette))
647 } else {
648 out = append(out, 0x80|uint8(n-1))
649 }
Nigel Tao31a2bc92019-05-18 17:32:24 +1000650 out = append(out, uint8(g.imageBackgroundColorIndex))
Nigel Tao981bf192018-05-23 12:17:55 +1000651 out = append(out, 0x00)
652 for _, x := range g.globalPalette {
653 out = append(out, x[0], x[1], x[2])
654 }
655 return stateGif, nil
656 }
657
658 const (
Nigel Tao31a2bc92019-05-18 17:32:24 +1000659 cmdBCI = "backgroundColorIndex "
Nigel Tao981bf192018-05-23 12:17:55 +1000660 cmdIWH = "imageWidthHeight "
661 cmdP = "palette {"
662 )
663 switch {
Nigel Tao31a2bc92019-05-18 17:32:24 +1000664 case strings.HasPrefix(line, cmdBCI):
665 s := line[len(cmdBCI):]
666 if i, _, ok := parseNum(s); ok {
667 g.imageBackgroundColorIndex = i
668 }
669 return stateGifImage, nil
670
Nigel Tao981bf192018-05-23 12:17:55 +1000671 case strings.HasPrefix(line, cmdIWH):
672 s := line[len(cmdIWH):]
673 if w, s, ok := parseNum(s); ok {
674 if h, _, ok := parseNum(s); ok {
675 g.imageWidth = w
676 g.imageHeight = h
677 return stateGifImage, nil
678 }
679 }
680
681 case strings.HasPrefix(line, cmdP):
682 return stateGifImagePalette, nil
683 }
684
685 return nil, fmt.Errorf("bad stateGifImage command: %q", line)
686}
687
688func stateGifFrame(line string) (stateFunc, error) {
689 g := &gifGlobals
690 if line == "}" {
691 out = appendU16LE(out, uint16(g.frameLeft))
692 out = appendU16LE(out, uint16(g.frameTop))
693 out = appendU16LE(out, uint16(g.frameWidth))
694 out = appendU16LE(out, uint16(g.frameHeight))
Nigel Tao746681e2019-05-04 13:49:22 +1000695 if g.localPalette == nil {
696 out = append(out, 0x00)
697 } else if n := log2(uint32(len(g.localPalette))); n < 2 || 8 < n {
698 return nil, fmt.Errorf("bad len(g.localPalette): %d", len(g.localPalette))
699 } else {
700 out = append(out, 0x80|uint8(n-1))
701 }
702 for _, x := range g.localPalette {
703 out = append(out, x[0], x[1], x[2])
704 }
Nigel Tao981bf192018-05-23 12:17:55 +1000705 return stateGif, nil
706 }
707
708 const (
709 cmdFLTWH = "frameLeftTopWidthHeight "
Nigel Tao746681e2019-05-04 13:49:22 +1000710 cmdP = "palette {"
Nigel Tao981bf192018-05-23 12:17:55 +1000711 )
712 switch {
713 case strings.HasPrefix(line, cmdFLTWH):
714 s := line[len(cmdFLTWH):]
715 if l, s, ok := parseNum(s); ok {
716 if t, s, ok := parseNum(s); ok {
717 if w, s, ok := parseNum(s); ok {
718 if h, _, ok := parseNum(s); ok {
719 g.frameLeft = l
720 g.frameTop = t
721 g.frameWidth = w
722 g.frameHeight = h
723 return stateGifFrame, nil
724 }
725 }
726 }
727 }
Nigel Tao746681e2019-05-04 13:49:22 +1000728
729 case strings.HasPrefix(line, cmdP):
730 return stateGifFramePalette, nil
Nigel Tao981bf192018-05-23 12:17:55 +1000731 }
732
733 return nil, fmt.Errorf("bad stateGifFrame command: %q", line)
734}
735
736func stateGifImagePalette(line string) (stateFunc, error) {
737 g := &gifGlobals
738 if line == "}" {
739 return stateGifImage, nil
740 }
741
742 s := line
743 if rgb0, s, ok := parseHex(s); ok {
744 if rgb1, s, ok := parseHex(s); ok {
745 if rgb2, _, ok := parseHex(s); ok {
746 g.globalPalette = append(g.globalPalette,
747 [4]uint8{uint8(rgb0), uint8(rgb1), uint8(rgb2), 0xFF})
748 return stateGifImagePalette, nil
749 }
750 }
751 }
752
753 return nil, fmt.Errorf("bad stateGifImagePalette command: %q", line)
754}
Nigel Tao746681e2019-05-04 13:49:22 +1000755
756func stateGifFramePalette(line string) (stateFunc, error) {
757 g := &gifGlobals
758 if line == "}" {
759 return stateGifFrame, nil
760 }
761
762 s := line
763 if rgb0, s, ok := parseHex(s); ok {
764 if rgb1, s, ok := parseHex(s); ok {
765 if rgb2, _, ok := parseHex(s); ok {
766 g.localPalette = append(g.localPalette,
767 [4]uint8{uint8(rgb0), uint8(rgb1), uint8(rgb2), 0xFF})
768 return stateGifFramePalette, nil
769 }
770 }
771 }
772
773 return nil, fmt.Errorf("bad stateGifFramePalette command: %q", line)
774}