Version 2 of Reading JPEG comments

Updated 2003-12-18 02:51:52

proc read_jpg_comment {file} {

    set fh [open $file r]
    fconfigure $fh -encoding binary -translation binary -eofchar {}
    if {[read $fh 2] != "\xFF\xD8"} { close $fh; return }

    set c 0
    while {$c != "\xDA"} {
        while {[read $fh 1] != "\xFF" && ![eof $fh]} {}
        while {[set c [read $fh 1]] == "\xFF"} {}
        binary scan [read $fh 2] S len
        if {$c == "\xFE"} {
            set com [read $fh [expr {$len - 2}]]
            close $fh
            return $com
        } elseif {$c == "" || $len < 2} {
            break
        } else {
            seek $fh [expr {$len - 2}] current
        }
    }
    close $fh
 }

--AF


Category Graphics