Reputation: 1173
There is a question with the same title but unfortunatly it doesn't help me.
I am trying to parse the data of a SOS marker. All documentations I can find say that after the marker (0xFFDA
) a two byte number follows which defines how long this segment is -- for example here -- as with most variable size markers. But I seem not to understand it correctly in this case. It works for every other marker type.
I checked multiple files but just can't get this right. Is this number not defining how long the complete SOS field is? So for a baseline JPEG there should be exaclty one SOS segment and after this should the End Of Image marker follow. If it is progressive there can be multiple SOS segments but still all should have a length field.
I have one picture with a SOF0
Marker so it should be baseline. I believe that this is the correct SOFn
marker because the image resolution can be found after that marker. With a hex editor I have found 3 0xFFDA
marker and all of them have 0x000C
in the following 2 bytes. So that segment, as I understand it, should always be 12 Byte long. But in all 3 cases no new marker is following after 12 byte of data. I guess the very last one is the scan I am looking for because if the value 0xFF
comes up it is followed by 0x00
-- except the reset markers.
Are those two byte following 0xFFDA
not the length fields?
EDIT: So thanks to the comments and answer there seems to be no length field for the actual compressed data and only way to know where it ends seems to be decoding it.
Why does a Baseline DCT Image have multiple scans? I would understand why it has two; the main image and a thumbnail, but what is the third scan?
But there is one more thing. According to DRI Marker (Define Restart Interval) it contains the value after which a Scan should have a restart marker 0xFFD0 - 0xFFD7
. But I seem to misunderstand that too or I'm not doing it right. For example a marker contained the value 0x0140
as restart interval. In the following Scan I started from the beginning and searched for the first 0xFFD0
but it came after 862 bytes instead of 320.
Upvotes: 17
Views: 18740
Reputation: 2036
Summary of how to find next marker after SOS marker (0xFFDA):
FFxx
marker (skip every FF00
and range from FFD0
to FFD7
because they are part of scan).*This is summary of comments below post of user3344003 + my knowledge + Table B.1 from https://www.w3.org/Graphics/JPEG/itu-t81.pdf.
*Basing on Table B.1 I can also suspect that values FF01
and FF02
through FFBF
should also be skipped in point 2 but I am not sure if they cannot appear as part of encoded SOS data.
Additional question above:
Why does a Baseline DCT Image have multiple scans? I would understand why it has two; the main image and a thumbnail, but what is the third scan?
If image stream contains APP2 marker (0xFFE2), that tells us it can be (but not have to be) Multi Picture JPEG (tag named MPF), we can have more than 3 images. In general APP markers can store anything, there are a lot of standards related to APP segments in JPEG files.
First table tells us about "things" that can be stored in each APP segment:
https://exiftool.org/TagNames/JPEG.html
Upvotes: 5
Reputation: 21627
The SOS marker contains the compressed data; the most complex part of the JPEG stream. The SOFn marker indicates the format of the data. SOF0 and SOF1 are processed identically. SOF2 (progressive) is quite a bit different. (The read of the SOFn markers are not in common use or commonly supported).
The length is that of the SOS header, not the compressed data. Most of the header is only applicable to progressive scans (SOF2).
The compressed data follows the header. The compressed data has no length. You have to scan through the data to find the next marker.
Upvotes: 5