mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 17:54:32 +03:00
Finish reading data at eof
This commit is contained in:
parent
9db527a473
commit
039b7ecd56
|
@ -147,6 +147,25 @@ def test_rle8():
|
||||||
im.load()
|
im.load()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"file_name,length",
|
||||||
|
(
|
||||||
|
# EOF immediately after the header
|
||||||
|
("Tests/images/hopper_rle8.bmp", 1078),
|
||||||
|
# EOF during delta
|
||||||
|
("Tests/images/bmp/q/pal8rletrns.bmp", 3670),
|
||||||
|
# EOF when reading data in absolute mode
|
||||||
|
("Tests/images/bmp/g/pal8rle.bmp", 1064),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_rle8_eof(file_name, length):
|
||||||
|
with open(file_name, "rb") as fp:
|
||||||
|
data = fp.read(length)
|
||||||
|
with Image.open(io.BytesIO(data)) as im:
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
im.load()
|
||||||
|
|
||||||
|
|
||||||
def test_offset():
|
def test_offset():
|
||||||
# This image has been hexedited
|
# This image has been hexedited
|
||||||
# to exclude the palette size from the pixel data offset
|
# to exclude the palette size from the pixel data offset
|
||||||
|
|
|
@ -283,8 +283,11 @@ class BmpRleDecoder(ImageFile.PyDecoder):
|
||||||
data = bytearray()
|
data = bytearray()
|
||||||
x = 0
|
x = 0
|
||||||
while len(data) < self.state.xsize * self.state.ysize:
|
while len(data) < self.state.xsize * self.state.ysize:
|
||||||
num_pixels = self.fd.read(1)[0]
|
pixels = self.fd.read(1)
|
||||||
byte = self.fd.read(1)
|
byte = self.fd.read(1)
|
||||||
|
if not pixels or not byte:
|
||||||
|
break
|
||||||
|
num_pixels = pixels[0]
|
||||||
if num_pixels:
|
if num_pixels:
|
||||||
# encoded mode
|
# encoded mode
|
||||||
if x + num_pixels > self.state.xsize:
|
if x + num_pixels > self.state.xsize:
|
||||||
|
@ -303,12 +306,18 @@ class BmpRleDecoder(ImageFile.PyDecoder):
|
||||||
break
|
break
|
||||||
elif byte[0] == 2:
|
elif byte[0] == 2:
|
||||||
# delta
|
# delta
|
||||||
|
bytes_read = self.fd.read(2)
|
||||||
|
if len(bytes_read) < 2:
|
||||||
|
break
|
||||||
right, up = self.fd.read(2)
|
right, up = self.fd.read(2)
|
||||||
data += b"\x00" * (right + up * self.state.xsize)
|
data += b"\x00" * (right + up * self.state.xsize)
|
||||||
x = len(data) % self.state.xsize
|
x = len(data) % self.state.xsize
|
||||||
else:
|
else:
|
||||||
# absolute mode
|
# absolute mode
|
||||||
data += self.fd.read(byte[0])
|
bytes_read = self.fd.read(byte[0])
|
||||||
|
data += bytes_read
|
||||||
|
if len(bytes_read) < byte[0]:
|
||||||
|
break
|
||||||
x += byte[0]
|
x += byte[0]
|
||||||
|
|
||||||
# align to 16-bit word boundary
|
# align to 16-bit word boundary
|
||||||
|
|
Loading…
Reference in New Issue
Block a user