On Mar 31, 2009, at 12:50 PM, Joe Strout wrote:
But there's no need for that. Open a socket, get the first few
bytes (for suitable values of "few"), and parse them to obtain the
desired information (image width and height). It seems very
wasteful to involve the disk here.
Joe,
There is a minor problem with your above comment. There is no set
position within a JPEG file as to where the width and height
information is located. The only absolute constant is that starting
in "position 2" you will get a two byte section type code. You must
read that section, determine if it is an x"FFCO" or x"FFC2" section.
If it is they you can obtain the width and height from within that
section. If it isn't then you have to read the next section type code
and continue doing this until you find the proper section that holds
the width and height info.
Most all other image types have this information in set locations
near the start of the file. JPEG, unfortunately, makes things a
little more difficult. So, to properly get the width and height you
must first determine that x"FFD8FFE0" resides in the first four bytes
of the file. Once that is determined a method, such as that below,
must be used to get the width and height info.
Private Sub analyseJPG()
// Get height and width values for a JPG (JPEG) image file
Dim sectionTypeCode, tries As Integer
Dim s As String
imageKind = "JPG"
tries = 0
bs.Position = 2
sectionTypeCode = bs.ReadShort
Do Until sectionTypeCode = -64 or sectionTypeCode<= -62 //
checking for &hFFC0 and &hFFC2
bs.Position = bs.Position + bs.readshort
sectionTypeCode = bs.ReadShort
tries = tries + 1
if tries > 50 then // looks like malformed JPEG file so ignore it
s = "Possible error encountered processing the JPEG file " +
f.Name _
+ ". Unable to find size section in 50 iterations."
showAlertDialog kPgmErr, s, "Okay", "", "", "a", "", 1
imageKind = "?"
return
end if
Loop
bs.position = bs.position + 3 // start of jpeg height and width
values
bs.LittleEndian = false // high byte comes first in JPEG files
imageHeight = bs.ReadShort
imageWidth = bs.ReadShort
End Sub
=== A Mac addict in Tennessee ===
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|