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.

IcnsImagePlugin.py 12KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # macOS icns file decoder, based on icns.py by Bob Ippolito.
  6. #
  7. # history:
  8. # 2004-10-09 fl Turned into a PIL plugin; removed 2.3 dependencies.
  9. # 2020-04-04 Allow saving on all operating systems.
  10. #
  11. # Copyright (c) 2004 by Bob Ippolito.
  12. # Copyright (c) 2004 by Secret Labs.
  13. # Copyright (c) 2004 by Fredrik Lundh.
  14. # Copyright (c) 2014 by Alastair Houghton.
  15. # Copyright (c) 2020 by Pan Jing.
  16. #
  17. # See the README file for information on usage and redistribution.
  18. #
  19. import io
  20. import os
  21. import struct
  22. import sys
  23. from PIL import Image, ImageFile, PngImagePlugin, features
  24. enable_jpeg2k = features.check_codec("jpg_2000")
  25. if enable_jpeg2k:
  26. from PIL import Jpeg2KImagePlugin
  27. MAGIC = b"icns"
  28. HEADERSIZE = 8
  29. def nextheader(fobj):
  30. return struct.unpack(">4sI", fobj.read(HEADERSIZE))
  31. def read_32t(fobj, start_length, size):
  32. # The 128x128 icon seems to have an extra header for some reason.
  33. (start, length) = start_length
  34. fobj.seek(start)
  35. sig = fobj.read(4)
  36. if sig != b"\x00\x00\x00\x00":
  37. msg = "Unknown signature, expecting 0x00000000"
  38. raise SyntaxError(msg)
  39. return read_32(fobj, (start + 4, length - 4), size)
  40. def read_32(fobj, start_length, size):
  41. """
  42. Read a 32bit RGB icon resource. Seems to be either uncompressed or
  43. an RLE packbits-like scheme.
  44. """
  45. (start, length) = start_length
  46. fobj.seek(start)
  47. pixel_size = (size[0] * size[2], size[1] * size[2])
  48. sizesq = pixel_size[0] * pixel_size[1]
  49. if length == sizesq * 3:
  50. # uncompressed ("RGBRGBGB")
  51. indata = fobj.read(length)
  52. im = Image.frombuffer("RGB", pixel_size, indata, "raw", "RGB", 0, 1)
  53. else:
  54. # decode image
  55. im = Image.new("RGB", pixel_size, None)
  56. for band_ix in range(3):
  57. data = []
  58. bytesleft = sizesq
  59. while bytesleft > 0:
  60. byte = fobj.read(1)
  61. if not byte:
  62. break
  63. byte = byte[0]
  64. if byte & 0x80:
  65. blocksize = byte - 125
  66. byte = fobj.read(1)
  67. for i in range(blocksize):
  68. data.append(byte)
  69. else:
  70. blocksize = byte + 1
  71. data.append(fobj.read(blocksize))
  72. bytesleft -= blocksize
  73. if bytesleft <= 0:
  74. break
  75. if bytesleft != 0:
  76. msg = f"Error reading channel [{repr(bytesleft)} left]"
  77. raise SyntaxError(msg)
  78. band = Image.frombuffer("L", pixel_size, b"".join(data), "raw", "L", 0, 1)
  79. im.im.putband(band.im, band_ix)
  80. return {"RGB": im}
  81. def read_mk(fobj, start_length, size):
  82. # Alpha masks seem to be uncompressed
  83. start = start_length[0]
  84. fobj.seek(start)
  85. pixel_size = (size[0] * size[2], size[1] * size[2])
  86. sizesq = pixel_size[0] * pixel_size[1]
  87. band = Image.frombuffer("L", pixel_size, fobj.read(sizesq), "raw", "L", 0, 1)
  88. return {"A": band}
  89. def read_png_or_jpeg2000(fobj, start_length, size):
  90. (start, length) = start_length
  91. fobj.seek(start)
  92. sig = fobj.read(12)
  93. if sig[:8] == b"\x89PNG\x0d\x0a\x1a\x0a":
  94. fobj.seek(start)
  95. im = PngImagePlugin.PngImageFile(fobj)
  96. Image._decompression_bomb_check(im.size)
  97. return {"RGBA": im}
  98. elif (
  99. sig[:4] == b"\xff\x4f\xff\x51"
  100. or sig[:4] == b"\x0d\x0a\x87\x0a"
  101. or sig == b"\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a"
  102. ):
  103. if not enable_jpeg2k:
  104. msg = (
  105. "Unsupported icon subimage format (rebuild PIL "
  106. "with JPEG 2000 support to fix this)"
  107. )
  108. raise ValueError(msg)
  109. # j2k, jpc or j2c
  110. fobj.seek(start)
  111. jp2kstream = fobj.read(length)
  112. f = io.BytesIO(jp2kstream)
  113. im = Jpeg2KImagePlugin.Jpeg2KImageFile(f)
  114. Image._decompression_bomb_check(im.size)
  115. if im.mode != "RGBA":
  116. im = im.convert("RGBA")
  117. return {"RGBA": im}
  118. else:
  119. msg = "Unsupported icon subimage format"
  120. raise ValueError(msg)
  121. class IcnsFile:
  122. SIZES = {
  123. (512, 512, 2): [(b"ic10", read_png_or_jpeg2000)],
  124. (512, 512, 1): [(b"ic09", read_png_or_jpeg2000)],
  125. (256, 256, 2): [(b"ic14", read_png_or_jpeg2000)],
  126. (256, 256, 1): [(b"ic08", read_png_or_jpeg2000)],
  127. (128, 128, 2): [(b"ic13", read_png_or_jpeg2000)],
  128. (128, 128, 1): [
  129. (b"ic07", read_png_or_jpeg2000),
  130. (b"it32", read_32t),
  131. (b"t8mk", read_mk),
  132. ],
  133. (64, 64, 1): [(b"icp6", read_png_or_jpeg2000)],
  134. (32, 32, 2): [(b"ic12", read_png_or_jpeg2000)],
  135. (48, 48, 1): [(b"ih32", read_32), (b"h8mk", read_mk)],
  136. (32, 32, 1): [
  137. (b"icp5", read_png_or_jpeg2000),
  138. (b"il32", read_32),
  139. (b"l8mk", read_mk),
  140. ],
  141. (16, 16, 2): [(b"ic11", read_png_or_jpeg2000)],
  142. (16, 16, 1): [
  143. (b"icp4", read_png_or_jpeg2000),
  144. (b"is32", read_32),
  145. (b"s8mk", read_mk),
  146. ],
  147. }
  148. def __init__(self, fobj):
  149. """
  150. fobj is a file-like object as an icns resource
  151. """
  152. # signature : (start, length)
  153. self.dct = dct = {}
  154. self.fobj = fobj
  155. sig, filesize = nextheader(fobj)
  156. if not _accept(sig):
  157. msg = "not an icns file"
  158. raise SyntaxError(msg)
  159. i = HEADERSIZE
  160. while i < filesize:
  161. sig, blocksize = nextheader(fobj)
  162. if blocksize <= 0:
  163. msg = "invalid block header"
  164. raise SyntaxError(msg)
  165. i += HEADERSIZE
  166. blocksize -= HEADERSIZE
  167. dct[sig] = (i, blocksize)
  168. fobj.seek(blocksize, io.SEEK_CUR)
  169. i += blocksize
  170. def itersizes(self):
  171. sizes = []
  172. for size, fmts in self.SIZES.items():
  173. for fmt, reader in fmts:
  174. if fmt in self.dct:
  175. sizes.append(size)
  176. break
  177. return sizes
  178. def bestsize(self):
  179. sizes = self.itersizes()
  180. if not sizes:
  181. msg = "No 32bit icon resources found"
  182. raise SyntaxError(msg)
  183. return max(sizes)
  184. def dataforsize(self, size):
  185. """
  186. Get an icon resource as {channel: array}. Note that
  187. the arrays are bottom-up like windows bitmaps and will likely
  188. need to be flipped or transposed in some way.
  189. """
  190. dct = {}
  191. for code, reader in self.SIZES[size]:
  192. desc = self.dct.get(code)
  193. if desc is not None:
  194. dct.update(reader(self.fobj, desc, size))
  195. return dct
  196. def getimage(self, size=None):
  197. if size is None:
  198. size = self.bestsize()
  199. if len(size) == 2:
  200. size = (size[0], size[1], 1)
  201. channels = self.dataforsize(size)
  202. im = channels.get("RGBA", None)
  203. if im:
  204. return im
  205. im = channels.get("RGB").copy()
  206. try:
  207. im.putalpha(channels["A"])
  208. except KeyError:
  209. pass
  210. return im
  211. ##
  212. # Image plugin for Mac OS icons.
  213. class IcnsImageFile(ImageFile.ImageFile):
  214. """
  215. PIL image support for Mac OS .icns files.
  216. Chooses the best resolution, but will possibly load
  217. a different size image if you mutate the size attribute
  218. before calling 'load'.
  219. The info dictionary has a key 'sizes' that is a list
  220. of sizes that the icns file has.
  221. """
  222. format = "ICNS"
  223. format_description = "Mac OS icns resource"
  224. def _open(self):
  225. self.icns = IcnsFile(self.fp)
  226. self.mode = "RGBA"
  227. self.info["sizes"] = self.icns.itersizes()
  228. self.best_size = self.icns.bestsize()
  229. self.size = (
  230. self.best_size[0] * self.best_size[2],
  231. self.best_size[1] * self.best_size[2],
  232. )
  233. @property
  234. def size(self):
  235. return self._size
  236. @size.setter
  237. def size(self, value):
  238. info_size = value
  239. if info_size not in self.info["sizes"] and len(info_size) == 2:
  240. info_size = (info_size[0], info_size[1], 1)
  241. if (
  242. info_size not in self.info["sizes"]
  243. and len(info_size) == 3
  244. and info_size[2] == 1
  245. ):
  246. simple_sizes = [
  247. (size[0] * size[2], size[1] * size[2]) for size in self.info["sizes"]
  248. ]
  249. if value in simple_sizes:
  250. info_size = self.info["sizes"][simple_sizes.index(value)]
  251. if info_size not in self.info["sizes"]:
  252. msg = "This is not one of the allowed sizes of this image"
  253. raise ValueError(msg)
  254. self._size = value
  255. def load(self):
  256. if len(self.size) == 3:
  257. self.best_size = self.size
  258. self.size = (
  259. self.best_size[0] * self.best_size[2],
  260. self.best_size[1] * self.best_size[2],
  261. )
  262. px = Image.Image.load(self)
  263. if self.im is not None and self.im.size == self.size:
  264. # Already loaded
  265. return px
  266. self.load_prepare()
  267. # This is likely NOT the best way to do it, but whatever.
  268. im = self.icns.getimage(self.best_size)
  269. # If this is a PNG or JPEG 2000, it won't be loaded yet
  270. px = im.load()
  271. self.im = im.im
  272. self.mode = im.mode
  273. self.size = im.size
  274. return px
  275. def _save(im, fp, filename):
  276. """
  277. Saves the image as a series of PNG files,
  278. that are then combined into a .icns file.
  279. """
  280. if hasattr(fp, "flush"):
  281. fp.flush()
  282. sizes = {
  283. b"ic07": 128,
  284. b"ic08": 256,
  285. b"ic09": 512,
  286. b"ic10": 1024,
  287. b"ic11": 32,
  288. b"ic12": 64,
  289. b"ic13": 256,
  290. b"ic14": 512,
  291. }
  292. provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])}
  293. size_streams = {}
  294. for size in set(sizes.values()):
  295. image = (
  296. provided_images[size]
  297. if size in provided_images
  298. else im.resize((size, size))
  299. )
  300. temp = io.BytesIO()
  301. image.save(temp, "png")
  302. size_streams[size] = temp.getvalue()
  303. entries = []
  304. for type, size in sizes.items():
  305. stream = size_streams[size]
  306. entries.append(
  307. {"type": type, "size": HEADERSIZE + len(stream), "stream": stream}
  308. )
  309. # Header
  310. fp.write(MAGIC)
  311. file_length = HEADERSIZE # Header
  312. file_length += HEADERSIZE + 8 * len(entries) # TOC
  313. file_length += sum(entry["size"] for entry in entries)
  314. fp.write(struct.pack(">i", file_length))
  315. # TOC
  316. fp.write(b"TOC ")
  317. fp.write(struct.pack(">i", HEADERSIZE + len(entries) * HEADERSIZE))
  318. for entry in entries:
  319. fp.write(entry["type"])
  320. fp.write(struct.pack(">i", entry["size"]))
  321. # Data
  322. for entry in entries:
  323. fp.write(entry["type"])
  324. fp.write(struct.pack(">i", entry["size"]))
  325. fp.write(entry["stream"])
  326. if hasattr(fp, "flush"):
  327. fp.flush()
  328. def _accept(prefix):
  329. return prefix[:4] == MAGIC
  330. Image.register_open(IcnsImageFile.format, IcnsImageFile, _accept)
  331. Image.register_extension(IcnsImageFile.format, ".icns")
  332. Image.register_save(IcnsImageFile.format, _save)
  333. Image.register_mime(IcnsImageFile.format, "image/icns")
  334. if __name__ == "__main__":
  335. if len(sys.argv) < 2:
  336. print("Syntax: python3 IcnsImagePlugin.py [file]")
  337. sys.exit()
  338. with open(sys.argv[1], "rb") as fp:
  339. imf = IcnsImageFile(fp)
  340. for size in imf.info["sizes"]:
  341. imf.size = size
  342. imf.save("out-%s-%s-%s.png" % size)
  343. with Image.open(sys.argv[1]) as im:
  344. im.save("out.png")
  345. if sys.platform == "windows":
  346. os.startfile("out.png")