Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/FpxImagePlugin.py: 2%
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# THIS IS WORK IN PROGRESS
3#
4# The Python Imaging Library.
5# $Id$
6#
7# FlashPix support for PIL
8#
9# History:
10# 97-01-25 fl Created (reads uncompressed RGB images only)
11#
12# Copyright (c) Secret Labs AB 1997.
13# Copyright (c) Fredrik Lundh 1997.
14#
15# See the README file for information on usage and redistribution.
16#
17from __future__ import annotations
19import olefile
21from . import Image, ImageFile
22from ._binary import i32le as i32
24# we map from colour field tuples to (mode, rawmode) descriptors
25MODES = {
26 # opacity
27 (0x00007FFE,): ("A", "L"),
28 # monochrome
29 (0x00010000,): ("L", "L"),
30 (0x00018000, 0x00017FFE): ("RGBA", "LA"),
31 # photo YCC
32 (0x00020000, 0x00020001, 0x00020002): ("RGB", "YCC;P"),
33 (0x00028000, 0x00028001, 0x00028002, 0x00027FFE): ("RGBA", "YCCA;P"),
34 # standard RGB (NIFRGB)
35 (0x00030000, 0x00030001, 0x00030002): ("RGB", "RGB"),
36 (0x00038000, 0x00038001, 0x00038002, 0x00037FFE): ("RGBA", "RGBA"),
37}
40#
41# --------------------------------------------------------------------
44def _accept(prefix: bytes) -> bool:
45 return prefix.startswith(olefile.MAGIC)
48##
49# Image plugin for the FlashPix images.
52class FpxImageFile(ImageFile.ImageFile):
53 format = "FPX"
54 format_description = "FlashPix"
56 def _open(self) -> None:
57 #
58 # read the OLE directory and see if this is a likely
59 # to be a FlashPix file
61 assert self.fp is not None
62 try:
63 self.ole = olefile.OleFileIO(self.fp)
64 except OSError as e:
65 msg = "not an FPX file; invalid OLE file"
66 raise SyntaxError(msg) from e
68 root = self.ole.root
69 if not root or root.clsid != "56616700-C154-11CE-8553-00AA00A1F95B":
70 msg = "not an FPX file; bad root CLSID"
71 raise SyntaxError(msg)
73 self._open_index(1)
75 def _open_index(self, index: int = 1) -> None:
76 #
77 # get the Image Contents Property Set
79 prop = self.ole.getproperties(
80 [f"Data Object Store {index:06d}", "\005Image Contents"]
81 )
83 # size (highest resolution)
85 assert isinstance(prop[0x1000002], int)
86 assert isinstance(prop[0x1000003], int)
87 self._size = prop[0x1000002], prop[0x1000003]
89 size = max(self.size)
90 i = 1
91 while size > 64:
92 size = size // 2
93 i += 1
94 self.maxid = i - 1
96 # mode. instead of using a single field for this, flashpix
97 # requires you to specify the mode for each channel in each
98 # resolution subimage, and leaves it to the decoder to make
99 # sure that they all match. for now, we'll cheat and assume
100 # that this is always the case.
102 id = self.maxid << 16
104 s = prop[0x2000002 | id]
106 if not isinstance(s, bytes) or (bands := i32(s, 4)) > 4:
107 msg = "Invalid number of bands"
108 raise OSError(msg)
110 # note: for now, we ignore the "uncalibrated" flag
111 colors = tuple(i32(s, 8 + i * 4) & 0x7FFFFFFF for i in range(bands))
113 self._mode, self.rawmode = MODES[colors]
115 # load JPEG tables, if any
116 self.jpeg = {}
117 for i in range(256):
118 id = 0x3000001 | (i << 16)
119 if id in prop:
120 self.jpeg[i] = prop[id]
122 self._open_subimage(1, self.maxid)
124 def _open_subimage(self, index: int = 1, subimage: int = 0) -> None:
125 #
126 # setup tile descriptors for a given subimage
128 stream = [
129 f"Data Object Store {index:06d}",
130 f"Resolution {subimage:04d}",
131 "Subimage 0000 Header",
132 ]
134 fp = self.ole.openstream(stream)
136 # skip prefix
137 fp.read(28)
139 # header stream
140 s = fp.read(36)
142 size = i32(s, 4), i32(s, 8)
143 # tilecount = i32(s, 12)
144 xtile, ytile = i32(s, 16), i32(s, 20)
145 if xtile != 64 or ytile != 64:
146 msg = "Tile must be 64 pixels by 64 pixels"
147 raise ValueError(msg)
149 # channels = i32(s, 24)
150 offset = i32(s, 28)
151 length = i32(s, 32)
153 if size != self.size:
154 msg = "subimage mismatch"
155 raise OSError(msg)
157 # get tile descriptors
158 fp.seek(28 + offset)
159 s = fp.read(i32(s, 12) * length)
161 x = y = 0
162 xsize, ysize = size
163 self.tile = []
165 for i in range(0, len(s), length):
166 x1 = min(xsize, x + xtile)
167 y1 = min(ysize, y + ytile)
169 compression = i32(s, i + 8)
171 if compression == 0:
172 self.tile.append(
173 ImageFile._Tile(
174 "raw",
175 (x, y, x1, y1),
176 i32(s, i) + 28,
177 self.rawmode,
178 )
179 )
181 elif compression == 1:
182 # FIXME: the fill decoder is not implemented
183 self.tile.append(
184 ImageFile._Tile(
185 "fill",
186 (x, y, x1, y1),
187 i32(s, i) + 28,
188 (self.rawmode, s[12:16]),
189 )
190 )
192 elif compression == 2:
193 internal_color_conversion = s[14]
194 jpeg_tables = s[15]
195 rawmode = self.rawmode
197 if internal_color_conversion:
198 # The image is stored as usual (usually YCbCr).
199 if rawmode == "RGBA":
200 # For "RGBA", data is stored as YCbCrA based on
201 # negative RGB. The following trick works around
202 # this problem :
203 jpegmode, rawmode = "YCbCrK", "CMYK"
204 else:
205 jpegmode = None # let the decoder decide
207 else:
208 # The image is stored as defined by rawmode
209 jpegmode = rawmode
211 self.tile.append(
212 ImageFile._Tile(
213 "jpeg",
214 (x, y, x1, y1),
215 i32(s, i) + 28,
216 (rawmode, jpegmode),
217 )
218 )
220 # FIXME: jpeg tables are tile dependent; the prefix
221 # data must be placed in the tile descriptor itself!
223 if jpeg_tables:
224 self.tile_prefix = self.jpeg[jpeg_tables]
226 else:
227 msg = "unknown/invalid compression"
228 raise OSError(msg)
230 x += xtile
231 if x >= xsize:
232 x, y = 0, y + ytile
233 if y >= ysize:
234 break # isn't really required
236 assert self.fp is not None
237 self.stream = stream
238 self._fp = self.fp
239 self.fp = None
241 def load(self) -> Image.core.PixelAccess | None:
242 if not self.fp:
243 self.fp = self.ole.openstream(self.stream[:2] + ["Subimage 0000 Data"])
245 return ImageFile.ImageFile.load(self)
247 def close(self) -> None:
248 self.ole.close()
249 super().close()
251 def __exit__(self, *args: object) -> None:
252 self.ole.close()
253 super().__exit__()
256#
257# --------------------------------------------------------------------
260Image.register_open(FpxImageFile.format, FpxImageFile, _accept)
262Image.register_extension(FpxImageFile.format, ".fpx")