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.

FtexImagePlugin.py 3.8KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. A Pillow loader for .ftc and .ftu files (FTEX)
  3. Jerome Leclanche <jerome@leclan.ch>
  4. The contents of this file are hereby released in the public domain (CC0)
  5. Full text of the CC0 license:
  6. https://creativecommons.org/publicdomain/zero/1.0/
  7. Independence War 2: Edge Of Chaos - Texture File Format - 16 October 2001
  8. The textures used for 3D objects in Independence War 2: Edge Of Chaos are in a
  9. packed custom format called FTEX. This file format uses file extensions FTC
  10. and FTU.
  11. * FTC files are compressed textures (using standard texture compression).
  12. * FTU files are not compressed.
  13. Texture File Format
  14. The FTC and FTU texture files both use the same format. This
  15. has the following structure:
  16. {header}
  17. {format_directory}
  18. {data}
  19. Where:
  20. {header} = {
  21. u32:magic,
  22. u32:version,
  23. u32:width,
  24. u32:height,
  25. u32:mipmap_count,
  26. u32:format_count
  27. }
  28. * The "magic" number is "FTEX".
  29. * "width" and "height" are the dimensions of the texture.
  30. * "mipmap_count" is the number of mipmaps in the texture.
  31. * "format_count" is the number of texture formats (different versions of the
  32. same texture) in this file.
  33. {format_directory} = format_count * { u32:format, u32:where }
  34. The format value is 0 for DXT1 compressed textures and 1 for 24-bit RGB
  35. uncompressed textures.
  36. The texture data for a format starts at the position "where" in the file.
  37. Each set of texture data in the file has the following structure:
  38. {data} = format_count * { u32:mipmap_size, mipmap_size * { u8 } }
  39. * "mipmap_size" is the number of bytes in that mip level. For compressed
  40. textures this is the size of the texture data compressed with DXT1. For 24 bit
  41. uncompressed textures, this is 3 * width * height. Following this are the image
  42. bytes for that mipmap level.
  43. Note: All data is stored in little-Endian (Intel) byte order.
  44. """
  45. import struct
  46. from enum import IntEnum
  47. from io import BytesIO
  48. from . import Image, ImageFile
  49. from ._deprecate import deprecate
  50. MAGIC = b"FTEX"
  51. class Format(IntEnum):
  52. DXT1 = 0
  53. UNCOMPRESSED = 1
  54. def __getattr__(name):
  55. for enum, prefix in {Format: "FORMAT_"}.items():
  56. if name.startswith(prefix):
  57. name = name[len(prefix) :]
  58. if name in enum.__members__:
  59. deprecate(f"{prefix}{name}", 10, f"{enum.__name__}.{name}")
  60. return enum[name]
  61. msg = f"module '{__name__}' has no attribute '{name}'"
  62. raise AttributeError(msg)
  63. class FtexImageFile(ImageFile.ImageFile):
  64. format = "FTEX"
  65. format_description = "Texture File Format (IW2:EOC)"
  66. def _open(self):
  67. if not _accept(self.fp.read(4)):
  68. msg = "not an FTEX file"
  69. raise SyntaxError(msg)
  70. struct.unpack("<i", self.fp.read(4)) # version
  71. self._size = struct.unpack("<2i", self.fp.read(8))
  72. mipmap_count, format_count = struct.unpack("<2i", self.fp.read(8))
  73. self.mode = "RGB"
  74. # Only support single-format files.
  75. # I don't know of any multi-format file.
  76. assert format_count == 1
  77. format, where = struct.unpack("<2i", self.fp.read(8))
  78. self.fp.seek(where)
  79. (mipmap_size,) = struct.unpack("<i", self.fp.read(4))
  80. data = self.fp.read(mipmap_size)
  81. if format == Format.DXT1:
  82. self.mode = "RGBA"
  83. self.tile = [("bcn", (0, 0) + self.size, 0, 1)]
  84. elif format == Format.UNCOMPRESSED:
  85. self.tile = [("raw", (0, 0) + self.size, 0, ("RGB", 0, 1))]
  86. else:
  87. msg = f"Invalid texture compression format: {repr(format)}"
  88. raise ValueError(msg)
  89. self.fp.close()
  90. self.fp = BytesIO(data)
  91. def load_seek(self, pos):
  92. pass
  93. def _accept(prefix):
  94. return prefix[:4] == MAGIC
  95. Image.register_open(FtexImageFile.format, FtexImageFile, _accept)
  96. Image.register_extensions(FtexImageFile.format, [".ftc", ".ftu"])