Merge pull request #1847 from wiredfool/revert-1813-DIB_1293

Revert 1813, plus second commit with tweaks.
This commit is contained in:
wiredfool 2016-05-01 10:31:59 +01:00
commit 460480dfff
4 changed files with 7 additions and 32 deletions

View File

@ -112,8 +112,9 @@ class BmpImageFile(ImageFile.ImageFile):
# 40 byte headers only have the three components in the bitfields masks, # 40 byte headers only have the three components in the bitfields masks,
# ref: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx # ref: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx
# See also https://github.com/python-pillow/Pillow/issues/1293 # See also https://github.com/python-pillow/Pillow/issues/1293
# Note below, None is a key in the SUPPORTED and MASK_MODES structures. # There is a 4th component in the RGBQuad, in the alpha location, but it
file_info['a_mask'] = None # is listed as a reserved component, and it is not generally an alpha channel
file_info['a_mask'] = 0x0
for mask in ['r_mask', 'g_mask', 'b_mask']: for mask in ['r_mask', 'g_mask', 'b_mask']:
file_info[mask] = i32(read(4)) file_info[mask] = i32(read(4))
file_info['rgb_mask'] = (file_info['r_mask'], file_info['g_mask'], file_info['b_mask']) file_info['rgb_mask'] = (file_info['r_mask'], file_info['g_mask'], file_info['b_mask'])
@ -135,22 +136,12 @@ class BmpImageFile(ImageFile.ImageFile):
# ----------------- Process BMP with Bitfields compression (not palette) # ----------------- Process BMP with Bitfields compression (not palette)
if file_info['compression'] == self.BITFIELDS: if file_info['compression'] == self.BITFIELDS:
SUPPORTED = { SUPPORTED = {
32: [(0xff0000, 0xff00, 0xff, 0x0), 32: [(0xff0000, 0xff00, 0xff, 0x0), (0xff0000, 0xff00, 0xff, 0xff000000), (0x0, 0x0, 0x0, 0x0)],
(0xff0000, 0xff00, 0xff, None),
(0xff0000, 0xff00, 0xff, 0xff000000),
(0x0, 0x0, 0x0, 0x0)],
24: [(0xff0000, 0xff00, 0xff)], 24: [(0xff0000, 0xff00, 0xff)],
16: [(0xf800, 0x7e0, 0x1f), (0x7c00, 0x3e0, 0x1f)] 16: [(0xf800, 0x7e0, 0x1f), (0x7c00, 0x3e0, 0x1f)]
} }
# From inspecting DIB files from Vista screenshots, the
# file format is a 32 bit color image, where the 'Alpha'
# channel for screenshots is actually a transparency mask
# (255-alpha) where 00 is solid and ff is transparent. I
# can't find documentation of this, but it matches
# imagemagick's conversion of the .dib file into a .png.
MASK_MODES = { MASK_MODES = {
(32, (0xff0000, 0xff00, 0xff, 0x0)): "BGRX", (32, (0xff0000, 0xff00, 0xff, 0x0)): "BGRX",
(32, (0xff0000, 0xff00, 0xff, None)): "BGRT",
(32, (0xff0000, 0xff00, 0xff, 0xff000000)): "BGRA", (32, (0xff0000, 0xff00, 0xff, 0xff000000)): "BGRA",
(32, (0x0, 0x0, 0x0, 0x0)): "BGRA", (32, (0x0, 0x0, 0x0, 0x0)): "BGRA",
(24, (0xff0000, 0xff00, 0xff)): "BGR", (24, (0xff0000, 0xff00, 0xff)): "BGR",
@ -160,7 +151,7 @@ class BmpImageFile(ImageFile.ImageFile):
if file_info['bits'] in SUPPORTED: if file_info['bits'] in SUPPORTED:
if file_info['bits'] == 32 and file_info['rgba_mask'] in SUPPORTED[file_info['bits']]: if file_info['bits'] == 32 and file_info['rgba_mask'] in SUPPORTED[file_info['bits']]:
raw_mode = MASK_MODES[(file_info['bits'], file_info['rgba_mask'])] raw_mode = MASK_MODES[(file_info['bits'], file_info['rgba_mask'])]
self.mode = "RGBA" if raw_mode in ("BGRA", "BGRT") else self.mode self.mode = "RGBA" if raw_mode in ("BGRA",) else self.mode
elif file_info['bits'] in (24, 16) and file_info['rgb_mask'] in SUPPORTED[file_info['bits']]: elif file_info['bits'] in (24, 16) and file_info['rgb_mask'] in SUPPORTED[file_info['bits']]:
raw_mode = MASK_MODES[(file_info['bits'], file_info['rgb_mask'])] raw_mode = MASK_MODES[(file_info['bits'], file_info['rgb_mask'])]
else: else:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.3 KiB

After

Width:  |  Height:  |  Size: 7.9 KiB

View File

@ -74,8 +74,8 @@ class TestFileBmp(PillowTestCase):
# test for #1293, Imagegrab returning Unsupported Bitfields Format # test for #1293, Imagegrab returning Unsupported Bitfields Format
im = BmpImagePlugin.DibImageFile('Tests/images/clipboard.dib') im = BmpImagePlugin.DibImageFile('Tests/images/clipboard.dib')
target = Image.open('Tests/images/clipboard_target.png') target = Image.open('Tests/images/clipboard_target.png')
self.assert_image_equal(im.convert('RGB'), target.convert('RGB')) self.assert_image_equal(im, target)
self.assert_image_similar(im, target, 1)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -832,21 +832,6 @@ unpackBGRA(UINT8* out, const UINT8* in, int pixels)
} }
} }
static void
unpackBGRT(UINT8* out, const UINT8* in, int pixels)
{
int i;
/* RGBA, reversed bytes */
/* Transparency map, rather than an alpha channel */
for (i = 0; i < pixels; i++) {
out[R] = in[2];
out[G] = in[1];
out[B] = in[0];
out[A] = 255 - in[3];
out += 4; in += 4;
}
}
/* Unpack to "CMYK" image */ /* Unpack to "CMYK" image */
@ -1228,7 +1213,6 @@ static struct {
{"RGBA", "RGBA;4B", 16, ImagingUnpackRGBA4B}, {"RGBA", "RGBA;4B", 16, ImagingUnpackRGBA4B},
{"RGBA", "RGBA;16B", 64, unpackRGBA16B}, {"RGBA", "RGBA;16B", 64, unpackRGBA16B},
{"RGBA", "BGRA", 32, unpackBGRA}, {"RGBA", "BGRA", 32, unpackBGRA},
{"RGBA", "BGRT", 32, unpackBGRT},
{"RGBA", "ARGB", 32, unpackARGB}, {"RGBA", "ARGB", 32, unpackARGB},
{"RGBA", "ABGR", 32, unpackABGR}, {"RGBA", "ABGR", 32, unpackABGR},
{"RGBA", "YCCA;P", 32, ImagingUnpackYCCA}, {"RGBA", "YCCA;P", 32, ImagingUnpackYCCA},