// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using Microsoft.DotNet.RemoteExecutor; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.TestUtilities; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; using static SixLabors.ImageSharp.Tests.TestImages.Bmp; // ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests.Formats.Bmp; [Trait("Format", "Bmp")] [ValidateDisposedMemoryAllocations] public class BmpDecoderTests { public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector; public static readonly string[] MiscBmpFiles = Miscellaneous; public static readonly string[] BitfieldsBmpFiles = BitFields; public static readonly TheoryData RatioFiles = new() { { Car, 3780, 3780, PixelResolutionUnit.PixelsPerMeter }, { V5Header, 3780, 3780, PixelResolutionUnit.PixelsPerMeter }, { RLE8, 2835, 2835, PixelResolutionUnit.PixelsPerMeter } }; [Theory] [WithFileCollection(nameof(MiscBmpFiles), PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_MiscellaneousBitmaps(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); if (TestEnvironment.IsWindows) { image.CompareToOriginal(provider); } } [Theory] [WithFile(Car, PixelTypes.Rgba32)] [WithFile(F, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_MiscellaneousBitmaps_WithLimitedAllocatorBufferCapacity( TestImageProvider provider) { static void RunTest(string providerDump, string nonContiguousBuffersStr) { TestImageProvider provider = BasicSerializer.Deserialize>(providerDump); provider.LimitAllocatorBufferCapacity().InPixelsSqrt(100); using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider, nonContiguousBuffersStr); if (TestEnvironment.IsWindows) { image.CompareToOriginal(provider); } } string providerDump = BasicSerializer.Serialize(provider); RemoteExecutor.Invoke(RunTest, providerDump, "Disco").Dispose(); } [Theory] [WithFile(Bit32Rgb, PixelTypes.Rgba32)] [WithFile(Bit16, PixelTypes.Rgba32)] public void BmpDecoder_DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatException(TestImageProvider provider) where TPixel : unmanaged, IPixel { provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10); InvalidImageContentException ex = Assert.Throws(() => provider.GetImage(BmpDecoder.Instance)); Assert.IsType(ex.InnerException); } [Theory] [WithFileCollection(nameof(BitfieldsBmpFiles), PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeBitfields(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(Bit16Inverted, PixelTypes.Rgba32)] [WithFile(Bit8Inverted, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_Inverted(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(Bit1, PixelTypes.Rgba32)] [WithFile(Bit1Pal1, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_1Bit(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider, new SystemDrawingReferenceDecoder(BmpFormat.Instance)); } [Theory] [WithFile(Bit2, PixelTypes.Rgba32)] [WithFile(Bit2Color, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_2Bit(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); // Reference decoder cant decode 2-bit, compare to reference output instead. image.CompareToReferenceOutput(provider, extension: "png"); } [Theory] [WithFile(Bit4, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_4Bit(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(Bit8, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_8Bit(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(Bit16, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_16Bit(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(Bit32Rgb, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_32Bit(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(Rgba32v4, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_32BitV4Header_Fast(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(RLE4Cut, PixelTypes.Rgba32)] [WithFile(RLE4Delta, PixelTypes.Rgba32)] [WithFile(Rle4Delta320240, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_RunLengthEncoded_4Bit_WithDelta(TestImageProvider provider) where TPixel : unmanaged, IPixel { RleSkippedPixelHandling skippedPixelHandling = TestEnvironment.IsWindows ? RleSkippedPixelHandling.Black : RleSkippedPixelHandling.FirstColorOfPalette; BmpDecoderOptions options = new() { RleSkippedPixelHandling = skippedPixelHandling }; using Image image = provider.GetImage(BmpDecoder.Instance, options); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(RLE4, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_RunLengthEncoded_4Bit(TestImageProvider provider) where TPixel : unmanaged, IPixel { RleSkippedPixelHandling skippedPixelHandling = TestEnvironment.IsWindows ? RleSkippedPixelHandling.Black : RleSkippedPixelHandling.FirstColorOfPalette; BmpDecoderOptions options = new() { RleSkippedPixelHandling = skippedPixelHandling }; using Image image = provider.GetImage(BmpDecoder.Instance, options); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(RLE8Cut, PixelTypes.Rgba32)] [WithFile(RLE8Delta, PixelTypes.Rgba32)] [WithFile(Rle8Delta320240, PixelTypes.Rgba32)] [WithFile(Rle8Blank160120, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_RunLengthEncoded_8Bit_WithDelta_SystemDrawingRefDecoder(TestImageProvider provider) where TPixel : unmanaged, IPixel { BmpDecoderOptions options = new() { RleSkippedPixelHandling = RleSkippedPixelHandling.Black }; using Image image = provider.GetImage(BmpDecoder.Instance, options); image.DebugSave(provider); if (TestEnvironment.IsWindows) { image.CompareToOriginal(provider, new SystemDrawingReferenceDecoder(BmpFormat.Instance)); } } [Theory] [WithFile(RLE8Cut, PixelTypes.Rgba32)] [WithFile(RLE8Delta, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_RunLengthEncoded_8Bit_WithDelta_MagickRefDecoder(TestImageProvider provider) where TPixel : unmanaged, IPixel { BmpDecoderOptions options = new() { RleSkippedPixelHandling = RleSkippedPixelHandling.FirstColorOfPalette }; using Image image = provider.GetImage(BmpDecoder.Instance, options); image.DebugSave(provider); image.CompareToOriginal(provider, MagickReferenceDecoder.Png); } [Theory] [WithFile(RLE8, PixelTypes.Rgba32, false)] [WithFile(RLE8Inverted, PixelTypes.Rgba32, false)] [WithFile(RLE8, PixelTypes.Rgba32, true)] [WithFile(RLE8Inverted, PixelTypes.Rgba32, true)] public void BmpDecoder_CanDecode_RunLengthEncoded_8Bit(TestImageProvider provider, bool enforceDiscontiguousBuffers) where TPixel : unmanaged, IPixel { if (enforceDiscontiguousBuffers) { provider.LimitAllocatorBufferCapacity().InBytesSqrt(400); } BmpDecoderOptions options = new() { RleSkippedPixelHandling = RleSkippedPixelHandling.FirstColorOfPalette }; using Image image = provider.GetImage(BmpDecoder.Instance, options); image.DebugSave(provider); image.CompareToOriginal(provider, MagickReferenceDecoder.Png); } [Theory] [WithFile(RLE24, PixelTypes.Rgba32, false)] [WithFile(RLE24Cut, PixelTypes.Rgba32, false)] [WithFile(RLE24Delta, PixelTypes.Rgba32, false)] [WithFile(RLE24, PixelTypes.Rgba32, true)] [WithFile(RLE24Cut, PixelTypes.Rgba32, true)] [WithFile(RLE24Delta, PixelTypes.Rgba32, true)] public void BmpDecoder_CanDecode_RunLengthEncoded_24Bit(TestImageProvider provider, bool enforceNonContiguous) where TPixel : unmanaged, IPixel { if (enforceNonContiguous) { provider.LimitAllocatorBufferCapacity().InBytesSqrt(400); } BmpDecoderOptions options = new() { RleSkippedPixelHandling = RleSkippedPixelHandling.Black }; using Image image = provider.GetImage(BmpDecoder.Instance, options); image.DebugSave(provider); // Neither System.Drawing nor MagickReferenceDecoder decode this file. // Compare to reference output instead. image.CompareToReferenceOutput(provider, extension: "png"); } [Theory] [WithFile(RgbaAlphaBitfields, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeAlphaBitfields(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); // Neither System.Drawing nor MagickReferenceDecoder decode this file. // Compare to reference output instead. image.CompareToReferenceOutput(provider, extension: "png"); } [Theory] [WithFile(Bit32Rgba, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeBitmap_WithAlphaChannel(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider, MagickReferenceDecoder.Png); } [Theory] [WithFile(Rgba321010102, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeBitfields_WithUnusualBitmasks(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); // Choosing large tolerance of 6.1 here, because for some reason with the MagickReferenceDecoder the alpha channel // seems to be wrong. This bitmap has an alpha channel of two bits. In many cases this alpha channel has a value of 3, // which should be remapped to 255 for RGBA32, but the magick decoder has a value of 191 set. // The total difference without the alpha channel is still: 0.0204% // Exporting the image as PNG with GIMP yields to the same result as the ImageSharp implementation. image.CompareToOriginal(provider, ImageComparer.TolerantPercentage(6.1f), MagickReferenceDecoder.Png); } [Theory] [WithFile(WinBmpv2, PixelTypes.Rgba32)] [WithFile(CoreHeader, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeBmpv2(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); // Do not validate. Reference files will fail validation. image.CompareToOriginal(provider, new MagickReferenceDecoder(PngFormat.Instance, false)); } [Theory] [WithFile(WinBmpv3, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeBmpv3(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(LessThanFullSizedPalette, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeLessThanFullPalette(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider, MagickReferenceDecoder.Png); } [Theory] [WithFile(OversizedPalette, PixelTypes.Rgba32)] [WithFile(Rgb24LargePalette, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeOversizedPalette(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); if (TestEnvironment.IsWindows) { image.CompareToOriginal(provider); } } [Theory] [WithFile(InvalidPaletteSize, PixelTypes.Rgba32)] public void BmpDecoder_ThrowsInvalidImageContentException_OnInvalidPaletteSize(TestImageProvider provider) where TPixel : unmanaged, IPixel => Assert.Throws(() => { using (provider.GetImage(BmpDecoder.Instance)) { } }); [Theory] [WithFile(Rgb24jpeg, PixelTypes.Rgba32)] [WithFile(Rgb24png, PixelTypes.Rgba32)] public void BmpDecoder_ThrowsNotSupportedException_OnUnsupportedBitmaps(TestImageProvider provider) where TPixel : unmanaged, IPixel => Assert.Throws(() => { using (provider.GetImage(BmpDecoder.Instance)) { } }); [Theory] [WithFile(Rgb32h52AdobeV3, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeAdobeBmpv3(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider, MagickReferenceDecoder.Png); } [Theory] [WithFile(Rgba32bf56AdobeV3, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeAdobeBmpv3_WithAlpha(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider, MagickReferenceDecoder.Png); } [Theory] [WithFile(WinBmpv4, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeBmpv4(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(WinBmpv5, PixelTypes.Rgba32)] [WithFile(V5Header, PixelTypes.Rgba32)] public void BmpDecoder_CanDecodeBmpv5(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(Pal8Offset, PixelTypes.Rgba32)] public void BmpDecoder_RespectsFileHeaderOffset(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(F, CommonNonDefaultPixelTypes)] public void BmpDecoder_IsNotBoundToSinglePixelType(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [WithFile(Bit8Palette4, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode4BytePerEntryPalette(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); image.CompareToOriginal(provider); } [Theory] [InlineData(Bit32Rgb, 32)] [InlineData(Bit32Rgba, 32)] [InlineData(Car, 24)] [InlineData(F, 24)] [InlineData(NegHeight, 24)] [InlineData(Bit16, 16)] [InlineData(Bit16Inverted, 16)] [InlineData(Bit8, 8)] [InlineData(Bit8Inverted, 8)] [InlineData(Bit4, 4)] [InlineData(Bit1, 1)] [InlineData(Bit1Pal1, 1)] public void Identify_DetectsCorrectPixelType(string imagePath, int expectedPixelSize) { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.Equal(expectedPixelSize, imageInfo.PixelType.BitsPerPixel); } [Theory] [InlineData(Bit32Rgb, 127, 64)] [InlineData(Car, 600, 450)] [InlineData(Bit16, 127, 64)] [InlineData(Bit16Inverted, 127, 64)] [InlineData(Bit8, 127, 64)] [InlineData(Bit8Inverted, 127, 64)] [InlineData(RLE8, 491, 272)] [InlineData(RLE8Inverted, 491, 272)] public void Identify_DetectsCorrectWidthAndHeight(string imagePath, int expectedWidth, int expectedHeight) { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.Equal(expectedWidth, imageInfo.Width); Assert.Equal(expectedHeight, imageInfo.Height); } [Theory] [MemberData(nameof(RatioFiles))] public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); using Image image = BmpDecoder.Instance.Decode(DecoderOptions.Default, stream); ImageMetadata meta = image.Metadata; Assert.Equal(xResolution, meta.HorizontalResolution); Assert.Equal(yResolution, meta.VerticalResolution); Assert.Equal(resolutionUnit, meta.ResolutionUnits); } [Theory] [WithFile(Os2v2Short, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_Os2v2XShortHeader(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); // Neither System.Drawing or MagickReferenceDecoder can correctly decode this file. // Compare to reference output instead. image.CompareToReferenceOutput(provider, extension: "png"); } [Theory] [WithFile(Os2v2, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_Os2v2Header(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); // System.Drawing can not decode this image. MagickReferenceDecoder can decode it, // Compare to reference output instead. image.CompareToReferenceOutput(provider, extension: "png"); } [Theory] [WithFile(Os2BitmapArray, PixelTypes.Rgba32)] [WithFile(Os2BitmapArray9s, PixelTypes.Rgba32)] [WithFile(Os2BitmapArrayDiamond, PixelTypes.Rgba32)] [WithFile(Os2BitmapArraySkater, PixelTypes.Rgba32)] [WithFile(Os2BitmapArraySpade, PixelTypes.Rgba32)] [WithFile(Os2BitmapArraySunflower, PixelTypes.Rgba32)] [WithFile(Os2BitmapArrayMarble, PixelTypes.Rgba32)] [WithFile(Os2BitmapArrayWarpd, PixelTypes.Rgba32)] [WithFile(Os2BitmapArrayPines, PixelTypes.Rgba32)] public void BmpDecoder_CanDecode_Os2BitmapArray(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(BmpDecoder.Instance); image.DebugSave(provider); // Neither System.Drawing or MagickReferenceDecoder can correctly decode this file. // Compare to reference output instead. image.CompareToReferenceOutput(provider, extension: "png"); } [Theory] [WithFile(Issue2696, PixelTypes.Rgba32)] public void BmpDecoder_ThrowsException_Issue2696(TestImageProvider provider) where TPixel : unmanaged, IPixel { InvalidImageContentException ex = Assert.Throws(() => { using Image image = provider.GetImage(BmpDecoder.Instance); }); Assert.IsType(ex.InnerException); } }