IcoImagePlugin.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # Windows Icon support for PIL
  6. #
  7. # History:
  8. # 96-05-27 fl Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1996.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15. # This plugin is a refactored version of Win32IconImagePlugin by Bryan Davis
  16. # <casadebender@gmail.com>.
  17. # https://code.google.com/archive/p/casadebender/wikis/Win32IconImagePlugin.wiki
  18. #
  19. # Icon format references:
  20. # * https://en.wikipedia.org/wiki/ICO_(file_format)
  21. # * https://msdn.microsoft.com/en-us/library/ms997538.aspx
  22. from __future__ import annotations
  23. import warnings
  24. from io import BytesIO
  25. from math import ceil, log
  26. from typing import IO, NamedTuple
  27. from . import BmpImagePlugin, Image, ImageFile, PngImagePlugin
  28. from ._binary import i16le as i16
  29. from ._binary import i32le as i32
  30. from ._binary import o8
  31. from ._binary import o16le as o16
  32. from ._binary import o32le as o32
  33. #
  34. # --------------------------------------------------------------------
  35. _MAGIC = b"\0\0\1\0"
  36. def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
  37. fp.write(_MAGIC) # (2+2)
  38. bmp = im.encoderinfo.get("bitmap_format") == "bmp"
  39. sizes = im.encoderinfo.get(
  40. "sizes",
  41. [(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)],
  42. )
  43. frames = []
  44. provided_ims = [im] + im.encoderinfo.get("append_images", [])
  45. width, height = im.size
  46. for size in sorted(set(sizes)):
  47. if size[0] > width or size[1] > height or size[0] > 256 or size[1] > 256:
  48. continue
  49. for provided_im in provided_ims:
  50. if provided_im.size != size:
  51. continue
  52. frames.append(provided_im)
  53. if bmp:
  54. bits = BmpImagePlugin.SAVE[provided_im.mode][1]
  55. bits_used = [bits]
  56. for other_im in provided_ims:
  57. if other_im.size != size:
  58. continue
  59. bits = BmpImagePlugin.SAVE[other_im.mode][1]
  60. if bits not in bits_used:
  61. # Another image has been supplied for this size
  62. # with a different bit depth
  63. frames.append(other_im)
  64. bits_used.append(bits)
  65. break
  66. else:
  67. # TODO: invent a more convenient method for proportional scalings
  68. frame = provided_im.copy()
  69. frame.thumbnail(size, Image.Resampling.LANCZOS, reducing_gap=None)
  70. frames.append(frame)
  71. fp.write(o16(len(frames))) # idCount(2)
  72. offset = fp.tell() + len(frames) * 16
  73. for frame in frames:
  74. width, height = frame.size
  75. # 0 means 256
  76. fp.write(o8(width if width < 256 else 0)) # bWidth(1)
  77. fp.write(o8(height if height < 256 else 0)) # bHeight(1)
  78. bits, colors = BmpImagePlugin.SAVE[frame.mode][1:] if bmp else (32, 0)
  79. fp.write(o8(colors)) # bColorCount(1)
  80. fp.write(b"\0") # bReserved(1)
  81. fp.write(b"\0\0") # wPlanes(2)
  82. fp.write(o16(bits)) # wBitCount(2)
  83. image_io = BytesIO()
  84. if bmp:
  85. frame.save(image_io, "dib")
  86. if bits != 32:
  87. and_mask = Image.new("1", size)
  88. ImageFile._save(
  89. and_mask,
  90. image_io,
  91. [ImageFile._Tile("raw", (0, 0) + size, 0, ("1", 0, -1))],
  92. )
  93. else:
  94. frame.save(image_io, "png")
  95. image_io.seek(0)
  96. image_bytes = image_io.read()
  97. if bmp:
  98. image_bytes = image_bytes[:8] + o32(height * 2) + image_bytes[12:]
  99. bytes_len = len(image_bytes)
  100. fp.write(o32(bytes_len)) # dwBytesInRes(4)
  101. fp.write(o32(offset)) # dwImageOffset(4)
  102. current = fp.tell()
  103. fp.seek(offset)
  104. fp.write(image_bytes)
  105. offset = offset + bytes_len
  106. fp.seek(current)
  107. def _accept(prefix: bytes) -> bool:
  108. return prefix[:4] == _MAGIC
  109. class IconHeader(NamedTuple):
  110. width: int
  111. height: int
  112. nb_color: int
  113. reserved: int
  114. planes: int
  115. bpp: int
  116. size: int
  117. offset: int
  118. dim: tuple[int, int]
  119. square: int
  120. color_depth: int
  121. class IcoFile:
  122. def __init__(self, buf: IO[bytes]) -> None:
  123. """
  124. Parse image from file-like object containing ico file data
  125. """
  126. # check magic
  127. s = buf.read(6)
  128. if not _accept(s):
  129. msg = "not an ICO file"
  130. raise SyntaxError(msg)
  131. self.buf = buf
  132. self.entry = []
  133. # Number of items in file
  134. self.nb_items = i16(s, 4)
  135. # Get headers for each item
  136. for i in range(self.nb_items):
  137. s = buf.read(16)
  138. # See Wikipedia
  139. width = s[0] or 256
  140. height = s[1] or 256
  141. # No. of colors in image (0 if >=8bpp)
  142. nb_color = s[2]
  143. bpp = i16(s, 6)
  144. icon_header = IconHeader(
  145. width=width,
  146. height=height,
  147. nb_color=nb_color,
  148. reserved=s[3],
  149. planes=i16(s, 4),
  150. bpp=i16(s, 6),
  151. size=i32(s, 8),
  152. offset=i32(s, 12),
  153. dim=(width, height),
  154. square=width * height,
  155. # See Wikipedia notes about color depth.
  156. # We need this just to differ images with equal sizes
  157. color_depth=bpp or (nb_color != 0 and ceil(log(nb_color, 2))) or 256,
  158. )
  159. self.entry.append(icon_header)
  160. self.entry = sorted(self.entry, key=lambda x: x.color_depth)
  161. # ICO images are usually squares
  162. self.entry = sorted(self.entry, key=lambda x: x.square, reverse=True)
  163. def sizes(self) -> set[tuple[int, int]]:
  164. """
  165. Get a set of all available icon sizes and color depths.
  166. """
  167. return {(h.width, h.height) for h in self.entry}
  168. def getentryindex(self, size: tuple[int, int], bpp: int | bool = False) -> int:
  169. for i, h in enumerate(self.entry):
  170. if size == h.dim and (bpp is False or bpp == h.color_depth):
  171. return i
  172. return 0
  173. def getimage(self, size: tuple[int, int], bpp: int | bool = False) -> Image.Image:
  174. """
  175. Get an image from the icon
  176. """
  177. return self.frame(self.getentryindex(size, bpp))
  178. def frame(self, idx: int) -> Image.Image:
  179. """
  180. Get an image from frame idx
  181. """
  182. header = self.entry[idx]
  183. self.buf.seek(header.offset)
  184. data = self.buf.read(8)
  185. self.buf.seek(header.offset)
  186. im: Image.Image
  187. if data[:8] == PngImagePlugin._MAGIC:
  188. # png frame
  189. im = PngImagePlugin.PngImageFile(self.buf)
  190. Image._decompression_bomb_check(im.size)
  191. else:
  192. # XOR + AND mask bmp frame
  193. im = BmpImagePlugin.DibImageFile(self.buf)
  194. Image._decompression_bomb_check(im.size)
  195. # change tile dimension to only encompass XOR image
  196. im._size = (im.size[0], int(im.size[1] / 2))
  197. d, e, o, a = im.tile[0]
  198. im.tile[0] = ImageFile._Tile(d, (0, 0) + im.size, o, a)
  199. # figure out where AND mask image starts
  200. if header.bpp == 32:
  201. # 32-bit color depth icon image allows semitransparent areas
  202. # PIL's DIB format ignores transparency bits, recover them.
  203. # The DIB is packed in BGRX byte order where X is the alpha
  204. # channel.
  205. # Back up to start of bmp data
  206. self.buf.seek(o)
  207. # extract every 4th byte (eg. 3,7,11,15,...)
  208. alpha_bytes = self.buf.read(im.size[0] * im.size[1] * 4)[3::4]
  209. # convert to an 8bpp grayscale image
  210. try:
  211. mask = Image.frombuffer(
  212. "L", # 8bpp
  213. im.size, # (w, h)
  214. alpha_bytes, # source chars
  215. "raw", # raw decoder
  216. ("L", 0, -1), # 8bpp inverted, unpadded, reversed
  217. )
  218. except ValueError:
  219. if ImageFile.LOAD_TRUNCATED_IMAGES:
  220. mask = None
  221. else:
  222. raise
  223. else:
  224. # get AND image from end of bitmap
  225. w = im.size[0]
  226. if (w % 32) > 0:
  227. # bitmap row data is aligned to word boundaries
  228. w += 32 - (im.size[0] % 32)
  229. # the total mask data is
  230. # padded row size * height / bits per char
  231. total_bytes = int((w * im.size[1]) / 8)
  232. and_mask_offset = header.offset + header.size - total_bytes
  233. self.buf.seek(and_mask_offset)
  234. mask_data = self.buf.read(total_bytes)
  235. # convert raw data to image
  236. try:
  237. mask = Image.frombuffer(
  238. "1", # 1 bpp
  239. im.size, # (w, h)
  240. mask_data, # source chars
  241. "raw", # raw decoder
  242. ("1;I", int(w / 8), -1), # 1bpp inverted, padded, reversed
  243. )
  244. except ValueError:
  245. if ImageFile.LOAD_TRUNCATED_IMAGES:
  246. mask = None
  247. else:
  248. raise
  249. # now we have two images, im is XOR image and mask is AND image
  250. # apply mask image as alpha channel
  251. if mask:
  252. im = im.convert("RGBA")
  253. im.putalpha(mask)
  254. return im
  255. ##
  256. # Image plugin for Windows Icon files.
  257. class IcoImageFile(ImageFile.ImageFile):
  258. """
  259. PIL read-only image support for Microsoft Windows .ico files.
  260. By default the largest resolution image in the file will be loaded. This
  261. can be changed by altering the 'size' attribute before calling 'load'.
  262. The info dictionary has a key 'sizes' that is a list of the sizes available
  263. in the icon file.
  264. Handles classic, XP and Vista icon formats.
  265. When saving, PNG compression is used. Support for this was only added in
  266. Windows Vista. If you are unable to view the icon in Windows, convert the
  267. image to "RGBA" mode before saving.
  268. This plugin is a refactored version of Win32IconImagePlugin by Bryan Davis
  269. <casadebender@gmail.com>.
  270. https://code.google.com/archive/p/casadebender/wikis/Win32IconImagePlugin.wiki
  271. """
  272. format = "ICO"
  273. format_description = "Windows Icon"
  274. def _open(self) -> None:
  275. self.ico = IcoFile(self.fp)
  276. self.info["sizes"] = self.ico.sizes()
  277. self.size = self.ico.entry[0].dim
  278. self.load()
  279. @property
  280. def size(self) -> tuple[int, int]:
  281. return self._size
  282. @size.setter
  283. def size(self, value: tuple[int, int]) -> None:
  284. if value not in self.info["sizes"]:
  285. msg = "This is not one of the allowed sizes of this image"
  286. raise ValueError(msg)
  287. self._size = value
  288. def load(self) -> Image.core.PixelAccess | None:
  289. if self._im is not None and self.im.size == self.size:
  290. # Already loaded
  291. return Image.Image.load(self)
  292. im = self.ico.getimage(self.size)
  293. # if tile is PNG, it won't really be loaded yet
  294. im.load()
  295. self.im = im.im
  296. self._mode = im.mode
  297. if im.palette:
  298. self.palette = im.palette
  299. if im.size != self.size:
  300. warnings.warn("Image was not the expected size")
  301. index = self.ico.getentryindex(self.size)
  302. sizes = list(self.info["sizes"])
  303. sizes[index] = im.size
  304. self.info["sizes"] = set(sizes)
  305. self.size = im.size
  306. return None
  307. def load_seek(self, pos: int) -> None:
  308. # Flag the ImageFile.Parser so that it
  309. # just does all the decode at the end.
  310. pass
  311. #
  312. # --------------------------------------------------------------------
  313. Image.register_open(IcoImageFile.format, IcoImageFile, _accept)
  314. Image.register_save(IcoImageFile.format, _save)
  315. Image.register_extension(IcoImageFile.format, ".ico")
  316. Image.register_mime(IcoImageFile.format, "image/x-icon")