Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/SunImagePlugin.py: 14%

50 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

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# 

18 

19 

20from . import Image, ImageFile, ImagePalette 

21from ._binary import i32be as i32 

22 

23 

24def _accept(prefix): 

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 

34 format = "SUN" 

35 format_description = "Sun Raster File" 

36 

37 def _open(self): 

38 

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

40 # and has the following format: 

41 

42 # typedef struct _SunRaster 

43 # { 

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

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

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

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

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

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

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

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

52 # } SUNRASTER; 

53 

54 # HEAD 

55 s = self.fp.read(32) 

56 if not _accept(s): 

57 raise SyntaxError("not an SUN raster file") 

58 

59 offset = 32 

60 

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

62 

63 depth = i32(s, 12) 

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

65 file_type = i32(s, 20) 

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

67 palette_length = i32(s, 28) 

68 

69 if depth == 1: 

70 self.mode, rawmode = "1", "1;I" 

71 elif depth == 4: 

72 self.mode, rawmode = "L", "L;4" 

73 elif depth == 8: 

74 self.mode = rawmode = "L" 

75 elif depth == 24: 

76 if file_type == 3: 

77 self.mode, rawmode = "RGB", "RGB" 

78 else: 

79 self.mode, rawmode = "RGB", "BGR" 

80 elif depth == 32: 

81 if file_type == 3: 

82 self.mode, rawmode = "RGB", "RGBX" 

83 else: 

84 self.mode, rawmode = "RGB", "BGRX" 

85 else: 

86 raise SyntaxError("Unsupported Mode/Bit Depth") 

87 

88 if palette_length: 

89 if palette_length > 1024: 

90 raise SyntaxError("Unsupported Color Palette Length") 

91 

92 if palette_type != 1: 

93 raise SyntaxError("Unsupported Palette Type") 

94 

95 offset = offset + palette_length 

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

97 if self.mode == "L": 

98 self.mode = "P" 

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

100 

101 # 16 bit boundaries on stride 

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

103 

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

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

106 # field: 

107 # 0000h Old 

108 # 0001h Standard 

109 # 0002h Byte-encoded 

110 # 0003h RGB format 

111 # 0004h TIFF format 

112 # 0005h IFF format 

113 # FFFFh Experimental 

114 

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

116 # byte-encoded is run-length-encoded 

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

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

119 # Experimental means that it's something else. 

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

121 

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

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

124 elif file_type == 2: 

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

126 else: 

127 raise SyntaxError("Unsupported Sun Raster file type") 

128 

129 

130# 

131# registry 

132 

133 

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

135 

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