Browse Source

Merge pull request #319 from SixLabors/tocsoft/expanded-headers

Enable reading bmp with DIB headers > BITMAPINFOHEADER
pull/328/head
James Jackson-South 9 years ago
committed by GitHub
parent
commit
68e6897c15
  1. 24
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  2. 4
      tests/ImageSharp.Tests/TestImages.cs
  3. BIN
      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

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB

Loading…
Cancel
Save