diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 108006ade..3dcbf4075 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/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); } /// diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index da27ab081..764d768ae 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/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 diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Bmp/BITMAPV5HEADER.bmp b/tests/ImageSharp.Tests/TestImages/Formats/Bmp/BITMAPV5HEADER.bmp new file mode 100644 index 000000000..1ab56bb00 --- /dev/null +++ b/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