mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-15 17:54:46 +03:00
Moved hotspots into separate list to avoid conflicting header types
This commit is contained in:
parent
60c89cb9e9
commit
879f4b7e54
|
@ -18,7 +18,8 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import IO, NamedTuple
|
from math import ceil, log
|
||||||
|
from typing import IO
|
||||||
|
|
||||||
from . import BmpImagePlugin, IcoImagePlugin, Image, ImageFile
|
from . import BmpImagePlugin, IcoImagePlugin, Image, ImageFile
|
||||||
from ._binary import i16le as i16
|
from ._binary import i16le as i16
|
||||||
|
@ -110,20 +111,6 @@ def _accept(prefix: bytes) -> bool:
|
||||||
return prefix.startswith(_MAGIC)
|
return prefix.startswith(_MAGIC)
|
||||||
|
|
||||||
|
|
||||||
class IconHeader(NamedTuple):
|
|
||||||
width: int
|
|
||||||
height: int
|
|
||||||
nb_color: int
|
|
||||||
reserved: int
|
|
||||||
bpp: int
|
|
||||||
x_hotspot: int
|
|
||||||
y_hotspot: int
|
|
||||||
size: int
|
|
||||||
offset: int
|
|
||||||
dim: tuple[int, int]
|
|
||||||
square: int
|
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Image plugin for Windows Cursor files.
|
# Image plugin for Windows Cursor files.
|
||||||
class CurFile(IcoImagePlugin.IcoFile):
|
class CurFile(IcoImagePlugin.IcoFile):
|
||||||
|
@ -139,7 +126,7 @@ class CurFile(IcoImagePlugin.IcoFile):
|
||||||
raise SyntaxError(msg)
|
raise SyntaxError(msg)
|
||||||
|
|
||||||
self.buf = buf
|
self.buf = buf
|
||||||
self.entry = []
|
entry = []
|
||||||
|
|
||||||
# Number of items in file
|
# Number of items in file
|
||||||
self.nb_items = i16(s, 4)
|
self.nb_items = i16(s, 4)
|
||||||
|
@ -151,9 +138,12 @@ class CurFile(IcoImagePlugin.IcoFile):
|
||||||
# See Wikipedia
|
# See Wikipedia
|
||||||
width = s[0] or 256
|
width = s[0] or 256
|
||||||
height = s[1] or 256
|
height = s[1] or 256
|
||||||
|
square = width * height
|
||||||
|
|
||||||
size = i32(s, 8)
|
size = i32(s, 8)
|
||||||
square = width * height
|
|
||||||
|
# No. of colors in image (0 if >=8bpp)
|
||||||
|
nb_color = s[2]
|
||||||
|
|
||||||
# TODO: This needs further investigation. Cursor files do not really
|
# TODO: This needs further investigation. Cursor files do not really
|
||||||
# specify their bpp like ICO's as those bits are used for the y_hotspot.
|
# specify their bpp like ICO's as those bits are used for the y_hotspot.
|
||||||
|
@ -167,26 +157,31 @@ class CurFile(IcoImagePlugin.IcoFile):
|
||||||
else:
|
else:
|
||||||
bpp = bpp_without_and
|
bpp = bpp_without_and
|
||||||
|
|
||||||
icon_header = IconHeader(
|
entry.append(
|
||||||
|
(
|
||||||
|
square,
|
||||||
|
IcoImagePlugin.IconHeader(
|
||||||
width=width,
|
width=width,
|
||||||
height=height,
|
height=height,
|
||||||
nb_color=s[2], # No. of colors in image (0 if >=8bpp)
|
nb_color=nb_color,
|
||||||
reserved=s[3],
|
reserved=s[3],
|
||||||
|
planes=0,
|
||||||
bpp=bpp,
|
bpp=bpp,
|
||||||
x_hotspot=i16(s, 4),
|
|
||||||
y_hotspot=i16(s, 6),
|
|
||||||
size=size,
|
size=size,
|
||||||
offset=i32(s, 12),
|
offset=i32(s, 12),
|
||||||
dim=(width, height),
|
dim=(width, height),
|
||||||
square=square,
|
square=square,
|
||||||
|
color_depth=bpp
|
||||||
|
or (nb_color != 0 and ceil(log(nb_color, 2)))
|
||||||
|
or 256,
|
||||||
|
),
|
||||||
|
(i16(s, 4), i16(s, 6)), # x, y
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.entry.append(icon_header)
|
entry = sorted(entry, key=lambda x: x[0], reverse=True)
|
||||||
|
self.entry = [x[1] for x in entry]
|
||||||
self.entry = sorted(self.entry, key=lambda x: x.square, reverse=True)
|
self.hotspots = [x[2] for x in entry]
|
||||||
|
|
||||||
def hotspots(self) -> list[tuple[int, int]]:
|
|
||||||
return [(h.x_hotspot, h.y_hotspot) for h in self.entry]
|
|
||||||
|
|
||||||
|
|
||||||
class CurImageFile(IcoImagePlugin.IcoImageFile):
|
class CurImageFile(IcoImagePlugin.IcoImageFile):
|
||||||
|
@ -218,7 +213,7 @@ class CurImageFile(IcoImagePlugin.IcoImageFile):
|
||||||
def _open(self) -> None:
|
def _open(self) -> None:
|
||||||
self.ico = CurFile(self.fp)
|
self.ico = CurFile(self.fp)
|
||||||
self.info["sizes"] = self.ico.sizes()
|
self.info["sizes"] = self.ico.sizes()
|
||||||
self.info["hotspots"] = self.ico.hotspots()
|
self.info["hotspots"] = self.ico.hotspots
|
||||||
if len(self.ico.entry) > 0:
|
if len(self.ico.entry) > 0:
|
||||||
self.size = self.ico.entry[0].dim
|
self.size = self.ico.entry[0].dim
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user