mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-05 20:33:24 +03:00
Skip additional data if past end of row
This commit is contained in:
parent
0ace3fcd26
commit
11f1ba3540
BIN
Tests/images/hopper_rle8_row_overflow.bmp
Normal file
BIN
Tests/images/hopper_rle8_row_overflow.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
|
@ -134,6 +134,11 @@ def test_rle8():
|
||||||
with Image.open("Tests/images/hopper_rle8.bmp") as im:
|
with Image.open("Tests/images/hopper_rle8.bmp") as im:
|
||||||
assert_image_similar_tofile(im.convert("RGB"), "Tests/images/hopper.bmp", 12)
|
assert_image_similar_tofile(im.convert("RGB"), "Tests/images/hopper.bmp", 12)
|
||||||
|
|
||||||
|
# This test image has been manually hexedited
|
||||||
|
# to have rows with too much data
|
||||||
|
with Image.open("Tests/images/hopper_rle8_row_overflow.bmp") as im:
|
||||||
|
assert_image_similar_tofile(im.convert("RGB"), "Tests/images/hopper.bmp", 12)
|
||||||
|
|
||||||
|
|
||||||
def test_offset():
|
def test_offset():
|
||||||
# This image has been hexedited
|
# This image has been hexedited
|
||||||
|
|
|
@ -281,17 +281,23 @@ class BmpRleDecoder(ImageFile.PyDecoder):
|
||||||
|
|
||||||
def decode(self, buffer):
|
def decode(self, buffer):
|
||||||
data = bytearray()
|
data = bytearray()
|
||||||
|
x = 0
|
||||||
while True:
|
while True:
|
||||||
num_pixels = self.fd.read(1)[0]
|
num_pixels = self.fd.read(1)[0]
|
||||||
byte = self.fd.read(1)
|
byte = self.fd.read(1)
|
||||||
if num_pixels:
|
if num_pixels:
|
||||||
# encoded mode
|
# encoded mode
|
||||||
|
if x + num_pixels > self.state.xsize:
|
||||||
|
# Too much data for row
|
||||||
|
num_pixels = max(0, self.state.xsize - x)
|
||||||
data += byte * num_pixels
|
data += byte * num_pixels
|
||||||
|
x += num_pixels
|
||||||
else:
|
else:
|
||||||
if byte[0] == 0:
|
if byte[0] == 0:
|
||||||
# end of line
|
# end of line
|
||||||
while len(data) % self.state.xsize != 0:
|
while len(data) % self.state.xsize != 0:
|
||||||
data += b"\x00"
|
data += b"\x00"
|
||||||
|
x = 0
|
||||||
elif byte[0] == 1:
|
elif byte[0] == 1:
|
||||||
# end of bitmap
|
# end of bitmap
|
||||||
break
|
break
|
||||||
|
@ -299,9 +305,11 @@ class BmpRleDecoder(ImageFile.PyDecoder):
|
||||||
# delta
|
# delta
|
||||||
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
|
||||||
else:
|
else:
|
||||||
# absolute mode
|
# absolute mode
|
||||||
data += self.fd.read(byte[0])
|
data += self.fd.read(byte[0])
|
||||||
|
x += byte[0]
|
||||||
|
|
||||||
# align to 16-bit word boundary
|
# align to 16-bit word boundary
|
||||||
if self.fd.tell() % 2 != 0:
|
if self.fd.tell() % 2 != 0:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user