Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/XbmImagePlugin.py: 61%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#
2# The Python Imaging Library.
3# $Id$
4#
5# XBM File handling
6#
7# History:
8# 1995-09-08 fl Created
9# 1996-11-01 fl Added save support
10# 1997-07-07 fl Made header parser more tolerant
11# 1997-07-22 fl Fixed yet another parser bug
12# 2001-02-17 fl Use 're' instead of 'regex' (Python 2.1) (0.4)
13# 2001-05-13 fl Added hotspot handling (based on code from Bernhard Herzog)
14# 2004-02-24 fl Allow some whitespace before first #define
15#
16# Copyright (c) 1997-2004 by Secret Labs AB
17# Copyright (c) 1996-1997 by Fredrik Lundh
18#
19# See the README file for information on usage and redistribution.
20#
21from __future__ import annotations
23import re
24from typing import IO
26from . import Image, ImageFile
28# XBM header
29xbm_head = re.compile(
30 rb"\s*#define[ \t]+.*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
31 b"#define[ \t]+.*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
32 b"(?P<hotspot>"
33 b"#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
34 b"#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
35 b")?"
36 rb"[\000-\377]*_bits\[]"
37)
40def _accept(prefix: bytes) -> bool:
41 return prefix.lstrip().startswith(b"#define")
44##
45# Image plugin for X11 bitmaps.
48class XbmImageFile(ImageFile.ImageFile):
49 format = "XBM"
50 format_description = "X11 Bitmap"
52 def _open(self) -> None:
53 assert self.fp is not None
55 m = xbm_head.match(self.fp.read(512))
57 if not m:
58 msg = "not a XBM file"
59 raise SyntaxError(msg)
61 xsize = int(m.group("width"))
62 ysize = int(m.group("height"))
64 if m.group("hotspot"):
65 self.info["hotspot"] = (int(m.group("xhot")), int(m.group("yhot")))
67 self._mode = "1"
68 self._size = xsize, ysize
70 self.tile = [ImageFile._Tile("xbm", (0, 0) + self.size, m.end())]
73def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
74 if im.mode != "1":
75 msg = f"cannot write mode {im.mode} as XBM"
76 raise OSError(msg)
78 fp.write(f"#define im_width {im.size[0]}\n".encode("ascii"))
79 fp.write(f"#define im_height {im.size[1]}\n".encode("ascii"))
81 hotspot = im.encoderinfo.get("hotspot")
82 if hotspot:
83 fp.write(f"#define im_x_hot {hotspot[0]}\n".encode("ascii"))
84 fp.write(f"#define im_y_hot {hotspot[1]}\n".encode("ascii"))
86 fp.write(b"static char im_bits[] = {\n")
88 ImageFile._save(im, fp, [ImageFile._Tile("xbm", (0, 0) + im.size)])
90 fp.write(b"};\n")
93Image.register_open(XbmImageFile.format, XbmImageFile, _accept)
94Image.register_save(XbmImageFile.format, _save)
96Image.register_extension(XbmImageFile.format, ".xbm")
98Image.register_mime(XbmImageFile.format, "image/xbm")