Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
synercoder opened this issue May 26, 2023 · 4 comments
Closed
4 tasks done

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

synercoder opened this issue May 26, 2023 · 4 comments

Comments

@synercoder
Copy link
Sponsor Contributor

synercoder commented May 26, 2023

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

@JimBobSquarePants
Copy link
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

@synercoder
Copy link
Sponsor Contributor Author

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

@synercoder
Copy link
Sponsor Contributor Author

synercoder commented May 26, 2023

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

@synercoder
Copy link
Sponsor Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants