2020-06-08 03:29:51 +03:00
|
|
|
package gif
|
|
|
|
|
|
|
|
// https://www.w3.org/Graphics/GIF/spec-gif87.txt
|
|
|
|
// https://en.wikipedia.org/wiki/GIF
|
|
|
|
// https://web.archive.org/web/20160304075538/http://qalle.net/gif89a.php#graphiccontrolextension
|
|
|
|
|
|
|
|
// TODO: local color map
|
|
|
|
// TODO: bit depth done correct?
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/format/registry"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-11-17 18:46:10 +03:00
|
|
|
registry.MustRegister(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.GIF,
|
|
|
|
Description: "Graphics Interchange Format",
|
|
|
|
Groups: []string{format.PROBE, format.IMAGE},
|
|
|
|
DecodeFn: gifDecode,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
extensionPlainText = 0x01
|
|
|
|
extensionGraphicalControl = 0xf9
|
|
|
|
extensionComment = 0xfe
|
|
|
|
extensionApplication = 0xff
|
|
|
|
)
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var extensionNames = scalar.UToSymStr{
|
2020-06-08 03:29:51 +03:00
|
|
|
extensionPlainText: "PlainText",
|
|
|
|
extensionGraphicalControl: "GraphicalControl",
|
|
|
|
extensionComment: "Comment",
|
|
|
|
extensionApplication: "Application",
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldColorMap(d *decode.D, name string, bitDepth int) {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray(name, func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for i := 0; i < 1<<bitDepth; i++ {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("color", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU8("r")
|
|
|
|
d.FieldU8("g")
|
|
|
|
d.FieldU8("b")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func gifDecode(d *decode.D, in any) any {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.Endian = decode.LittleEndian
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldUTF8("header", 6, d.AssertStr("GIF87a", "GIF89a"))
|
2021-10-19 02:53:37 +03:00
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU16("width")
|
|
|
|
d.FieldU16("height")
|
|
|
|
gcpFollows := d.FieldBool("gcp_follows")
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldUFn("color_resolution", func(d *decode.D) uint64 { return d.U3() + 1 })
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU1("zero")
|
2021-11-05 17:04:26 +03:00
|
|
|
bitDepth := d.FieldUFn("bit_depth", func(d *decode.D) uint64 { return d.U3() + 1 })
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU8("black_color")
|
|
|
|
d.FieldU8("pixel_aspect_ratio")
|
|
|
|
|
|
|
|
if gcpFollows {
|
|
|
|
fieldColorMap(d, "global_color_map", int(bitDepth))
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("blocks", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
blocks:
|
|
|
|
for {
|
|
|
|
switch d.PeekBits(8) {
|
2022-01-24 23:21:48 +03:00
|
|
|
case ';':
|
2020-06-08 03:29:51 +03:00
|
|
|
break blocks
|
2022-01-24 23:21:48 +03:00
|
|
|
case '!': /* "!" */
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("extension_block", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU8("introducer")
|
2022-05-07 13:46:34 +03:00
|
|
|
functionCode := d.FieldU8("function_code", extensionNames, scalar.ActualHex)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
dataBytes := &bytes.Buffer{}
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("func_data_bytes", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
seenTerminator := false
|
|
|
|
for !seenTerminator {
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("func_data_byte", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
byteCount := d.FieldU8("byte_count")
|
2021-11-05 17:04:26 +03:00
|
|
|
b := d.FieldRawLen("data", int64(byteCount*8))
|
2020-06-08 03:29:51 +03:00
|
|
|
if d.PeekBits(8) == 0 {
|
|
|
|
d.FieldU8("terminator")
|
|
|
|
seenTerminator = true
|
|
|
|
}
|
2022-06-30 13:13:36 +03:00
|
|
|
d.CopyBits(dataBytes, d.CloneReadSeeker(b))
|
2020-06-08 03:29:51 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
_ = functionCode
|
|
|
|
|
|
|
|
// TODO: need a FieldStructBitBuf or something
|
|
|
|
// switch functionCode {
|
|
|
|
// case extensionGraphicalControl:
|
2021-08-16 13:46:59 +03:00
|
|
|
// d.FieldFormatBitBuf(
|
2020-06-08 03:29:51 +03:00
|
|
|
// "graphics_control",
|
2022-01-24 23:21:48 +03:00
|
|
|
// bitio.NewReader(dataBytes.Bytes(), -1),
|
2020-06-08 03:29:51 +03:00
|
|
|
// )
|
|
|
|
|
|
|
|
// }
|
|
|
|
})
|
2022-01-24 23:21:48 +03:00
|
|
|
case ',':
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("image", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU8("separator_character")
|
|
|
|
d.FieldU16("left")
|
|
|
|
d.FieldU16("top")
|
|
|
|
d.FieldU16("width")
|
|
|
|
d.FieldU16("height")
|
|
|
|
|
|
|
|
localFollows := d.FieldBool("local_color_map_follows")
|
|
|
|
d.FieldBool("image_interlaced")
|
|
|
|
d.FieldU3("zero")
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldUFn("bit_depth", func(d *decode.D) uint64 { return d.U3() + 1 })
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU8("code_size")
|
|
|
|
|
|
|
|
if localFollows {
|
|
|
|
fieldColorMap(d, "local_color_map", int(bitDepth))
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("image_bytes", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
seenTerminator := false
|
|
|
|
for !seenTerminator {
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("func_data_byte", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
byteCount := d.FieldU8("byte_count")
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("data", int64(byteCount*8))
|
2020-06-08 03:29:51 +03:00
|
|
|
if d.PeekBits(8) == 0 {
|
|
|
|
d.FieldU8("terminator")
|
|
|
|
seenTerminator = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-11-16 19:11:26 +03:00
|
|
|
default:
|
2021-11-17 18:26:13 +03:00
|
|
|
d.Fatalf("unknown block")
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
d.FieldU8("terminator")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|