Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/DcxImagePlugin.py: 89%

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

46 statements  

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 

25__lazy_modules__ = {"PIL._binary", "PIL._util"} 

26 

27from . import Image 

28from ._binary import i32le as i32 

29from ._util import DeferredError 

30from .PcxImagePlugin import PcxImageFile 

31 

32MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then? 

33 

34 

35def _accept(prefix: bytes) -> bool: 

36 return len(prefix) >= 4 and i32(prefix) == MAGIC 

37 

38 

39## 

40# Image plugin for the Intel DCX format. 

41 

42 

43class DcxImageFile(PcxImageFile): 

44 format = "DCX" 

45 format_description = "Intel DCX" 

46 _close_exclusive_fp_after_loading = False 

47 

48 def _open(self) -> None: 

49 # Header 

50 assert self.fp is not None 

51 s = self.fp.read(4) 

52 if not _accept(s): 

53 msg = "not a DCX file" 

54 raise SyntaxError(msg) 

55 

56 # Component directory 

57 self._offset = [] 

58 for i in range(1024): 

59 offset = i32(self.fp.read(4)) 

60 if not offset: 

61 break 

62 self._offset.append(offset) 

63 

64 self._fp = self.fp 

65 self.frame = -1 

66 self.n_frames = len(self._offset) 

67 self.is_animated = self.n_frames > 1 

68 self.seek(0) 

69 

70 def seek(self, frame: int) -> None: 

71 if not self._seek_check(frame): 

72 return 

73 if isinstance(self._fp, DeferredError): 

74 raise self._fp.ex 

75 self.frame = frame 

76 self.fp = self._fp 

77 self.fp.seek(self._offset[frame]) 

78 PcxImageFile._open(self) 

79 Image._decompression_bomb_check(self.size) 

80 Image.Image.seek(self, frame) 

81 

82 def tell(self) -> int: 

83 return self.frame 

84 

85 

86Image.register_open(DcxImageFile.format, DcxImageFile, _accept) 

87 

88Image.register_extension(DcxImageFile.format, ".dcx")