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.

FliImagePlugin.py 4.3KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. import os
  18. from . import Image, ImageFile, ImagePalette
  19. from ._binary import i16le as i16
  20. from ._binary import i32le as i32
  21. from ._binary import o8
  22. #
  23. # decoder
  24. def _accept(prefix):
  25. return (
  26. len(prefix) >= 6
  27. and i16(prefix, 4) in [0xAF11, 0xAF12]
  28. and i16(prefix, 14) in [0, 3] # flags
  29. )
  30. ##
  31. # Image plugin for the FLI/FLC animation format. Use the <b>seek</b>
  32. # method to load individual frames.
  33. class FliImageFile(ImageFile.ImageFile):
  34. format = "FLI"
  35. format_description = "Autodesk FLI/FLC Animation"
  36. _close_exclusive_fp_after_loading = False
  37. def _open(self):
  38. # HEAD
  39. s = self.fp.read(128)
  40. if not (_accept(s) and s[20:22] == b"\x00\x00"):
  41. msg = "not an FLI/FLC file"
  42. raise SyntaxError(msg)
  43. # frames
  44. self.n_frames = i16(s, 6)
  45. self.is_animated = self.n_frames > 1
  46. # image characteristics
  47. self.mode = "P"
  48. self._size = i16(s, 8), i16(s, 10)
  49. # animation speed
  50. duration = i32(s, 16)
  51. magic = i16(s, 4)
  52. if magic == 0xAF11:
  53. duration = (duration * 1000) // 70
  54. self.info["duration"] = duration
  55. # look for palette
  56. palette = [(a, a, a) for a in range(256)]
  57. s = self.fp.read(16)
  58. self.__offset = 128
  59. if i16(s, 4) == 0xF100:
  60. # prefix chunk; ignore it
  61. self.__offset = self.__offset + i32(s)
  62. s = self.fp.read(16)
  63. if i16(s, 4) == 0xF1FA:
  64. # look for palette chunk
  65. number_of_subchunks = i16(s, 6)
  66. chunk_size = None
  67. for _ in range(number_of_subchunks):
  68. if chunk_size is not None:
  69. self.fp.seek(chunk_size - 6, os.SEEK_CUR)
  70. s = self.fp.read(6)
  71. chunk_type = i16(s, 4)
  72. if chunk_type in (4, 11):
  73. self._palette(palette, 2 if chunk_type == 11 else 0)
  74. break
  75. chunk_size = i32(s)
  76. if not chunk_size:
  77. break
  78. palette = [o8(r) + o8(g) + o8(b) for (r, g, b) in palette]
  79. self.palette = ImagePalette.raw("RGB", b"".join(palette))
  80. # set things up to decode first frame
  81. self.__frame = -1
  82. self._fp = self.fp
  83. self.__rewind = self.fp.tell()
  84. self.seek(0)
  85. def _palette(self, palette, shift):
  86. # load palette
  87. i = 0
  88. for e in range(i16(self.fp.read(2))):
  89. s = self.fp.read(2)
  90. i = i + s[0]
  91. n = s[1]
  92. if n == 0:
  93. n = 256
  94. s = self.fp.read(n * 3)
  95. for n in range(0, len(s), 3):
  96. r = s[n] << shift
  97. g = s[n + 1] << shift
  98. b = s[n + 2] << shift
  99. palette[i] = (r, g, b)
  100. i += 1
  101. def seek(self, frame):
  102. if not self._seek_check(frame):
  103. return
  104. if frame < self.__frame:
  105. self._seek(0)
  106. for f in range(self.__frame + 1, frame + 1):
  107. self._seek(f)
  108. def _seek(self, frame):
  109. if frame == 0:
  110. self.__frame = -1
  111. self._fp.seek(self.__rewind)
  112. self.__offset = 128
  113. else:
  114. # ensure that the previous frame was loaded
  115. self.load()
  116. if frame != self.__frame + 1:
  117. msg = f"cannot seek to frame {frame}"
  118. raise ValueError(msg)
  119. self.__frame = frame
  120. # move to next frame
  121. self.fp = self._fp
  122. self.fp.seek(self.__offset)
  123. s = self.fp.read(4)
  124. if not s:
  125. raise EOFError
  126. framesize = i32(s)
  127. self.decodermaxblock = framesize
  128. self.tile = [("fli", (0, 0) + self.size, self.__offset, None)]
  129. self.__offset += framesize
  130. def tell(self):
  131. return self.__frame
  132. #
  133. # registry
  134. Image.register_open(FliImageFile.format, FliImageFile, _accept)
  135. Image.register_extensions(FliImageFile.format, [".fli", ".flc"])