Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better _binary module use #5156

Merged
merged 2 commits into from Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/PIL/BmpImagePlugin.py
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 All @@ -52,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]


# =============================================================================
Expand Down Expand Up @@ -87,34 +86,34 @@ def _bitmap(self, header=0, offset=0):
# -------------------------------------------------- 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

# --------------------------------------------- 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["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"]
Expand All @@ -124,7 +123,7 @@ def _bitmap(self, header=0, offset=0):
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:
Expand Down Expand Up @@ -267,7 +266,7 @@ def _open(self):
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)

Expand Down
7 changes: 3 additions & 4 deletions src/PIL/CurImagePlugin.py
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 @@ -48,17 +47,17 @@ def _open(self):

# 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
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")

# 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
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/EpsImagePlugin.py
Expand Up @@ -312,14 +312,14 @@ def _find_offset(self, fp):
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")

Expand Down
31 changes: 15 additions & 16 deletions src/PIL/FliImagePlugin.py
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 All @@ -27,7 +26,7 @@


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]


##
Expand All @@ -47,22 +46,22 @@ def _open(self):
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
Expand All @@ -74,17 +73,17 @@ def _open(self):

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]
Expand All @@ -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
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
2 changes: 1 addition & 1 deletion src/PIL/GbrImagePlugin.py
Expand Up @@ -29,7 +29,7 @@


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)


##
Expand Down
9 changes: 4 additions & 5 deletions src/PIL/GdImageFile.py
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 @@ -49,17 +48,17 @@ def _open(self):
# 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 = i8(s[6])
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

Expand Down