I have a few different JPEG images I’ve been testing with. As far as I’ve seen the 0th and first bytes are always 0xFF and 0xD8.
The second and third are usually either 0xFF and 0xE0 ( APP0 ) indicating either a JFIF segment or JFIF extension segment or 0xFF and 0xE1 ( APP1 ) indicating an EXIF segment.
My question: is this always the case? Are the 2nd and 3rd bytes always APP0 or APP1?
No. There are e.g. several cameras that create JPEGs without these markers, or with other APP markers. The only thing you can rely on is the SOI sequence,
FF D8, not even EOI is produced by all cameras. Also be aware that JPEGs with embedded JPEGs exist – you can have nested SOI/EOI within an image.If you need to deal with embedded JPEG data in raw camera images, several models produce JPEG-like data that can only be parsed by being a bit slack with the jpeg spec – especially in relation to escaped
FFbytes in data. And then you have cameras that produce proprietary data that at first glance looks like jpeg data (e.g. some of Sony’s “encrypted” raw formats)