Skip to content

Commit

Permalink
remove extra i8 calls where input is proved bytes[] or int
Browse files Browse the repository at this point in the history
  • Loading branch information
homm authored and radarhere committed Dec 30, 2020
1 parent 85d61ca commit 3757b8c
Show file tree
Hide file tree
Showing 22 changed files with 79 additions and 96 deletions.
3 changes: 1 addition & 2 deletions src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


from . import Image, ImageFile, ImagePalette
from ._binary import i8
from ._binary import i16le as i16
from ._binary import i32le as i32
from ._binary import o8
Expand Down Expand Up @@ -97,7 +96,7 @@ def _bitmap(self, header=0, offset=0):
# --------------------------------------------- Windows Bitmap v2 to v5
# v3, OS/2 v2, v4, v5
elif file_info["header_size"] in (40, 64, 108, 124):
file_info["y_flip"] = i8(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["width"] = i32(header_data[0:4])
file_info["height"] = (
Expand Down
3 changes: 1 addition & 2 deletions src/PIL/CurImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# See the README file for information on usage and redistribution.
#
from . import BmpImagePlugin, Image
from ._binary import i8
from ._binary import i16le as i16
from ._binary import i32le as i32

Expand Down Expand Up @@ -52,7 +51,7 @@ def _open(self):
s = self.fp.read(16)
if not m:
m = s
elif i8(s[0]) > i8(m[0]) and i8(s[1]) > i8(m[1]):
elif s[0] > m[0] and s[1] > m[1]:
m = s
if not m:
raise TypeError("No cursors were found")
Expand Down
11 changes: 5 additions & 6 deletions src/PIL/FliImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


from . import Image, ImageFile, ImagePalette
from ._binary import i8
from ._binary import i16le as i16
from ._binary import i32le as i32
from ._binary import o8
Expand Down Expand Up @@ -102,15 +101,15 @@ def _palette(self, palette, shift):
i = 0
for e in range(i16(self.fp.read(2))):
s = self.fp.read(2)
i = i + i8(s[0])
n = i8(s[1])
i = i + s[0]
n = s[1]
if n == 0:
n = 256
s = self.fp.read(n * 3)
for n in range(0, len(s), 3):
r = i8(s[n]) << shift
g = i8(s[n + 1]) << shift
b = i8(s[n + 2]) << shift
r = s[n] << shift
g = s[n + 1] << shift
b = s[n + 2] << shift
palette[i] = (r, g, b)
i += 1

Expand Down
5 changes: 2 additions & 3 deletions src/PIL/FpxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import olefile

from . import Image, ImageFile
from ._binary import i8
from ._binary import i32le as i32

# we map from colour field tuples to (mode, rawmode) descriptors
Expand Down Expand Up @@ -181,8 +180,8 @@ def _open_subimage(self, index=1, subimage=0):

elif compression == 2:

internal_color_conversion = i8(s[14])
jpeg_tables = i8(s[15])
internal_color_conversion = s[14]
jpeg_tables = s[15]
rawmode = self.rawmode

if internal_color_conversion:
Expand Down
3 changes: 1 addition & 2 deletions src/PIL/GdImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class is not registered for use with :py:func:`PIL.Image.open()`. To open a


from . import ImageFile, ImagePalette, UnidentifiedImageError
from ._binary import i8
from ._binary import i16be as i16
from ._binary import i32be as i32

Expand All @@ -55,7 +54,7 @@ def _open(self):
self.mode = "L" # FIXME: "P"
self._size = i16(s[2:4]), i16(s[4:6])

trueColor = i8(s[6])
trueColor = s[6]
trueColorOffset = 2 if trueColor else 0

# transparency index
Expand Down
29 changes: 14 additions & 15 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import subprocess

from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence
from ._binary import i8
from ._binary import i16le as i16
from ._binary import o8
from ._binary import o16le as o16
Expand Down Expand Up @@ -58,8 +57,8 @@ class GifImageFile(ImageFile.ImageFile):

def data(self):
s = self.fp.read(1)
if s and i8(s):
return self.fp.read(i8(s))
if s and s[0]:
return self.fp.read(s[0])
return None

def _open(self):
Expand All @@ -72,16 +71,16 @@ def _open(self):
self.info["version"] = s[:6]
self._size = i16(s[6:]), i16(s[8:])
self.tile = []
flags = i8(s[10])
flags = s[10]
bits = (flags & 7) + 1

if flags & 128:
# get global palette
self.info["background"] = i8(s[11])
self.info["background"] = s[11]
# check if palette contains colour indices
p = self.fp.read(3 << bits)
for i in range(0, len(p), 3):
if not (i // 3 == i8(p[i]) == i8(p[i + 1]) == i8(p[i + 2])):
if not (i // 3 == p[i] == p[i + 1] == p[i + 2]):
p = ImagePalette.raw("RGB", p)
self.global_palette = self.palette = p
break
Expand Down Expand Up @@ -187,13 +186,13 @@ def _seek(self, frame):
#
s = self.fp.read(1)
block = self.data()
if i8(s) == 249:
if s[0] == 249:
#
# graphic control extension
#
flags = i8(block[0])
flags = block[0]
if flags & 1:
info["transparency"] = i8(block[3])
info["transparency"] = block[3]
info["duration"] = i16(block[1:3]) * 10

# disposal method - find the value of bits 4 - 6
Expand All @@ -205,7 +204,7 @@ def _seek(self, frame):
# correct, but it seems to prevent the last
# frame from looking odd for some animations
self.disposal_method = dispose_bits
elif i8(s) == 254:
elif s[0] == 254:
#
# comment extension
#
Expand All @@ -216,14 +215,14 @@ def _seek(self, frame):
info["comment"] = block
block = self.data()
continue
elif i8(s) == 255:
elif s[0] == 255:
#
# application extension
#
info["extension"] = block, self.fp.tell()
if block[:11] == b"NETSCAPE2.0":
block = self.data()
if len(block) >= 3 and i8(block[0]) == 1:
if len(block) >= 3 and block[0] == 1:
info["loop"] = i16(block[1:3])
while self.data():
pass
Expand All @@ -240,7 +239,7 @@ def _seek(self, frame):
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
flags = i8(s[8])
flags = s[8]

interlace = (flags & 64) != 0

Expand All @@ -249,7 +248,7 @@ def _seek(self, frame):
self.palette = ImagePalette.raw("RGB", self.fp.read(3 << bits))

# image data
bits = i8(self.fp.read(1))
bits = self.fp.read(1)[0]
self.__offset = self.fp.tell()
self.tile = [
("gif", (x0, y0, x1, y1), self.__offset, (bits, interlace))
Expand All @@ -258,7 +257,7 @@ def _seek(self, frame):

else:
pass
# raise OSError, "illegal GIF tag `%x`" % i8(s)
# raise OSError, "illegal GIF tag `%x`" % s[0]

try:
if self.disposal_method < 2:
Expand Down
3 changes: 1 addition & 2 deletions src/PIL/GribStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#

from . import Image, ImageFile
from ._binary import i8

_handler = None

Expand All @@ -30,7 +29,7 @@ def register_handler(handler):


def _accept(prefix):
return prefix[0:4] == b"GRIB" and i8(prefix[7]) == 1
return prefix[0:4] == b"GRIB" and prefix[7] == 1


class GribStubImageFile(ImageFile.StubImageFile):
Expand Down
3 changes: 1 addition & 2 deletions src/PIL/IcnsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import tempfile

from PIL import Image, ImageFile, PngImagePlugin, features
from PIL._binary import i8

enable_jpeg2k = features.check_codec("jpg_2000")
if enable_jpeg2k:
Expand Down Expand Up @@ -70,7 +69,7 @@ def read_32(fobj, start_length, size):
byte = fobj.read(1)
if not byte:
break
byte = i8(byte)
byte = byte[0]
if byte & 0x80:
blocksize = byte - 125
byte = fobj.read(1)
Expand Down
9 changes: 4 additions & 5 deletions src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from math import ceil, log

from . import BmpImagePlugin, Image, ImageFile, PngImagePlugin
from ._binary import i8
from ._binary import i16le as i16
from ._binary import i32le as i32

Expand Down Expand Up @@ -110,10 +109,10 @@ def __init__(self, buf):
s = buf.read(16)

icon_header = {
"width": i8(s[0]),
"height": i8(s[1]),
"nb_color": i8(s[2]), # No. of colors in image (0 if >=8bpp)
"reserved": i8(s[3]),
"width": s[0],
"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:]),
Expand Down
7 changes: 3 additions & 4 deletions src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import re

from . import Image, ImageFile, ImagePalette
from ._binary import i8

# --------------------------------------------------------------------
# Standard tags
Expand Down Expand Up @@ -223,14 +222,14 @@ def _open(self):
linear = 1 # linear greyscale palette
for i in range(256):
if palette[i] == palette[i + 256] == palette[i + 512]:
if i8(palette[i]) != i:
if palette[i] != i:
linear = 0
else:
greyscale = 0
if self.mode in ["L", "LA", "P", "PA"]:
if greyscale:
if not linear:
self.lut = [i8(c) for c in palette[:256]]
self.lut = list(palette[:256])
else:
if self.mode in ["L", "P"]:
self.mode = self.rawmode = "P"
Expand All @@ -240,7 +239,7 @@ def _open(self):
self.palette = ImagePalette.raw("RGB;L", palette)
elif self.mode == "RGB":
if not greyscale or not linear:
self.lut = [i8(c) for c in palette]
self.lut = list(palette)

self.frame = 0

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
_plugins,
_raise_version_warning,
)
from ._binary import i8, i32le
from ._binary import i32le
from ._util import deferred_error, isPath

if sys.version_info >= (3, 7):
Expand Down Expand Up @@ -1378,7 +1378,7 @@ def getprojection(self):

self.load()
x, y = self.im.getprojection()
return [i8(c) for c in x], [i8(c) for c in y]
return list(x), list(y)

def histogram(self, mask=None, extrema=None):
"""
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/IptcImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ def field(self):
if not len(s):
return None, 0

tag = i8(s[1]), i8(s[2])
tag = s[1], s[2]

# syntax
if i8(s[0]) != 0x1C or tag[0] < 1 or tag[0] > 9:
if s[0] != 0x1C or tag[0] < 1 or tag[0] > 9:
raise SyntaxError("invalid IPTC/NAA file")

# field size
size = i8(s[3])
size = s[3]
if size > 132:
raise OSError("illegal field length in IPTC/NAA file")
elif size == 128:
Expand Down

0 comments on commit 3757b8c

Please sign in to comment.