Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
DcxImagePlugin.py85 linesDownload Raw Back to PIL
1#
2# The Python Imaging Library.
3# $Id$
4#
5# DCX file handling
6#
7# DCX is a container file format defined by Intel, commonly used
8# for fax applications.  Each DCX file consists of a directory
9# (a list of file offsets) followed by a set of (usually 1-bit)
10# PCX files.
11#
12# History:
13# 1995-09-09 fl   Created
14# 1996-03-20 fl   Properly derived from PcxImageFile.
15# 1998-07-15 fl   Renamed offset attribute to avoid name clash
16# 2002-07-30 fl   Fixed file handling
17#
18# Copyright (c) 1997-98 by Secret Labs AB.
19# Copyright (c) 1995-96 by Fredrik Lundh.
20#
21# See the README file for information on usage and redistribution.
22#
23from __future__ import annotations
24
25from . import Image
26from ._binary import i32le as i32
27from ._util import DeferredError
28from .PcxImagePlugin import PcxImageFile
29
30MAGIC = 0x3ADE68B1  # QUIZ: what's this value, then?
31
32
33def _accept(prefix: bytes) -> bool:
34    return len(prefix) >= 4 and i32(prefix) == MAGIC
35
36
37##
38# Image plugin for the Intel DCX format.
39
40
41class DcxImageFile(PcxImageFile):
42    format = "DCX"
43    format_description = "Intel DCX"
44    _close_exclusive_fp_after_loading = False
45
46    def _open(self) -> None:
47        # Header
48        assert self.fp is not None
49        s = self.fp.read(4)
50        if not _accept(s):
51            msg = "not a DCX file"
52            raise SyntaxError(msg)
53
54        # Component directory
55        self._offset = []
56        for i in range(1024):
57            offset = i32(self.fp.read(4))
58            if not offset:
59                break
60            self._offset.append(offset)
61
62        self._fp = self.fp
63        self.frame = -1
64        self.n_frames = len(self._offset)
65        self.is_animated = self.n_frames > 1
66        self.seek(0)
67
68    def seek(self, frame: int) -> None:
69        if not self._seek_check(frame):
70            return
71        if isinstance(self._fp, DeferredError):
72            raise self._fp.ex
73        self.frame = frame
74        self.fp = self._fp
75        self.fp.seek(self._offset[frame])
76        PcxImageFile._open(self)
77
78    def tell(self) -> int:
79        return self.frame
80
81
82Image.register_open(DcxImageFile.format, DcxImageFile, _accept)
83
84Image.register_extension(DcxImageFile.format, ".dcx")
85