30

I am sending many images from my server to clients in sequence, continuously through TCP.

Now at the client, how should I detect efficiently that this is the end of one image so I can save it to the file system and then process the next image and so on?

4
  • "Sending" using what protocol? Doesn't your protocol know when a file ends automatically?
    – Pekka
    Jan 3, 2011 at 15:05
  • 3
    Why not send length of the image beforehand?
    – Kornel
    Jan 3, 2011 at 15:06
  • 2
    This question has a general value - what if it's not about a protocol you have control over, but for example about recovering data from a serialization format nobody has the code to open, which happens to contain embedded JPGs? "Use a protocol" is not the way to "Detect EOF in JPG images" Jul 12, 2018 at 13:34
  • In my case I am using an Arduino Uno (2K RAM) to copy data from an OV5642 camera to an ethernet shield. It does not provide an accurate length field, so you have to scan the bytes as they come through.
    – Mutant Bob
    Mar 22, 2022 at 13:24

3 Answers 3

45

Well, there's no guarantee that you won't find FFD9 within a jpeg image. The best way you can find the end of a jpeg image is to parse it. Every marker, except for FFD0 to FFD9 and FF01(reserved), is immediately followed by a length specifier that will give you the length of that marker segment, including the length specifier but not the marker. FF00 is not a marker, but for your purposes you can treat it as marker without a length specifier.

The length specifier is two bytes long and it's big endian. So what you'll do is search for FF, and if the following byte is not one of 0x00, 0x01 or 0xD0-0xD8, you read the length specifier and skips forward in the stream as long as the length specifier says minus two bytes.

Also, every marker can be padded in the beginning with any number of FF's.

When you get to FFD9 you're at the end of the stream.

Of course you could read the stream word by word, searching for FF if you want performance but that's left as an exercise for the reader. ;-)

6
  • 2
    I should clarify the first sentence. It's obvious that FFD9 is at the end of an image. But FFD9 could appear embedded within a JPEG image without being an EOI marker. That's why you have to parse the JPEG to find the EOI marker.
    – onemasse
    May 8, 2015 at 8:32
  • 9
    Summarized: Read 0xFF. Read marker. Read the length specifier L and skip forward by L - 2 bytes. After an SOS (0xFFDA) segment (followed by compressed data) skip forward to the first 0xFF not followed by 0x00 or 0xD0-0xD8. Repeat from start until you encounter 0xFFD9. Works on this multi-scan JPEG. Jun 18, 2015 at 16:18
  • 4
    This approach is vulnerable to corrupt images (either files that were cut by accident or maliciously engineered fake images)
    – Alex Cohn
    Nov 2, 2017 at 9:58
  • 3
    @Alex Well, the point is, you can't just search sequentially for FFD9 and expect to find the end of the jpeg. My approach is no different from a regular jpeg decoder so if that is vulnerable so is every jpeg decoder out there. Of course you should be aware of how big your buffer is and check so that you don't try to read outside of it.
    – onemasse
    Aug 12, 2018 at 12:53
  • To the best of my knowledge, in the scan section of a jpeg (the actual data), every FF byte (representing a pixel, not a marker) is followed by a 00. To avoid exactly the issue of a conflict. For example, in a sample jpeg i just opened, an FFD9 was represented as FF 00 D9. So no, there's a guarantee that you're not going to find an FFD9 in the actual "pixel" section. Posting for confused people, as me.
    – PauLEffect
    Mar 18, 2021 at 9:33
21

A quick look at Wikipedia's JPEG article would have given you the answer:

  • bytes 0xFF, 0xD8 indicate start of image
  • bytes 0xFF, 0xD9 indicate end of image
3
  • 12
    Important to note that there can be JPEGs embedded in JPEGs (the thumbnail image for example) so you might see SOI SOI EOI EOI markers. Make sure to take that into consideration.
    – NG.
    Jan 3, 2011 at 15:15
  • @SB: true. A foolproof way would be to scan for EOI SOI markers in sequence if reading from stream isn't finished, and for EOI if it's finished.
    – darioo
    Jan 3, 2011 at 15:17
  • @NG. Thumbnails (to use your example) are contained within segments, which house a length field - you can use that to not check within the segment and prevent premature detection of an EOI.
    – IAmJersh
    Oct 16, 2018 at 15:56
2

If you're sending the images via a byte array then you can simply add the file size of the image as a pair of bytes before the start of the file.
Client grabs the first two bytes to find specified number of bytes (we'll call that x) and discards them, then pumps the next x number of bytes into a buffer which it can write to file.
Rinse and repeat for all following jpegs.

An alternative is just looking for the FFD9 marker - if I'm not mistaken any compressed value FF will be encoded as FF00 (the 00 byte is discarded and the FF byte is kept).
The problem with this is that you get things like thumbnails with their own FFD9 headers, but those are contained within a segment in the headers. Those segments have a length value in the two bytes after their marker so you can just skip to the end of any segment you encounter to avoid premature eoi detection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.