2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# BMP file handler
|
|
|
|
#
|
|
|
|
# Windows (and OS/2) native bitmap storage format.
|
|
|
|
#
|
|
|
|
# history:
|
|
|
|
# 1995-09-01 fl Created
|
|
|
|
# 1996-04-30 fl Added save
|
|
|
|
# 1997-08-27 fl Fixed save of 1-bit images
|
|
|
|
# 1998-03-06 fl Load P images as L where possible
|
|
|
|
# 1998-07-03 fl Load P images as 1 where possible
|
|
|
|
# 1998-12-29 fl Handle small palettes
|
|
|
|
# 2002-12-30 fl Fixed load of 1-bit palette images
|
|
|
|
# 2003-04-21 fl Fixed load of 1-bit monochrome images
|
|
|
|
# 2003-04-23 fl Added limited support for BI_BITFIELDS compression
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997-2003 by Secret Labs AB
|
|
|
|
# Copyright (c) 1995-2003 by Fredrik Lundh
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2015-03-05 13:32:03 +03:00
|
|
|
|
2022-03-09 14:35:48 +03:00
|
|
|
import os
|
|
|
|
|
2017-01-17 16:22:18 +03:00
|
|
|
from . import Image, ImageFile, ImagePalette
|
2020-09-01 20:16:46 +03:00
|
|
|
from ._binary import i16le as i16
|
|
|
|
from ._binary import i32le as i32
|
|
|
|
from ._binary import o8
|
|
|
|
from ._binary import o16le as o16
|
|
|
|
from ._binary import o32le as o32
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Read BMP file
|
|
|
|
|
|
|
|
BIT2MODE = {
|
|
|
|
# bits => mode, rawmode
|
|
|
|
1: ("P", "P;1"),
|
|
|
|
4: ("P", "P;4"),
|
|
|
|
8: ("P", "P"),
|
|
|
|
16: ("RGB", "BGR;15"),
|
|
|
|
24: ("RGB", "BGR"),
|
2015-03-05 13:32:03 +03:00
|
|
|
32: ("RGB", "BGRX"),
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _accept(prefix):
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return prefix[:2] == b"BM"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2019-03-06 05:59:07 +03:00
|
|
|
def _dib_accept(prefix):
|
2020-05-08 21:11:58 +03:00
|
|
|
return i32(prefix) in [12, 40, 64, 108, 124]
|
2019-03-06 05:59:07 +03:00
|
|
|
|
|
|
|
|
2018-10-24 22:24:03 +03:00
|
|
|
# =============================================================================
|
2010-07-31 06:52:47 +04:00
|
|
|
# Image plugin for the Windows BMP format.
|
2018-10-24 22:24:03 +03:00
|
|
|
# =============================================================================
|
2010-07-31 06:52:47 +04:00
|
|
|
class BmpImageFile(ImageFile.ImageFile):
|
2021-08-12 14:50:09 +03:00
|
|
|
"""Image plugin for the Windows Bitmap format (BMP)"""
|
2015-04-01 22:38:18 +03:00
|
|
|
|
2018-10-24 22:24:03 +03:00
|
|
|
# ------------------------------------------------------------- Description
|
2015-03-05 01:06:21 +03:00
|
|
|
format_description = "Windows Bitmap"
|
2015-03-05 10:19:14 +03:00
|
|
|
format = "BMP"
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# -------------------------------------------------- BMP Compression values
|
2019-03-21 16:28:20 +03:00
|
|
|
COMPRESSIONS = {"RAW": 0, "RLE8": 1, "RLE4": 2, "BITFIELDS": 3, "JPEG": 4, "PNG": 5}
|
2019-05-25 09:05:07 +03:00
|
|
|
for k, v in COMPRESSIONS.items():
|
|
|
|
vars()[k] = v
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-03-05 10:19:14 +03:00
|
|
|
def _bitmap(self, header=0, offset=0):
|
2021-08-12 14:50:09 +03:00
|
|
|
"""Read relevant info about the BMP"""
|
2015-03-05 10:19:14 +03:00
|
|
|
read, seek = self.fp.read, self.fp.seek
|
2015-03-05 12:40:10 +03:00
|
|
|
if header:
|
|
|
|
seek(header)
|
2018-06-24 15:32:25 +03:00
|
|
|
# read bmp header size @offset 14 (this is part of the header size)
|
2022-04-10 19:59:42 +03:00
|
|
|
file_info = {"header_size": i32(read(4)), "direction": -1}
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# -------------------- If requested, read header at a specific position
|
2018-06-24 15:32:25 +03:00
|
|
|
# read the rest of the bmp header, without its size
|
2019-03-21 16:28:20 +03:00
|
|
|
header_data = ImageFile._safe_read(self.fp, file_info["header_size"] - 4)
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# -------------------------------------------------- IBM OS/2 Bitmap v1
|
|
|
|
# ----- This format has different offsets because of width/height types
|
2019-03-21 16:28:20 +03:00
|
|
|
if file_info["header_size"] == 12:
|
2020-05-08 21:11:58 +03:00
|
|
|
file_info["width"] = i16(header_data, 0)
|
|
|
|
file_info["height"] = i16(header_data, 2)
|
|
|
|
file_info["planes"] = i16(header_data, 4)
|
|
|
|
file_info["bits"] = i16(header_data, 6)
|
2019-03-21 16:28:20 +03:00
|
|
|
file_info["compression"] = self.RAW
|
|
|
|
file_info["palette_padding"] = 3
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# --------------------------------------------- Windows Bitmap v2 to v5
|
|
|
|
# v3, OS/2 v2, v4, v5
|
2019-03-21 16:28:20 +03:00
|
|
|
elif file_info["header_size"] in (40, 64, 108, 124):
|
2020-05-08 19:48:02 +03:00
|
|
|
file_info["y_flip"] = header_data[7] == 0xFF
|
2019-03-21 16:28:20 +03:00
|
|
|
file_info["direction"] = 1 if file_info["y_flip"] else -1
|
2020-05-08 21:11:58 +03:00
|
|
|
file_info["width"] = i32(header_data, 0)
|
2019-03-21 16:28:20 +03:00
|
|
|
file_info["height"] = (
|
2020-05-08 21:11:58 +03:00
|
|
|
i32(header_data, 4)
|
2019-03-21 16:28:20 +03:00
|
|
|
if not file_info["y_flip"]
|
2022-03-04 08:42:24 +03:00
|
|
|
else 2**32 - i32(header_data, 4)
|
2019-03-21 16:28:20 +03:00
|
|
|
)
|
2020-05-08 21:11:58 +03:00
|
|
|
file_info["planes"] = i16(header_data, 8)
|
|
|
|
file_info["bits"] = i16(header_data, 10)
|
|
|
|
file_info["compression"] = i32(header_data, 12)
|
2019-03-15 02:46:34 +03:00
|
|
|
# byte size of pixel data
|
2020-05-08 21:11:58 +03:00
|
|
|
file_info["data_size"] = i32(header_data, 16)
|
2019-03-21 16:28:20 +03:00
|
|
|
file_info["pixels_per_meter"] = (
|
2020-05-08 21:11:58 +03:00
|
|
|
i32(header_data, 20),
|
|
|
|
i32(header_data, 24),
|
2019-03-21 16:28:20 +03:00
|
|
|
)
|
2020-05-08 21:11:58 +03:00
|
|
|
file_info["colors"] = i32(header_data, 28)
|
2019-03-21 16:28:20 +03:00
|
|
|
file_info["palette_padding"] = 4
|
2021-05-07 13:50:18 +03:00
|
|
|
self.info["dpi"] = tuple(x / 39.3701 for x in file_info["pixels_per_meter"])
|
2019-03-21 16:28:20 +03:00
|
|
|
if file_info["compression"] == self.BITFIELDS:
|
2019-03-15 02:46:34 +03:00
|
|
|
if len(header_data) >= 52:
|
2019-03-21 16:28:20 +03:00
|
|
|
for idx, mask in enumerate(
|
|
|
|
["r_mask", "g_mask", "b_mask", "a_mask"]
|
|
|
|
):
|
2020-05-08 21:11:58 +03:00
|
|
|
file_info[mask] = i32(header_data, 36 + idx * 4)
|
2019-03-15 02:46:34 +03:00
|
|
|
else:
|
|
|
|
# 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
|
|
|
|
# See also
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/1293
|
|
|
|
# There is a 4th component in the RGBQuad, in the alpha
|
|
|
|
# location, but it is listed as a reserved component,
|
|
|
|
# and it is not generally an alpha channel
|
2019-03-21 16:28:20 +03:00
|
|
|
file_info["a_mask"] = 0x0
|
|
|
|
for mask in ["r_mask", "g_mask", "b_mask"]:
|
2019-03-15 02:46:34 +03:00
|
|
|
file_info[mask] = i32(read(4))
|
2019-03-21 16:28:20 +03:00
|
|
|
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"],
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2020-07-16 12:43:29 +03:00
|
|
|
raise OSError(f"Unsupported BMP header type ({file_info['header_size']})")
|
2018-10-24 22:24:03 +03:00
|
|
|
|
2015-04-01 22:38:18 +03:00
|
|
|
# ------------------ Special case : header is reported 40, which
|
|
|
|
# ---------------------- is shorter than real size for bpp >= 16
|
2019-03-21 16:28:20 +03:00
|
|
|
self._size = file_info["width"], file_info["height"]
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# ------- If color count was not found in the header, compute from bits
|
2019-03-21 16:28:20 +03:00
|
|
|
file_info["colors"] = (
|
|
|
|
file_info["colors"]
|
|
|
|
if file_info.get("colors", 0)
|
|
|
|
else (1 << file_info["bits"])
|
|
|
|
)
|
2021-12-21 04:43:50 +03:00
|
|
|
if offset == 14 + file_info["header_size"] and file_info["bits"] <= 8:
|
|
|
|
offset += 4 * file_info["colors"]
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# ---------------------- Check bit depth for unusual unsupported values
|
2019-03-21 16:28:20 +03:00
|
|
|
self.mode, raw_mode = BIT2MODE.get(file_info["bits"], (None, None))
|
2015-03-05 10:19:14 +03:00
|
|
|
if self.mode is None:
|
2020-07-16 12:43:29 +03:00
|
|
|
raise OSError(f"Unsupported BMP pixel depth ({file_info['bits']})")
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# ---------------- Process BMP with Bitfields compression (not palette)
|
2022-03-09 14:35:48 +03:00
|
|
|
decoder_name = "raw"
|
2019-03-21 16:28:20 +03:00
|
|
|
if file_info["compression"] == self.BITFIELDS:
|
2015-03-10 04:08:23 +03:00
|
|
|
SUPPORTED = {
|
2019-03-21 16:28:20 +03:00
|
|
|
32: [
|
|
|
|
(0xFF0000, 0xFF00, 0xFF, 0x0),
|
2022-07-14 00:08:25 +03:00
|
|
|
(0xFF000000, 0xFF0000, 0xFF00, 0x0),
|
2022-07-14 00:25:28 +03:00
|
|
|
(0xFF000000, 0xFF0000, 0xFF00, 0xFF),
|
2019-03-21 16:28:20 +03:00
|
|
|
(0xFF, 0xFF00, 0xFF0000, 0xFF000000),
|
2022-07-14 00:08:25 +03:00
|
|
|
(0xFF0000, 0xFF00, 0xFF, 0xFF000000),
|
2019-03-21 16:28:20 +03:00
|
|
|
(0x0, 0x0, 0x0, 0x0),
|
|
|
|
],
|
|
|
|
24: [(0xFF0000, 0xFF00, 0xFF)],
|
|
|
|
16: [(0xF800, 0x7E0, 0x1F), (0x7C00, 0x3E0, 0x1F)],
|
2015-12-30 23:27:27 +03:00
|
|
|
}
|
2015-03-10 04:08:23 +03:00
|
|
|
MASK_MODES = {
|
2019-03-21 16:28:20 +03:00
|
|
|
(32, (0xFF0000, 0xFF00, 0xFF, 0x0)): "BGRX",
|
|
|
|
(32, (0xFF000000, 0xFF0000, 0xFF00, 0x0)): "XBGR",
|
2022-07-14 00:25:28 +03:00
|
|
|
(32, (0xFF000000, 0xFF0000, 0xFF00, 0xFF)): "ABGR",
|
2019-03-21 16:28:20 +03:00
|
|
|
(32, (0xFF, 0xFF00, 0xFF0000, 0xFF000000)): "RGBA",
|
|
|
|
(32, (0xFF0000, 0xFF00, 0xFF, 0xFF000000)): "BGRA",
|
2015-12-10 01:23:04 +03:00
|
|
|
(32, (0x0, 0x0, 0x0, 0x0)): "BGRA",
|
2019-03-21 16:28:20 +03:00
|
|
|
(24, (0xFF0000, 0xFF00, 0xFF)): "BGR",
|
|
|
|
(16, (0xF800, 0x7E0, 0x1F)): "BGR;16",
|
|
|
|
(16, (0x7C00, 0x3E0, 0x1F)): "BGR;15",
|
2015-12-10 01:23:04 +03:00
|
|
|
}
|
2019-03-21 16:28:20 +03:00
|
|
|
if file_info["bits"] in SUPPORTED:
|
|
|
|
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"])]
|
2019-03-08 11:26:01 +03:00
|
|
|
self.mode = "RGBA" if "A" in raw_mode else self.mode
|
2019-03-21 16:28:20 +03:00
|
|
|
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"])]
|
2015-03-05 10:19:14 +03:00
|
|
|
else:
|
2019-09-30 17:56:31 +03:00
|
|
|
raise OSError("Unsupported BMP bitfields layout")
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2019-09-30 17:56:31 +03:00
|
|
|
raise OSError("Unsupported BMP bitfields layout")
|
2019-03-21 16:28:20 +03:00
|
|
|
elif file_info["compression"] == self.RAW:
|
|
|
|
if file_info["bits"] == 32 and header == 22: # 32-bit .cur offset
|
2015-03-05 12:40:10 +03:00
|
|
|
raw_mode, self.mode = "BGRA", "RGBA"
|
2022-03-09 14:35:48 +03:00
|
|
|
elif file_info["compression"] == self.RLE8:
|
2022-10-22 02:44:48 +03:00
|
|
|
decoder_name = "bmp_rle8"
|
|
|
|
elif file_info["compression"] == self.RLE4:
|
|
|
|
decoder_name = "bmp_rle4"
|
2015-03-05 12:40:10 +03:00
|
|
|
else:
|
2020-07-16 12:43:29 +03:00
|
|
|
raise OSError(f"Unsupported BMP compression ({file_info['compression']})")
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# --------------- Once the header is processed, process the palette/LUT
|
2015-03-05 10:19:14 +03:00
|
|
|
if self.mode == "P": # Paletted for 1, 4 and 8 bit images
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# ---------------------------------------------------- 1-bit images
|
2019-03-21 16:28:20 +03:00
|
|
|
if not (0 < file_info["colors"] <= 65536):
|
2020-07-16 12:43:29 +03:00
|
|
|
raise OSError(f"Unsupported BMP Palette size ({file_info['colors']})")
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2019-03-21 16:28:20 +03:00
|
|
|
padding = file_info["palette_padding"]
|
|
|
|
palette = read(padding * file_info["colors"])
|
2015-03-05 10:19:14 +03:00
|
|
|
greyscale = True
|
2019-03-21 16:28:20 +03:00
|
|
|
indices = (
|
2022-10-22 02:44:48 +03:00
|
|
|
(0, file_info["colors"])
|
2019-03-21 16:28:20 +03:00
|
|
|
if file_info["colors"] == 2
|
|
|
|
else list(range(file_info["colors"]))
|
|
|
|
)
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# ----------------- Check if greyscale and ignore palette if so
|
2015-03-05 10:19:14 +03:00
|
|
|
for ind, val in enumerate(indices):
|
2019-03-21 16:28:20 +03:00
|
|
|
rgb = palette[ind * padding : ind * padding + 3]
|
2015-03-05 10:19:14 +03:00
|
|
|
if rgb != o8(val) * 3:
|
|
|
|
greyscale = False
|
2018-10-24 22:24:03 +03:00
|
|
|
|
|
|
|
# ------- If all colors are grey, white or black, ditch palette
|
2015-03-05 10:19:14 +03:00
|
|
|
if greyscale:
|
2019-03-21 16:28:20 +03:00
|
|
|
self.mode = "1" if file_info["colors"] == 2 else "L"
|
2015-03-05 10:19:14 +03:00
|
|
|
raw_mode = self.mode
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2015-03-05 10:19:14 +03:00
|
|
|
self.mode = "P"
|
2018-06-24 15:32:25 +03:00
|
|
|
self.palette = ImagePalette.raw(
|
2019-03-21 16:28:20 +03:00
|
|
|
"BGRX" if padding == 4 else "BGR", palette
|
|
|
|
)
|
2015-04-01 22:38:18 +03:00
|
|
|
|
2018-10-24 22:24:03 +03:00
|
|
|
# ---------------------------- Finally set the tile data for the plugin
|
2019-03-21 16:28:20 +03:00
|
|
|
self.info["compression"] = file_info["compression"]
|
2018-06-24 15:32:25 +03:00
|
|
|
self.tile = [
|
2019-03-21 16:28:20 +03:00
|
|
|
(
|
2022-03-09 14:35:48 +03:00
|
|
|
decoder_name,
|
2019-03-21 16:28:20 +03:00
|
|
|
(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"],
|
|
|
|
),
|
|
|
|
)
|
2018-06-24 15:32:25 +03:00
|
|
|
]
|
2015-03-05 10:19:14 +03:00
|
|
|
|
2015-03-05 13:32:03 +03:00
|
|
|
def _open(self):
|
2021-08-12 14:50:09 +03:00
|
|
|
"""Open file, check magic number and read header"""
|
2015-03-05 13:32:03 +03:00
|
|
|
# read 14 bytes: magic number, filesize, reserved, header final offset
|
|
|
|
head_data = self.fp.read(14)
|
|
|
|
# choke if the file does not have the required magic bytes
|
2020-06-23 11:09:12 +03:00
|
|
|
if not _accept(head_data):
|
2015-03-05 13:32:03 +03:00
|
|
|
raise SyntaxError("Not a BMP file")
|
2015-04-01 22:38:18 +03:00
|
|
|
# read the start position of the BMP image data (u32)
|
2020-05-08 21:11:58 +03:00
|
|
|
offset = i32(head_data, 10)
|
2015-03-05 13:32:03 +03:00
|
|
|
# load bitmap information (offset=raster info)
|
|
|
|
self._bitmap(offset=offset)
|
|
|
|
|
|
|
|
|
2022-10-22 02:44:48 +03:00
|
|
|
class BmpRle8Decoder(ImageFile.PyDecoder):
|
2022-03-09 14:35:48 +03:00
|
|
|
_pulls_fd = True
|
|
|
|
|
|
|
|
def decode(self, buffer):
|
|
|
|
data = bytearray()
|
2022-03-02 03:55:18 +03:00
|
|
|
x = 0
|
2022-03-05 01:15:07 +03:00
|
|
|
while len(data) < self.state.xsize * self.state.ysize:
|
2022-03-09 14:35:00 +03:00
|
|
|
pixels = self.fd.read(1)
|
2022-03-09 14:35:48 +03:00
|
|
|
byte = self.fd.read(1)
|
2022-03-09 14:35:00 +03:00
|
|
|
if not pixels or not byte:
|
|
|
|
break
|
|
|
|
num_pixels = pixels[0]
|
2022-03-09 14:35:48 +03:00
|
|
|
if num_pixels:
|
|
|
|
# encoded mode
|
2022-03-02 03:55:18 +03:00
|
|
|
if x + num_pixels > self.state.xsize:
|
|
|
|
# Too much data for row
|
|
|
|
num_pixels = max(0, self.state.xsize - x)
|
2022-03-09 14:35:48 +03:00
|
|
|
data += byte * num_pixels
|
2022-03-02 03:55:18 +03:00
|
|
|
x += num_pixels
|
2022-03-09 14:35:48 +03:00
|
|
|
else:
|
|
|
|
if byte[0] == 0:
|
|
|
|
# end of line
|
|
|
|
while len(data) % self.state.xsize != 0:
|
|
|
|
data += b"\x00"
|
2022-03-02 03:55:18 +03:00
|
|
|
x = 0
|
2022-03-09 14:35:48 +03:00
|
|
|
elif byte[0] == 1:
|
|
|
|
# end of bitmap
|
|
|
|
break
|
|
|
|
elif byte[0] == 2:
|
|
|
|
# delta
|
2022-03-09 14:35:00 +03:00
|
|
|
bytes_read = self.fd.read(2)
|
|
|
|
if len(bytes_read) < 2:
|
|
|
|
break
|
2022-03-09 14:35:48 +03:00
|
|
|
right, up = self.fd.read(2)
|
|
|
|
data += b"\x00" * (right + up * self.state.xsize)
|
2022-03-02 03:55:18 +03:00
|
|
|
x = len(data) % self.state.xsize
|
2022-03-09 14:35:48 +03:00
|
|
|
else:
|
|
|
|
# absolute mode
|
2022-03-09 14:35:00 +03:00
|
|
|
bytes_read = self.fd.read(byte[0])
|
|
|
|
data += bytes_read
|
|
|
|
if len(bytes_read) < byte[0]:
|
|
|
|
break
|
2022-03-02 03:55:18 +03:00
|
|
|
x += byte[0]
|
2022-03-09 14:35:48 +03:00
|
|
|
|
|
|
|
# align to 16-bit word boundary
|
|
|
|
if self.fd.tell() % 2 != 0:
|
|
|
|
self.fd.seek(1, os.SEEK_CUR)
|
2022-06-20 14:01:10 +03:00
|
|
|
rawmode = "L" if self.mode == "L" else "P"
|
|
|
|
self.set_as_raw(bytes(data), (rawmode, 0, self.args[-1]))
|
2022-03-09 14:35:48 +03:00
|
|
|
return -1, 0
|
|
|
|
|
|
|
|
|
2022-10-22 02:44:48 +03:00
|
|
|
class BmpRle4Decoder(ImageFile.PyDecoder):
|
|
|
|
_pulls_fd = True
|
|
|
|
|
|
|
|
def decode(self, buffer):
|
|
|
|
data = bytearray()
|
|
|
|
x = 0
|
|
|
|
while len(data) < self.state.xsize * self.state.ysize:
|
|
|
|
pixels = self.fd.read(1)
|
|
|
|
byte = self.fd.read(1)
|
|
|
|
if not pixels or not byte:
|
|
|
|
break
|
|
|
|
num_pixels = pixels[0]
|
|
|
|
if num_pixels:
|
|
|
|
# encoded mode
|
|
|
|
if x + num_pixels > self.state.xsize:
|
|
|
|
# Too much data for row
|
|
|
|
num_pixels = max(0, self.state.xsize - x)
|
|
|
|
first_pixel = o8(byte[0] >> 4)
|
|
|
|
second_pixel = o8(byte[0] & 0x0f)
|
|
|
|
for index in range(num_pixels):
|
|
|
|
if index % 2 == 0:
|
|
|
|
data += first_pixel
|
|
|
|
else:
|
|
|
|
data += second_pixel
|
|
|
|
x += num_pixels
|
|
|
|
else:
|
|
|
|
if byte[0] == 0:
|
|
|
|
# end of line
|
|
|
|
while len(data) % self.state.xsize != 0:
|
|
|
|
data += b"\x00"
|
|
|
|
x = 0
|
|
|
|
elif byte[0] == 1:
|
|
|
|
# end of bitmap
|
|
|
|
break
|
|
|
|
elif byte[0] == 2:
|
|
|
|
# delta
|
|
|
|
bytes_read = self.fd.read(2)
|
|
|
|
if len(bytes_read) < 2:
|
|
|
|
break
|
|
|
|
right, up = self.fd.read(2)
|
|
|
|
data += b"\x00" * (right + up * self.state.xsize)
|
|
|
|
x = len(data) % self.state.xsize
|
|
|
|
else:
|
|
|
|
# absolute mode (2 pixels per byte)
|
|
|
|
total_bytes_to_read = byte[0] // 2
|
|
|
|
bytes_read = self.fd.read(total_bytes_to_read)
|
|
|
|
for byte_read in bytes_read:
|
|
|
|
first_pixel = o8(byte_read >> 4)
|
|
|
|
data += first_pixel
|
|
|
|
second_pixel = o8(byte_read & 0x0f)
|
|
|
|
data += second_pixel
|
|
|
|
if len(bytes_read) < total_bytes_to_read:
|
|
|
|
break
|
|
|
|
x += byte[0]
|
|
|
|
|
|
|
|
# align to 16-bit word boundary
|
|
|
|
if self.fd.tell() % 2 != 0:
|
|
|
|
self.fd.seek(1, os.SEEK_CUR)
|
|
|
|
rawmode = "L" if self.mode == "L" else "P"
|
|
|
|
self.set_as_raw(bytes(data), (rawmode, 0, self.args[-1]))
|
|
|
|
return -1, 0
|
|
|
|
|
2018-10-24 22:24:03 +03:00
|
|
|
# =============================================================================
|
2015-03-05 10:19:14 +03:00
|
|
|
# Image plugin for the DIB format (BMP alias)
|
2018-10-24 22:24:03 +03:00
|
|
|
# =============================================================================
|
2010-07-31 06:52:47 +04:00
|
|
|
class DibImageFile(BmpImageFile):
|
|
|
|
|
|
|
|
format = "DIB"
|
|
|
|
format_description = "Windows Bitmap"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
self._bitmap()
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Write BMP file
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
SAVE = {
|
|
|
|
"1": ("1", 1, 2),
|
|
|
|
"L": ("L", 8, 256),
|
|
|
|
"P": ("P", 8, 256),
|
|
|
|
"RGB": ("BGR", 24, 0),
|
2015-03-05 10:19:14 +03:00
|
|
|
"RGBA": ("BGRA", 32, 0),
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-06 05:59:07 +03:00
|
|
|
def _dib_save(im, fp, filename):
|
|
|
|
_save(im, fp, filename, False)
|
|
|
|
|
|
|
|
|
|
|
|
def _save(im, fp, filename, bitmap_header=True):
|
2010-07-31 06:52:47 +04:00
|
|
|
try:
|
|
|
|
rawmode, bits, colors = SAVE[im.mode]
|
2020-06-21 13:13:35 +03:00
|
|
|
except KeyError as e:
|
2020-07-16 12:43:29 +03:00
|
|
|
raise OSError(f"cannot write mode {im.mode} as BMP") from e
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-06-29 00:18:47 +04:00
|
|
|
info = im.encoderinfo
|
|
|
|
|
|
|
|
dpi = info.get("dpi", (96, 96))
|
|
|
|
|
|
|
|
# 1 meter == 39.3701 inches
|
2019-03-30 07:03:57 +03:00
|
|
|
ppm = tuple(map(lambda x: int(x * 39.3701 + 0.5), dpi))
|
2014-06-29 00:18:47 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
stride = ((im.size[0] * bits + 7) // 8 + 3) & (~3)
|
2014-08-26 17:47:10 +04:00
|
|
|
header = 40 # or 64 for OS/2 version 2
|
|
|
|
image = stride * im.size[1]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2022-09-13 02:10:03 +03:00
|
|
|
if im.mode == "1":
|
|
|
|
palette = b"".join(o8(i) * 4 for i in (0, 255))
|
|
|
|
elif im.mode == "L":
|
|
|
|
palette = b"".join(o8(i) * 4 for i in range(256))
|
|
|
|
elif im.mode == "P":
|
|
|
|
palette = im.im.getpalette("RGB", "BGRX")
|
|
|
|
colors = len(palette) // 4
|
|
|
|
else:
|
|
|
|
palette = None
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# bitmap header
|
2019-03-06 05:59:07 +03:00
|
|
|
if bitmap_header:
|
|
|
|
offset = 14 + header + colors * 4
|
2019-12-21 00:37:40 +03:00
|
|
|
file_size = offset + image
|
2022-03-04 08:42:24 +03:00
|
|
|
if file_size > 2**32 - 1:
|
2019-12-21 00:37:40 +03:00
|
|
|
raise ValueError("File size is too large for the BMP format")
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(
|
2019-12-21 00:15:48 +03:00
|
|
|
b"BM" # file type (magic)
|
2019-12-21 00:37:40 +03:00
|
|
|
+ o32(file_size) # file size
|
2019-12-21 00:15:48 +03:00
|
|
|
+ o32(0) # reserved
|
|
|
|
+ o32(offset) # image data offset
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# bitmap info header
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(
|
|
|
|
o32(header) # info header size
|
|
|
|
+ o32(im.size[0]) # width
|
|
|
|
+ o32(im.size[1]) # height
|
|
|
|
+ o16(1) # planes
|
|
|
|
+ o16(bits) # depth
|
|
|
|
+ o32(0) # compression (0=uncompressed)
|
|
|
|
+ o32(image) # size of bitmap
|
|
|
|
+ o32(ppm[0]) # resolution
|
|
|
|
+ o32(ppm[1]) # resolution
|
|
|
|
+ o32(colors) # colors used
|
|
|
|
+ o32(colors) # colors important
|
|
|
|
)
|
|
|
|
|
|
|
|
fp.write(b"\0" * (header - 40)) # padding (for OS/2 format)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2022-09-13 02:10:03 +03:00
|
|
|
if palette:
|
|
|
|
fp.write(palette)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, stride, -1))])
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Registry
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
Image.register_open(BmpImageFile.format, BmpImageFile, _accept)
|
|
|
|
Image.register_save(BmpImageFile.format, _save)
|
|
|
|
|
|
|
|
Image.register_extension(BmpImageFile.format, ".bmp")
|
2015-06-16 20:33:27 +03:00
|
|
|
|
|
|
|
Image.register_mime(BmpImageFile.format, "image/bmp")
|
2019-03-06 05:59:07 +03:00
|
|
|
|
2022-10-22 02:44:48 +03:00
|
|
|
Image.register_decoder("bmp_rle8", BmpRle8Decoder)
|
|
|
|
Image.register_decoder("bmp_rle4", BmpRle4Decoder)
|
2022-03-09 14:35:48 +03:00
|
|
|
|
2019-03-06 05:59:07 +03:00
|
|
|
Image.register_open(DibImageFile.format, DibImageFile, _dib_accept)
|
|
|
|
Image.register_save(DibImageFile.format, _dib_save)
|
|
|
|
|
|
|
|
Image.register_extension(DibImageFile.format, ".dib")
|
|
|
|
|
|
|
|
Image.register_mime(DibImageFile.format, "image/bmp")
|