Subject: Re: Reading binary data from a file (jpg) - DN [1]


"Odeen" <odeen95@hotmail.com> - 27 Apr 2000 - comp.lang.tcl

 "Joe" <Joe@supply.nospamm.com> wrote in message
 news:4nregs82pejs75ncjjvrmf75lnftfot1dq@4ax.com...
 > I am trying to pull the height and width information out of a jpeg.
 > The data can be at a variable offset in the file but is always in the
 > same position with regards to the start of frame zero. The start of
 > frame zero is marked by the two byte code "FF C0". My plan was to scan
 > through the header until I found the marker then seek ahead six bytes
 > to the height & width data. Unfortunately I can't for the life of me
 > figure out how to search for the start of frame marker. I have tried
 > all sorts of stuff with read and binary scan but can not get it to
 > work. Can anyone help?
 >
 > Thanks,
 > Joe

 The following code should do what you want.  Note that the second
 byte of the marker can be anywhere from c0 to c3.  Also note that
 this method is not foolproof, because these bytes may be present
 in previous headers.  To do it correctly, you need to traverse through
 the various headers one by one.  See the JPEG FAQ, question 22,
 for more info and some links to C/Perl implementations :

 http://www.faqs.org/faqs/jpeg-faq/part1/

 proc GetJPGDimensions { filename } {
    set fd [ open $filename ]
    fconfigure $fd -translation binary
    set ch1 "00"
    while { ! [ eof $fd ] } {
       binary scan [ read $fd 1 ] "H2" ch2
       if { ( $ch1 == "ff" ) && ( $ch2 >= "c0" ) && ($ch2 <= "c3" ) } {
          binary scan [ read $fd 7 ] "x3SS" height width
          return [ list $height $width ]
       }
       set ch1 $ch2
    }
    error "Couldn't find JPG header for $filename"
 }

Last modified
2000-07-20

(195.108.246.52)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated