IcoImagePlugin: refactor _check_header

This commit is contained in:
Aarni Koskela 2023-02-22 10:34:01 +02:00
parent b7055aeaba
commit fd96ed3f8b

View File

@ -121,14 +121,12 @@ def _accept(prefix):
class IcoFile:
def __init__(self, buf):
"""
Parse image from file-like object containing ico file data
Parse image from file-like object containing ico/cur file data
"""
# check magic
s = buf.read(6)
if not _accept(s):
msg = "not an ICO file"
raise SyntaxError(msg)
self._check_header(s)
self.buf = buf
self.entry = []
@ -182,6 +180,11 @@ class IcoFile:
self.entry = sorted(self.entry, key=lambda x: x["square"])
self.entry.reverse()
def _check_header(self, header):
if not _accept(header):
msg = "not an ICO file"
raise SyntaxError(msg)
def sizes(self):
"""
Get a list of all available icon sizes and color depths.
@ -308,8 +311,11 @@ class IcoImageFile(ImageFile.ImageFile):
format = "ICO"
format_description = "Windows Icon"
# For subclasses.
_ico_file_class = IcoFile
def _open(self):
self.ico = IcoFile(self.fp)
self.ico = self._ico_file_class(self.fp)
self.info["sizes"] = self.ico.sizes()
self.size = self.ico.entry[0]["dim"]
self.load()