diff --git a/script/make-artificial.go b/script/make-artificial.go index cc8452ee..575d0c08 100644 --- a/script/make-artificial.go +++ b/script/make-artificial.go @@ -26,7 +26,9 @@ package main import ( "bytes" "compress/lzw" + "compress/zlib" "fmt" + "hash/crc32" "io" "os" "strconv" @@ -138,6 +140,10 @@ func appendU16LE(b []byte, u uint16) []byte { return append(b, uint8(u), uint8(u>>8)) } +func appendU32BE(b []byte, u uint32) []byte { + return append(b, uint8(u>>24), uint8(u>>16), uint8(u>>8), uint8(u)) +} + func log2(u uint32) (i int32) { for i, pow := uint32(0), uint32(1); i < 32; i, pow = i+1, pow<<1 { if u == pow { @@ -1220,3 +1226,106 @@ func stateGifFramePalette(line string) (stateFunc, error) { return nil, fmt.Errorf("bad stateGifFramePalette command: %q", line) } + +// ---- + +func init() { + formats["png"] = statePng +} + +var pngGlobals struct { + scratch [4]byte + + animSeqNum uint32 + chunkData bytes.Buffer + chunkType string + zlibWriter *zlib.Writer +} + +func statePng(line string) (stateFunc, error) { + g := &pngGlobals + switch { + case line == "": + return statePng, nil + + case line == "magic": + out = append(out, "\x89PNG\x0D\x0A\x1A\x0A"...) + return statePng, nil + + case (len(line) == 6) && (line[4] == ' ') && (line[5] == '{'): + g.chunkData.Reset() + g.chunkType = line[:4] + return statePngChunk, nil + } + + return nil, fmt.Errorf("bad statePng command: %q", line) +} + +func statePngChunk(line string) (stateFunc, error) { + g := &pngGlobals + if line == "}" { + if n := g.chunkData.Len(); n > 0xFFFF_FFFF { + return nil, fmt.Errorf("chunkData is too long") + } + out = appendU32BE(out, uint32(g.chunkData.Len())) + n := len(out) + out = append(out, g.chunkType...) + out = append(out, g.chunkData.Bytes()...) + out = appendU32BE(out, crc32.ChecksumIEEE(out[n:])) + return statePng, nil + } + + switch { + case line == "animSeqNum++": + g.chunkData.Write(appendU32BE(g.scratch[:0], g.animSeqNum)) + g.animSeqNum++ + return statePngChunk, nil + + case line == "raw {": + return statePngRaw, nil + + case line == "zlib {": + g.zlibWriter = zlib.NewWriter(&g.chunkData) + return statePngZlib, nil + } + + return nil, fmt.Errorf("bad statePngChunk command: %q", line) +} + +func statePngRaw(line string) (stateFunc, error) { + g := &pngGlobals + if line == "}" { + return statePngChunk, nil + } + + for s := line; s != ""; { + if x, remaining, ok := parseHex(s); ok { + g.chunkData.WriteByte(byte(x)) + s = remaining + } else { + return nil, fmt.Errorf("bad statePngRaw command: %q", line) + } + } + return statePngRaw, nil +} + +func statePngZlib(line string) (stateFunc, error) { + g := &pngGlobals + if line == "}" { + if err := g.zlibWriter.Close(); err != nil { + return nil, fmt.Errorf("statePngZlib: %w", err) + } + return statePngChunk, nil + } + + for s := line; s != ""; { + if x, remaining, ok := parseHex(s); ok { + g.scratch[0] = byte(x) + g.zlibWriter.Write(g.scratch[:1]) + s = remaining + } else { + return nil, fmt.Errorf("bad statePngZlib command: %q", line) + } + } + return statePngZlib, nil +} diff --git a/test/data/artificial/png-apng-skip-idat.png b/test/data/artificial/png-apng-skip-idat.png new file mode 100644 index 00000000..6e47cbe1 Binary files /dev/null and b/test/data/artificial/png-apng-skip-idat.png differ diff --git a/test/data/artificial/png-apng-skip-idat.png.make-artificial.txt b/test/data/artificial/png-apng-skip-idat.png.make-artificial.txt new file mode 100644 index 00000000..b7fc4098 --- /dev/null +++ b/test/data/artificial/png-apng-skip-idat.png.make-artificial.txt @@ -0,0 +1,87 @@ +# Feed this file to script/make-artificial.go + +# This APNG image has its first fcTL chunk after the IDAT chunks, so that the +# IDAT data (the "default image") is not displayed. See +# https://wiki.mozilla.org/APNG_Specification + +make png + +magic + +IHDR { + raw { + # Width, height. + 0x00 0x00 0x00 0x03 + 0x00 0x00 0x00 0x02 + # Depth, color, compression, filter, interlace. + 0x08 0x02 0x00 0x00 0x00 + } +} + +acTL { + raw { + # Frame count. + 0x00 0x00 0x00 0x02 + # Loop count. + 0x00 0x00 0x00 0x00 + } +} + +IDAT { + zlib { + # 3x2 RGB pixels (with filter bytes), shades of red. + 0x00 0x20 0x00 0x00 0x50 0x00 0x00 0x80 0x00 0x00 + 0x00 0x40 0x00 0x00 0xA0 0x00 0x00 0xFF 0x00 0x00 + } +} + +fcTL { + animSeqNum++ + raw { + # Width, height, left, top. + 0x00 0x00 0x00 0x03 + 0x00 0x00 0x00 0x02 + 0x00 0x00 0x00 0x00 + 0x00 0x00 0x00 0x00 + # Duration (1/1 second). + 0x00 0x01 0x00 0x01 + # Disposal, blend. + 0x00 0x00 + } +} + +fdAT { + animSeqNum++ + zlib { + # 3x2 RGB pixels (with filter bytes), shades of cyan. + 0x00 0x00 0x20 0x20 0x00 0x50 0x50 0x00 0x80 0x80 + 0x00 0x00 0x40 0x40 0x00 0xA0 0xA0 0x00 0xFF 0xFF + } +} + +fcTL { + animSeqNum++ + raw { + # Width, height, left, top. + 0x00 0x00 0x00 0x01 + 0x00 0x00 0x00 0x02 + 0x00 0x00 0x00 0x02 + 0x00 0x00 0x00 0x00 + # Duration (1/1 second). + 0x00 0x01 0x00 0x01 + # Disposal, blend. + 0x00 0x00 + } +} + +fdAT { + animSeqNum++ + zlib { + # 1x2 RGB pixels (with filter bytes), shades of gray. + 0x00 0x80 0x80 0x80 + 0x00 0xFF 0xFF 0xFF + } +} + +IEND { +}