realbasic-nug
[Top] [All Lists]

Re: Getting image data

To: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Subject: Re: Getting image data
From: Harrie Westphal <harriew@frontiernet.net>
Date: Tue, 31 Mar 2009 14:22:10 -0500
Authentication-results: mx.google.com; spf=neutral (google.com: 74.124.194.228 is neither permitted nor denied by best guess record for domain of realbasic-nug-bounces@lists.realsoftware.com) smtp.mail=realbasic-nug-bounces@lists.realsoftware.com
Delivered-to: listarchive@realsoftware.com
In-reply-to: <49D257D6.5020307@inspiringapps.com>
References: <3753b0ed0903310421h6e2ea82dg8e21f6d054e123d3@mail.gmail.com> <49D22A47.608@inspiringapps.com> <3753b0ed0903310747n228c7763j33890fc403829be6@mail.gmail.com> <55534378039663975067776974302513501206-Webmail@me.com> <3753b0ed0903310937k7e79084by5f9e777ffef381d@mail.gmail.com> <22102D0B-170C-4E67-9F92-AA4B56DCD631@tolisgroup.com> <49D257D6.5020307@inspiringapps.com>
Reply-to: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Sender: realbasic-nug-bounces@lists.realsoftware.com

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>


<Prev in Thread] Current Thread [Next in Thread>