CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
FpxImagePlugin.py260 linesDownload Raw Back to PIL
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
18
19import olefile
20
21from . import Image, ImageFile
22from ._binary import i32le as i32
23
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}
38
39
40#
41# --------------------------------------------------------------------
42
43
44def _accept(prefix: bytes) -> bool:
45    return prefix.startswith(olefile.MAGIC)
46
47
48##
49# Image plugin for the FlashPix images.
50
51
52class FpxImageFile(ImageFile.ImageFile):
53    format = "FPX"
54    format_description = "FlashPix"
55
56    def _open(self) -> None:
57        #
58        # read the OLE directory and see if this is a likely
59        # to be a FlashPix file
60
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
67
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)
72
73        self._open_index(1)
74
75    def _open_index(self, index: int = 1) -> None:
76        #
77        # get the Image Contents Property Set
78
79        prop = self.ole.getproperties(
80            [f"Data Object Store {index:06d}", "\005Image Contents"]
81        )
82
83        # size (highest resolution)
84
85        assert isinstance(prop[0x1000002], int)
86        assert isinstance(prop[0x1000003], int)
87        self._size = prop[0x1000002], prop[0x1000003]
88
89        size = max(self.size)
90        i = 1
91        while size > 64:
92            size = size // 2
93            i += 1
94        self.maxid = i - 1
95
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.
101
102        id = self.maxid << 16
103
104        s = prop[0x2000002 | id]
105
106        if not isinstance(s, bytes) or (bands := i32(s, 4)) > 4:
107            msg = "Invalid number of bands"
108            raise OSError(msg)
109
110        # note: for now, we ignore the "uncalibrated" flag
111        colors = tuple(i32(s, 8 + i * 4) & 0x7FFFFFFF for i in range(bands))
112
113        self._mode, self.rawmode = MODES[colors]
114
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]
121
122        self._open_subimage(1, self.maxid)
123
124    def _open_subimage(self, index: int = 1, subimage: int = 0) -> None:
125        #
126        # setup tile descriptors for a given subimage
127
128        stream = [
129            f"Data Object Store {index:06d}",
130            f"Resolution {subimage:04d}",
131            "Subimage 0000 Header",
132        ]
133
134        fp = self.ole.openstream(stream)
135
136        # skip prefix
137        fp.read(28)
138
139        # header stream
140        s = fp.read(36)
141
142        size = i32(s, 4), i32(s, 8)
143        # tilecount = i32(s, 12)
144        tilesize = i32(s, 16), i32(s, 20)
145        # channels = i32(s, 24)
146        offset = i32(s, 28)
147        length = i32(s, 32)
148
149        if size != self.size:
150            msg = "subimage mismatch"
151            raise OSError(msg)
152
153        # get tile descriptors
154        fp.seek(28 + offset)
155        s = fp.read(i32(s, 12) * length)
156
157        x = y = 0
158        xsize, ysize = size
159        xtile, ytile = tilesize
160        self.tile = []
161
162        for i in range(0, len(s), length):
163            x1 = min(xsize, x + xtile)
164            y1 = min(ysize, y + ytile)
165
166            compression = i32(s, i + 8)
167
168            if compression == 0:
169                self.tile.append(
170                    ImageFile._Tile(
171                        "raw",
172                        (x, y, x1, y1),
173                        i32(s, i) + 28,
174                        self.rawmode,
175                    )
176                )
177
178            elif compression == 1:
179                # FIXME: the fill decoder is not implemented
180                self.tile.append(
181                    ImageFile._Tile(
182                        "fill",
183                        (x, y, x1, y1),
184                        i32(s, i) + 28,
185                        (self.rawmode, s[12:16]),
186                    )
187                )
188
189            elif compression == 2:
190                internal_color_conversion = s[14]
191                jpeg_tables = s[15]
192                rawmode = self.rawmode
193
194                if internal_color_conversion:
195                    # The image is stored as usual (usually YCbCr).
196                    if rawmode == "RGBA":
197                        # For "RGBA", data is stored as YCbCrA based on
198                        # negative RGB. The following trick works around
199                        # this problem :
200                        jpegmode, rawmode = "YCbCrK", "CMYK"
201                    else:
202                        jpegmode = None  # let the decoder decide
203
204                else:
205                    # The image is stored as defined by rawmode
206                    jpegmode = rawmode
207
208                self.tile.append(
209                    ImageFile._Tile(
210                        "jpeg",
211                        (x, y, x1, y1),
212                        i32(s, i) + 28,
213                        (rawmode, jpegmode),
214                    )
215                )
216
217                # FIXME: jpeg tables are tile dependent; the prefix
218                # data must be placed in the tile descriptor itself!
219
220                if jpeg_tables:
221                    self.tile_prefix = self.jpeg[jpeg_tables]
222
223            else:
224                msg = "unknown/invalid compression"
225                raise OSError(msg)
226
227            x = x + xtile
228            if x >= xsize:
229                x, y = 0, y + ytile
230                if y >= ysize:
231                    break  # isn't really required
232
233        assert self.fp is not None
234        self.stream = stream
235        self._fp = self.fp
236        self.fp = None
237
238    def load(self) -> Image.core.PixelAccess | None:
239        if not self.fp:
240            self.fp = self.ole.openstream(self.stream[:2] + ["Subimage 0000 Data"])
241
242        return ImageFile.ImageFile.load(self)
243
244    def close(self) -> None:
245        self.ole.close()
246        super().close()
247
248    def __exit__(self, *args: object) -> None:
249        self.ole.close()
250        super().__exit__()
251
252
253#
254# --------------------------------------------------------------------
255
256
257Image.register_open(FpxImageFile.format, FpxImageFile, _accept)
258
259Image.register_extension(FpxImageFile.format, ".fpx")
260 
Aluode/PerceptionLabPortable · CoolFace