IcoFile: remove redundant dim/square from entry dicts

This commit is contained in:
Aarni Koskela 2023-02-22 13:05:06 +02:00
parent 17c376c309
commit d9d5c0b9af

View File

@ -142,11 +142,11 @@ class IcoFile:
msg = "No images were found" msg = "No images were found"
raise TypeError(msg) raise TypeError(msg)
self.entry = sorted(self.entry, key=lambda x: x["color_depth"]) # Ensure self.entry is sorted by size and colorfulness, largest first
# ICO images are usually squares self.entry.sort(
# self.entry = sorted(self.entry, key=lambda x: x['width']) key=lambda x: (x["width"] * x["height"], x["color_depth"]),
self.entry = sorted(self.entry, key=lambda x: x["square"]) reverse=True,
self.entry.reverse() )
def _check_header(self, header): def _check_header(self, header):
if not _accept(header): if not _accept(header):
@ -181,8 +181,6 @@ class IcoFile:
or (icon_header["nb_color"] != 0 and ceil(log(icon_header["nb_color"], 2))) or (icon_header["nb_color"] != 0 and ceil(log(icon_header["nb_color"], 2)))
or 256 or 256
) )
icon_header["dim"] = (icon_header["width"], icon_header["height"])
icon_header["square"] = icon_header["width"] * icon_header["height"]
return icon_header return icon_header
def sizes(self): def sizes(self):
@ -191,9 +189,17 @@ class IcoFile:
""" """
return {(h["width"], h["height"]) for h in self.entry} return {(h["width"], h["height"]) for h in self.entry}
def largest_size(self):
"""
Get the largest icon size.
"""
return max(self.sizes(), key=lambda x: x[0] * x[1])
def getentryindex(self, size, bpp=False): def getentryindex(self, size, bpp=False):
for i, h in enumerate(self.entry): for i, h in enumerate(self.entry):
if size == h["dim"] and (bpp is False or bpp == h["color_depth"]): if size == (h["width"], h["height"]) and (
bpp is False or bpp == h["color_depth"]
):
return i return i
return 0 return 0
@ -317,7 +323,7 @@ class IcoImageFile(ImageFile.ImageFile):
def _open(self): def _open(self):
self.ico = self._ico_file_class(self.fp) self.ico = self._ico_file_class(self.fp)
self.info["sizes"] = self.ico.sizes() self.info["sizes"] = self.ico.sizes()
self.size = self.ico.entry[0]["dim"] self.size = self.ico.largest_size()
self.load() self.load()
@property @property