From 76c967c35311a1eee01f3d8601e252f6824c637f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 14 Aug 2018 10:40:33 +0100 Subject: [PATCH] Fix 1 bit bmp decoding and add extra test images. --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 4 +--- .../Formats/Bmp/BmpDecoderTests.cs | 5 +---- tests/ImageSharp.Tests/TestImages.cs | 20 ++++++++++++++++++- tests/Images/Input/Bmp/pal1.bmp | 3 +++ tests/Images/Input/Bmp/pal1p1.bmp | 3 +++ tests/Images/Input/Bmp/pal4.bmp | 3 +++ 6 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 tests/Images/Input/Bmp/pal1.bmp create mode 100644 tests/Images/Input/Bmp/pal1p1.bmp create mode 100644 tests/Images/Input/Bmp/pal4.bmp diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 128ae0854..d67beb036 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -373,11 +373,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp for (int x = 0; x < arrayWidth; x++) { int colOffset = x * ppb; - - for (int shift = 0; shift < ppb && (x + shift) < width; shift++) + for (int shift = 0, newX = colOffset; shift < ppb && newX < width; shift++, newX++) { int colorIndex = ((rowSpan[offset] >> (8 - bits - (shift * bits))) & mask) * 4; - int newX = colOffset + shift; // Stored in b-> g-> r order. rgba.Bgr = Unsafe.As(ref colors[colorIndex]); diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index 09c3d1545..5f2de9f51 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -17,10 +17,7 @@ namespace SixLabors.ImageSharp.Tests { public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector; - public static readonly string[] AllBmpFiles = - { - Car, F, NegHeight, CoreHeader, V5Header, RLE, RLEInverted, Bit8, Bit8Inverted, Bit16, Bit16Inverted - }; + public static readonly string[] AllBmpFiles = All; public static readonly TheoryData RatioFiles = new TheoryData diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 142b923ed..5eb70117e 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -165,12 +165,30 @@ namespace SixLabors.ImageSharp.Tests public const string V5Header = "Bmp/BITMAPV5HEADER.bmp"; public const string RLE = "Bmp/RunLengthEncoded.bmp"; public const string RLEInverted = "Bmp/RunLengthEncoded-inverted.bmp"; + public const string Bit1 = "Bmp/pal1.bmp"; + public const string Bit1Pal1 = "Bmp/pal1p1.bmp"; + public const string Bit4 = "Bmp/pal4.bmp"; public const string Bit8 = "Bmp/test8.bmp"; public const string Bit8Inverted = "Bmp/test8-inverted.bmp"; public const string Bit16 = "Bmp/test16.bmp"; public const string Bit16Inverted = "Bmp/test16-inverted.bmp"; - public static readonly string[] All = { Car, F, NegHeight, CoreHeader, V5Header, RLE, RLEInverted, Bit8, Bit8Inverted, Bit16, Bit16Inverted }; + public static readonly string[] All + = { + Car, + F, + NegHeight, + CoreHeader, + V5Header, RLE, + RLEInverted, + Bit1, + Bit1Pal1, + Bit4, + Bit8, + Bit8Inverted, + Bit16, + Bit16Inverted + }; } public static class Gif diff --git a/tests/Images/Input/Bmp/pal1.bmp b/tests/Images/Input/Bmp/pal1.bmp new file mode 100644 index 000000000..fbf770c50 --- /dev/null +++ b/tests/Images/Input/Bmp/pal1.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c631861fa4c4e959d2abc6e0db0106253989b0101223cb08020e116e19c97f77 +size 1086 diff --git a/tests/Images/Input/Bmp/pal1p1.bmp b/tests/Images/Input/Bmp/pal1p1.bmp new file mode 100644 index 000000000..0898a2d28 --- /dev/null +++ b/tests/Images/Input/Bmp/pal1p1.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:620eb83715dd99a640b6c61f5337c2f01fd19246d4759a2bc0480b8ee960bf63 +size 1082 diff --git a/tests/Images/Input/Bmp/pal4.bmp b/tests/Images/Input/Bmp/pal4.bmp new file mode 100644 index 000000000..1c039e6cf --- /dev/null +++ b/tests/Images/Input/Bmp/pal4.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:01f7bfaaf5110a404fb68451a860970750c155e5c46e1721fa6b9c500be0ca2c +size 4198