How to Parse JPEG Header????

  • Thread starter Thread starter Ryan Cooper
  • Start date Start date
R

Ryan Cooper

Can anyone give me an idea or better yet, a code sample, demonstrating how
to parse a JPEG header and search the header for markers located in the
stream?

Basically, I need to analyze an uploaded JPEG file, search for the markers
C2, C6 and CA in the stream, and then set a variable of some sort to specify
whether or not these markers are present. (The reason I need these is to
determine whether the JPEG is in "Progressive" or "Baseline" format and
perform additional processing on it.) I have been told this should be easy,
but I don't quite know where to start on this. Any example in VB or C# would
be greatly appreciated!

Thanks in advance!

Ryan
 
Ryan said:
Can anyone give me an idea or better yet, a code sample,
demonstrating how to parse a JPEG header and search the header for
markers located in the stream?

Basically, I need to analyze an uploaded JPEG file, search for the
markers C2, C6 and CA in the stream, and then set a variable of some
sort to specify whether or not these markers are present. (The reason
I need these is to determine whether the JPEG is in "Progressive" or
"Baseline" format and perform additional processing on it.) I have
been told this should be easy, but I don't quite know where to start
on this. Any example in VB or C# would be greatly appreciated!

Thanks in advance!

http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=UTF-8&q=parse+jpeg+file+format+VB
--- and ---
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&q=parse+jpeg+file+format+VB&sa=N&tab=wg

HTH

-- Mark
 
Hi Fergus;
Thanks for the link to those files.... Definitely more helpful that the
last guy's reply with nothing but a google query.... lol... as if I havent
already searched google... I've been working on this problem on and off for
months... and as far as I have gotten is to be told it can be done.

However, the info in the files you referred me to is for the most part
information I already have. I have figured out the placement of the JPEG
Markers for progressive encoding determination.... But what I really need is
some direction as to how to actually parse this header information in VB.NET
into an array or string of some kind, and then to search for the presence of
these markers. I get the basic idea of opening streamreaders, using the
system.io class, etc. but I get confused about how to put it all together.
Anyways, if you have any idea how to search the JPEG header for specific
markers, I appreciate any direction you can give me.

Thanks again.
Ryan
 
Ryan Cooper said:
Can anyone give me an idea or better yet, a code sample, demonstrating how
to parse a JPEG header and search the header for markers located in the
stream?

Maybe something here... they have C source code.

http://www.ijg.org/

-- Alan
 
Ryan said:
Hi Fergus;
Thanks for the link to those files.... Definitely more helpful
that the last guy's reply with nothing but a google query.... lol...
as if I havent already searched google... I've been working on this
problem on and off for months... and as far as I have gotten is to be
told it can be done.

;-) wotsit.org was the 4th entry on the google search, and VB source code
was all over the first page of the groups.google return.

-- Mark
 
Uh Mark, not to rain on your parade or sound ungrateful, but replying with a
google search just doesn't seem appropriate for a newsgroup like this. I
mean, anybody with a basic knowledge of the internet can search google, and
I guess I just find it a little frustrating when I get a reply like this,
because it doesn't really serve to answer my question but is more like a
"RTFM" comment. I appreciate the fact that you replied, but it seems to me
that your reply wasn't much more than an attempt to increase the number of
threads you had posted on, and not really a genuine attempt to answer my
question. Maybe some people don't know about search engines, but I doubt
that they would be asking about parsing file headers in .net...!

I understand that the link that Fergus posted was in your list, but at least
he seemed to make a genuine attempt to give me relevant info, even if I
already know all about the jpeg specification and the markers and so forth.
Maybe I'm wrong, but I'd think most people would rather get one decent lead
than 100 vague search results they had already seen.

I was looking for help from someone with actual .NET programming knowledge,
specifically with the ability to give me some direction on how to search a
header's byte stream for a particular marker. Again, I'm not trying to start
a flame war here, I just feel like taking my subject line and doing a google
search does not really provide a valuable informational reply, and it's just
a little insulting to an experienced programmer and web user to be given a
google search in reply to a very specific programming question. I don't
know, maybe I'm just taking it personally, but I wanted to say this because
you took the time to reply to my reply, and I want you to understand where
I'm coming from.

Thanks anyway,
Ryan
 
