FpxImagePlugin.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 i8
  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):
  38. return prefix[:8] == 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):
  45. #
  46. # read the OLE directory and see if this is a likely
  47. # to be a FlashPix file
  48. try:
  49. self.ole = olefile.OleFileIO(self.fp)
  50. except OSError as e:
  51. raise SyntaxError("not an FPX file; invalid OLE file") from e
  52. if self.ole.root.clsid != "56616700-C154-11CE-8553-00AA00A1F95B":
  53. raise SyntaxError("not an FPX file; bad root CLSID")
  54. self._open_index(1)
  55. def _open_index(self, index=1):
  56. #
  57. # get the Image Contents Property Set
  58. prop = self.ole.getproperties(
  59. [f"Data Object Store {index:06d}", "\005Image Contents"]
  60. )
  61. # size (highest resolution)
  62. self._size = prop[0x1000002], prop[0x1000003]
  63. size = max(self.size)
  64. i = 1
  65. while size > 64:
  66. size = size / 2
  67. i += 1
  68. self.maxid = i - 1
  69. # mode. instead of using a single field for this, flashpix
  70. # requires you to specify the mode for each channel in each
  71. # resolution subimage, and leaves it to the decoder to make
  72. # sure that they all match. for now, we'll cheat and assume
  73. # that this is always the case.
  74. id = self.maxid << 16
  75. s = prop[0x2000002 | id]
  76. colors = []
  77. bands = i32(s, 4)
  78. if bands > 4:
  79. raise OSError("Invalid number of bands")
  80. for i in range(bands):
  81. # note: for now, we ignore the "uncalibrated" flag
  82. colors.append(i32(s, 8 + i * 4) & 0x7FFFFFFF)
  83. self.mode, self.rawmode = MODES[tuple(colors)]
  84. # load JPEG tables, if any
  85. self.jpeg = {}
  86. for i in range(256):
  87. id = 0x3000001 | (i << 16)
  88. if id in prop:
  89. self.jpeg[i] = prop[id]
  90. self._open_subimage(1, self.maxid)
  91. def _open_subimage(self, index=1, subimage=0):
  92. #
  93. # setup tile descriptors for a given subimage
  94. stream = [
  95. f"Data Object Store {index:06d}",
  96. f"Resolution {subimage:04d}",
  97. "Subimage 0000 Header",
  98. ]
  99. fp = self.ole.openstream(stream)
  100. # skip prefix
  101. fp.read(28)
  102. # header stream
  103. s = fp.read(36)
  104. size = i32(s, 4), i32(s, 8)
  105. # tilecount = i32(s, 12)
  106. tilesize = i32(s, 16), i32(s, 20)
  107. # channels = i32(s, 24)
  108. offset = i32(s, 28)
  109. length = i32(s, 32)
  110. if size != self.size:
  111. raise OSError("subimage mismatch")
  112. # get tile descriptors
  113. fp.seek(28 + offset)
  114. s = fp.read(i32(s, 12) * length)
  115. x = y = 0
  116. xsize, ysize = size
  117. xtile, ytile = tilesize
  118. self.tile = []
  119. for i in range(0, len(s), length):
  120. compression = i32(s, i + 8)
  121. if compression == 0:
  122. self.tile.append(
  123. (
  124. "raw",
  125. (x, y, x + xtile, y + ytile),
  126. i32(s, i) + 28,
  127. (self.rawmode),
  128. )
  129. )
  130. elif compression == 1:
  131. # FIXME: the fill decoder is not implemented
  132. self.tile.append(
  133. (
  134. "fill",
  135. (x, y, x + xtile, y + ytile),
  136. i32(s, i) + 28,
  137. (self.rawmode, s[12:16]),
  138. )
  139. )
  140. elif compression == 2:
  141. internal_color_conversion = i8(s[14])
  142. jpeg_tables = i8(s[15])
  143. rawmode = self.rawmode
  144. if internal_color_conversion:
  145. # The image is stored as usual (usually YCbCr).
  146. if rawmode == "RGBA":
  147. # For "RGBA", data is stored as YCbCrA based on
  148. # negative RGB. The following trick works around
  149. # this problem :
  150. jpegmode, rawmode = "YCbCrK", "CMYK"
  151. else:
  152. jpegmode = None # let the decoder decide
  153. else:
  154. # The image is stored as defined by rawmode
  155. jpegmode = rawmode
  156. self.tile.append(
  157. (
  158. "jpeg",
  159. (x, y, x + xtile, y + ytile),
  160. i32(s, i) + 28,
  161. (rawmode, jpegmode),
  162. )
  163. )
  164. # FIXME: jpeg tables are tile dependent; the prefix
  165. # data must be placed in the tile descriptor itself!
  166. if jpeg_tables:
  167. self.tile_prefix = self.jpeg[jpeg_tables]
  168. else:
  169. raise OSError("unknown/invalid compression")
  170. x = x + xtile
  171. if x >= xsize:
  172. x, y = 0, y + ytile
  173. if y >= ysize:
  174. break # isn't really required
  175. self.stream = stream
  176. self.fp = None
  177. def load(self):
  178. if not self.fp:
  179. self.fp = self.ole.openstream(self.stream[:2] + ["Subimage 0000 Data"])
  180. return ImageFile.ImageFile.load(self)
  181. #
  182. # --------------------------------------------------------------------
  183. Image.register_open(FpxImageFile.format, FpxImageFile, _accept)
  184. Image.register_extension(FpxImageFile.format, ".fpx")