Fix for Jpeg image load
General-Purpose PDF Library for Java and .NET
Status: Beta
Brought to you by:
stechio
Hi,
I came accross an issue with Load of a Jpeg file, the loop to find the frame header was throwing an exception when index overflown and the seek method received a negative value.
I made some change to make it work, moreover the frame header 0xFFC0 was apparently wrong, as far as I tested the correct one is 0xFFC2, with the following two bytes discarded, one byte for color component, two bytes for height, and two bytes for width.
In the end the Load method look like:
private void Load()
{
/*
NOTE: Big-endian data expected.
*/
System.IO.Stream stream = Stream;
BigEndianBinaryReader streamReader = new BigEndianBinaryReader(stream);
int index = 4;
stream.Seek(index, SeekOrigin.Begin);
byte[] markerBytes = new byte[2];
while (true)
{
stream.Seek(index, SeekOrigin.Begin);
stream.Read(markerBytes, 0, 2);
// Frame header?
if (markerBytes[0] == 0xFF
&& markerBytes[1] == 0xC2)
{
stream.Seek(2, SeekOrigin.Current);
// Get the image bits per color component (sample precision)!
BitsPerComponent = stream.ReadByte();
// Get the image size!
Height = streamReader.ReadUInt16();
Width = streamReader.ReadUInt16();
break;
}
else if (markerBytes[1] == 0xFF)
{
index++;
}
else
{
index += 2;
}
}
}
Cheers
PJ