From cce1bf93d4dec21e2a54e4a83791d1a8e3cbf1e9 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 12 Oct 2017 09:48:45 +1100 Subject: [PATCH] Fix alpha channel and add inverted test --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 45 ++++++++++--------- tests/ImageSharp.Tests/TestImages.cs | 4 +- tests/Images/Input/Bmp/test8-inverted.bmp | Bin 0 -> 9270 bytes tests/Images/Input/Bmp/test8.bmp | Bin 0 -> 9270 bytes 4 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 tests/Images/Input/Bmp/test8-inverted.bmp create mode 100644 tests/Images/Input/Bmp/test8.bmp diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index e962d6df6f..39bab442f7 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 948f519d4c..4a0d248e44 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 0000000000000000000000000000000000000000..b0909ae6e5a7dcfc7482797ff25dde5fe1105219 GIT binary patch literal 9270 zcmeI&jaOx37zOY>gb+dq;Ua_(k`QHt(2$Q1LI@#*5HdmtA%qY@K0*i~WP}hx2qAr6nRPEec5Dv)Rm?$;rtXh@N^O zH1)9X_o%2SL`O#>CME{4v9XAYi$i>TJQ5NT5DJBmn3#y9q$DIKCnF^#1*xg2NKa2k zMn(p*va*n!osFEF9OUNaA}=ow`T6-MC@4T-VIhi&icnl!jFOTPl$MsFtgH;><>jcT zs6b_9C90~bP+eV(nwlEa*4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