diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index 6ca95ccb9..0ca7ba87c 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -47,7 +47,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] # ============================================================================= @@ -82,10 +82,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 @@ -94,22 +94,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"] @@ -119,7 +119,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 head_data[0:2] != b"BM": 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) diff --git a/src/PIL/CurImagePlugin.py b/src/PIL/CurImagePlugin.py index d1ed61798..b194511b9 100644 --- a/src/PIL/CurImagePlugin.py +++ b/src/PIL/CurImagePlugin.py @@ -46,7 +46,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 @@ -56,7 +56,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 diff --git a/src/PIL/EpsImagePlugin.py b/src/PIL/EpsImagePlugin.py index e27a57671..19baa1ee9 100644 --- a/src/PIL/EpsImagePlugin.py +++ b/src/PIL/EpsImagePlugin.py @@ -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") diff --git a/src/PIL/FliImagePlugin.py b/src/PIL/FliImagePlugin.py index ffb255825..e2af919a6 100644 --- a/src/PIL/FliImagePlugin.py +++ b/src/PIL/FliImagePlugin.py @@ -24,7 +24,7 @@ from ._binary import i16le as i16, i32le as i32, 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] ## @@ -42,24 +42,24 @@ class FliImageFile(ImageFile.ImageFile): # HEAD s = self.fp.read(128) - magic = i16(s[4:6]) + magic = i16(s, 4) if not ( magic in [0xAF11, 0xAF12] - 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]) + duration = i32(s, 16) if magic == 0xAF11: duration = (duration * 1000) // 70 self.info["duration"] = duration @@ -71,17 +71,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] diff --git a/src/PIL/GbrImagePlugin.py b/src/PIL/GbrImagePlugin.py index 292de435c..172866639 100644 --- a/src/PIL/GbrImagePlugin.py +++ b/src/PIL/GbrImagePlugin.py @@ -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) ## diff --git a/src/PIL/GdImageFile.py b/src/PIL/GdImageFile.py index 0a05c2bcf..92af6a22a 100644 --- a/src/PIL/GdImageFile.py +++ b/src/PIL/GdImageFile.py @@ -43,17 +43,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 diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 9275d69a0..2e958483a 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -67,7 +67,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 @@ -191,7 +191,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 @@ -221,7 +221,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 @@ -232,8 +232,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 diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index efd134cbf..e0daf7c5e 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -98,7 +98,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): @@ -109,10 +109,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 diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 81ee52a46..da896f123 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -3303,7 +3303,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 = {} diff --git a/src/PIL/IptcImagePlugin.py b/src/PIL/IptcImagePlugin.py index 3f0aae557..586099ee8 100644 --- a/src/PIL/IptcImagePlugin.py +++ b/src/PIL/IptcImagePlugin.py @@ -74,7 +74,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 diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 8c6a61936..2c04c7edd 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -121,10 +121,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 @@ -191,7 +191,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: diff --git a/src/PIL/MspImagePlugin.py b/src/PIL/MspImagePlugin.py index 6f6da8e15..accea9df6 100644 --- a/src/PIL/MspImagePlugin.py +++ b/src/PIL/MspImagePlugin.py @@ -57,12 +57,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))] diff --git a/src/PIL/PixarImagePlugin.py b/src/PIL/PixarImagePlugin.py index 5ea32ba89..0ea6ca528 100644 --- a/src/PIL/PixarImagePlugin.py +++ b/src/PIL/PixarImagePlugin.py @@ -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" diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 7132aedca..3fff61a60 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -369,7 +369,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: @@ -424,7 +424,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): @@ -457,7 +457,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) @@ -579,7 +579,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 @@ -591,13 +591,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 diff --git a/src/PIL/PsdImagePlugin.py b/src/PIL/PsdImagePlugin.py index 044df443d..1eca3ddfd 100644 --- a/src/PIL/PsdImagePlugin.py +++ b/src/PIL/PsdImagePlugin.py @@ -61,12 +61,12 @@ class PsdImageFile(ImageFile.ImageFile): # header s = read(26) - if s[:4] != b"8BPS" or i16(s[4:]) != 1: + if s[:4] != b"8BPS" 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)] @@ -74,7 +74,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 @@ -289,7 +289,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) diff --git a/src/PIL/SgiImagePlugin.py b/src/PIL/SgiImagePlugin.py index 19b4e113d..d2947b130 100644 --- a/src/PIL/SgiImagePlugin.py +++ b/src/PIL/SgiImagePlugin.py @@ -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 diff --git a/src/PIL/SunImagePlugin.py b/src/PIL/SunImagePlugin.py index fd7ca8a40..4b3d3f746 100644 --- a/src/PIL/SunImagePlugin.py +++ b/src/PIL/SunImagePlugin.py @@ -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" diff --git a/src/PIL/TgaImagePlugin.py b/src/PIL/TgaImagePlugin.py index 57b47fbb8..7f4d8f3b5 100644 --- a/src/PIL/TgaImagePlugin.py +++ b/src/PIL/TgaImagePlugin.py @@ -62,7 +62,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 ( @@ -108,7 +108,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)