SgiImagePlugin.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #
  23. from __future__ import annotations
  24. import os
  25. import struct
  26. from . import Image, ImageFile
  27. from ._binary import i16be as i16
  28. from ._binary import o8
  29. def _accept(prefix):
  30. return len(prefix) >= 2 and i16(prefix) == 474
  31. MODES = {
  32. (1, 1, 1): "L",
  33. (1, 2, 1): "L",
  34. (2, 1, 1): "L;16B",
  35. (2, 2, 1): "L;16B",
  36. (1, 3, 3): "RGB",
  37. (2, 3, 3): "RGB;16B",
  38. (1, 3, 4): "RGBA",
  39. (2, 3, 4): "RGBA;16B",
  40. }
  41. ##
  42. # Image plugin for SGI images.
  43. class SgiImageFile(ImageFile.ImageFile):
  44. format = "SGI"
  45. format_description = "SGI Image File Format"
  46. def _open(self):
  47. # HEAD
  48. headlen = 512
  49. s = self.fp.read(headlen)
  50. if not _accept(s):
  51. msg = "Not an SGI image file"
  52. raise ValueError(msg)
  53. # compression : verbatim or RLE
  54. compression = s[2]
  55. # bpc : 1 or 2 bytes (8bits or 16bits)
  56. bpc = s[3]
  57. # dimension : 1, 2 or 3 (depending on xsize, ysize and zsize)
  58. dimension = i16(s, 4)
  59. # xsize : width
  60. xsize = i16(s, 6)
  61. # ysize : height
  62. ysize = i16(s, 8)
  63. # zsize : channels count
  64. zsize = i16(s, 10)
  65. # layout
  66. layout = bpc, dimension, zsize
  67. # determine mode from bits/zsize
  68. rawmode = ""
  69. try:
  70. rawmode = MODES[layout]
  71. except KeyError:
  72. pass
  73. if rawmode == "":
  74. msg = "Unsupported SGI image mode"
  75. raise ValueError(msg)
  76. self._size = xsize, ysize
  77. self._mode = rawmode.split(";")[0]
  78. if self.mode == "RGB":
  79. self.custom_mimetype = "image/rgb"
  80. # orientation -1 : scanlines begins at the bottom-left corner
  81. orientation = -1
  82. # decoder info
  83. if compression == 0:
  84. pagesize = xsize * ysize * bpc
  85. if bpc == 2:
  86. self.tile = [
  87. ("SGI16", (0, 0) + self.size, headlen, (self.mode, 0, orientation))
  88. ]
  89. else:
  90. self.tile = []
  91. offset = headlen
  92. for layer in self.mode:
  93. self.tile.append(
  94. ("raw", (0, 0) + self.size, offset, (layer, 0, orientation))
  95. )
  96. offset += pagesize
  97. elif compression == 1:
  98. self.tile = [
  99. ("sgi_rle", (0, 0) + self.size, headlen, (rawmode, orientation, bpc))
  100. ]
  101. def _save(im, fp, filename):
  102. if im.mode not in {"RGB", "RGBA", "L"}:
  103. msg = "Unsupported SGI image mode"
  104. raise ValueError(msg)
  105. # Get the keyword arguments
  106. info = im.encoderinfo
  107. # Byte-per-pixel precision, 1 = 8bits per pixel
  108. bpc = info.get("bpc", 1)
  109. if bpc not in (1, 2):
  110. msg = "Unsupported number of bytes per pixel"
  111. raise ValueError(msg)
  112. # Flip the image, since the origin of SGI file is the bottom-left corner
  113. orientation = -1
  114. # Define the file as SGI File Format
  115. magic_number = 474
  116. # Run-Length Encoding Compression - Unsupported at this time
  117. rle = 0
  118. # Number of dimensions (x,y,z)
  119. dim = 3
  120. # X Dimension = width / Y Dimension = height
  121. x, y = im.size
  122. if im.mode == "L" and y == 1:
  123. dim = 1
  124. elif im.mode == "L":
  125. dim = 2
  126. # Z Dimension: Number of channels
  127. z = len(im.mode)
  128. if dim in {1, 2}:
  129. z = 1
  130. # assert we've got the right number of bands.
  131. if len(im.getbands()) != z:
  132. msg = f"incorrect number of bands in SGI write: {z} vs {len(im.getbands())}"
  133. raise ValueError(msg)
  134. # Minimum Byte value
  135. pinmin = 0
  136. # Maximum Byte value (255 = 8bits per pixel)
  137. pinmax = 255
  138. # Image name (79 characters max, truncated below in write)
  139. img_name = os.path.splitext(os.path.basename(filename))[0]
  140. img_name = img_name.encode("ascii", "ignore")
  141. # Standard representation of pixel in the file
  142. colormap = 0
  143. fp.write(struct.pack(">h", magic_number))
  144. fp.write(o8(rle))
  145. fp.write(o8(bpc))
  146. fp.write(struct.pack(">H", dim))
  147. fp.write(struct.pack(">H", x))
  148. fp.write(struct.pack(">H", y))
  149. fp.write(struct.pack(">H", z))
  150. fp.write(struct.pack(">l", pinmin))
  151. fp.write(struct.pack(">l", pinmax))
  152. fp.write(struct.pack("4s", b"")) # dummy
  153. fp.write(struct.pack("79s", img_name)) # truncates to 79 chars
  154. fp.write(struct.pack("s", b"")) # force null byte after img_name
  155. fp.write(struct.pack(">l", colormap))
  156. fp.write(struct.pack("404s", b"")) # dummy
  157. rawmode = "L"
  158. if bpc == 2:
  159. rawmode = "L;16B"
  160. for channel in im.split():
  161. fp.write(channel.tobytes("raw", rawmode, 0, orientation))
  162. if hasattr(fp, "flush"):
  163. fp.flush()
  164. class SGI16Decoder(ImageFile.PyDecoder):
  165. _pulls_fd = True
  166. def decode(self, buffer):
  167. rawmode, stride, orientation = self.args
  168. pagesize = self.state.xsize * self.state.ysize
  169. zsize = len(self.mode)
  170. self.fd.seek(512)
  171. for band in range(zsize):
  172. channel = Image.new("L", (self.state.xsize, self.state.ysize))
  173. channel.frombytes(
  174. self.fd.read(2 * pagesize), "raw", "L;16B", stride, orientation
  175. )
  176. self.im.putband(channel.im, band)
  177. return -1, 0
  178. #
  179. # registry
  180. Image.register_decoder("SGI16", SGI16Decoder)
  181. Image.register_open(SgiImageFile.format, SgiImageFile, _accept)
  182. Image.register_save(SgiImageFile.format, _save)
  183. Image.register_mime(SgiImageFile.format, "image/sgi")
  184. Image.register_extensions(SgiImageFile.format, [".bw", ".rgb", ".rgba", ".sgi"])
  185. # End of file