1#
2# The Python Imaging Library.
3# $Id$
4#
5# SGI image file handling
6#
7# See "The SGI Image File Format (Draft version 0.97)", Paul Haeberli.
8# <ftp://ftp.sgi.com/graphics/SGIIMAGESPEC>
9#
10#
11# History:
12# 2017-22-07 mb Add RLE decompression
13# 2016-16-10 mb Add save method without compression
14# 1995-09-10 fl Created
15#
16# Copyright (c) 2016 by Mickael Bonfill.
17# Copyright (c) 2008 by Karsten Hiddemann.
18# Copyright (c) 1997 by Secret Labs AB.
19# Copyright (c) 1995 by Fredrik Lundh.
20#
21# See the README file for information on usage and redistribution.
22#
23from __future__ import annotations
24
25import os
26import struct
27from typing import IO
28
29from . import Image, ImageFile
30from ._binary import i16be as i16
31from ._binary import o8
32
33
34def _accept(prefix: bytes) -> bool:
35 return len(prefix) >= 2 and i16(prefix) == 474
36
37
38MODES = {
39 (1, 1, 1): "L",
40 (1, 2, 1): "L",
41 (2, 1, 1): "L;16B",
42 (2, 2, 1): "L;16B",
43 (1, 3, 3): "RGB",
44 (2, 3, 3): "RGB;16B",
45 (1, 3, 4): "RGBA",
46 (2, 3, 4): "RGBA;16B",
47}
48
49
50##
51# Image plugin for SGI images.
52class SgiImageFile(ImageFile.ImageFile):
53 format = "SGI"
54 format_description = "SGI Image File Format"
55
56 def _open(self) -> None:
57 # HEAD
58 assert self.fp is not None
59
60 headlen = 512
61 s = self.fp.read(headlen)
62
63 if not _accept(s):
64 msg = "Not an SGI image file"
65 raise ValueError(msg)
66
67 # compression : verbatim or RLE
68 compression = s[2]
69
70 # bpc : 1 or 2 bytes (8bits or 16bits)
71 bpc = s[3]
72
73 # dimension : 1, 2 or 3 (depending on xsize, ysize and zsize)
74 dimension = i16(s, 4)
75
76 # xsize : width
77 xsize = i16(s, 6)
78
79 # ysize : height
80 ysize = i16(s, 8)
81
82 # zsize : channels count
83 zsize = i16(s, 10)
84
85 # layout
86 layout = bpc, dimension, zsize
87
88 # determine mode from bits/zsize
89 rawmode = ""
90 try:
91 rawmode = MODES[layout]
92 except KeyError:
93 pass
94
95 if rawmode == "":
96 msg = "Unsupported SGI image mode"
97 raise ValueError(msg)
98
99 self._size = xsize, ysize
100 self._mode = rawmode.split(";")[0]
101 if self.mode == "RGB":
102 self.custom_mimetype = "image/rgb"
103
104 # orientation -1 : scanlines begins at the bottom-left corner
105 orientation = -1
106
107 # decoder info
108 if compression == 0:
109 pagesize = xsize * ysize * bpc
110 if bpc == 2:
111 self.tile = [
112 ("SGI16", (0, 0) + self.size, headlen, (self.mode, 0, orientation))
113 ]
114 else:
115 self.tile = []
116 offset = headlen
117 for layer in self.mode:
118 self.tile.append(
119 ("raw", (0, 0) + self.size, offset, (layer, 0, orientation))
120 )
121 offset += pagesize
122 elif compression == 1:
123 self.tile = [
124 ("sgi_rle", (0, 0) + self.size, headlen, (rawmode, orientation, bpc))
125 ]
126
127
128def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
129 if im.mode not in {"RGB", "RGBA", "L"}:
130 msg = "Unsupported SGI image mode"
131 raise ValueError(msg)
132
133 # Get the keyword arguments
134 info = im.encoderinfo
135
136 # Byte-per-pixel precision, 1 = 8bits per pixel
137 bpc = info.get("bpc", 1)
138
139 if bpc not in (1, 2):
140 msg = "Unsupported number of bytes per pixel"
141 raise ValueError(msg)
142
143 # Flip the image, since the origin of SGI file is the bottom-left corner
144 orientation = -1
145 # Define the file as SGI File Format
146 magic_number = 474
147 # Run-Length Encoding Compression - Unsupported at this time
148 rle = 0
149
150 # Number of dimensions (x,y,z)
151 dim = 3
152 # X Dimension = width / Y Dimension = height
153 x, y = im.size
154 if im.mode == "L" and y == 1:
155 dim = 1
156 elif im.mode == "L":
157 dim = 2
158 # Z Dimension: Number of channels
159 z = len(im.mode)
160
161 if dim in {1, 2}:
162 z = 1
163
164 # assert we've got the right number of bands.
165 if len(im.getbands()) != z:
166 msg = f"incorrect number of bands in SGI write: {z} vs {len(im.getbands())}"
167 raise ValueError(msg)
168
169 # Minimum Byte value
170 pinmin = 0
171 # Maximum Byte value (255 = 8bits per pixel)
172 pinmax = 255
173 # Image name (79 characters max, truncated below in write)
174 img_name = os.path.splitext(os.path.basename(filename))[0]
175 if isinstance(img_name, str):
176 img_name = img_name.encode("ascii", "ignore")
177 # Standard representation of pixel in the file
178 colormap = 0
179 fp.write(struct.pack(">h", magic_number))
180 fp.write(o8(rle))
181 fp.write(o8(bpc))
182 fp.write(struct.pack(">H", dim))
183 fp.write(struct.pack(">H", x))
184 fp.write(struct.pack(">H", y))
185 fp.write(struct.pack(">H", z))
186 fp.write(struct.pack(">l", pinmin))
187 fp.write(struct.pack(">l", pinmax))
188 fp.write(struct.pack("4s", b"")) # dummy
189 fp.write(struct.pack("79s", img_name)) # truncates to 79 chars
190 fp.write(struct.pack("s", b"")) # force null byte after img_name
191 fp.write(struct.pack(">l", colormap))
192 fp.write(struct.pack("404s", b"")) # dummy
193
194 rawmode = "L"
195 if bpc == 2:
196 rawmode = "L;16B"
197
198 for channel in im.split():
199 fp.write(channel.tobytes("raw", rawmode, 0, orientation))
200
201 if hasattr(fp, "flush"):
202 fp.flush()
203
204
205class SGI16Decoder(ImageFile.PyDecoder):
206 _pulls_fd = True
207
208 def decode(self, buffer: bytes) -> tuple[int, int]:
209 assert self.fd is not None
210 assert self.im is not None
211
212 rawmode, stride, orientation = self.args
213 pagesize = self.state.xsize * self.state.ysize
214 zsize = len(self.mode)
215 self.fd.seek(512)
216
217 for band in range(zsize):
218 channel = Image.new("L", (self.state.xsize, self.state.ysize))
219 channel.frombytes(
220 self.fd.read(2 * pagesize), "raw", "L;16B", stride, orientation
221 )
222 self.im.putband(channel.im, band)
223
224 return -1, 0
225
226
227#
228# registry
229
230
231Image.register_decoder("SGI16", SGI16Decoder)
232Image.register_open(SgiImageFile.format, SgiImageFile, _accept)
233Image.register_save(SgiImageFile.format, _save)
234Image.register_mime(SgiImageFile.format, "image/sgi")
235
236Image.register_extensions(SgiImageFile.format, [".bw", ".rgb", ".rgba", ".sgi"])
237
238# End of file