Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/ImtImagePlugin.py: 72%
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# IM Tools support for PIL
6#
7# history:
8# 1996-05-27 fl Created (read 8-bit images only)
9# 2001-02-17 fl Use 're' instead of 'regex' (Python 2.1) (0.2)
10#
11# Copyright (c) Secret Labs AB 1997-2001.
12# Copyright (c) Fredrik Lundh 1996-2001.
13#
14# See the README file for information on usage and redistribution.
15#
16from __future__ import annotations
18import re
20from . import Image, ImageFile
22#
23# --------------------------------------------------------------------
25field = re.compile(rb"([a-z]*) ([^ \r\n]*)")
28##
29# Image plugin for IM Tools images.
32class ImtImageFile(ImageFile.ImageFile):
33 format = "IMT"
34 format_description = "IM Tools"
36 def _open(self) -> None:
37 # Quick rejection: if there's not a LF among the first
38 # 100 bytes, this is (probably) not a text header.
40 assert self.fp is not None
42 buffer = self.fp.read(100)
43 if b"\n" not in buffer:
44 msg = "not an IM file"
45 raise SyntaxError(msg)
47 xsize = ysize = 0
49 while True:
50 if buffer:
51 s = buffer[:1]
52 buffer = buffer[1:]
53 else:
54 s = self.fp.read(1)
55 if not s:
56 break
58 if s == b"\x0c":
59 # image data begins
60 self.tile = [
61 ImageFile._Tile(
62 "raw",
63 (0, 0) + self.size,
64 self.fp.tell() - len(buffer),
65 self.mode,
66 )
67 ]
69 break
71 else:
72 # read key/value pair
73 if b"\n" not in buffer:
74 buffer += self.fp.read(100)
75 lines = buffer.split(b"\n")
76 s += lines.pop(0)
77 buffer = b"\n".join(lines)
78 if len(s) == 1 or len(s) > 100:
79 break
80 if s[0] == ord(b"*"):
81 continue # comment
83 m = field.match(s)
84 if not m:
85 break
86 k, v = m.group(1, 2)
87 if k == b"width":
88 xsize = int(v)
89 self._size = xsize, ysize
90 elif k == b"height":
91 ysize = int(v)
92 self._size = xsize, ysize
93 elif k == b"pixel" and v == b"n8":
94 self._mode = "L"
97#
98# --------------------------------------------------------------------
100Image.register_open(ImtImageFile.format, ImtImageFile)
102#
103# no extension registered (".im" is simply too common)