GIMP saves corrupt Windows BMP files if using colorspace information and less than 16 bits...
GIMP version: 2.10.8-2
Operating System: Debian/testing
Package: Installed from the package manager, but it gets the package from https://packages.debian.org/bullseye/gimp
Description of the bug
The GIMP currently exports corrupt BMP files if colorspace information is included for images that use less than 16 bits.
Reproduction
Is the bug reproducible? Always
Reproduction steps:
- Download your favorite image
- Run "gimp gimp-wilber.png" from the command line
- Click "Image > Mode > Indexed..." in the menu
- In the dialog check "Generate optimum palette" and makes sure "Maximum number of colors" is 255
- Click the "Convert" button
- Click "File > Export As..." in the menu
- In the top of the dialog, change the file name to gimp-wilber.bmp
- At the bottom of the dialog, make sure that it states "Select File Type (By Extension)"
- At the bottom of the dialog, click the "Export" button
- In the warning dialog about losing alpha, click the "OK" button
- In the next dialog box uncheck "Run-Length Encoded"
- Expand the "Compatibility Options" and make sure to uncheck "Do not write color space information"
- Click the "Export" button
- View gimp-wilber.bmp as hexdata, e.g. by running
xxd -g1 gimp-wilber.bmp | less
Expected result:
- At offset
0x0000000e
ingimp-wilber.bmp
the header size in bytes should say0x0000007c
which is equal to 124 bytes, and thereby indicating aBITMAPV5HEADER
. - Starting at offset
0x00000036
you would expect 16 bytes of RGBA masks (bV5RedMask
throughbV5AlphaMask
, followed by colorspace information starting withbV5CSType
.
I describe why I expect a BITMAPV5HEADER
and the RGBA masks in the additional information below.
Actual result:
- At offset
0x0000000e
ingimp-wilber.bmp
I find that the header size is0x0000006c
, equal to 108 bytes, indicating aBITMAPV4HEADER
notBITMAPV5HEADER
! - Starting at offset
0x00000036
I do not find the RGBA masks, but instead the colorspace information starting with thebV5CSType
field containing the valuesRGB
because the RGBA masks have been omitted.
Additional information
The plugin code responsible for exporting BMP code appears to make an incorrect assumption about the fields that must be present in BMP files.
The current implementation omits the RGBA color masks unless the image is 16 or 32 bits.
It is worth noting that the code clearly includes fields present in the BITMAPV5HEADER
. The specification for the BITMAPV5HEADER
states that the RGBA color masks must always be included. Additionally the header size is 124 bytes
. Correspondingly the specification state that the header size for BITMAPv4HEADER
is 108 bytes
.
The current code computes the header size for a 8bit BMP with colorspace to be 40 + 0 + 68 = 108 bytes
. This value corresponds not to the desired BITMAPV5HEADER
, but to BITMAPV4HEADER
! Had the RGBA masks been included the header size would have been 40 + 16 + 68 = 124 bytes
which does correspond to a BITMAPV5HEADER
.
This means that GIMP currently exports corrupt BMP files if colorspace information is included for images that use less than 16 bits.
I will try to provide a pull request fixing this in a bit.