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

ColorTable for 4bit BMP is parsed wrong #2907

Open
4 tasks done
zabulus opened this issue Mar 27, 2025 · 3 comments
Open
4 tasks done

ColorTable for 4bit BMP is parsed wrong #2907

zabulus opened this issue Mar 27, 2025 · 3 comments

Comments

@zabulus
Copy link

zabulus commented Mar 27, 2025

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

4.0.0-alpha.0.18+1ec479e404455608db862b4d607207b4bf2063c1

Other ImageSharp packages and versions

SixLabors.ImageSharp.Drawing,3.0.0-alpha.0.1+316fb2e528e2fd131a22f4d05a9dfdc4d908dbcc

Environment (Operating system, version and so on)

Win11 24h2

.NET Framework version

.net8.0

Description

We have 4bit bmp files and during parsing, ImageSharp produces wrong palette metadata for it.

result = {ReadOnlyMemory<Color>} System.ReadOnlyMemory<Color>[21]
 [0] = {Color} 000000FF
 [1] = {Color} 111100FF
 [2] = {Color} 220011FF
 [3] = {Color} 002222FF
 [4] = {Color} 333333FF
 [5] = {Color} 444400FF
 [6] = {Color} 550044FF
 [7] = {Color} 005555FF
 [8] = {Color} 666666FF
 [9] = {Color} 777700FF
 [10] = {Color} 880077FF
 [11] = {Color} 008888FF
 [12] = {Color} 999999FF
 [13] = {Color} AAAA00FF
 [14] = {Color} BB00AAFF
 [15] = {Color} 00BBBBFF
 [16] = {Color} CCCCCCFF
 [17] = {Color} DDDD00FF
 [18] = {Color} EE00DDFF
 [19] = {Color} 00EEEEFF
 [20] = {Color} FFFFFFFF

but actual palette is just steps of gray, starting from
111111 to FFFFFF
Actual on-disk-values of bmp file
Image

FWIW image itself is parsed correctly.

Steps to Reproduce

var image = Image.Load<Rgb24>("1.0.bmp");
var colorTable = image.Metadata.GetBmpMetadata().ColorTable.GetValueOrDefault();

Observe values of colorTable.

Images

1.0.bmp.zip

@zabulus
Copy link
Author

zabulus commented Mar 27, 2025

Similar issue is with 8bit indexed images, also colors are shifted to one byte

@zabulus
Copy link
Author

zabulus commented Mar 27, 2025

Looks like issue is here:

Color[] colorTable = new Color[palette.Length / Unsafe.SizeOf<Bgr24>()];

Rgb24 used instead Rgb32
From documentation: https://learn.microsoft.com/en-us/windows/win32/gdi/bitmap-storage, palette is RGBQUAD array.

@JimBobSquarePants
Copy link
Member

Thanks for the additional detective work. I'll have a look ASAP.

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