IcoImagePlugin.py 11 KB

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