6

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)
mafu
  • 31,798
  • 42
  • 154
  • 247
  • 1
    The length of a comment is specified the same way as the length of the other JPEG metadata. It is not possible to skip the length because it's part of the JPEG tag structure. I'm not sure if a JPEG file is considered valid if it's missing the image data; it depends on the specific software author if it would get rejected or not. – BitBank Jul 03 '13 at 17:40
  • @BitBank Thanks, could you elaborate on that a bit more (how is the length specified), and add it as an answer? – mafu Jul 04 '13 at 09:30

2 Answers2

5

JPEG metadata is stored in a tag structure as follows:

0xFF - tag introducer
0xXX - tag value
0xXX 0xXX - tag length in big-endian order including the length of the length (2)
< tag data (length-2 bytes)>

This structure requires that each tag can contain a maximum of 65534 bytes of metadata. For larger structures, a true length value is stored within the tag data and multiple tags contain the entire structure.

An example of a comment tag. It includes a zero terminator, but this is not required.

FF FE 00 08 48 45 4C 4C 4F 00 - "HELLO"

BitBank
  • 8,500
  • 3
  • 28
  • 46
2
  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.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • 1
    As for software interpreting it, I tried the file `FF D8 FF E0 00 10 4A 46 49 46 00 01 02 00 02 80 02 7F 00 00 FF FE 00 08 48 65 6C 6C 6F 21 FF D9` is a bunch of programs, and none of them took it in any way. Many gave a generic error, some said it wasn't a valid JPEG, and ImageMagick's `identify` said that there was insufficient image data. It'd be interesting to try one with a table in it too to see if that changes anything. – Ethan Chapman Oct 21 '20 at 16:39
  • @EthanChapman Actually, JFIF format add some additional constraints that I'm sure your file does not fulfil. `FFD8FFFE 00084865 6C6C6F21 FFD9` should be all you need. ;-) – Harald K Oct 22 '20 at 07:51