Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

SgiImagePlugin.py 6.0KB

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