use offset for all binary input functions instead of slicing

This commit is contained in:
Alexander 2020-05-08 21:11:58 +03:00 committed by Andrew Murray
parent 3757b8c748
commit 1ff61bcaa6
18 changed files with 82 additions and 82 deletions

View File

@ -51,7 +51,7 @@ def _accept(prefix):
def _dib_accept(prefix):
return i32(prefix[:4]) in [12, 40, 64, 108, 124]
return i32(prefix) in [12, 40, 64, 108, 124]
# =============================================================================
@ -86,10 +86,10 @@ class BmpImageFile(ImageFile.ImageFile):
# -------------------------------------------------- IBM OS/2 Bitmap v1
# ----- This format has different offsets because of width/height types
if file_info["header_size"] == 12:
file_info["width"] = i16(header_data[0:2])
file_info["height"] = i16(header_data[2:4])
file_info["planes"] = i16(header_data[4:6])
file_info["bits"] = i16(header_data[6:8])
file_info["width"] = i16(header_data, 0)
file_info["height"] = i16(header_data, 2)
file_info["planes"] = i16(header_data, 4)
file_info["bits"] = i16(header_data, 6)
file_info["compression"] = self.RAW
file_info["palette_padding"] = 3
@ -98,22 +98,22 @@ class BmpImageFile(ImageFile.ImageFile):
elif file_info["header_size"] in (40, 64, 108, 124):
file_info["y_flip"] = header_data[7] == 0xFF
file_info["direction"] = 1 if file_info["y_flip"] else -1
file_info["width"] = i32(header_data[0:4])
file_info["width"] = i32(header_data, 0)
file_info["height"] = (
i32(header_data[4:8])
i32(header_data, 4)
if not file_info["y_flip"]
else 2 ** 32 - i32(header_data[4:8])
else 2 ** 32 - i32(header_data, 4)
)
file_info["planes"] = i16(header_data[8:10])
file_info["bits"] = i16(header_data[10:12])
file_info["compression"] = i32(header_data[12:16])
file_info["planes"] = i16(header_data, 8)
file_info["bits"] = i16(header_data, 10)
file_info["compression"] = i32(header_data, 12)
# byte size of pixel data
file_info["data_size"] = i32(header_data[16:20])
file_info["data_size"] = i32(header_data, 16)
file_info["pixels_per_meter"] = (
i32(header_data[20:24]),
i32(header_data[24:28]),
i32(header_data, 20),
i32(header_data, 24),
)
file_info["colors"] = i32(header_data[28:32])
file_info["colors"] = i32(header_data, 28)
file_info["palette_padding"] = 4
self.info["dpi"] = tuple(
int(x / 39.3701 + 0.5) for x in file_info["pixels_per_meter"]
@ -123,7 +123,7 @@ class BmpImageFile(ImageFile.ImageFile):
for idx, mask in enumerate(
["r_mask", "g_mask", "b_mask", "a_mask"]
):
file_info[mask] = i32(header_data[36 + idx * 4 : 40 + idx * 4])
file_info[mask] = i32(header_data, 36 + idx * 4)
else:
# 40 byte headers only have the three components in the
# bitfields masks, ref:
@ -266,7 +266,7 @@ class BmpImageFile(ImageFile.ImageFile):
if not _accept(head_data):
raise SyntaxError("Not a BMP file")
# read the start position of the BMP image data (u32)
offset = i32(head_data[10:14])
offset = i32(head_data, 10)
# load bitmap information (offset=raster info)
self._bitmap(offset=offset)

View File

@ -47,7 +47,7 @@ class CurImageFile(BmpImagePlugin.BmpImageFile):
# pick the largest cursor in the file
m = b""
for i in range(i16(s[4:])):
for i in range(i16(s, 4)):
s = self.fp.read(16)
if not m:
m = s
@ -57,7 +57,7 @@ class CurImageFile(BmpImagePlugin.BmpImageFile):
raise TypeError("No cursors were found")
# load as bitmap
self._bitmap(i32(m[12:]) + offset)
self._bitmap(i32(m, 12) + offset)
# patch up the bitmap height
self._size = self.size[0], self.size[1] // 2

View File

@ -312,14 +312,14 @@ class EpsImageFile(ImageFile.ImageFile):
fp.seek(0, io.SEEK_END)
length = fp.tell()
offset = 0
elif i32(s[0:4]) == 0xC6D3D0C5:
elif i32(s, 0) == 0xC6D3D0C5:
# FIX for: Some EPS file not handled correctly / issue #302
# EPS can contain binary data
# or start directly with latin coding
# more info see:
# https://web.archive.org/web/20160528181353/http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf
offset = i32(s[4:8])
length = i32(s[8:12])
offset = i32(s, 4)
length = i32(s, 8)
else:
raise SyntaxError("not an EPS file")

View File

@ -26,7 +26,7 @@ from ._binary import o8
def _accept(prefix):
return len(prefix) >= 6 and i16(prefix[4:6]) in [0xAF11, 0xAF12]
return len(prefix) >= 6 and i16(prefix, 4) in [0xAF11, 0xAF12]
##
@ -46,22 +46,22 @@ class FliImageFile(ImageFile.ImageFile):
s = self.fp.read(128)
if not (
_accept(s)
and i16(s[14:16]) in [0, 3] # flags
and i16(s, 14) in [0, 3] # flags
and s[20:22] == b"\x00\x00" # reserved
):
raise SyntaxError("not an FLI/FLC file")
# frames
self.n_frames = i16(s[6:8])
self.n_frames = i16(s, 6)
self.is_animated = self.n_frames > 1
# image characteristics
self.mode = "P"
self._size = i16(s[8:10]), i16(s[10:12])
self._size = i16(s, 8), i16(s, 10)
# animation speed
duration = i32(s[16:20])
magic = i16(s[4:6])
duration = i32(s, 16)
magic = i16(s, 4)
if magic == 0xAF11:
duration = (duration * 1000) // 70
self.info["duration"] = duration
@ -73,17 +73,17 @@ class FliImageFile(ImageFile.ImageFile):
self.__offset = 128
if i16(s[4:6]) == 0xF100:
if i16(s, 4) == 0xF100:
# prefix chunk; ignore it
self.__offset = self.__offset + i32(s)
s = self.fp.read(16)
if i16(s[4:6]) == 0xF1FA:
if i16(s, 4) == 0xF1FA:
# look for palette chunk
s = self.fp.read(6)
if i16(s[4:6]) == 11:
if i16(s, 4) == 11:
self._palette(palette, 2)
elif i16(s[4:6]) == 4:
elif i16(s, 4) == 4:
self._palette(palette, 0)
palette = [o8(r) + o8(g) + o8(b) for (r, g, b) in palette]

View File

@ -29,7 +29,7 @@ from ._binary import i32be as i32
def _accept(prefix):
return len(prefix) >= 8 and i32(prefix[:4]) >= 20 and i32(prefix[4:8]) in (1, 2)
return len(prefix) >= 8 and i32(prefix, 0) >= 20 and i32(prefix, 4) in (1, 2)
##

View File

@ -48,17 +48,17 @@ class GdImageFile(ImageFile.ImageFile):
# Header
s = self.fp.read(1037)
if not i16(s[:2]) in [65534, 65535]:
if not i16(s) in [65534, 65535]:
raise SyntaxError("Not a valid GD 2.x .gd file")
self.mode = "L" # FIXME: "P"
self._size = i16(s[2:4]), i16(s[4:6])
self._size = i16(s, 2), i16(s, 4)
trueColor = s[6]
trueColorOffset = 2 if trueColor else 0
# transparency index
tindex = i32(s[7 + trueColorOffset : 7 + trueColorOffset + 4])
tindex = i32(s, 7 + trueColorOffset)
if tindex < 256:
self.info["transparency"] = tindex

View File

@ -69,7 +69,7 @@ class GifImageFile(ImageFile.ImageFile):
raise SyntaxError("not a GIF file")
self.info["version"] = s[:6]
self._size = i16(s[6:]), i16(s[8:])
self._size = i16(s, 6), i16(s, 8)
self.tile = []
flags = s[10]
bits = (flags & 7) + 1
@ -193,7 +193,7 @@ class GifImageFile(ImageFile.ImageFile):
flags = block[0]
if flags & 1:
info["transparency"] = block[3]
info["duration"] = i16(block[1:3]) * 10
info["duration"] = i16(block, 1) * 10
# disposal method - find the value of bits 4 - 6
dispose_bits = 0b00011100 & flags
@ -223,7 +223,7 @@ class GifImageFile(ImageFile.ImageFile):
if block[:11] == b"NETSCAPE2.0":
block = self.data()
if len(block) >= 3 and block[0] == 1:
info["loop"] = i16(block[1:3])
info["loop"] = i16(block, 1)
while self.data():
pass
@ -234,8 +234,8 @@ class GifImageFile(ImageFile.ImageFile):
s = self.fp.read(9)
# extent
x0, y0 = i16(s[0:]), i16(s[2:])
x1, y1 = x0 + i16(s[4:]), y0 + i16(s[6:])
x0, y0 = i16(s, 0), i16(s, 2)
x1, y1 = x0 + i16(s, 4), y0 + i16(s, 6)
if x1 > self.size[0] or y1 > self.size[1]:
self._size = max(x1, self.size[0]), max(y1, self.size[1])
self.dispose_extent = x0, y0, x1, y1

View File

@ -102,7 +102,7 @@ class IcoFile:
self.entry = []
# Number of items in file
self.nb_items = i16(s[4:])
self.nb_items = i16(s, 4)
# Get headers for each item
for i in range(self.nb_items):
@ -113,10 +113,10 @@ class IcoFile:
"height": s[1],
"nb_color": s[2], # No. of colors in image (0 if >=8bpp)
"reserved": s[3],
"planes": i16(s[4:]),
"bpp": i16(s[6:]),
"size": i32(s[8:]),
"offset": i32(s[12:]),
"planes": i16(s, 4),
"bpp": i16(s, 6),
"size": i32(s, 8),
"offset": i32(s, 12),
}
# See Wikipedia

