Reputation: 146
I have a JPEG image which is JFIF formatted. I want to decode it and get the dimension.
Here is my code:
while (binaryReader.ReadByte() == 0xff)
{
byte marker = binaryReader.ReadByte();
ushort chunkLength = binaryReader.ReadLittleEndianInt16();
if (marker == 0xc0)
{
binaryReader.ReadByte();
int height = binaryReader.ReadLittleEndianInt16();
int width = binaryReader.ReadLittleEndianInt16();
return new Size(width, height);
}
binaryReader.ReadBytes(chunkLength - 2);
}
Ok. This piece of code is common and you can find it all over the internet. It works fine for most of the JPEG images.
Now, this specific image which was taken by the camera - "Canon EOS 300D DIGITAL", does not support this piece of code. The marker for the dimension is 0xFFC2 instead of 0xFFC0.
My question is which one is correct? If the code is correct, then how can a Canon camera produce a non-standard image? If the Canon camera is correct, then how can we fix the code to correct find the dimension of this image?
Thanks.
Upvotes: 1
Views: 5905
Reputation: 29536
FFC2 seems to be the marker for progressive images.
please see for example http://en.wikipedia.org/wiki/JPEG which explains what "progressive" format is (see "JPEG compression" section).
yes, i think you can change your if statement to check for both 0xc0 (SOF0 marker) and 0xc2 (SOF2 marker) because they seem to have similar structure (see "syntax and structure" section). see also here: http://fjcore.googlecode.com/svn/trunk/FJCore/Decoder/JpegDecoder.cs
i am not expert in JPEG formats, so you might want to check with specialised forums if you are developing a mission critical code.
Upvotes: 3