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

View File

@ -47,7 +47,7 @@ class CurImageFile(BmpImagePlugin.BmpImageFile):
# pick the largest cursor in the file # pick the largest cursor in the file
m = b"" m = b""
for i in range(i16(s[4:])): for i in range(i16(s, 4)):
s = self.fp.read(16) s = self.fp.read(16)
if not m: if not m:
m = s m = s
@ -57,7 +57,7 @@ class CurImageFile(BmpImagePlugin.BmpImageFile):
raise TypeError("No cursors were found") raise TypeError("No cursors were found")
# load as bitmap # load as bitmap
self._bitmap(i32(m[12:]) + offset) self._bitmap(i32(m, 12) + offset)
# patch up the bitmap height # patch up the bitmap height
self._size = self.size[0], self.size[1] // 2 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) fp.seek(0, io.SEEK_END)
length = fp.tell() length = fp.tell()
offset = 0 offset = 0
elif i32(s[0:4]) == 0xC6D3D0C5: elif i32(s, 0) == 0xC6D3D0C5:
# FIX for: Some EPS file not handled correctly / issue #302 # FIX for: Some EPS file not handled correctly / issue #302
# EPS can contain binary data # EPS can contain binary data
# or start directly with latin coding # or start directly with latin coding
# more info see: # more info see:
# https://web.archive.org/web/20160528181353/http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf # https://web.archive.org/web/20160528181353/http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf
offset = i32(s[4:8]) offset = i32(s, 4)
length = i32(s[8:12]) length = i32(s, 8)
else: else:
raise SyntaxError("not an EPS file") raise SyntaxError("not an EPS file")

View File

@ -26,7 +26,7 @@ from ._binary import o8
def _accept(prefix): 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) s = self.fp.read(128)
if not ( if not (
_accept(s) _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 and s[20:22] == b"\x00\x00" # reserved
): ):
raise SyntaxError("not an FLI/FLC file") raise SyntaxError("not an FLI/FLC file")
# frames # frames
self.n_frames = i16(s[6:8]) self.n_frames = i16(s, 6)
self.is_animated = self.n_frames > 1 self.is_animated = self.n_frames > 1
# image characteristics # image characteristics
self.mode = "P" self.mode = "P"
self._size = i16(s[8:10]), i16(s[10:12]) self._size = i16(s, 8), i16(s, 10)
# animation speed # animation speed
duration = i32(s[16:20]) duration = i32(s, 16)
magic = i16(s[4:6]) magic = i16(s, 4)
if magic == 0xAF11: if magic == 0xAF11:
duration = (duration * 1000) // 70 duration = (duration * 1000) // 70
self.info["duration"] = duration self.info["duration"] = duration
@ -73,17 +73,17 @@ class FliImageFile(ImageFile.ImageFile):
self.__offset = 128 self.__offset = 128
if i16(s[4:6]) == 0xF100: if i16(s, 4) == 0xF100:
# prefix chunk; ignore it # prefix chunk; ignore it
self.__offset = self.__offset + i32(s) self.__offset = self.__offset + i32(s)
s = self.fp.read(16) s = self.fp.read(16)
if i16(s[4:6]) == 0xF1FA: if i16(s, 4) == 0xF1FA:
# look for palette chunk # look for palette chunk
s = self.fp.read(6) s = self.fp.read(6)
if i16(s[4:6]) == 11: if i16(s, 4) == 11:
self._palette(palette, 2) self._palette(palette, 2)
elif i16(s[4:6]) == 4: elif i16(s, 4) == 4:
self._palette(palette, 0) self._palette(palette, 0)
palette = [o8(r) + o8(g) + o8(b) for (r, g, b) in palette] 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): 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 # Header
s = self.fp.read(1037) 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") raise SyntaxError("Not a valid GD 2.x .gd file")
self.mode = "L" # FIXME: "P" 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] trueColor = s[6]
trueColorOffset = 2 if trueColor else 0 trueColorOffset = 2 if trueColor else 0
# transparency index # transparency index
tindex = i32(s[7 + trueColorOffset : 7 + trueColorOffset + 4]) tindex = i32(s, 7 + trueColorOffset)
if tindex < 256: if tindex < 256:
self.info["transparency"] = tindex self.info["transparency"] = tindex

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -58,12 +58,12 @@ class MspImageFile(ImageFile.ImageFile):
# Header checksum # Header checksum
checksum = 0 checksum = 0
for i in range(0, 32, 2): for i in range(0, 32, 2):
checksum = checksum ^ i16(s[i : i + 2]) checksum = checksum ^ i16(s, i)
if checksum != 0: if checksum != 0:
raise SyntaxError("bad MSP checksum") raise SyntaxError("bad MSP checksum")
self.mode = "1" self.mode = "1"
self._size = i16(s[4:]), i16(s[6:]) self._size = i16(s, 4), i16(s, 6)
if s[:4] == b"DanM": if s[:4] == b"DanM":
self.tile = [("raw", (0, 0) + self.size, 32, ("1", 0, 1))] 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 # read rest of header
s = s + self.fp.read(508) 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 # get channel/depth descriptions
mode = i16(s[424:426]), i16(s[426:428]) mode = i16(s, 424), i16(s, 426)
if mode == (14, 2): if mode == (14, 2):
self.mode = "RGB" self.mode = "RGB"

View File

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

View File

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

View File

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

View File

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

View File

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