jpeglibjpegprogressive

How many bytes are needed for a complete thumbnail of a progressive JPEG?


I am trying to build an uploader which uploads progressive files in two steps:

  1. Upload the minimal number of bytes to create a thumbnail (0-10%)
  2. Upload the rest of the bytes for the thumbnail. (11%-100%)

I want to do this to have thumbnails available earlier without having to upload a seperate thumbnail. Taking an image (3426398 Byte) which was created with jpegtran -copy all -progressive -outfile progressive.jpg original.jpg and then taking the first 350000 bytes with dd if=progressive.jpg of=thumb.jpg bs=1 count=350000 I got a thumbnail which works.

Is there a way to find out how many bytes I have to transfer in order to get an image where all lines are available? Thus only losing image quality?


Solution

  • A progressive JPEG image is made up of a series of "Scans". Each Scan provides progressively more information for each MCU block (more A/C coefficients). The first scan is usually just the DC values for each MCU. This happens to be the perfect amount of information to get you a 1/8th sized thumbnail image.

    In the JPEG file, each scan starts with a SOS (start of scan) marker of FFDA. In order to know how much data is needed to completely hold the first scan, simply search the file data for the start of the second scan (the second FFDA marker) and you have your answer. Because of the marker byte rules, the 2 byte sequence FF DA cannot be anything but the SOS marker. Compressed data which needs to encode the value FF followed by DA would have to be encoded as FF 00 DA.