From 2e14f0fc6d2a709c1e0b58bc9de19afb07cb9865 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 9 Jun 2018 07:39:11 +1000 Subject: [PATCH] Decode a GIF's transparent index --- gen/c/std/gif.c | 26 ++++++++++++++++++++++++-- std/gif/decode_gif.wuffs | 16 ++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index c853f62b..cf814c30 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -3569,6 +3569,27 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( v_i += 1; } } + if (self->private_impl.f_gc_has_transparent_index) { + if (!v_use_local_palette) { + wuffs_base__slice_u8__copy_from_slice( + ((wuffs_base__slice_u8){.ptr = self->private_impl.f_palettes[1], + .len = 1024}), + ((wuffs_base__slice_u8){.ptr = self->private_impl.f_palettes[0], + .len = 1024})); + } + self->private_impl.f_palettes[1][( + (4 * ((uint32_t)(self->private_impl.f_gc_transparent_index))) + 0)] = + 0; + self->private_impl.f_palettes[1][( + (4 * ((uint32_t)(self->private_impl.f_gc_transparent_index))) + 1)] = + 0; + self->private_impl.f_palettes[1][( + (4 * ((uint32_t)(self->private_impl.f_gc_transparent_index))) + 2)] = + 0; + self->private_impl.f_palettes[1][( + (4 * ((uint32_t)(self->private_impl.f_gc_transparent_index))) + 3)] = + 0; + } if (self->private_impl.f_previous_lzw_decode_ended_abruptly) { (memset(&self->private_impl.f_lzw, 0, sizeof((wuffs_lzw__decoder){})), wuffs_lzw__decoder__check_wuffs_version(&self->private_impl.f_lzw, @@ -3681,7 +3702,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( } } v_palette = ((wuffs_base__slice_u8){}); - if (v_use_local_palette) { + if (v_use_local_palette || self->private_impl.f_gc_has_transparent_index) { v_palette = ((wuffs_base__slice_u8){ .ptr = self->private_impl.f_palettes[1], .len = 1024}); } else if (!self->private_impl.f_previous_use_global_palette) { @@ -3692,7 +3713,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( self->private_impl.f_gc_duration, true, self->private_impl.f_gc_disposal, v_palette); - self->private_impl.f_previous_use_global_palette = !v_use_local_palette; + self->private_impl.f_previous_use_global_palette = + !(v_use_local_palette || self->private_impl.f_gc_has_transparent_index); 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; diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 81032667..7092bb52 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -424,6 +424,18 @@ pri func decoder.decode_id_part1?(dst ptr base.image_buffer, src base.io_reader) } } + if this.gc_has_transparent_index { + if not use_local_palette { + this.palettes[1][:].copy_from_slice(s:this.palettes[0][:]) + } + + // Set the gc_transparent_index palette entry to transparent black. + this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 0] = 0x00 + this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 1] = 0x00 + this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 2] = 0x00 + this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 3] = 0x00 + } + // Other GIF implementations accept GIF files that aren't completely spec // compliant. For example, the test/data/gifplayer-muybridge.gif file // (created by the gifsicle program) is accepted by other GIF decoders. @@ -498,7 +510,7 @@ pri func decoder.decode_id_part1?(dst ptr base.image_buffer, src base.io_reader) // TODO: check for "not enough pixel data". var palette slice base.u8 - if use_local_palette { + if use_local_palette or this.gc_has_transparent_index { palette = this.palettes[1][:] } else if not this.previous_use_global_palette { // This frame uses the global palette but the previous one didn't. @@ -510,7 +522,7 @@ pri func decoder.decode_id_part1?(dst ptr base.image_buffer, src base.io_reader) in.dst.update!(dirty_rect:this.frame_rect, duration:this.gc_duration, blend:true, disposal:this.gc_disposal, palette:palette) - this.previous_use_global_palette = not use_local_palette + this.previous_use_global_palette = not (use_local_palette or this.gc_has_transparent_index) // The Image Descriptor is mandatory, but the Graphic Control extension is // optional. Reset the GC related fields for the next decode_frame call.