diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index fd23a29e37..ce1660a912 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -668,7 +668,7 @@ private void Write1BitPixelData(Configuration configuration, Stream stre if (quantizedPixelRow.Length % 8 != 0) { - int startIdx = quantizedPixelRow.Length - 7; + int startIdx = quantizedPixelRow.Length - (quantizedPixelRow.Length % 8); endIdx = quantizedPixelRow.Length; Write1BitPalette(stream, startIdx, endIdx, quantizedPixelRow); } diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs index f486310b78..a0d91c2088 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs @@ -370,6 +370,30 @@ public void Encode_WorksWithDiscontiguousBuffers(TestImageProvider(TestImageProvider provider, BmpBitsPerPixel bitsPerPixel) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + + using var reencodedStream = new MemoryStream(); + var encoder = new BmpEncoder + { + BitsPerPixel = bitsPerPixel, + SupportTransparency = false, + Quantizer = KnownQuantizers.Octree + }; + image.SaveAsBmp(reencodedStream, encoder); + reencodedStream.Seek(0, SeekOrigin.Begin); + + using Image reencodedImage = Image.Load(reencodedStream); + + reencodedImage.DebugSave(provider); + + reencodedImage.CompareToOriginal(provider); + } + private static void TestBmpEncoderCore( TestImageProvider provider, BmpBitsPerPixel bitsPerPixel, diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 1fc964a064..8a676c02d3 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -407,6 +407,8 @@ public static class Bmp public const string Rgba321010102 = "Bmp/rgba32-1010102.bmp"; public const string RgbaAlphaBitfields = "Bmp/rgba32abf.bmp"; + public const string BlackWhitePalletDataMatrix = "Bmp/bit1datamatrix.bmp"; + public static readonly string[] BitFields = { Rgb32bfdef, diff --git a/tests/Images/Input/Bmp/bit1datamatrix.bmp b/tests/Images/Input/Bmp/bit1datamatrix.bmp new file mode 100644 index 0000000000..ee5535d112 --- /dev/null +++ b/tests/Images/Input/Bmp/bit1datamatrix.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b2288e2a059b15c7855eb141c05e3ce69431e7c3ddef851033f7fd9ca39a2d4 +size 102