Skip to content

Commit

Permalink
Merge pull request #5845 from radarhere/icns
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Nov 21, 2021
2 parents b692faf + 838c8ef commit 877d97e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 7 additions & 1 deletion Tests/test_file_icns.py
@@ -1,8 +1,9 @@
import io
import os

import pytest

from PIL import IcnsImagePlugin, Image, features
from PIL import IcnsImagePlugin, Image, _binary, features

from .helper import assert_image_equal, assert_image_similar_tofile

Expand Down Expand Up @@ -38,6 +39,11 @@ def test_save(tmp_path):
assert reread.size == (1024, 1024)
assert reread.format == "ICNS"

file_length = os.path.getsize(temp_file)
with open(temp_file, "rb") as fp:
fp.seek(4)
assert _binary.i32be(fp.read(4)) == file_length


def test_save_append_images(tmp_path):
temp_file = str(tmp_path / "temp.icns")
Expand Down
13 changes: 9 additions & 4 deletions src/PIL/IcnsImagePlugin.py
Expand Up @@ -337,23 +337,28 @@ def _save(im, fp, filename):
entries = []
for type, size in sizes.items():
stream = size_streams[size]
entries.append({"type": type, "size": len(stream), "stream": stream})
entries.append(
{"type": type, "size": HEADERSIZE + len(stream), "stream": stream}
)

# Header
fp.write(MAGIC)
fp.write(struct.pack(">i", sum(entry["size"] for entry in entries)))
file_length = HEADERSIZE # Header
file_length += HEADERSIZE + 8 * len(entries) # TOC
file_length += sum(entry["size"] for entry in entries)
fp.write(struct.pack(">i", file_length))

# TOC
fp.write(b"TOC ")
fp.write(struct.pack(">i", HEADERSIZE + len(entries) * HEADERSIZE))
for entry in entries:
fp.write(entry["type"])
fp.write(struct.pack(">i", HEADERSIZE + entry["size"]))
fp.write(struct.pack(">i", entry["size"]))

# Data
for entry in entries:
fp.write(entry["type"])
fp.write(struct.pack(">i", HEADERSIZE + entry["size"]))
fp.write(struct.pack(">i", entry["size"]))
fp.write(entry["stream"])

if hasattr(fp, "flush"):
Expand Down

0 comments on commit 877d97e

Please sign in to comment.