Merge pull request #1125 from artscoop/patch-1

Fix 32-bit BMP loading (RGBA or RGBX)
This commit is contained in:
Alex Clark 2015-04-01 15:23:32 -04:00
commit ea65087a20
2 changed files with 133 additions and 113 deletions

View File

@ -30,6 +30,7 @@ __version__ = "0.7"
from PIL import Image, ImageFile, ImagePalette, _binary from PIL import Image, ImageFile, ImagePalette, _binary
import math import math
i8 = _binary.i8 i8 = _binary.i8
i16 = _binary.i16le i16 = _binary.i16le
i32 = _binary.i32le i32 = _binary.i32le
@ -48,7 +49,7 @@ BIT2MODE = {
8: ("P", "P"), 8: ("P", "P"),
16: ("RGB", "BGR;15"), 16: ("RGB", "BGR;15"),
24: ("RGB", "BGR"), 24: ("RGB", "BGR"),
32: ("RGB", "BGRX") 32: ("RGB", "BGRX"),
} }
@ -56,131 +57,147 @@ def _accept(prefix):
return prefix[:2] == b"BM" return prefix[:2] == b"BM"
## #===============================================================================
# Image plugin for the Windows BMP format. # Image plugin for the Windows BMP format.
#===============================================================================
class BmpImageFile(ImageFile.ImageFile): class BmpImageFile(ImageFile.ImageFile):
""" Image plugin for the Windows Bitmap format (BMP) """
format = "BMP"
#--------------------------------------------------------------- Description
format_description = "Windows Bitmap" format_description = "Windows Bitmap"
format = "BMP"
#---------------------------------------------------- BMP Compression values
COMPRESSIONS = {'RAW': 0, 'RLE8': 1, 'RLE4': 2, 'BITFIELDS': 3, 'JPEG': 4, 'PNG': 5}
RAW, RLE8, RLE4, BITFIELDS, JPEG, PNG = 0, 1, 2, 3, 4, 5
def _bitmap(self, header=0, offset=0): def _bitmap(self, header=0, offset=0):
""" Read relevant info about the BMP """
read, seek = self.fp.read, self.fp.seek
if header: if header:
self.fp.seek(header) seek(header)
file_info = dict()
read = self.fp.read file_info['header_size'] = i32(read(4)) # read bmp header size @offset 14 (this is part of the header size)
file_info['direction'] = -1
# CORE/INFO #---------------------- If requested, read header at a specific position
s = read(4) header_data = ImageFile._safe_read(self.fp, file_info['header_size'] - 4) # read the rest of the bmp header, without its size
s = s + ImageFile._safe_read(self.fp, i32(s)-4) #---------------------------------------------------- IBM OS/2 Bitmap v1
#------- This format has different offsets because of width/height types
if len(s) == 12: if file_info['header_size'] == 12:
file_info['width'] = i16(header_data[0:2])
# OS/2 1.0 CORE file_info['height'] = i16(header_data[2:4])
bits = i16(s[10:]) file_info['planes'] = i16(header_data[4:6])
self.size = i16(s[4:]), i16(s[6:]) file_info['bits'] = i16(header_data[6:8])
compression = 0 file_info['compression'] = self.RAW
lutsize = 3 file_info['palette_padding'] = 3
colors = 0 #----------------------------------------------- Windows Bitmap v2 to v5
direction = -1 elif file_info['header_size'] in (40, 64, 108, 124): # v3, OS/2 v2, v4, v5
if file_info['header_size'] >= 40: # v3 and OS/2
elif len(s) in [40, 64, 108, 124]: file_info['y_flip'] = i8(header_data[7]) == 0xff
file_info['direction'] = 1 if file_info['y_flip'] else -1
# WIN 3.1 or OS/2 2.0 INFO file_info['width'] = i32(header_data[0:4])
bits = i16(s[14:]) file_info['height'] = i32(header_data[4:8]) if not file_info['y_flip'] else 2**32 - i32(header_data[4:8])
self.size = i32(s[4:]), i32(s[8:]) file_info['planes'] = i16(header_data[8:10])
compression = i32(s[16:]) file_info['bits'] = i16(header_data[10:12])
pxperm = (i32(s[24:]), i32(s[28:])) # Pixels per meter file_info['compression'] = i32(header_data[12:16])
lutsize = 4 file_info['data_size'] = i32(header_data[16:20]) # byte size of pixel data
colors = i32(s[32:]) file_info['pixels_per_meter'] = (i32(header_data[20:24]), i32(header_data[24:28]))
direction = -1 file_info['colors'] = i32(header_data[28:32])
if i8(s[11]) == 0xff: file_info['palette_padding'] = 4
# upside-down storage self.info["dpi"] = tuple(map(lambda x: math.ceil(x / 39.3701), file_info['pixels_per_meter']))
self.size = self.size[0], 2**32 - self.size[1] if file_info['compression'] == self.BITFIELDS:
direction = 0 if len(header_data) >= 52:
for idx, mask in enumerate(['r_mask', 'g_mask', 'b_mask', 'a_mask']):
self.info["dpi"] = tuple(map(lambda x: math.ceil(x / 39.3701), file_info[mask] = i32(header_data[36+idx*4:40+idx*4])
pxperm)) else:
for mask in ['r_mask', 'g_mask', 'b_mask', 'a_mask']:
file_info[mask] = i32(read(4))
file_info['rgb_mask'] = (file_info['r_mask'], file_info['g_mask'], file_info['b_mask'])
file_info['rgba_mask'] = (file_info['r_mask'], file_info['g_mask'], file_info['b_mask'], file_info['a_mask'])
else: else:
raise IOError("Unsupported BMP header type (%d)" % len(s)) raise IOError("Unsupported BMP header type (%d)" % file_info['header_size'])
#------------------- Special case : header is reported 40, which
if (self.size[0]*self.size[1]) > 2**31: #----------------------- is shorter than real size for bpp >= 16
# Prevent DOS for > 2gb images self.size = file_info['width'], file_info['height']
#--------- If color count was not found in the header, compute from bits
file_info['colors'] = file_info['colors'] if file_info.get('colors', 0) else (1 << file_info['bits'])
#--------------------------------- Check abnormal values for DOS attacks
if file_info['width'] * file_info['height'] > 2**31:
raise IOError("Unsupported BMP Size: (%dx%d)" % self.size) raise IOError("Unsupported BMP Size: (%dx%d)" % self.size)
#------------------------ Check bit depth for unusual unsupported values
if not colors: self.mode, raw_mode = BIT2MODE.get(file_info['bits'], (None, None))
colors = 1 << bits if self.mode is None:
raise IOError("Unsupported BMP pixel depth (%d)" % file_info['bits'])
# MODE #------------------ Process BMP with Bitfields compression (not palette)
try: if file_info['compression'] == self.BITFIELDS:
self.mode, rawmode = BIT2MODE[bits] SUPPORTED = {
except KeyError: 32: [(0xff0000, 0xff00, 0xff, 0x0), (0xff0000, 0xff00, 0xff, 0xff000000), (0x0, 0x0, 0x0, 0x0)],
raise IOError("Unsupported BMP pixel depth (%d)" % bits) 24: [(0xff0000, 0xff00, 0xff)],
16: [(0xf800, 0x7e0, 0x1f), (0x7c00, 0x3e0, 0x1f)]}
if compression == 3: MASK_MODES = {
# BI_BITFIELDS compression (32, (0xff0000, 0xff00, 0xff, 0x0)): "BGRX", (32, (0xff0000, 0xff00, 0xff, 0xff000000)): "BGRA", (32, (0x0, 0x0, 0x0, 0x0)): "BGRA",
mask = i32(read(4)), i32(read(4)), i32(read(4)) (24, (0xff0000, 0xff00, 0xff)): "BGR",
if bits == 32 and mask == (0xff0000, 0x00ff00, 0x0000ff): (16, (0xf800, 0x7e0, 0x1f)): "BGR;16", (16, (0x7c00, 0x3e0, 0x1f)): "BGR;15"}
rawmode = "BGRX" if file_info['bits'] in SUPPORTED:
elif bits == 16 and mask == (0x00f800, 0x0007e0, 0x00001f): if file_info['bits'] == 32 and file_info['rgba_mask'] in SUPPORTED[file_info['bits']]:
rawmode = "BGR;16" raw_mode = MASK_MODES[(file_info['bits'], file_info['rgba_mask'])]
elif bits == 16 and mask == (0x007c00, 0x0003e0, 0x00001f): self.mode = "RGBA" if raw_mode in ("BGRA",) else self.mode
rawmode = "BGR;15" elif file_info['bits'] in (24, 16) and file_info['rgb_mask'] in SUPPORTED[file_info['bits']]:
else: raw_mode = MASK_MODES[(file_info['bits'], file_info['rgb_mask'])]
# print bits, map(hex, mask)
raise IOError("Unsupported BMP bitfields layout")
elif compression != 0:
raise IOError("Unsupported BMP compression (%d)" % compression)
# LUT
if self.mode == "P":
palette = []
greyscale = 1
if colors == 2:
indices = (0, 255)
elif colors > 2**16 or colors <= 0: # We're reading a i32.
raise IOError("Unsupported BMP Palette size (%d)" % colors)
else:
indices = list(range(colors))
for i in indices:
rgb = read(lutsize)[:3]
if rgb != o8(i)*3:
greyscale = 0
palette.append(rgb)
if greyscale:
if colors == 2:
self.mode = rawmode = "1"
else: else:
self.mode = rawmode = "L" raise IOError("Unsupported BMP bitfields layout")
else: else:
self.mode = "P" raise IOError("Unsupported BMP bitfields layout")
self.palette = ImagePalette.raw( elif file_info['compression'] == self.RAW:
"BGR", b"".join(palette) if file_info['bits'] == 32 and header == 22: # 32-bit .cur offset
) raw_mode, self.mode = "BGRA", "RGBA"
else:
if not offset: raise IOError("Unsupported BMP compression (%d)" % file_info['compression'])
offset = self.fp.tell() #----------------- Once the header is processed, process the palette/LUT
if self.mode == "P": # Paletted for 1, 4 and 8 bit images
self.tile = [("raw", #------------------------------------------------------ 1-bit images
(0, 0) + self.size, if not (0 < file_info['colors'] <= 65536):
offset, raise IOError("Unsupported BMP Palette size (%d)" % file_info['colors'])
(rawmode, ((self.size[0]*bits+31) >> 3) & (~3), else:
direction))] padding = file_info['palette_padding']
palette = read(padding * file_info['colors'])
self.info["compression"] = compression greyscale = True
indices = (0, 255) if file_info['colors'] == 2 else list(range(file_info['colors']))
#------------------- Check if greyscale and ignore palette if so
for ind, val in enumerate(indices):
rgb = palette[ind*padding:ind*padding + 3]
if rgb != o8(val) * 3:
greyscale = False
#--------- If all colors are grey, white or black, ditch palette
if greyscale:
self.mode = "1" if file_info['colors'] == 2 else "L"
raw_mode = self.mode
else:
self.mode = "P"
self.palette = ImagePalette.raw("BGRX" if padding == 4 else "BGR", palette)
#------------------------------ Finally set the tile data for the plugin
self.info['compression'] = file_info['compression']
self.tile = [('raw', (0, 0, file_info['width'], file_info['height']), offset or self.fp.tell(),
(raw_mode, ((file_info['width'] * file_info['bits'] + 31) >> 3) & (~3), file_info['direction'])
)]
def _open(self): def _open(self):
""" Open file, check magic number and read header """
# HEAD # read 14 bytes: magic number, filesize, reserved, header final offset
s = self.fp.read(14) head_data = self.fp.read(14)
if s[:2] != b"BM": # choke if the file does not have the required magic bytes
if head_data[0:2] != b"BM":
raise SyntaxError("Not a BMP file") raise SyntaxError("Not a BMP file")
offset = i32(s[10:]) # read the start position of the BMP image data (u32)
offset = i32(head_data[10:14])
# load bitmap information (offset=raster info)
self._bitmap(offset=offset) self._bitmap(offset=offset)
#===============================================================================
# Image plugin for the DIB format (BMP alias)
#===============================================================================
class DibImageFile(BmpImageFile): class DibImageFile(BmpImageFile):
format = "DIB" format = "DIB"
@ -189,6 +206,8 @@ class DibImageFile(BmpImageFile):
def _open(self): def _open(self):
self._bitmap() self._bitmap()
# #
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# Write BMP file # Write BMP file
@ -198,6 +217,7 @@ SAVE = {
"L": ("L", 8, 256), "L": ("L", 8, 256),
"P": ("P", 8, 256), "P": ("P", 8, 256),
"RGB": ("BGR", 24, 0), "RGB": ("BGR", 24, 0),
"RGBA": ("BGRA", 32, 0),
} }

View File

@ -16,9 +16,9 @@ class TestFileCur(PillowTestCase):
self.assertEqual(im.size, (32, 32)) self.assertEqual(im.size, (32, 32))
self.assertIsInstance(im, CurImagePlugin.CurImageFile) self.assertIsInstance(im, CurImagePlugin.CurImageFile)
# Check some pixel colors to ensure image is loaded properly # Check some pixel colors to ensure image is loaded properly
self.assertEqual(im.getpixel((10, 1)), (0, 0, 0)) self.assertEqual(im.getpixel((10, 1)), (0, 0, 0, 0))
self.assertEqual(im.getpixel((11, 1)), (253, 254, 254)) self.assertEqual(im.getpixel((11, 1)), (253, 254, 254, 1))
self.assertEqual(im.getpixel((16, 16)), (84, 87, 86)) self.assertEqual(im.getpixel((16, 16)), (84, 87, 86, 255))
if __name__ == '__main__': if __name__ == '__main__':