Skip to content

Commit

Permalink
Layer co-ordinates may be negative
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jan 10, 2024
1 parent df99d48 commit c2907dc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
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 @@ -112,6 +112,11 @@ def test_rgba():
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():
with Image.open("Tests/images/five_channels.psd") as im:
assert im.n_frames == 1
Expand Down
9 changes: 5 additions & 4 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

MODES = {
# (photoshop mode, bits) -> (pil mode, required channels)
Expand Down Expand Up @@ -177,10 +178,10 @@ def read(size):

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))

# image info
mode = []
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 @@ def si32le(c: bytes, o: int = 0) -> int:
return unpack_from("<i", c, o)[0]


def si32be(c: bytes, o: int = 0) -> int:
"""
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]


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

Expand Down

0 comments on commit c2907dc

Please sign in to comment.