FpxImagePlugin.py 7.2 KB

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