Decode multiple graphic controls in a GIF

This commit is contained in:
Nigel Tao committed 2019-05-12 22:02:39 +10:00
1 parent 5bea867f72
commit b1ff27fded
6 files changed
+89 -16

No files matched your search

-7
View File
@@ -3425,7 +3425,6 @@ struct wuffs_gif__decoder__struct {
uint8_t f_interlace;
bool f_seen_num_loops;
uint32_t f_num_loops;
bool f_seen_graphic_control;
bool f_gc_has_transparent_index;
uint8_t f_gc_transparent_index;
uint8_t f_gc_disposal;
@@ -9376,7 +9375,6 @@ exit:
static wuffs_base__empty_struct //
wuffs_gif__decoder__reset_gc(wuffs_gif__decoder* self) {
self->private_impl.f_call_sequence = 5;
self->private_impl.f_seen_graphic_control = false;
self->private_impl.f_gc_has_transparent_index = false;
self->private_impl.f_gc_transparent_index = 0;
self->private_impl.f_gc_disposal = 0;
@@ -10256,10 +10254,6 @@ wuffs_gif__decoder__decode_gc(wuffs_gif__decoder* self,
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_seen_graphic_control) {
status = wuffs_gif__error__bad_graphic_control;
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {
@@ -10344,7 +10338,6 @@ wuffs_gif__decoder__decode_gc(wuffs_gif__decoder* self,
status = wuffs_gif__error__bad_graphic_control;
goto exit;
}
self->private_impl.f_seen_graphic_control = true;
goto ok;
ok:
+23 -3
View File
@@ -513,9 +513,10 @@ var gifGlobals struct {
func stateGif(line string) (stateFunc, error) {
const (
cmdB = "bytes "
cmdL = "lzw "
cmdLC = "loopCount "
cmdB = "bytes "
cmdGCD = "graphicControlDuration "
cmdL = "lzw "
cmdLC = "loopCount "
)
outer:
switch {
@@ -547,6 +548,25 @@ outer:
}
return stateGif, nil
case strings.HasPrefix(line, cmdGCD):
s := line[len(cmdGCD):]
if !strings.HasSuffix(s, "ms") {
break
}
s = s[:len(s)-2]
duration, s, ok := parseNum(s)
if !ok || s != "" {
break
}
duration /= 10 // GIF's unit of time is 10ms.
out = append(out,
0x21, 0xF9, 0x04, 0x00,
uint8(duration>>0),
uint8(duration>>8),
0x00, 0x00,
)
return stateGif, nil
case strings.HasPrefix(line, cmdL):
s := line[len(cmdL):]
litWidth, s, ok := parseNum(s)
-6
View File
@@ -146,7 +146,6 @@ pub struct decoder?(
seen_num_loops base.bool,
num_loops base.u32,
seen_graphic_control base.bool,
gc_has_transparent_index base.bool,
gc_transparent_index base.u8,
gc_disposal base.u8,
@@ -414,7 +413,6 @@ pri func decoder.reset_gc!() {
this.call_sequence = 5
// The Image Descriptor is mandatory, but the Graphic Control extension is
// optional. Reset the GC related fields for the next decode_frame call.
this.seen_graphic_control = false
this.gc_has_transparent_index = false
this.gc_transparent_index = 0
this.gc_disposal = 0
@@ -681,9 +679,6 @@ pri func decoder.decode_gc?(src base.io_reader) {
var flags base.u8
var gc_duration_centiseconds base.u16
if this.seen_graphic_control {
return "#bad graphic control"
}
c = args.src.read_u8?()
if c <> 4 {
return "#bad graphic control"
@@ -719,7 +714,6 @@ pri func decoder.decode_gc?(src base.io_reader) {
if c <> 0 {
return "#bad graphic control"
}
this.seen_graphic_control = true
}
// decode_id_partX reads an Image Descriptor. The Image Separator byte has
+35
View File
@@ -1245,6 +1245,40 @@ const char* test_wuffs_gif_decode_missing_two_src_bytes() {
src, 0, wuffs_base__suspension__short_read, false);
}
const char* test_wuffs_gif_decode_multiple_graphic_controls() {
CHECK_FOCUS(__func__);
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
.data = global_src_slice,
});
const char* status =
read_file(&src, "test/data/artificial/gif-multiple-graphic-controls.gif");
if (status) {
return status;
}
wuffs_gif__decoder dec;
status = wuffs_gif__decoder__initialize(
&dec, sizeof dec, WUFFS_VERSION,
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED);
if (status) {
RETURN_FAIL("initialize: \"%s\"", status);
}
wuffs_base__frame_config fc = ((wuffs_base__frame_config){});
wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src);
status = wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader);
if (status) {
RETURN_FAIL("decode_frame_config: \"%s\"", status);
}
int got = wuffs_base__frame_config__duration(&fc) /
WUFFS_BASE__FLICKS_PER_MILLISECOND;
int want = 300;
if (got != want) {
RETURN_FAIL("duration: got %d, want %d", got, want);
}
return NULL;
}
const char* test_wuffs_gif_decode_multiple_loop_counts() {
CHECK_FOCUS(__func__);
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
@@ -2025,6 +2059,7 @@ proc tests[] = {
test_wuffs_gif_decode_input_is_a_png, //
test_wuffs_gif_decode_metadata, //
test_wuffs_gif_decode_missing_two_src_bytes, //
test_wuffs_gif_decode_multiple_graphic_controls, //
test_wuffs_gif_decode_multiple_loop_counts, //
test_wuffs_gif_decode_pixel_data_none, //
test_wuffs_gif_decode_pixel_data_not_enough, //
Binary file not shown.

After

Width:  |  Height:  |  Size: 56 B

@@ -0,0 +1,31 @@
# Feed this file to script/make-artificial.go
# This GIF image contains multiple "Graphic Control Extensions" for a frame.
#
# The GIF89a specification says that "at most one Graphic Control Extension may
# precede a graphic rendering block", but in practice, some encoders emit more
# than one, and decoders ignore all but the last one.
make gif
header
image {
imageWidthHeight 1 1
palette {
0x00 0x00 0xFF
0x11 0x00 0xFF
0x22 0x00 0xFF
0x33 0x00 0xFF
}
}
graphicControlDuration 200ms
graphicControlDuration 300ms
frame {
frameLeftTopWidthHeight 0 0 1 1
}
lzw 2 0x00
trailer