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.

FpxImagePlugin.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #
  2. # THIS IS WORK IN PROGRESS
  3. #
  4. # The Python Imaging Library.
  5. # $Id$
  6. #
  7. # FlashPix support for PIL
  8. #
  9. # History:
  10. # 97-01-25 fl Created (reads uncompressed RGB images only)
  11. #
  12. # Copyright (c) Secret Labs AB 1997.
  13. # Copyright (c) Fredrik Lundh 1997.
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17. import olefile
  18. from . import Image, ImageFile
  19. from ._binary import i32le as i32
  20. # we map from colour field tuples to (mode, rawmode) descriptors
  21. MODES = {
  22. # opacity
  23. (0x00007FFE,): ("A", "L"),
  24. # monochrome
  25. (0x00010000,): ("L", "L"),
  26. (0x00018000, 0x00017FFE): ("RGBA", "LA"),
  27. # photo YCC
  28. (0x00020000, 0x00020001, 0x00020002): ("RGB", "YCC;P"),
  29. (0x00028000, 0x00028001, 0x00028002, 0x00027FFE): ("RGBA", "YCCA;P"),
  30. # standard RGB (NIFRGB)
  31. (0x00030000, 0x00030001, 0x00030002): ("RGB", "RGB"),
  32. (0x00038000, 0x00038001, 0x00038002, 0x00037FFE): ("RGBA", "RGBA"),
  33. }
  34. #
  35. # --------------------------------------------------------------------
  36. def _accept(prefix):
  37. return prefix[:8] == olefile.MAGIC
  38. ##
  39. # Image plugin for the FlashPix images.
  40. class FpxImageFile(ImageFile.ImageFile):
  41. format = "FPX"
  42. format_description = "FlashPix"
  43. def _open(self):
  44. #
  45. # read the OLE directory and see if this is a likely
  46. # to be a FlashPix file
  47. try:
  48. self.ole = olefile.OleFileIO(self.fp)
  49. except OSError as e:
  50. msg = "not an FPX file; invalid OLE file"
  51. raise SyntaxError(msg) from e
  52. if self.ole.root.clsid != "56616700-C154-11CE-8553-00AA00A1F95B":
  53. msg = "not an FPX file; bad root CLSID"
  54. raise SyntaxError(msg)
  55. self._open_index(1)
  56. def _open_index(self, index=1):
  57. #
  58. # get the Image Contents Property Set
  59. prop = self.ole.getproperties(
  60. [f"Data Object Store {index:06d}", "\005Image Contents"]
  61. )
  62. # size (highest resolution)
  63. self._size = prop[0x1000002], prop[0x1000003]
  64. size = max(self.size)
  65. i = 1
  66. while size > 64:
  67. size = size / 2
  68. i += 1
  69. self.maxid = i - 1
  70. # mode. instead of using a single field for this, flashpix
  71. # requires you to specify the mode for each channel in each
  72. # resolution subimage, and leaves it to the decoder to make
  73. # sure that they all match. for now, we'll cheat and assume
  74. # that this is always the case.
  75. id = self.maxid << 16
  76. s = prop[0x2000002 | id]
  77. colors = []
  78. bands = i32(s, 4)
  79. if bands > 4:
  80. msg = "Invalid number of bands"
  81. raise OSError(msg)
  82. for i in range(bands):
  83. # note: for now, we ignore the "uncalibrated" flag
  84. colors.append(i32(s, 8 + i * 4) & 0x7FFFFFFF)
  85. self.mode, self.rawmode = MODES[tuple(colors)]
  86. # load JPEG tables, if any
  87. self.jpeg = {}
  88. for i in range(256):
  89. id = 0x3000001 | (i << 16)
  90. if id in prop:
  91. self.jpeg[i] = prop[id]
  92. self._open_subimage(1, self.maxid)
  93. def _open_subimage(self, index=1, subimage=0):
  94. #
  95. # setup tile descriptors for a given subimage
  96. stream = [
  97. f"Data Object Store {index:06d}",
  98. f"Resolution {subimage:04d}",
  99. "Subimage 0000 Header",
  100. ]
  101. fp = self.ole.openstream(stream)
  102. # skip prefix
  103. fp.read(28)
  104. # header stream
  105. s = fp.read(36)
  106. size = i32(s, 4), i32(s, 8)
  107. # tilecount = i32(s, 12)
  108. tilesize = i32(s, 16), i32(s, 20)
  109. # channels = i32(s, 24)
  110. offset = i32(s, 28)
  111. length = i32(s, 32)
  112. if size != self.size:
  113. msg = "subimage mismatch"
  114. raise OSError(msg)
  115. # get tile descriptors
  116. fp.seek(28 + offset)
  117. s = fp.read(i32(s, 12) * length)
  118. x = y = 0
  119. xsize, ysize = size
  120. xtile, ytile = tilesize
  121. self.tile = []
  122. for i in range(0, len(s), length):
  123. x1 = min(xsize, x + xtile)
  124. y1 = min(ysize, y + ytile)
  125. compression = i32(s, i + 8)
  126. if compression == 0:
  127. self.tile.append(
  128. (
  129. "raw",
  130. (x, y, x1, y1),
  131. i32(s, i) + 28,
  132. (self.rawmode,),
  133. )
  134. )
  135. elif compression == 1:
  136. # FIXME: the fill decoder is not implemented
  137. self.tile.append(
  138. (
  139. "fill",
  140. (x, y, x1, y1),
  141. i32(s, i) + 28,
  142. (self.rawmode, s[12:16]),
  143. )
  144. )
  145. elif compression == 2:
  146. internal_color_conversion = s[14]
  147. jpeg_tables = s[15]
  148. rawmode = self.rawmode
  149. if internal_color_conversion:
  150. # The image is stored as usual (usually YCbCr).
  151. if rawmode == "RGBA":
  152. # For "RGBA", data is stored as YCbCrA based on
  153. # negative RGB. The following trick works around
  154. # this problem :
  155. jpegmode, rawmode = "YCbCrK", "CMYK"
  156. else:
  157. jpegmode = None # let the decoder decide
  158. else:
  159. # The image is stored as defined by rawmode
  160. jpegmode = rawmode
  161. self.tile.append(
  162. (
  163. "jpeg",
  164. (x, y, x1, y1),
  165. i32(s, i) + 28,
  166. (rawmode, jpegmode),
  167. )
  168. )
  169. # FIXME: jpeg tables are tile dependent; the prefix
  170. # data must be placed in the tile descriptor itself!
  171. if jpeg_tables:
  172. self.tile_prefix = self.jpeg[jpeg_tables]
  173. else:
  174. msg = "unknown/invalid compression"
  175. raise OSError(msg)
  176. x = x + xtile
  177. if x >= xsize:
  178. x, y = 0, y + ytile
  179. if y >= ysize:
  180. break # isn't really required
  181. self.stream = stream
  182. self.fp = None
  183. def load(self):
  184. if not self.fp:
  185. self.fp = self.ole.openstream(self.stream[:2] + ["Subimage 0000 Data"])
  186. return ImageFile.ImageFile.load(self)
  187. def close(self):
  188. self.ole.close()
  189. super().close()
  190. def __exit__(self, *args):
  191. self.ole.close()
  192. super().__exit__()
  193. #
  194. # --------------------------------------------------------------------
  195. Image.register_open(FpxImageFile.format, FpxImageFile, _accept)
  196. Image.register_extension(FpxImageFile.format, ".fpx")