You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BmpImagePlugin - Unsupported 32bpp DIBs with Alpha #8594
Sorry to bother you all with such a niche problem. 😅
For context, the attached bitmap (DIB) comes from one of the themes in Windows XP - I am trying to look into writing a general tool for converting .MSSTYLES based themes to various Linux toolkits.
Removing this check for 22 byte header does resolve the issue for me, however it breaks the 32bpp bitmap test case. Unfortunately it seems like the alpha channel behaviour is undocumented, at least I cannot find anything. Both images use the 40-byte BITMAPINFOHEADER, and the only real difference is biSizeImage being set to 0 in the broken bitmap. Not something I think can be relied on to change how the data should be interpreted.
I don't think this is something easily solvable via parsing the format alone, so I have a fork of Pillow to include BMPA and DIBA as additional formats for explicitly hinting that the alpha channel is to be read.
I'll submit a corresponding PR, though my solution might be rubbish - I don't use Python all that much. 😝
The bitmap has a maximum of 2^32 colors. If the biCompression member is BI_RGB, the bmiColors member is NULL. Each DWORD in the bitmap array represents the relative intensities of blue, green, and red, respectively, for a pixel. The high byte in each DWORD is not used.
Because this DIB comes directly from the resource file luna.msstyles - the theme engine does actually use the alpha channel (they moved to using PNGs in Windows Vista). All themes on Windows XP make use of RGBA bitmaps in the same way.
Here's a comparison between the current read and what is intended:
I know the documentation does not say anything about it, I'm not sure if it was ever formally documented or whether it's just modern MSDN missing stuff. From searching around there's old forum posts and pages on CodeProject (RIP) about these 32bpp bitmaps. I think potentially it is possible to create them in Photoshop, but I'm not sure if that's still the case in newer versions.
The question I was trying to ask is - what program are you using to view this file that shows transparency?
Should I understand your response to mean that you're not viewing them in a program, and your expectation is based on the fact that Windows loads this data and then you see the result in the OS? Not just the transparent corners, but also a transparent inside under some circumstances?
You caught me just before I finished writing this update. 😛
In that post I compared between DIB in Pillow, and DIBA in my branch - so it was based off the expectation of how Windows handles the data. I have just had a look out of curiosity to see if what I read about Photoshop is true, and it does appear to be the case that Photoshop (CS2 here) also recognises the alpha channel:
Hmm. I'm concerned about interpreting data that the documentation says to ignore, because that's what third-party software would follow when creating images, and it's possible that what they write to this fourth channel may not actually work as an alpha channel. So it might fix your image, but it might also break others.
What do you think of this code, that will work with Pillow as-is to read your image as RGBA?
Hmm. I'm concerned about interpreting data that the documentation says to ignore, because that's what third-party software would follow when creating images, and it's possible that what they write to this fourth channel may not actually work as an alpha channel. So it might fix your image, but it might also break others.
This was why I opted to add the extra formats to act as 'hints', rather than trying to guess what the interpretation should be - so it cannot break existing images: rozniak@79683d3
What do you think of this code, that will work with Pillow as-is to read your image as RGBA?
It works perfectly. 😄 I mean it's up to you really, I suppose it might not be a very common type of bitmap for people to read so I don't mind working with this snippet in my own project. I did try to keep the change minimal and to avoid breaking any existing images according to spec, depends whether you think it's worth having or not. I suppose there is always this issue thread for future reference otherwise. 😛
Really appreciate you being so helpful with this problem, I didn't expect much at all considering it's an undocumented use of a niche variant of the file format so I wanted to express my thanks. 😁
Activity
rozniak commentedon Dec 13, 2024
Sorry to bother you all with such a niche problem. 😅
For context, the attached bitmap (DIB) comes from one of the themes in Windows XP - I am trying to look into writing a general tool for converting .MSSTYLES based themes to various Linux toolkits.
I pinpointed this line of interest in
BmpImagePlugin: https://github.com/python-pillow/Pillow/blob/main/src/PIL/BmpImagePlugin.py#L245Removing this check for 22 byte header does resolve the issue for me, however it breaks the 32bpp bitmap test case. Unfortunately it seems like the alpha channel behaviour is undocumented, at least I cannot find anything. Both images use the 40-byte BITMAPINFOHEADER, and the only real difference is
biSizeImagebeing set to 0 in the broken bitmap. Not something I think can be relied on to change how the data should be interpreted.I don't think this is something easily solvable via parsing the format alone, so I have a fork of Pillow to include
BMPAandDIBAas additional formats for explicitly hinting that the alpha channel is to be read.I'll submit a corresponding PR, though my solution might be rubbish - I don't use Python all that much. 😝
radarhere commentedon Dec 13, 2024
For context, #1125 is the PR that introduced that line of code.
Can I ask - how do you know the image should be RGBA? Googling, I found https://jumpshare.com/viewer/bmp, and it showed no transparency when given the file.
radarhere commentedon Dec 13, 2024
I found https://learn.microsoft.com/en-us/windows/win32/wmdm/-bitmapinfoheader, which describes 32 bitcount as
So it sounds like RGB is the intention.
rozniak commentedon Dec 13, 2024
Because this DIB comes directly from the resource file
luna.msstyles- the theme engine does actually use the alpha channel (they moved to using PNGs in Windows Vista). All themes on Windows XP make use of RGBA bitmaps in the same way.Here's a comparison between the current read and what is intended:

I know the documentation does not say anything about it, I'm not sure if it was ever formally documented or whether it's just modern MSDN missing stuff. From searching around there's old forum posts and pages on CodeProject (RIP) about these 32bpp bitmaps. I think potentially it is possible to create them in Photoshop, but I'm not sure if that's still the case in newer versions.
radarhere commentedon Dec 13, 2024
The question I was trying to ask is - what program are you using to view this file that shows transparency?
Should I understand your response to mean that you're not viewing them in a program, and your expectation is based on the fact that Windows loads this data and then you see the result in the OS? Not just the transparent corners, but also a transparent inside under some circumstances?
rozniak commentedon Dec 13, 2024
You caught me just before I finished writing this update. 😛
In that post I compared between
DIBin Pillow, andDIBAin my branch - so it was based off the expectation of how Windows handles the data. I have just had a look out of curiosity to see if what I read about Photoshop is true, and it does appear to be the case that Photoshop (CS2 here) also recognises the alpha channel:radarhere commentedon Dec 13, 2024
Hmm. I'm concerned about interpreting data that the documentation says to ignore, because that's what third-party software would follow when creating images, and it's possible that what they write to this fourth channel may not actually work as an alpha channel. So it might fix your image, but it might also break others.
What do you think of this code, that will work with Pillow as-is to read your image as RGBA?
rozniak commentedon Dec 13, 2024
This was why I opted to add the extra formats to act as 'hints', rather than trying to guess what the interpretation should be - so it cannot break existing images: rozniak@79683d3
It works perfectly. 😄 I mean it's up to you really, I suppose it might not be a very common type of bitmap for people to read so I don't mind working with this snippet in my own project. I did try to keep the change minimal and to avoid breaking any existing images according to spec, depends whether you think it's worth having or not. I suppose there is always this issue thread for future reference otherwise. 😛
radarhere commentedon Dec 16, 2024
I've created #8602 as a suggestion - it will retain the current Pillow behaviour by default, but if you run
first, then the fourth channel will be read.
rozniak commentedon Dec 16, 2024
Really appreciate you being so helpful with this problem, I didn't expect much at all considering it's an undocumented use of a niche variant of the file format so I wanted to express my thanks. 😁