Data, Maps, Usability, and Performance

Detecting Progressive JPEG

Last updated on October 7, 2012 in Development

detect progressive jpeg

How do you detect a progressive jpeg? I have been searching for a way to easily check if a JPG image is progressive or not without installing extra libraries (like imageMagick) and I did not find many results. It would be ideal if progressive JPEGs just had a different mime type or something you can quickly retrieve via bash (file -mime-type p.jpg gives me p.jpg: image/jpeg) but this type of information is not easily available. I did find a php script to determine if a jpeg is progressive, C# code to detect if a JPEG is progressive, and other scripts that leverage third-party libraries, as well as an image metadata extractor written in Java, but I really just want something that I can run in bash or in JavaScript.

Learning more about the JPEG format makes me realize that this information can be grabbed by just running some regex on the binary data. A progressive DCT-based JPEG can be identified by bytes “0xFF, 0xC2″ so that should be enough. Also, Progressive JPEG images usually contain multiple scans so I would expect a couple “Start of Scan” matches (bytes: “0xFF, 0xDA”). So, I did some testing in bash and got accurate results:

>grep -c -P “xffxc2″ progressive1.jpg

>grep -c -P “xffxc2″ progressive2.jpg

>grep -c -P “xffxc2″ baseline1.jpg

>grep -c -P “xffxc2″ baseline2.jpg

>grep -c -P “xffxDA” progressive1.jpg

>grep -c -P “xffxDA” progressive2.jpg

Now, to do the same thing in JavaScript, I just need to grab binary data from an image and use regular expressions. I could use the HTML5 File API to get this and do a JavaScript match on the binary image code. Check out the JPEG progressive image detection test. After doing some more research, I found a JavaScript library for parsing Jpeg file meta data which gives you all the information you need about your JPEG files. You can check out the blog post about this script as well as the demo. In terms of getting Exif data in general, the following blog posts from nihilogic and flickr are excellent.

Tags: ,

Facebook Twitter Hacker News Reddit More...