Search code examples
headerjpegfile-format

How do text comments in JPG files work?


JPG files can contain text comments via the FF FE marker. I have a few questions about this:

  1. How do I specify the length of the comment? Is it possible to not specify the length at all, if the comment is at the end of the file?

  2. Is it possible to have a valid jpg file without an image that only consists of a comment? How would such a file look like in binary? I'm assuming it would be:

FF D8    - SOI: start of image (note that no frame data follow)
FF D9    - EOI: end of image
FF FE    - COM: text comment
(binary) - (text)

Solution

    1. Most JPEG segments contains a 2 byte marker (0xFFFE in the case of COM), followed by the segment length (2 bytes). See JPEG syntax and structure (Wikipedia) for more details. You must specify the length field for the COM marker.

    2. It is valid to have a tables only (only DHT and DQT segments) JPEG, with no image data. I don't think a no tables nor image data one is valid, but at least you don't need the image data. Not sure how useful it is, or how most JPEG software would interpret it...

      The use case for a tables only JPEG, is to use it with "abbreviated streams", (JPEG with only image data, no tables), to share common tables between multiple images.