Browse Source

enable reading bmp with larger headers

af/merge-core
Scott Williams 9 years ago
parent
commit
4d5beeec14
  1. 24
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  2. 4
      tests/ImageSharp.Tests/TestImages.cs
  3. 3
      tests/ImageSharp.Tests/TestImages/Formats/Bmp/BITMAPV5HEADER.bmp

24
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -375,11 +375,18 @@ namespace ImageSharp.Formats
// read header size
this.currentStream.Read(data, 0, BmpInfoHeader.HeaderSizeSize);
int headerSize = BitConverter.ToInt32(data, 0);
if (headerSize < BmpInfoHeader.HeaderSizeSize || headerSize > BmpInfoHeader.MaxHeaderSize)
if (headerSize < BmpInfoHeader.BitmapCoreHeaderSize)
{
throw new NotSupportedException($"This kind of bitmap files (header size $headerSize) is not supported.");
}
int skipAmmount = 0;
if (headerSize > BmpInfoHeader.MaxHeaderSize)
{
skipAmmount = headerSize - BmpInfoHeader.MaxHeaderSize;
headerSize = BmpInfoHeader.MaxHeaderSize;
}
// read the rest of the header
this.currentStream.Read(data, BmpInfoHeader.HeaderSizeSize, headerSize - BmpInfoHeader.HeaderSizeSize);
@ -388,14 +395,23 @@ namespace ImageSharp.Formats
case BmpInfoHeader.BitmapCoreHeaderSize:
this.infoHeader = this.ParseBitmapCoreHeader(data);
break;
case BmpInfoHeader.BitmapInfoHeaderSize:
this.infoHeader = this.ParseBitmapInfoHeader(data);
break;
default:
throw new NotSupportedException($"This kind of bitmap files (header size $headerSize) is not supported.");
if (headerSize > BmpInfoHeader.BitmapInfoHeaderSize)
{
this.infoHeader = this.ParseBitmapInfoHeader(data);
break;
}
else
{
throw new NotSupportedException($"This kind of bitmap files (header size $headerSize) is not supported.");
}
}
// skip the remaining header because we can't read those parts
this.currentStream.Skip(skipAmmount);
}
/// <summary>

4
tests/ImageSharp.Tests/TestImages.cs

@ -117,7 +117,9 @@ namespace ImageSharp.Tests
public const string F = "Bmp/F.bmp";
public const string NegHeight = "Bmp/neg_height.bmp";
public const string CoreHeader = "Bmp/BitmapCoreHeaderQR.bmp";
public static readonly string[] All = { Car, F, NegHeight, CoreHeader };
public const string V5Header = "Bmp/BITMAPV5HEADER.bmp";
public static readonly string[] All = { Car, F, NegHeight, CoreHeader, V5Header };
}
public static class Gif

3
tests/ImageSharp.Tests/TestImages/Formats/Bmp/BITMAPV5HEADER.bmp

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:9d99f80a05e0ddb6309c84846dfac7d2ef4e9cdda6ed437646bc0edfee7d3130
size 174026
Loading…
Cancel
Save