Can't paste Device Independent Bitmap with palette if 'biClrUsed' is zero (black image)
Environment/Versions
- GIMP version: 2.99.19 gimp@15ffc4a6 (also 2.10.36)
- Package: Installer from gimp.org
- Operating System: Windows 10
Description of the bug
This is a long-standing issue in Gimp where when you try copying an indexed-color image from one program, then pasting it into Gimp, you get a black rectangle instead of an image.
This took a bit of tracking down, but I believe I figured out why Gimp was giving black images when pasting an indexed-color Device Independent Bitmap into Gimp.
I'm using the field names from the MSDN documentation of BITMAPINFOHEADER.
typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
('DWORD' and 'LONG' are both 32-bit values)
In particular, it's the field 'biClrUsed' (colors in the palette). When this field is zero, you are supposed to use the color depth of the image to determine how many colors are in the palette. MSDN says: "If biClrUsed is zero, the array contains the maximum number of colors for the given bitdepth; that is, 2^biBitCount colors."
However, Gimp seems to fail to paste any indexed-color image where this is set to zero. You just get a black image instead.
I modified the clipboard data, and found that Gimp would successfully paste the image if the 'biClrUsed' field was changed to 256 instead of 0.
(Oddly enough, GIMP's .bmp file handling code does not suffer from this bug. The difference between a DIB on the clipboard and a .bmp file is a 14 byte header which is not present in the DIB)
Reproduction
Is the bug reproducible? Yes
Reproduction steps:
- Find a program that sets 'biClrUsed' to 0 when it copies an indexed-color image to the clipboard. I've found two programs that do this so far, "Ulead Photoimpact" and "YY-CHR 0.98 beta 2", but I've also made a test program that directly copies such an image to the clipboard if you need it.
- Paste in Gimp
Expected result:
Should paste the image
Actual result:
Pastes a black image instead