IcoImagePlugin.py 13 KB

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