View File

@ -3374,7 +3374,7 @@ class Exif(MutableMapping):
if self[0x927C][:8] == b"FUJIFILM":
exif_data = self[0x927C]
ifd_offset = i32le(exif_data[8:12])
ifd_offset = i32le(exif_data, 8)
ifd_data = exif_data[ifd_offset:]
makernote = {}

View File

@ -77,7 +77,7 @@ class IptcImageFile(ImageFile.ImageFile):
elif size > 128:
size = i(self.fp.read(size - 128))
else:
size = i16(s[3:])
size = i16(s, 3)
return tag, size

View File

@ -124,10 +124,10 @@ def APP(self, marker):
data = s[offset : offset + size]
if code == 0x03ED: # ResolutionInfo
data = {
"XResolution": i32(data[:4]) / 65536,
"DisplayedUnitsX": i16(data[4:8]),
"YResolution": i32(data[8:12]) / 65536,
"DisplayedUnitsY": i16(data[12:]),
"XResolution": i32(data, 0) / 65536,
"DisplayedUnitsX": i16(data, 4),
"YResolution": i32(data, 8) / 65536,
"DisplayedUnitsY": i16(data, 12),
}
photoshop[code] = data
offset += size
@ -194,7 +194,7 @@ def SOF(self, marker):
n = i16(self.fp.read(2)) - 2
s = ImageFile._safe_read(self.fp, n)
self._size = i16(s[3:]), i16(s[1:])
self._size = i16(s, 3), i16(s, 1)
self.bits = s[0]
if self.bits != 8:

View File

@ -58,12 +58,12 @@ class MspImageFile(ImageFile.ImageFile):
# Header checksum
checksum = 0
for i in range(0, 32, 2):
checksum = checksum ^ i16(s[i : i + 2])
checksum = checksum ^ i16(s, i)
if checksum != 0:
raise SyntaxError("bad MSP checksum")
self.mode = "1"
self._size = i16(s[4:]), i16(s[6:])
self._size = i16(s, 4), i16(s, 6)
if s[:4] == b"DanM":
self.tile = [("raw", (0, 0) + self.size, 32, ("1", 0, 1))]

View File

@ -49,10 +49,10 @@ class PixarImageFile(ImageFile.ImageFile):
# read rest of header
s = s + self.fp.read(508)
self._size = i16(s[418:420]), i16(s[416:418])
self._size = i16(s, 418), i16(s, 416)
# get channel/depth descriptions
mode = i16(s[424:426]), i16(s[426:428])
mode = i16(s, 424), i16(s, 426)
if mode == (14, 2):
self.mode = "RGB"

View File

@ -409,7 +409,7 @@ class PngStream(ChunkStream):
# image header
s = ImageFile._safe_read(self.fp, length)
self.im_size = i32(s), i32(s[4:])
self.im_size = i32(s, 0), i32(s, 4)
try:
self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]
except Exception:
@ -464,7 +464,7 @@ class PngStream(ChunkStream):
elif self.im_mode in ("1", "L", "I"):
self.im_info["transparency"] = i16(s)
elif self.im_mode == "RGB":
self.im_info["transparency"] = i16(s), i16(s[2:]), i16(s[4:])
self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4)
return s
def chunk_gAMA(self, pos, length):
@ -497,7 +497,7 @@ class PngStream(ChunkStream):
# pixels per unit
s = ImageFile._safe_read(self.fp, length)
px, py = i32(s), i32(s[4:])
px, py = i32(s, 0), i32(s, 4)
unit = s[8]
if unit == 1: # meter
dpi = int(px * 0.0254 + 0.5), int(py * 0.0254 + 0.5)
@ -618,7 +618,7 @@ class PngStream(ChunkStream):
warnings.warn("Invalid APNG, will use default PNG image if possible")
return s
self.im_n_frames = n_frames
self.im_info["loop"] = i32(s[4:])
self.im_info["loop"] = i32(s, 4)
self.im_custom_mimetype = "image/apng"
return s
@ -630,13 +630,13 @@ class PngStream(ChunkStream):
):
raise SyntaxError("APNG contains frame sequence errors")
self._seq_num = seq
width, height = i32(s[4:]), i32(s[8:])
px, py = i32(s[12:]), i32(s[16:])
width, height = i32(s, 4), i32(s, 8)
px, py = i32(s, 12), i32(s, 16)
im_w, im_h = self.im_size
if px + width > im_w or py + height > im_h:
raise SyntaxError("APNG contains invalid frames")
self.im_info["bbox"] = (px, py, px + width, py + height)
delay_num, delay_den = i16(s[20:]), i16(s[22:])
delay_num, delay_den = i16(s, 20), i16(s, 22)
if delay_den == 0:
delay_den = 100
self.im_info["duration"] = float(delay_num) / float(delay_den) * 1000

View File

@ -63,12 +63,12 @@ class PsdImageFile(ImageFile.ImageFile):
# header
s = read(26)
if not _accept(s) or i16(s[4:]) != 1:
if not _accept(s) or i16(s, 4) != 1:
raise SyntaxError("not a PSD file")
psd_bits = i16(s[22:])
psd_channels = i16(s[12:])
psd_mode = i16(s[24:])
psd_bits = i16(s, 22)
psd_channels = i16(s, 12)
psd_mode = i16(s, 24)
mode, channels = MODES[(psd_mode, psd_bits)]
@ -76,7 +76,7 @@ class PsdImageFile(ImageFile.ImageFile):
raise OSError("not enough channels")
self.mode = mode
self._size = i32(s[18:]), i32(s[14:])
self._size = i32(s, 18), i32(s, 14)
#
# color mode data
@ -291,7 +291,7 @@ def _maketile(file, mode, bbox, channels):
layer += ";I"
tile.append(("packbits", bbox, offset, layer))
for y in range(ysize):
offset = offset + i16(bytecount[i : i + 2])
offset = offset + i16(bytecount, i)
i += 2
file.seek(offset)

View File

@ -69,16 +69,16 @@ class SgiImageFile(ImageFile.ImageFile):
bpc = s[3]
# dimension : 1, 2 or 3 (depending on xsize, ysize and zsize)
dimension = i16(s[4:])
dimension = i16(s, 4)
# xsize : width
xsize = i16(s[6:])
xsize = i16(s, 6)
# ysize : height
ysize = i16(s[8:])
ysize = i16(s, 8)
# zsize : channels count
zsize = i16(s[10:])
zsize = i16(s, 10)
# layout
layout = bpc, dimension, zsize

View File

@ -58,13 +58,13 @@ class SunImageFile(ImageFile.ImageFile):
offset = 32
self._size = i32(s[4:8]), i32(s[8:12])
self._size = i32(s, 4), i32(s, 8)
depth = i32(s[12:16])
# data_length = i32(s[16:20]) # unreliable, ignore.
file_type = i32(s[20:24])
palette_type = i32(s[24:28]) # 0: None, 1: RGB, 2: Raw/arbitrary
palette_length = i32(s[28:32])
depth = i32(s, 12)
# data_length = i32(s, 16) # unreliable, ignore.
file_type = i32(s, 20)
palette_type = i32(s, 24) # 0: None, 1: RGB, 2: Raw/arbitrary
palette_length = i32(s, 28)
if depth == 1:
self.mode, rawmode = "1", "1;I"

View File

@ -64,7 +64,7 @@ class TgaImageFile(ImageFile.ImageFile):
flags = s[17]
self._size = i16(s[12:]), i16(s[14:])
self._size = i16(s, 12), i16(s, 14)
# validate header fields
if (
@ -110,7 +110,7 @@ class TgaImageFile(ImageFile.ImageFile):
if colormaptype:
# read palette
start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])
start, size, mapdepth = i16(s, 3), i16(s, 5), i16(s, 7)
if mapdepth == 16:
self.palette = ImagePalette.raw(
"BGR;16", b"\0" * 2 * start + self.fp.read(2 * size)