Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pillow-10.4.0-py3.8-linux-x86_64.egg/PIL/SunImagePlugin.py: 19%

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

57 statements  

1# 

2# The Python Imaging Library. 

3# $Id$ 

4# 

5# Sun image file handling 

6# 

7# History: 

8# 1995-09-10 fl Created 

9# 1996-05-28 fl Fixed 32-bit alignment 

10# 1998-12-29 fl Import ImagePalette module 

11# 2001-12-18 fl Fixed palette loading (from Jean-Claude Rimbault) 

12# 

13# Copyright (c) 1997-2001 by Secret Labs AB 

14# Copyright (c) 1995-1996 by Fredrik Lundh 

15# 

16# See the README file for information on usage and redistribution. 

17# 

18from __future__ import annotations 

19 

20from . import Image, ImageFile, ImagePalette 

21from ._binary import i32be as i32 

22 

23 

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

25 return len(prefix) >= 4 and i32(prefix) == 0x59A66A95 

26 

27 

28## 

29# Image plugin for Sun raster files. 

30 

31 

32class SunImageFile(ImageFile.ImageFile): 

33 format = "SUN" 

34 format_description = "Sun Raster File" 

35 

36 def _open(self) -> None: 

37 # The Sun Raster file header is 32 bytes in length 

38 # and has the following format: 

39 

40 # typedef struct _SunRaster 

41 # { 

42 # DWORD MagicNumber; /* Magic (identification) number */ 

43 # DWORD Width; /* Width of image in pixels */ 

44 # DWORD Height; /* Height of image in pixels */ 

45 # DWORD Depth; /* Number of bits per pixel */ 

46 # DWORD Length; /* Size of image data in bytes */ 

47 # DWORD Type; /* Type of raster file */ 

48 # DWORD ColorMapType; /* Type of color map */ 

49 # DWORD ColorMapLength; /* Size of the color map in bytes */ 

50 # } SUNRASTER; 

51 

52 assert self.fp is not None 

53 

54 # HEAD 

55 s = self.fp.read(32) 

56 if not _accept(s): 

57 msg = "not an SUN raster file" 

58 raise SyntaxError(msg) 

59 

60 offset = 32 

61 

62 self._size = i32(s, 4), i32(s, 8) 

63 

64 depth = i32(s, 12) 

65 # data_length = i32(s, 16) # unreliable, ignore. 

66 file_type = i32(s, 20) 

67 palette_type = i32(s, 24) # 0: None, 1: RGB, 2: Raw/arbitrary 

68 palette_length = i32(s, 28) 

69 

70 if depth == 1: 

71 self._mode, rawmode = "1", "1;I" 

72 elif depth == 4: 

73 self._mode, rawmode = "L", "L;4" 

74 elif depth == 8: 

75 self._mode = rawmode = "L" 

76 elif depth == 24: 

77 if file_type == 3: 

78 self._mode, rawmode = "RGB", "RGB" 

79 else: 

80 self._mode, rawmode = "RGB", "BGR" 

81 elif depth == 32: 

82 if file_type == 3: 

83 self._mode, rawmode = "RGB", "RGBX" 

84 else: 

85 self._mode, rawmode = "RGB", "BGRX" 

86 else: 

87 msg = "Unsupported Mode/Bit Depth" 

88 raise SyntaxError(msg) 

89 

90 if palette_length: 

91 if palette_length > 1024: 

92 msg = "Unsupported Color Palette Length" 

93 raise SyntaxError(msg) 

94 

95 if palette_type != 1: 

96 msg = "Unsupported Palette Type" 

97 raise SyntaxError(msg) 

98 

99 offset = offset + palette_length 

100 self.palette = ImagePalette.raw("RGB;L", self.fp.read(palette_length)) 

101 if self.mode == "L": 

102 self._mode = "P" 

103 rawmode = rawmode.replace("L", "P") 

104 

105 # 16 bit boundaries on stride 

106 stride = ((self.size[0] * depth + 15) // 16) * 2 

107 

108 # file type: Type is the version (or flavor) of the bitmap 

109 # file. The following values are typically found in the Type 

110 # field: 

111 # 0000h Old 

112 # 0001h Standard 

113 # 0002h Byte-encoded 

114 # 0003h RGB format 

115 # 0004h TIFF format 

116 # 0005h IFF format 

117 # FFFFh Experimental 

118 

119 # Old and standard are the same, except for the length tag. 

120 # byte-encoded is run-length-encoded 

121 # RGB looks similar to standard, but RGB byte order 

122 # TIFF and IFF mean that they were converted from T/IFF 

123 # Experimental means that it's something else. 

124 # (https://www.fileformat.info/format/sunraster/egff.htm) 

125 

126 if file_type in (0, 1, 3, 4, 5): 

127 self.tile = [("raw", (0, 0) + self.size, offset, (rawmode, stride))] 

128 elif file_type == 2: 

129 self.tile = [("sun_rle", (0, 0) + self.size, offset, rawmode)] 

130 else: 

131 msg = "Unsupported Sun Raster file type" 

132 raise SyntaxError(msg) 

133 

134 

135# 

136# registry 

137 

138 

139Image.register_open(SunImageFile.format, SunImageFile, _accept) 

140 

141Image.register_extension(SunImageFile.format, ".ras")