FliImagePlugin.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # FLI/FLC file handling.
  6. #
  7. # History:
  8. # 95-09-01 fl Created
  9. # 97-01-03 fl Fixed parser, setup decoder tile
  10. # 98-07-15 fl Renamed offset attribute to avoid name clash
  11. #
  12. # Copyright (c) Secret Labs AB 1997-98.
  13. # Copyright (c) Fredrik Lundh 1995-97.
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17. from . import Image, ImageFile, ImagePalette
  18. from ._binary import i8, i16le as i16, i32le as i32, o8
  19. # __version__ is deprecated and will be removed in a future version. Use
  20. # PIL.__version__ instead.
  21. __version__ = "0.2"
  22. #
  23. # decoder
  24. def _accept(prefix):
  25. return len(prefix) >= 6 and i16(prefix[4:6]) in [0xAF11, 0xAF12]
  26. ##
  27. # Image plugin for the FLI/FLC animation format. Use the <b>seek</b>
  28. # method to load individual frames.
  29. class FliImageFile(ImageFile.ImageFile):
  30. format = "FLI"
  31. format_description = "Autodesk FLI/FLC Animation"
  32. _close_exclusive_fp_after_loading = False
  33. def _open(self):
  34. # HEAD
  35. s = self.fp.read(128)
  36. magic = i16(s[4:6])
  37. if not (
  38. magic in [0xAF11, 0xAF12]
  39. and i16(s[14:16]) in [0, 3] # flags
  40. and s[20:22] == b"\x00\x00" # reserved
  41. ):
  42. raise SyntaxError("not an FLI/FLC file")
  43. # frames
  44. self.__framecount = i16(s[6:8])
  45. # image characteristics
  46. self.mode = "P"
  47. self._size = i16(s[8:10]), i16(s[10:12])
  48. # animation speed
  49. duration = i32(s[16:20])
  50. if magic == 0xAF11:
  51. duration = (duration * 1000) // 70
  52. self.info["duration"] = duration
  53. # look for palette
  54. palette = [(a, a, a) for a in range(256)]
  55. s = self.fp.read(16)
  56. self.__offset = 128
  57. if i16(s[4:6]) == 0xF100:
  58. # prefix chunk; ignore it
  59. self.__offset = self.__offset + i32(s)
  60. s = self.fp.read(16)
  61. if i16(s[4:6]) == 0xF1FA:
  62. # look for palette chunk
  63. s = self.fp.read(6)
  64. if i16(s[4:6]) == 11:
  65. self._palette(palette, 2)
  66. elif i16(s[4:6]) == 4:
  67. self._palette(palette, 0)
  68. palette = [o8(r) + o8(g) + o8(b) for (r, g, b) in palette]
  69. self.palette = ImagePalette.raw("RGB", b"".join(palette))
  70. # set things up to decode first frame
  71. self.__frame = -1
  72. self.__fp = self.fp
  73. self.__rewind = self.fp.tell()
  74. self.seek(0)
  75. def _palette(self, palette, shift):
  76. # load palette
  77. i = 0
  78. for e in range(i16(self.fp.read(2))):
  79. s = self.fp.read(2)
  80. i = i + i8(s[0])
  81. n = i8(s[1])
  82. if n == 0:
  83. n = 256
  84. s = self.fp.read(n * 3)
  85. for n in range(0, len(s), 3):
  86. r = i8(s[n]) << shift
  87. g = i8(s[n + 1]) << shift
  88. b = i8(s[n + 2]) << shift
  89. palette[i] = (r, g, b)
  90. i += 1
  91. @property
  92. def n_frames(self):
  93. return self.__framecount
  94. @property
  95. def is_animated(self):
  96. return self.__framecount > 1
  97. def seek(self, frame):
  98. if not self._seek_check(frame):
  99. return
  100. if frame < self.__frame:
  101. self._seek(0)
  102. for f in range(self.__frame + 1, frame + 1):
  103. self._seek(f)
  104. def _seek(self, frame):
  105. if frame == 0:
  106. self.__frame = -1
  107. self.__fp.seek(self.__rewind)
  108. self.__offset = 128
  109. else:
  110. # ensure that the previous frame was loaded
  111. self.load()
  112. if frame != self.__frame + 1:
  113. raise ValueError("cannot seek to frame %d" % frame)
  114. self.__frame = frame
  115. # move to next frame
  116. self.fp = self.__fp
  117. self.fp.seek(self.__offset)
  118. s = self.fp.read(4)
  119. if not s:
  120. raise EOFError
  121. framesize = i32(s)
  122. self.decodermaxblock = framesize
  123. self.tile = [("fli", (0, 0) + self.size, self.__offset, None)]
  124. self.__offset += framesize
  125. def tell(self):
  126. return self.__frame
  127. def _close__fp(self):
  128. try:
  129. if self.__fp != self.fp:
  130. self.__fp.close()
  131. except AttributeError:
  132. pass
  133. finally:
  134. self.__fp = None
  135. #
  136. # registry
  137. Image.register_open(FliImageFile.format, FliImageFile, _accept)
  138. Image.register_extensions(FliImageFile.format, [".fli", ".flc"])