I have been having problems with certain images refusing to resize. The Resizer was apparently failing due to a file stream already closed exception. I have traced this back to the JFIF Decoder not looking for the markers found in my jpeg images and also experiencing an int16 overflow.
I have experimented with an amended JFIF decoder and that seems to overcome my problems. I thought that I would share...
```
private static Size DecodeJfif(BinaryReader binaryReader)
{
while (binaryReader.ReadByte() == byte.MaxValue)
{
var marker = binaryReader.ReadByte();
var chunkLength = (ushort)binaryReader.ReadLittleEndianInt16();
if (marker == 0xc0 || marker == 0xc2)
{
binaryReader.ReadByte();
var height = binaryReader.ReadLittleEndianInt16();
var width = binaryReader.ReadLittleEndianInt16();
return new Size(width, height);
}
binaryReader.ReadBytes(chunkLength - 2);
}
throw new ArgumentException("Could not recognise image format.");
}
```
The differences are minor and there are only three of them:
1. The type for chunkLength has been changed from int to ushort
1. Using the binaryReader's little endian read method directly
1. Checking the marker for both 0xC0 and 0xC2 values
I am not an image processing expert so there may be unexpected traps with this modification, but it does seem to have solved the problems I was experiencing.
Comments: Fix verified and checked into HQ trunk for 4.2. Next code sync to CodePlex should have this fix.
I have experimented with an amended JFIF decoder and that seems to overcome my problems. I thought that I would share...
```
private static Size DecodeJfif(BinaryReader binaryReader)
{
while (binaryReader.ReadByte() == byte.MaxValue)
{
var marker = binaryReader.ReadByte();
var chunkLength = (ushort)binaryReader.ReadLittleEndianInt16();
if (marker == 0xc0 || marker == 0xc2)
{
binaryReader.ReadByte();
var height = binaryReader.ReadLittleEndianInt16();
var width = binaryReader.ReadLittleEndianInt16();
return new Size(width, height);
}
binaryReader.ReadBytes(chunkLength - 2);
}
throw new ArgumentException("Could not recognise image format.");
}
```
The differences are minor and there are only three of them:
1. The type for chunkLength has been changed from int to ushort
1. Using the binaryReader's little endian read method directly
1. Checking the marker for both 0xC0 and 0xC2 values
I am not an image processing expert so there may be unexpected traps with this modification, but it does seem to have solved the problems I was experiencing.
Comments: Fix verified and checked into HQ trunk for 4.2. Next code sync to CodePlex should have this fix.