TIP 632: return gif animated metadata

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:		Harald Oehlmann <harald.oehlmann@elmicron.de>
State:		Final
Type:		Project
Vote:		Done
Created:	12-Aug-2022
Tcl-Version:	8.7
Vote:	Done
Vote-Summary:   Accepted 6/0/0
Votes-For:      MC, BG, SL, JN, FV, KW 
Votes-Against:  none
Votes-Present:  none
Tk-Ticket:	f285ddcd23
Tk-Branch:	rfe-f285ddcd-animated-gif-metadata
Keywords:	tk image

Abstract

Return the animation parameters of an animated GIF within the image metadata.

Rationale

Tk provides the possibility to get one image of an animated image sequence as follows:

$image configure -format "gif -index $index"

Nevertheless, the parameters for animated gif display are not returned. This TIP proposes to return those parameters to be able to display the animated gif with the help of TCL code.

Please refer to this stack overflow question and answer: https://stackoverflow.com/questions/72486189/tcl-tk-animated-gif-not-decoded-correctly

Specification

If the gif file indexed image is loaded to a tcl image, the following metadata keys are set if the relevant data is present:

  • delay time: returns the delay time in unit "10ms". This is only present, if delay time is given and greater 0.
  • disposal method: one of do not dispose, restore to background color, restore to previous. This key is not present if no disposal method specified.
  • user interaction: value 1, if specified as required. This key is not present, if no user interaction required.
  • update region: 4 numbers in pixel unit: X0, Y0, width, height. This is only given, if the update box is not identical to the whole image.

The key and value names are taken from the GIF89a standard document.

Example

The example solution given in the stack overflow ticket by Schelte Bron may be modified as follows:

proc nextFrame {image {index 0}} {
    if {[catch {tmpimg configure -format "gif -index $index"} stderr]} {
        set nextIndex 0
        set time 1
    } else {
        set nextIndex [expr {$index + 1}]
        set metadata [tmpimg cget -metadata]
        if {    [dict exists $metadata "disposal method"]
                && [dict get $metadata "disposal method"] eq "do not dispose"
        } {
            $image copy tmpimg -compositingrule overlay
        } else {
            $image copy tmpimg -compositingrule set
        }
        if {[dict exists $metadata "delay time"]} {
            set time [expr {[dict get $metadata "delay time"]*10}]
        } else {
            set time 1
        }
    }
    after $time nextFrame $image $nextIndex
}

set img [image create photo -file [file join $dir animated.gif]]
# Create a helper image
image create photo tmpimg -file [$img cget -file]

label .w -image $img
pack .w

nextFrame $img

Now, the values for delay time and disposal mehtod (partly) are used instead of heuristic values.

Implementation

The implementation is available in the given Tk branch.

Discussion

Discussion took place at the RFE Tk ticket: https://core.tcl-lang.org/tk/tktview?name=f285ddcd23.

Copyright

This document has been placed in the public domain.

History