Ryan said:
Uh Mark, not to rain on your parade or sound ungrateful, but replying
with a google search just doesn't seem appropriate for a newsgroup
like this. I mean, anybody with a basic knowledge of the internet can
search google, and I guess I just find it a little frustrating when I
get a reply like this, because it doesn't really serve to answer my
question but is more like a "RTFM" comment. I appreciate the fact
that you replied, but it seems to me that your reply wasn't much more
than an attempt to increase the number of threads you had posted on,
and not really a genuine attempt to answer my question. Maybe some
people don't know about search engines, but I doubt that they would
be asking about parsing file headers in .net...!

Sorry I wasted your time... <g>

I've introduced a number of people to internet searching, especially
effective use of Google's groups. "Thanks! I didn't know that" is not an
uncommon reply.

If your original post had stated you'd googled and google grouped without
success, I obviously wouldn't have posted my "hay foot, straw foot" reply.

Best of luck figuring out your problem.

Now for something potentially helpful: ISTR needing this JPEG info some
time ago in a VB3 or VB5/6 project. I didn't have to bit fiddle, though, as
it was a property of the imaging component I was using. If you're still
stuck in (choose a timeframe) an inexpensive 3rd party component could be
the trick. I don't recall the specific vendor; it was an employer or two
ago, and I had several imaging packages.

-- Mark
 
Thanks Mark for the thoughtful reply. I understand your point and I guess I
can't generalize too much... there are those who post to newsgroups as a
first resort, I just tend to do so as a last resort after the googling has
run dry. In this case I have googled until I was red in the fingers and
still haven't found a sufficiently detailed explanation to really answer my
needs.

Regarding your suggestion, I HAVE actually contacted several vendors of
imaging related com objects (including SoftArtisans ImgWriter, etc.), and
they all are able to set the progressive property, but none have been able
to actually detect/read it. For some reason all the third party objects
make the progressive encoding property write-only. This is fine if I want to
re-save an image as baseline, but who wants to recompress an already
compressed jpeg if they don't have to (e.g. if it was baseline to begin
with...)? The re-compressed image (even if saved at 100 quality) ends up at
lower visual quality with a considerably larger file size.

I would think this problem would have a solution out there somewhere
considering that flash movies (.swf's) can not display progressive JPG's,
meaning that there is NO WAY to allow user-uploaded images to display in a
flash UI with any kind of proper validation on the upload to ensure the
image is baseline. Agh!

Check it out.,.. this PHP code is supposed to do the trick... but I
absolutely refuse to run php on my server and deal with yet another
programming environment, just for this one feature... it has to be possible
with .net... it just has to:

<php code>
$fp = @fopen( $filename, "rb" );
$imageData = fread( $fp, 100000 );
@fclose( $fp );

function checkImage($imageData) {
if ( strstr( $imageData, pack( "n", 0xFFC2 ) ) ) {
return false;
}
if ( strstr( $imageData, pack( "n", 0xFFC6 ) ) ) {
return false;
}
if ( strstr( $imageData, pack( "n", 0xFFCA ) ) ) {
return false;
}
return true;
}
</php code>
 
I am not familiar with header format. That said if you have issue with
searching through stream of bytes, which I expect you get from BinaryReader
or similar, you might want to consider using Regex if offsets are floating
and you use some specific search patterns.
As Regex support hexadecimal notation you only need to ensure that string
with header information will not translate original bytes.

You can either use standard byte to char loop (e.g. with StringBuilder) to
build string from header stream e.g.
foreach (byte b in data) {
sb.Append((char)b);
}

or Encoding for iso-8859-1 - thanks Jon Skeet! like


sb.Append(System.Text.Encoding.GetEncoding(28591).GetString(br.ReadBytes((in
t)fs.Length)));

sb - StringBuilder, br - BinaryReader for fs - FileStream.

HTH
Alex
 
Ryan said:
Can anyone give me an idea or better yet, a code sample,
demonstrating how to parse a JPEG header and search the header for
markers located in the stream?

Basically, I need to analyze an uploaded JPEG file, search for the
markers C2, C6 and CA in the stream, and then set a variable of some
sort to specify whether or not these markers are present. (The reason
I need these is to determine whether the JPEG is in "Progressive" or
"Baseline" format and perform additional processing on it.) I have
been told this should be easy, but I don't quite know where to start
on this. Any example in VB or C# would be greatly appreciated!

Thanks in advance!

Ryan

Have you made any progress?

-- Mark
 
Back
Top