Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSD layer co-ordinates may be negative #7706

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Tests/images/negative_top_left_layer.psd
Binary file not shown.
5 changes: 5 additions & 0 deletions Tests/test_file_psd.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ def test_rgba() -> None:
assert_image_equal_tofile(im, "Tests/images/imagedraw_square.png")


def test_negative_top_left_layer() -> None:
with Image.open("Tests/images/negative_top_left_layer.psd") as im:
assert im.layers[0][2] == (-50, -50, 50, 50)


def test_layer_skip() -> None:
with Image.open("Tests/images/five_channels.psd") as im:
assert im.n_frames == 1
Expand Down
16 changes: 8 additions & 8 deletions src/PIL/PsdImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from ._binary import i16be as i16
from ._binary import i32be as i32
from ._binary import si16be as si16
from ._binary import si32be as si32

Check warning on line 27 in src/PIL/PsdImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PsdImagePlugin.py#L27

Added line #L27 was not covered by tests

MODES = {
# (photoshop mode, bits) -> (pil mode, required channels)
Expand Down Expand Up @@ -177,22 +178,21 @@

for _ in range(abs(ct)):
# bounding box
y0 = i32(read(4))
x0 = i32(read(4))
y1 = i32(read(4))
x1 = i32(read(4))
y0 = si32(read(4))
x0 = si32(read(4))
y1 = si32(read(4))
x1 = si32(read(4))

Check warning on line 184 in src/PIL/PsdImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PsdImagePlugin.py#L181-L184

Added lines #L181 - L184 were not covered by tests

# image info
mode = []
ct_types = i16(read(2))
types = list(range(ct_types))
if len(types) > 4:
fp.seek(len(types) * 6 + 12, io.SEEK_CUR)
if ct_types > 4:
fp.seek(ct_types * 6 + 12, io.SEEK_CUR)

Check warning on line 190 in src/PIL/PsdImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PsdImagePlugin.py#L189-L190

Added lines #L189 - L190 were not covered by tests
size = i32(read(4))
fp.seek(size, io.SEEK_CUR)
continue

for _ in types:
for _ in range(ct_types):

Check warning on line 195 in src/PIL/PsdImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PsdImagePlugin.py#L195

Added line #L195 was not covered by tests
type = i16(read(2))

if type == 65535:
Expand Down
10 changes: 10 additions & 0 deletions src/PIL/_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
return unpack_from("<i", c, o)[0]


def si32be(c: bytes, o: int = 0) -> int:

Check warning on line 80 in src/PIL/_binary.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/_binary.py#L80

Added line #L80 was not covered by tests
"""
Converts a 4-bytes (32 bits) string to a signed integer, big endian.

:param c: string containing bytes to convert
:param o: offset of bytes to convert in string
"""
return unpack_from(">i", c, o)[0]

Check warning on line 87 in src/PIL/_binary.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/_binary.py#L87

Added line #L87 was not covered by tests


def i16be(c: bytes, o: int = 0) -> int:
return unpack_from(">H", c, o)[0]

Expand Down
Loading