Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
lambda-image-processing/python/PIL/SgiImagePlugin.py at master · lpalad/lambda-image-processing · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
lpalad
/
lambda-image-processing
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
lambda-image-processing
/
python
/
PIL
/
SgiImagePlugin.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
247 lines (199 loc) · 6.57 KB
master
Breadcrumbs
lambda-image-processing
/
python
/
PIL
/
SgiImagePlugin.py
Copy path
Top
File metadata and controls
Code
Blame
247 lines (199 loc) · 6.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#
# The Python Imaging Library.
# $Id$
#
# SGI image file handling
#
# See "The SGI Image File Format (Draft version 0.97)", Paul Haeberli.
# <ftp://ftp.sgi.com/graphics/SGIIMAGESPEC>
#
#
# History:
# 2017-22-07 mb Add RLE decompression
# 2016-16-10 mb Add save method without compression
# 1995-09-10 fl Created
#
# Copyright (c) 2016 by Mickael Bonfill.
# Copyright (c) 2008 by Karsten Hiddemann.
# Copyright (c) 1997 by Secret Labs AB.
# Copyright (c) 1995 by Fredrik Lundh.
#
# See the README file for information on usage and redistribution.
#
from
__future__
import
annotations
import
os
import
struct
from
typing
import
IO
from
.
import
Image
,
ImageFile
from
.
_binary
import
i16be
as
i16
from
.
_binary
import
o8
def
_accept
(
prefix
:
bytes
)
->
bool
:
return
len
(
prefix
)
>=
2
and
i16
(
prefix
)
==
474
MODES
=
{
(
1
,
1
,
1
):
"L"
,
(
1
,
2
,
1
):
"L"
,
(
2
,
1
,
1
):
"L;16B"
,
(
2
,
2
,
1
):
"L;16B"
,
(
1
,
3
,
3
):
"RGB"
,
(
2
,
3
,
3
):
"RGB;16B"
,
(
1
,
3
,
4
):
"RGBA"
,
(
2
,
3
,
4
):
"RGBA;16B"
,
}
##
# Image plugin for SGI images.
class
SgiImageFile
(
ImageFile
.
ImageFile
):
format
=
"SGI"
format_description
=
"SGI Image File Format"
def
_open
(
self
)
->
None
:
# HEAD
assert
self
.
fp
is
not
None
headlen
=
512
s
=
self
.
fp
.
read
(
headlen
)
if
not
_accept
(
s
):
msg
=
"Not an SGI image file"
raise
ValueError
(
msg
)
# compression : verbatim or RLE
compression
=
s
[
2
]
# bpc : 1 or 2 bytes (8bits or 16bits)
bpc
=
s
[
3
]
# dimension : 1, 2 or 3 (depending on xsize, ysize and zsize)
dimension
=
i16
(
s
,
4
)
# xsize : width
xsize
=
i16
(
s
,
6
)
# ysize : height
ysize
=
i16
(
s
,
8
)
# zsize : channels count
zsize
=
i16
(
s
,
10
)
# layout
layout
=
bpc
,
dimension
,
zsize
# determine mode from bits/zsize
rawmode
=
""
try
:
rawmode
=
MODES
[
layout
]
except
KeyError
:
pass
if
rawmode
==
""
:
msg
=
"Unsupported SGI image mode"
raise
ValueError
(
msg
)
self
.
_size
=
xsize
,
ysize
self
.
_mode
=
rawmode
.
split
(
";"
)[
0
]
if
self
.
mode
==
"RGB"
:
self
.
custom_mimetype
=
"image/rgb"
# orientation -1 : scanlines begins at the bottom-left corner
orientation
=
-
1
# decoder info
if
compression
==
0
:
pagesize
=
xsize
*
ysize
*
bpc
if
bpc
==
2
:
self
.
tile
=
[
ImageFile
.
_Tile
(
"SGI16"
,
(
0
,
0
)
+
self
.
size
,
headlen
,
(
self
.
mode
,
0
,
orientation
),
)
]
else
:
self
.
tile
=
[]
offset
=
headlen
for
layer
in
self
.
mode
:
self
.
tile
.
append
(
ImageFile
.
_Tile
(
"raw"
, (
0
,
0
)
+
self
.
size
,
offset
, (
layer
,
0
,
orientation
)
)
)
offset
+=
pagesize
elif
compression
==
1
:
self
.
tile
=
[
ImageFile
.
_Tile
(
"sgi_rle"
, (
0
,
0
)
+
self
.
size
,
headlen
, (
rawmode
,
orientation
,
bpc
)
)
]
def
_save
(
im
:
Image
.
Image
,
fp
:
IO
[
bytes
],
filename
:
str
|
bytes
)
->
None
:
if
im
.
mode
not
in
{
"RGB"
,
"RGBA"
,
"L"
}:
msg
=
"Unsupported SGI image mode"
raise
ValueError
(
msg
)
# Get the keyword arguments
info
=
im
.
encoderinfo
# Byte-per-pixel precision, 1 = 8bits per pixel
bpc
=
info
.
get
(
"bpc"
,
1
)
if
bpc
not
in
(
1
,
2
):
msg
=
"Unsupported number of bytes per pixel"
raise
ValueError
(
msg
)
# Flip the image, since the origin of SGI file is the bottom-left corner
orientation
=
-
1
# Define the file as SGI File Format
magic_number
=
474
# Run-Length Encoding Compression - Unsupported at this time
rle
=
0
# Number of dimensions (x,y,z)
dim
=
3
# X Dimension = width / Y Dimension = height
x
,
y
=
im
.
size
if
im
.
mode
==
"L"
and
y
==
1
:
dim
=
1
elif
im
.
mode
==
"L"
:
dim
=
2
# Z Dimension: Number of channels
z
=
len
(
im
.
mode
)
if
dim
in
{
1
,
2
}:
z
=
1
# assert we've got the right number of bands.
if
len
(
im
.
getbands
())
!=
z
:
msg
=
f"incorrect number of bands in SGI write:
{
z
}
vs
{
len
(
im
.
getbands
())
}
"
raise
ValueError
(
msg
)
# Minimum Byte value
pinmin
=
0
# Maximum Byte value (255 = 8bits per pixel)
pinmax
=
255
# Image name (79 characters max, truncated below in write)
img_name
=
os
.
path
.
splitext
(
os
.
path
.
basename
(
filename
))[
0
]
if
isinstance
(
img_name
,
str
):
img_name
=
img_name
.
encode
(
"ascii"
,
"ignore"
)
# Standard representation of pixel in the file
colormap
=
0
fp
.
write
(
struct
.
pack
(
">h"
,
magic_number
))
fp
.
write
(
o8
(
rle
))
fp
.
write
(
o8
(
bpc
))
fp
.
write
(
struct
.
pack
(
">H"
,
dim
))
fp
.
write
(
struct
.
pack
(
">H"
,
x
))
fp
.
write
(
struct
.
pack
(
">H"
,
y
))
fp
.
write
(
struct
.
pack
(
">H"
,
z
))
fp
.
write
(
struct
.
pack
(
">l"
,
pinmin
))
fp
.
write
(
struct
.
pack
(
">l"
,
pinmax
))
fp
.
write
(
struct
.
pack
(
"4s"
,
b""
))
# dummy
fp
.
write
(
struct
.
pack
(
"79s"
,
img_name
))
# truncates to 79 chars
fp
.
write
(
struct
.
pack
(
"s"
,
b""
))
# force null byte after img_name
fp
.
write
(
struct
.
pack
(
">l"
,
colormap
))
fp
.
write
(
struct
.
pack
(
"404s"
,
b""
))
# dummy
rawmode
=
"L"
if
bpc
==
2
:
rawmode
=
"L;16B"
for
channel
in
im
.
split
():
fp
.
write
(
channel
.
tobytes
(
"raw"
,
rawmode
,
0
,
orientation
))
if
hasattr
(
fp
,
"flush"
):
fp
.
flush
()
class
SGI16Decoder
(
ImageFile
.
PyDecoder
):
_pulls_fd
=
True
def
decode
(
self
,
buffer
:
bytes
|
Image
.
SupportsArrayInterface
)
->
tuple
[
int
,
int
]:
assert
self
.
fd
is
not
None
assert
self
.
im
is
not
None
rawmode
,
stride
,
orientation
=
self
.
args
pagesize
=
self
.
state
.
xsize
*
self
.
state
.
ysize
zsize
=
len
(
self
.
mode
)
self
.
fd
.
seek
(
512
)
for
band
in
range
(
zsize
):
channel
=
Image
.
new
(
"L"
, (
self
.
state
.
xsize
,
self
.
state
.
ysize
))
channel
.
frombytes
(
self
.
fd
.
read
(
2
*
pagesize
),
"raw"
,
"L;16B"
,
stride
,
orientation
)
self
.
im
.
putband
(
channel
.
im
,
band
)
return
-
1
,
0
#
# registry
Image
.
register_decoder
(
"SGI16"
,
SGI16Decoder
)
Image
.
register_open
(
SgiImageFile
.
format
,
SgiImageFile
,
_accept
)
Image
.
register_save
(
SgiImageFile
.
format
,
_save
)
Image
.
register_mime
(
SgiImageFile
.
format
,
"image/sgi"
)
Image
.
register_extensions
(
SgiImageFile
.
format
, [
".bw"
,
".rgb"
,
".rgba"
,
".sgi"
])
# End of file
You can’t perform that action at this time.