You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
6.0 KiB

4 years ago
  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 . import Image, ImageFile
  24. from ._binary import i8, o8, i16be as i16
  25. from ._util import py3
  26. import struct
  27. import os
  28. __version__ = "0.3"
  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. # magic number : 474
  51. if i16(s) != 474:
  52. raise ValueError("Not an SGI image file")
  53. # compression : verbatim or RLE
  54. compression = i8(s[2])
  55. # bpc : 1 or 2 bytes (8bits or 16bits)
  56. bpc = i8(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. raise ValueError("Unsupported SGI image mode")
  75. self._size = xsize, ysize
  76. self.mode = rawmode.split(";")[0]
  77. # orientation -1 : scanlines begins at the bottom-left corner
  78. orientation = -1
  79. # decoder info
  80. if compression == 0:
  81. pagesize = xsize * ysize * bpc
  82. if bpc == 2:
  83. self.tile = [("SGI16", (0, 0) + self.size,
  84. headlen, (self.mode, 0, orientation))]
  85. else:
  86. self.tile = []
  87. offset = headlen
  88. for layer in self.mode:
  89. self.tile.append(
  90. ("raw", (0, 0) + self.size,
  91. offset, (layer, 0, orientation)))
  92. offset += pagesize
  93. elif compression == 1:
  94. self.tile = [("sgi_rle", (0, 0) + self.size,
  95. headlen, (rawmode, orientation, bpc))]
  96. def _save(im, fp, filename):
  97. if im.mode != "RGB" and im.mode != "RGBA" and im.mode != "L":
  98. raise ValueError("Unsupported SGI image mode")
  99. # Get the keyword arguments
  100. info = im.encoderinfo
  101. # Byte-per-pixel precision, 1 = 8bits per pixel
  102. bpc = info.get("bpc", 1)
  103. if bpc not in (1, 2):
  104. raise ValueError("Unsupported number of bytes per pixel")
  105. # Flip the image, since the origin of SGI file is the bottom-left corner
  106. orientation = -1
  107. # Define the file as SGI File Format
  108. magicNumber = 474
  109. # Run-Length Encoding Compression - Unsupported at this time
  110. rle = 0
  111. # Number of dimensions (x,y,z)
  112. dim = 3
  113. # X Dimension = width / Y Dimension = height
  114. x, y = im.size
  115. if im.mode == "L" and y == 1:
  116. dim = 1
  117. elif im.mode == "L":
  118. dim = 2
  119. # Z Dimension: Number of channels
  120. z = len(im.mode)
  121. if dim == 1 or dim == 2:
  122. z = 1
  123. # assert we've got the right number of bands.
  124. if len(im.getbands()) != z:
  125. raise ValueError("incorrect number of bands in SGI write: %s vs %s" %
  126. (z, len(im.getbands())))
  127. # Minimum Byte value
  128. pinmin = 0
  129. # Maximum Byte value (255 = 8bits per pixel)
  130. pinmax = 255
  131. # Image name (79 characters max, truncated below in write)
  132. imgName = os.path.splitext(os.path.basename(filename))[0]
  133. if py3:
  134. imgName = imgName.encode('ascii', 'ignore')
  135. # Standard representation of pixel in the file
  136. colormap = 0
  137. fp.write(struct.pack('>h', magicNumber))
  138. fp.write(o8(rle))
  139. fp.write(o8(bpc))
  140. fp.write(struct.pack('>H', dim))
  141. fp.write(struct.pack('>H', x))
  142. fp.write(struct.pack('>H', y))
  143. fp.write(struct.pack('>H', z))
  144. fp.write(struct.pack('>l', pinmin))
  145. fp.write(struct.pack('>l', pinmax))
  146. fp.write(struct.pack('4s', b'')) # dummy
  147. fp.write(struct.pack('79s', imgName)) # truncates to 79 chars
  148. fp.write(struct.pack('s', b'')) # force null byte after imgname
  149. fp.write(struct.pack('>l', colormap))
  150. fp.write(struct.pack('404s', b'')) # dummy
  151. rawmode = 'L'
  152. if bpc == 2:
  153. rawmode = 'L;16B'
  154. for channel in im.split():
  155. fp.write(channel.tobytes('raw', rawmode, 0, orientation))
  156. fp.close()
  157. class SGI16Decoder(ImageFile.PyDecoder):
  158. _pulls_fd = True
  159. def decode(self, buffer):
  160. rawmode, stride, orientation = self.args
  161. pagesize = self.state.xsize * self.state.ysize
  162. zsize = len(self.mode)
  163. self.fd.seek(512)
  164. for band in range(zsize):
  165. channel = Image.new('L', (self.state.xsize, self.state.ysize))
  166. channel.frombytes(self.fd.read(2 * pagesize), 'raw',
  167. 'L;16B', stride, orientation)
  168. self.im.putband(channel.im, band)
  169. return -1, 0
  170. #
  171. # registry
  172. Image.register_decoder("SGI16", SGI16Decoder)
  173. Image.register_open(SgiImageFile.format, SgiImageFile, _accept)
  174. Image.register_save(SgiImageFile.format, _save)
  175. Image.register_mime(SgiImageFile.format, "image/sgi")
  176. Image.register_mime(SgiImageFile.format, "image/rgb")
  177. Image.register_extensions(SgiImageFile.format,
  178. [".bw", ".rgb", ".rgba", ".sgi"])
  179. # End of file