Version 3 of Reading GIF image dimensions

Updated 2002-01-11 22:25:23

From a news:comp.lang.tcl posting by Berry Kercheval < mailto:[email protected] >:

And this works for simple GIF files:

 proc gifsize {name} {
    set f [open $name r]
    fconfigure $f -translation binary
    # read GIF signature -- check that this is 
    # either GIF87a or GIF89a 
    set sig [read $f 6]
    switch $sig {
        "GIF87a" -
        "GIF89a" {
            # do nothing
        default {
            close $f
            error "$f is not a GIF file"
    # read "logical screen size", this is USUALLY the image size too.
    # interpreting the rest of the GIF specification is left as an exercise
    binary scan [read $f 2] s wid
    binary scan [read $f 2] s hgt
    close $f

    return [list $wid $hgt]

It leaked channels. Added some missing closes 11-jan-2002, Dave Griffin

See also Reading JPEG image dimensions and Reading PNG image dimensions. Category Graphics