title: Parsing JPEG SOS Marker
body: <p>There is a <a href="https://stackoverflow.com/questions/8511326/parsing-jpeg-file-sos-marker">question with the same title</a> but unfortunatly it doesn't help me. </p>
  
  <p>I am trying to parse the data of a SOS marker. All documentations I can find say that after the marker (<code>0xFFDA</code>) a two byte number follows which defines how long this segment is -- <a href="http://www.videotechnology.com/jpeg/j1.html" rel="noreferrer">for example here</a> -- as with most variable size markers. But I seem not to understand it correctly in this case. It works for every other marker type.</p>
  
  <p>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. </p>
  
  <p>I have one picture with a <code>SOF0</code> Marker so it should be baseline. I believe that this is the correct <code>SOFn</code> marker because the image resolution can be found after that marker. With a hex editor I have found 3 <code>0xFFDA</code> marker and all of them have <code>0x000C</code> 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 <code>0xFF</code> comes up it is followed by <code>0x00</code> -- except the reset markers.</p>
  
  <p>Are those two byte following <code>0xFFDA</code> not the length fields?</p>
  
  <p><strong>EDIT:</strong>
  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. </p>
  
  <p>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?</p>
  
  <p>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 <code>0xFFD0 - 0xFFD7</code>. But I seem to misunderstand that too or I'm not doing it right. For example a marker contained the value <code>0x0140</code> as restart interval. In the following Scan I started from the beginning and searched for the first <code>0xFFD0</code> but it came after 862 bytes instead of 320. </p>