Martin Hennig
Martin Hennig

Reputation: 551

reading multiple jpg images from one file

I have a file that contains multiple jpg images. I want to read them one by one into buffers to decode them seperately. I dont know if there is any different information saved in the file other than the image headers and the image data.

A jpg image starts with the SOI byte and ends with the EOI byte. Can I assume that these bytes only appear at the start and the end of an image? Then I would try to read unsigned char pieces into a buffer and only check if I found SOI or EOI.

Would that work? (Still in the planning phase, therefore no code yet...Will be in c or c++ though)

Upvotes: 0

Views: 425

Answers (1)

mark
mark

Reputation: 5459

Markers are actually two octets, 0xFF plus the actual marker code. SOI is 0xFF,0xD8 and EOI is 0xFF,0xD9. Additionally, the standard says when you're encoding image data, if you encode a 0xFF octet (i.e. it's not a marker) then you need to follow it with a 0x00 so that decoders/scanners don't think it's a marker (this is called byte stuffing). Anyway, given this, you should be fine scanning for SOI and EOI markers.

Edit: You may find images that have embedded thumbnails... in which case you might need to keep count of the markers... e.g. 0xFF,0xD8,...0xFF,0xD8,...0xFF,D9...0xFF,D9 would be a single image with an embedded thumbnail.

Upvotes: 3

Related Questions