diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index e962d6df6..39bab442f 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -371,35 +371,36 @@ namespace SixLabors.ImageSharp.Formats.Bmp padding = 4 - padding; } - byte[] row = new byte[arrayWidth + padding]; - var color = default(TPixel); - - var rgba = default(Rgba32); - - for (int y = 0; y < height; y++) + using (var row = Buffer.CreateClean(arrayWidth + padding)) { - int newY = Invert(y, height, inverted); - this.currentStream.Read(row, 0, row.Length); - int offset = 0; - Span pixelRow = pixels.GetRowSpan(newY); + var color = default(TPixel); + var rgba = new Rgba32(0, 0, 0, 255); - // TODO: Could use PixelOperations here! - for (int x = 0; x < arrayWidth; x++) + for (int y = 0; y < height; y++) { - int colOffset = x * ppb; + int newY = Invert(y, height, inverted); + this.currentStream.Read(row.Array, 0, row.Length); + int offset = 0; + Span pixelRow = pixels.GetRowSpan(newY); - for (int shift = 0; shift < ppb && (x + shift) < width; shift++) + // TODO: Could use PixelOperations here! + for (int x = 0; x < arrayWidth; x++) { - int colorIndex = ((row[offset] >> (8 - bits - (shift * bits))) & mask) * 4; - int newX = colOffset + shift; + int colOffset = x * ppb; - // Stored in b-> g-> r order. - rgba.Bgr = Unsafe.As(ref colors[colorIndex]); - color.PackFromRgba32(rgba); - pixelRow[newX] = color; - } + for (int shift = 0; shift < ppb && (x + shift) < width; shift++) + { + int colorIndex = ((row[offset] >> (8 - bits - (shift * bits))) & mask) * 4; + int newX = colOffset + shift; - offset++; + // Stored in b-> g-> r order. + rgba.Bgr = Unsafe.As(ref colors[colorIndex]); + color.PackFromRgba32(rgba); + pixelRow[newX] = color; + } + + offset++; + } } } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 948f519d4..4a0d248e4 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -135,8 +135,10 @@ namespace SixLabors.ImageSharp.Tests public const string CoreHeader = "Bmp/BitmapCoreHeaderQR.bmp"; public const string V5Header = "Bmp/BITMAPV5HEADER.bmp"; public const string RLE = "Bmp/RunLengthEncoded.bmp"; + public const string Bit8 = "Bmp/test8.bmp"; + public const string Bit8Inverted = "Bmp/test8-inverted.bmp"; - public static readonly string[] All = { Car, F, NegHeight, CoreHeader, V5Header, RLE }; + public static readonly string[] All = { Car, F, NegHeight, CoreHeader, V5Header, RLE, Bit8, Bit8Inverted }; } public static class Gif diff --git a/tests/Images/Input/Bmp/test8-inverted.bmp b/tests/Images/Input/Bmp/test8-inverted.bmp new file mode 100644 index 000000000..4ec260b66 --- /dev/null +++ b/tests/Images/Input/Bmp/test8-inverted.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:407c948e576330f603b3692c3d37248f8df31067f79e424dba6757066c97845e +size 9270 diff --git a/tests/Images/Input/Bmp/test8.bmp b/tests/Images/Input/Bmp/test8.bmp new file mode 100644 index 000000000..157c96879 --- /dev/null +++ b/tests/Images/Input/Bmp/test8.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3551fbb385b6d7d54e2f0372f79e5701ca9215e4efdddb429ec5d7942367107f +size 9270