Skip to content

BMP with 1 bitcount (B/W) corrupts after save #2467

Description

@synercoder

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

3.0.1

Other ImageSharp packages and versions

>= 2.0.0 (tested up to 3.0.2-alpha.0.17)

Environment (Operating system, version and so on)

Ubuntu-latest & Windows-latest

.NET Framework version

7.0.0

Description

I have a bmp file that is 10x10 pixels. The bmp file has a bitcount of 1, and thus a color pallet of 2 colors, namely: FFFFFF & 000000.

When loading the bmp, the pixeldata is loaded correctly, but when saving the bmp, the image somehow becomes corrupt.

When you save the image by first copying the pixeldata into a new Image object, the resulting image is correct. Indicating that the bug has to do with the original metadata messing somehow with the encoding process.

The error bug occurs since ImageSharp version 2.0.0.

Input image:
Original image
original-large

Corrupt image:
Corrupt image
corrupt-large

Test project: https://github.com/synercoder/BitmapBug/blob/main/ImageSharp.BmpBug/BmpSavedWrong.cs
Test run showing matrix of os, mode & ImageSharp versions: https://github.com/synercoder/BitmapBug/actions/runs/5094509469

Steps to Reproduce

Use the attached image (datamatrix.bmp), and execute the following code:

using (var dmImage = Image.Load<Rgb24>("datamatrix.bmp"))
using (var fs = File.OpenWrite("corrupt.bmp")))
{
    dmImage.SaveAsBmp(fs);
}

The resulting corrupt.bmp is visibly (zoom in, it is 10x10 pixels) different on the right side of the image.

Images

datamatrix.zip

Activity

  1. JimBobSquarePants commented on May 26, 2023

    @JimBobSquarePants
    Member

    Most likely error diffusion being turned on by default in the encoder. Try creating a new instance of the BmpEncoder using the Octree Quantizer with the dither property set to null and passing it as the second parameter in your save method.

    The reason it will work with a new image is that the default metadata will cause the image to be saved with full color

  2. synercoder commented on May 26, 2023

    @synercoder
    SponsorContributorAuthor

    I tried your suggestion as followed:

    var quantizer = SixLabors.ImageSharp.Processing.KnownQuantizers.Octree;
    quantizer.Options.Dither = null;
    input.SaveAsBmp(ms, new SixLabors.ImageSharp.Formats.Bmp.BmpEncoder() { Quantizer = quantizer });
    

    But the result is the same: https://github.com/synercoder/BitmapBug/actions/runs/5095035910

  3. synercoder commented on May 26, 2023

    @synercoder
    SponsorContributorAuthor

    This is an "FYI bug report", not an "I could use a fix" bug report.

  4. synercoder commented on Jun 7, 2023

    @synercoder
    SponsorContributorAuthor

    Now it has become an "I have made a fix for this bug" bug report. 😉

  5. added a commit that references this issue on Feb 23, 2024
    6a7e5ff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions