Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/XbmImagePlugin.py: 68%
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
25from . import Image, ImageFile
27TYPE_CHECKING = False
28if TYPE_CHECKING:
29 from typing import IO
31# XBM header
32xbm_head = re.compile(
33 rb"\s*#define[ \t]+.*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
34 b"#define[ \t]+.*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
35 b"(?P<hotspot>"
36 b"#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
37 b"#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
38 b")?"
39 rb"[\000-\377]*_bits\[]"
40)
43def _accept(prefix: bytes) -> bool:
44 return prefix.startswith((b"#define", b"\n#define", b"/*"))
47##
48# Image plugin for X11 bitmaps.
51class XbmImageFile(ImageFile.ImageFile):
52 format = "XBM"
53 format_description = "X11 Bitmap"
55 def _open(self) -> None:
56 assert self.fp is not None
58 width = None
59 height = None
60 x_hot = None
61 offset = None
62 starting = True
63 in_block_comment = False
64 while True:
65 line = self.fp.readline(75)
66 if not line:
67 break
68 line = line.rstrip()
69 if in_block_comment:
70 if line.endswith(b"*/"):
71 in_block_comment = False
72 continue
73 if line.startswith(b"/*"):
74 in_block_comment = not line.endswith(b"*/")
75 continue
76 if starting and not line:
77 starting = False
78 continue
79 if not line.startswith(b"#define "):
80 if line.endswith(b"_bits[] = {"):
81 offset = self.fp.tell()
82 break
83 try:
84 value = int(line.split().pop())
85 except ValueError:
86 break
87 if b"width " in line:
88 width = value
89 elif b"height " in line:
90 height = value
91 elif b"_x_hot " in line:
92 x_hot = value
93 elif b"_y_hot " in line:
94 if x_hot is not None:
95 self.info["hotspot"] = x_hot, value
96 if width is None or height is None or offset is None:
97 msg = "not a XBM file"
98 raise SyntaxError(msg)
100 self._mode = "1"
101 self._size = width, height
103 self.tile = [ImageFile._Tile("xbm", None, offset)]
106def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
107 if im.mode != "1":
108 msg = f"cannot write mode {im.mode} as XBM"
109 raise OSError(msg)
111 fp.write(f"#define im_width {im.size[0]}\n".encode("ascii"))
112 fp.write(f"#define im_height {im.size[1]}\n".encode("ascii"))
114 hotspot = im.encoderinfo.get("hotspot")
115 if hotspot:
116 fp.write(f"#define im_x_hot {hotspot[0]}\n".encode("ascii"))
117 fp.write(f"#define im_y_hot {hotspot[1]}\n".encode("ascii"))
119 fp.write(b"static char im_bits[] = {\n")
121 ImageFile._save(im, fp, [ImageFile._Tile("xbm")])
123 fp.write(b"};\n")
126Image.register_open(XbmImageFile.format, XbmImageFile, _accept)
127Image.register_save(XbmImageFile.format, _save)
129Image.register_extension(XbmImageFile.format, ".xbm")
131Image.register_mime(XbmImageFile.format, "image/xbm")