Search code examples
imagejpeghead

Calculate and varify JPG dimension and filesize only by markers?


Suppose I read a data stream and don't have the image as it is to work with it. And also don't know where in this stream of the image ends. So I can't just count the bytes between the start and end of the image.

As far as I have been able to research there are markers in the JPG file which can tell me the height and width (in pixels) of that image. For example I looked through multiple JPG files with a hex editor and found multiple SOF0 (0xFF 0xC0) marker. But there are multiple of these in one file. Sometimes the very first one had the width and height which I could read but sometimes when there are 3 to 4 of these markers the very first one had a smaller dimension.

Are those other SOF0 markers thumbnails? If yes, why are there more than two if there is one for the full dimension and one for the thumbnail? Is there a way to verify that this marker actually contains the dimension of the full picture? And do all SOFn markers contain width and height information?

And: is there a way to calculate the filesize of the whole picture, again using only the markers given?


Solution

  • The image dimensions are in the Start of Frame marker (SOFn). You have to check of for the different variants of SOF but the structure is the same.

    There is only one SOF marker per image but there could be a thumbnail as you guessed.

    All SOF markets contain the width and height.

    Multiply width, height, image depth and component count to get the size in bits.