Search code examples
imageimage-processingjpegjfif

Get width and height from jpeg without 0xFF 0xC0


I'm trying to get the file dimensions (width and height) from a jpeg (in this case an Instagram picture)

https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/11264864_1701024620182742_1335691074_n.jpg

As I understand if, the width and height are defined after the 0xFF 0xC0 marker, however I cannot find this marker in this picture. Has it been stripped or is there an alternative marker I should check for?


Solution

  • The JPEG Start-Of-Frame (SOF) marker has 4 possible values:

    • FFC0 (baseline) - This is the usual mode chosen for photos and encodes fully specified DCT blocks in groupings depending on the color/subsample options chosen
    • FFC1 (extended) - This is similar to baseline, but has more than 8-bits per color stimulus
    • FFC2 (progressive) - This mode is often found on web pages to allow the image to load progressively as the data is received. Each "scan" of the image progressively defines more coefficients of the DCT blocks until they're fully defined. This effectively provides more and more detail as more scans are decoded
    • FFC3 (lossless) - This mode uses a simple Huffman encoding to losslessly encode the image. The only place I've seen this used is on 16-bit grayscale DICOM medical images

    If you're scanning through the bytes looking for an FFCx pattern, be aware that you may encounter one in the embedded thumbnail image (inside an FFE1 Exif marker). To properly find the SOFx of the main image, you'll need to walk the chain of JPEG markers. The 2-byte length (big-endian) follows the 2-byte marker. One last pitfall to avoid is that some JPEG encoders stick extra FF values in between the valid markers. If you encounter a FFFF where a marker should be, just increment the pointer by 1 byte and try again until you hit a valid marker.