completed cursor tests

This commit is contained in:
jlwoolf 2022-09-27 12:16:32 -06:00
parent 01d71be3fa
commit 551e2a6afc
3 changed files with 91 additions and 9 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B

View File

@ -1,11 +1,13 @@
import pytest
from io import BytesIO
from PIL import CurImagePlugin, Image
def test_deerstalker():
with Image.open("Tests/images/cur/deerstalker.cur") as im:
assert im.size == (32, 32)
assert im.info['hotspots'] == [(0, 0)]
assert isinstance(im, CurImagePlugin.CurImageFile)
# Check some pixel colors to ensure image is loaded properly
assert im.getpixel((10, 1)) == (0, 0, 0, 0)
@ -16,10 +18,37 @@ def test_deerstalker():
def test_posy_link():
with Image.open("Tests/images/cur/posy_link.cur") as im:
assert im.size == (128, 128)
assert im.info['sizes'] == [(128, 128), (96, 96), (64, 64), (48, 48), (32, 32)]
assert im.info['hotspots'] == [(25, 7), (18, 5), (12, 3), (9, 2), (5, 1)]
# check some pixel colors
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
assert im.getpixel((20, 20)) == (0, 0, 0, 255)
assert im.getpixel((40, 40)) == (255, 255, 255, 255)
im.size = (32, 32)
im.load()
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
assert im.getpixel((10, 10)) == (191, 191, 191, 255)
def test_stopwtch():
with Image.open("Tests/images/cur/stopwtch.cur") as im:
assert im.size == (32, 32)
assert im.info["hotspots"] == [(16, 19)]
assert im.getpixel((16, 16)) == (0, 0, 255, 255)
assert im.getpixel((8, 16)) == (255, 0, 0, 255)
def test_win98_arrow():
with Image.open("Tests/images/cur/win98_arrow.cur") as im:
assert im.size == (32, 32)
assert im.info["hotspots"] == [(10, 10)]
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
assert im.getpixel((16, 16)) == (0, 0, 0, 255)
assert im.getpixel((14, 19)) == (255, 255, 255, 255)
def test_invalid_file():
invalid_file = "Tests/images/cur/posy_link.png"
@ -34,3 +63,56 @@ def test_invalid_file():
with open(no_cursors_file, "rb") as cur.fp:
with pytest.raises(TypeError):
cur._open()
def test_save_win98_arrow():
with Image.open("Tests/images/cur/win98_arrow.png") as im:
# save the data
with BytesIO() as output:
im.save(output, format="CUR", sizes=[(32, 32)], hotspots=[
(10, 10)], bitmap_format="bmp")
with Image.open(output) as im2:
assert im.tobytes() == im2.tobytes()
with BytesIO() as output:
im.save(output, format="CUR")
# check default save params
with Image.open(output) as im2:
assert im2.size == (32, 32)
assert im2.info["sizes"] == [(32, 32), (24, 24), (16, 16)]
assert im2.info["hotspots"] == [(0, 0), (0, 0), (0, 0)]
def test_save_posy_link():
sizes = [(128, 128), (96, 96), (64, 64), (48, 48), (32, 32)]
hotspots = [(25, 7), (18, 5), (12, 3), (9, 2), (5, 1)]
with Image.open("Tests/images/cur/posy_link.png") as im:
# save the data
with BytesIO() as output:
im.save(output, sizes=sizes, hotspots=hotspots,
format="CUR", bitmap_format="bmp")
# make sure saved output is readable
# and sizes/hotspots are correct
with Image.open(output, formats=["CUR"]) as im2:
assert (128, 128) == im2.size
assert sizes == im2.info['sizes']
with BytesIO() as output:
im.save(output, sizes=sizes[3:], hotspots=hotspots[3:],
format="CUR")
# make sure saved output is readable
# and sizes/hotspots are correct
with Image.open(output, formats=["CUR"]) as im2:
assert (48, 48) == im2.size
assert sizes[3:] == im2.info['sizes']
# make sure error is thrown when size and hotspot len's
# don't match
with pytest.raises(ValueError):
im.save(output, sizes=sizes[2:], hotspots=hotspots[3:],
format="CUR", bitmap_format="bmp")

View File

@ -27,7 +27,7 @@ from ._binary import o32le as o32
#
# --------------------------------------------------------------------
_MAGIC = b"\0\0\2\0"
_MAGIC = b"\x00\x00\x02\x00"
def _save(im: Image.Image, fp: BytesIO, filename: str):
@ -43,12 +43,11 @@ def _save(im: Image.Image, fp: BytesIO, filename: str):
raise ValueError("Number of hotspots must be equal to number of cursor sizes")
# sort and remove duplicate sizes
sizes = []
hotspots = []
for size, hotspot in zip(*sorted(zip(s, h), lambda x: x[0])):
sizes, hotspots = [], []
for size, hotspot in sorted(zip(s, h), key=lambda x: x[0]):
if size not in sizes:
sizes.append(size)
hotspots.append(hotspots)
hotspots.append(hotspot)
frames = []
width, height = im.size
@ -90,6 +89,7 @@ def _save(im: Image.Image, fp: BytesIO, filename: str):
image_bytes = image_io.read()
if bmp:
image_bytes = image_bytes[:8] + o32(height * 2) + image_bytes[12:]
bytes_len = len(image_bytes)
fp.write(o32(bytes_len)) # dwBytesInRes(4)
fp.write(o32(offset)) # dwImageOffset(4)
@ -146,10 +146,10 @@ class CurFile(IcoImagePlugin.IcoFile):
icon_header["dim"] = (icon_header["width"], icon_header["height"])
icon_header["square"] = icon_header["width"] * icon_header["height"]
# 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.
# For now, bpp is calculated by subtracting the AND mask (equal to number
# of pixels * 1bpp) and dividing by the number of pixels. This seems
# 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.
# For now, bpp is calculated by subtracting the AND mask (equal to number
# of pixels * 1bpp) and dividing by the number of pixels. This seems
# to work well so far.
BITMAP_INFO_HEADER_SIZE = 40
bpp_without_and = (