PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Windows/Python3.8/WPy64-3830/WPy64-3830/python-3.8.3.amd64/Lib/site-packages/PIL/IcoImagePlugin.py

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