Browse Source

Merge pull request #2471 from synercoder/fixes/bit1-pallet-bmp-bug

Fix #2467 bmp encoding issue for BMP with 1 bit per pixel and more pixels per row than divisible by 8.
pull/2473/head
James Jackson-South 3 years ago
committed by GitHub
parent
commit
6a7e5ff57d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  2. 24
      tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs
  3. 2
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/Input/Bmp/bit1datamatrix.bmp

2
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -668,7 +668,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals
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);
}

24
tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs

@ -370,6 +370,30 @@ public class BmpEncoderTests
TestBmpEncoderCore(provider, bitsPerPixel);
}
[Theory]
[WithFile(BlackWhitePalletDataMatrix, PixelTypes.Rgb24, BmpBitsPerPixel.Pixel1)]
public void Encode_Issue2467<TPixel>(TestImageProvider<TPixel> provider, BmpBitsPerPixel bitsPerPixel)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> 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<TPixel> reencodedImage = Image.Load<TPixel>(reencodedStream);
reencodedImage.DebugSave(provider);
reencodedImage.CompareToOriginal(provider);
}
private static void TestBmpEncoderCore<TPixel>(
TestImageProvider<TPixel> provider,
BmpBitsPerPixel bitsPerPixel,

2
tests/ImageSharp.Tests/TestImages.cs

@ -407,6 +407,8 @@ public static class TestImages
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,

3
tests/Images/Input/Bmp/bit1datamatrix.bmp

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:3b2288e2a059b15c7855eb141c05e3ce69431e7c3ddef851033f7fd9ca39a2d4
size 102
Loading…
Cancel
Save