Skip to content

8Bit Depth Grayscale Image: Loss of pixel precision when setting palette #1075

@otori

Description

@otori

Description

I would like to export a grayscale MagickImage as a windows bitmap (.bmp).
The image object properties are:

ColorSpace: ColorSpace.Gray
ColorType: ColorType.Grayscale

If I directly write a bitmap ( img.Write("testimg.bmp", MagickFormat.Bmp) ) the generated bmp file stores 24bits per pixel.
Hence, I set img.ColorType = ColorType.Palette in order to have 8 bits per pixel in the output image (since real grayscale bmp's always are with a palette).
However, the pixels in the produced images differ slightly from the input images.
A test with a 16x16 bitmap with all 256 shades of gray showed, that the result only stored 64 shades of gray.

Steps to Reproduce

I uploaded a sample program that creates the described 256 shades of gray image and exports it as a bitmap.
A manual iteration over the pixels already shows that the pixel colors do not match.
When I opened the bmp with IrfanView it gave me the count of 64 distinct colors.
Note: the output image is hardcoded to "C:\Temp\MagickNetGrayBmpTest.bmp"
Program.zip

Question

Am I doing the Palette setting correctly, or do I have to do another thing in order to prevent a loss of pixel precision?
Is there another way to generate BMPs with 8 bits per pixel?

System Configuration

  • Magick.NET version:
    The installed nuget package is Magick.NET-Q8-AnyCPU version 8.40
  • Environment (Operating system, version and so on):
    Windows 10, target framework: .Net 5.0 Core

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions