CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
SgiImagePlugin.py232 linesDownload Raw Back to PIL
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        # determine mode from bits/zsize
86        try:
87            rawmode = MODES[(bpc, dimension, zsize)]
88        except KeyError:
89            msg = "Unsupported SGI image mode"
90            raise ValueError(msg)
91
92        self._size = xsize, ysize
93        self._mode = rawmode.split(";")[0]
94        if self.mode == "RGB":
95            self.custom_mimetype = "image/rgb"
96
97        # orientation -1 : scanlines begins at the bottom-left corner
98        orientation = -1
99
100        # decoder info
101        if compression == 0:
102            pagesize = xsize * ysize * bpc
103            if bpc == 2:
104                self.tile = [
105                    ImageFile._Tile(
106                        "SGI16",
107                        (0, 0) + self.size,
108                        headlen,
109                        (self.mode, 0, orientation),
110                    )
111                ]
112            else:
113                self.tile = []
114                offset = headlen
115                for layer in self.mode:
116                    self.tile.append(
117                        ImageFile._Tile(
118                            "raw", (0, 0) + self.size, offset, (layer, 0, orientation)
119                        )
120                    )
121                    offset += pagesize
122        elif compression == 1:
123            self.tile = [
124                ImageFile._Tile(
125                    "sgi_rle", (0, 0) + self.size, headlen, (rawmode, orientation, bpc)
126                )
127            ]
128
129
130def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
131    if im.mode not in {"RGB", "RGBA", "L"}:
132        msg = "Unsupported SGI image mode"
133        raise ValueError(msg)
134
135    # Get the keyword arguments
136    info = im.encoderinfo
137
138    # Byte-per-pixel precision, 1 = 8bits per pixel
139    bpc = info.get("bpc", 1)
140
141    if bpc not in (1, 2):
142        msg = "Unsupported number of bytes per pixel"
143        raise ValueError(msg)
144
145    # Flip the image, since the origin of SGI file is the bottom-left corner
146    orientation = -1
147    # Define the file as SGI File Format
148    magic_number = 474
149    # Run-Length Encoding Compression - Unsupported at this time
150    rle = 0
151
152    # X Dimension = width / Y Dimension = height
153    x, y = im.size
154    # Z Dimension: Number of channels
155    z = len(im.mode)
156    # Number of dimensions (x,y,z)
157    if im.mode == "L":
158        dimension = 1 if y == 1 else 2
159    else:
160        dimension = 3
161
162    # Minimum Byte value
163    pinmin = 0
164    # Maximum Byte value (255 = 8bits per pixel)
165    pinmax = 255
166    # Image name (79 characters max, truncated below in write)
167    img_name = os.path.splitext(os.path.basename(filename))[0]
168    if isinstance(img_name, str):
169        img_name = img_name.encode("ascii", "ignore")
170    # Standard representation of pixel in the file
171    colormap = 0
172    fp.write(struct.pack(">h", magic_number))
173    fp.write(o8(rle))
174    fp.write(o8(bpc))
175    fp.write(struct.pack(">H", dimension))
176    fp.write(struct.pack(">H", x))
177    fp.write(struct.pack(">H", y))
178    fp.write(struct.pack(">H", z))
179    fp.write(struct.pack(">l", pinmin))
180    fp.write(struct.pack(">l", pinmax))
181    fp.write(struct.pack("4s", b""))  # dummy
182    fp.write(struct.pack("79s", img_name))  # truncates to 79 chars
183    fp.write(struct.pack("s", b""))  # force null byte after img_name
184    fp.write(struct.pack(">l", colormap))
185    fp.write(struct.pack("404s", b""))  # dummy
186
187    rawmode = "L"
188    if bpc == 2:
189        rawmode = "L;16B"
190
191    for channel in im.split():
192        fp.write(channel.tobytes("raw", rawmode, 0, orientation))
193
194    if hasattr(fp, "flush"):
195        fp.flush()
196
197
198class SGI16Decoder(ImageFile.PyDecoder):
199    _pulls_fd = True
200
201    def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
202        assert self.fd is not None
203        assert self.im is not None
204
205        rawmode, stride, orientation = self.args
206        pagesize = self.state.xsize * self.state.ysize
207        zsize = len(self.mode)
208        self.fd.seek(512)
209
210        for band in range(zsize):
211            channel = Image.new("L", (self.state.xsize, self.state.ysize))
212            channel.frombytes(
213                self.fd.read(2 * pagesize), "raw", "L;16B", stride, orientation
214            )
215            self.im.putband(channel.im, band)
216
217        return -1, 0
218
219
220#
221# registry
222
223
224Image.register_decoder("SGI16", SGI16Decoder)
225Image.register_open(SgiImageFile.format, SgiImageFile, _accept)
226Image.register_save(SgiImageFile.format, _save)
227Image.register_mime(SgiImageFile.format, "image/sgi")
228
229Image.register_extensions(SgiImageFile.format, [".bw", ".rgb", ".rgba", ".sgi"])
230
231# End of file
232 
Aluode/PerceptionLabPortable · CoolFace