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.

225 lines
6.1 KiB

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