Reading BMP image dimensions

Keith Vetter : 2006-09-06


 proc bmpsize {fname} {
    set fin [open $fname r]
    fconfigure $fin -translation binary
    set sig [read $fin 2]
    if {$sig ne "BM"} {
        close $fin
        error "$fname is not a BMP file"
    }
    seek $fin 18                                ;# Past 4 integer header fields
    binary scan [read $fin 4] i width
    binary scan [read $fin 4] i height
    close $fin

    return [list $width $height]
 }

AMG: The width and height are stored at offset 18, not 14. At 14 is the DIB header size, which is normally 40 bytes [L1 ]. I updated the above code accordingly.

See also