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

BmpEncoder does not respect QuantizerOptions.Dither when BitsPerPixel is 1, 2 or 4 #2817

Open
4 tasks done
mistoll opened this issue Sep 30, 2024 · 1 comment
Open
4 tasks done

Comments

@mistoll
Copy link
Contributor

mistoll commented Sep 30, 2024

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.1.5

Other ImageSharp packages and versions

None

Environment (Operating system, version and so on)

Win 11

.NET Framework version

.net 8

Description

When writing 1-bit (or 2, 4) files using BmpEncoder, the dither settings are not respected.

That is, because in

using IQuantizer<TPixel> frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer<TPixel>(configuration, new QuantizerOptions()
{
MaxColors = 2
});

the QuantizerOptions is replaced with new QuanitzerOptions, which does not include the inital Dither values. Similar code is in Write2BitPixelData and Write4BitPixelData.

I'd write a pull request, but I don't know how you want to handle that. Like modify the original options, create a full clone opt the original options, remove the new options completely, or whatever you come up with.

Steps to Reproduce

var image = // Load any image
var palette =new Color[] { Color.Black, Color.White };
var quantizerOptions = new QuantizerOptions() { MaxColors = 2, Dither = **null** };
var quantizer = new PaletteQuantizer(palette, quantizerOptions);
var encoder = new BmpEncoder { BitsPerPixel = BmpBitsPerPixel.Pixel1, Quantizer = quantizer };
image.SaveAsync(anyFile, encoder);

The result is a image with dithering (example created from car.bmp from test images)
grafik

Images

No response

@JimBobSquarePants
Copy link
Member

Thanks for the detail here and analysis. It's very much appreciated!

The correct approach would be to do the following all places where we require new options.

using IQuantizer<TPixel> frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer<TPixel>(configuration, new QuantizerOptions()
{
    MaxColors = 2, // Use correct value for each method
    Dither = this.quantizer.Options.Dither,
    DitherScale = this.quantizer.Options.DitherScale
});

If you could submit a PR that would be really great.

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