mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-29 17:33:08 +03:00
Remove unused code, tighten up readline for all the line endings
This commit is contained in:
parent
8f75cc2bbf
commit
ee46f45b96
|
@ -143,58 +143,28 @@ def Ghostscript(tile, size, fp, scale=1):
|
||||||
|
|
||||||
|
|
||||||
class PSFile:
|
class PSFile:
|
||||||
"""Wrapper that treats either CR or LF as end of line."""
|
"""Wrapper for bytesio object that treats either CR or LF as end of line."""
|
||||||
def __init__(self, fp):
|
def __init__(self, fp):
|
||||||
self.fp = fp
|
self.fp = fp
|
||||||
self.char = None
|
self.char = None
|
||||||
def __getattr__(self, id):
|
|
||||||
v = getattr(self.fp, id)
|
|
||||||
setattr(self, id, v)
|
|
||||||
return v
|
|
||||||
def seek(self, offset, whence=0):
|
def seek(self, offset, whence=0):
|
||||||
self.char = None
|
self.char = None
|
||||||
self.fp.seek(offset, whence)
|
self.fp.seek(offset, whence)
|
||||||
def read(self, count):
|
|
||||||
return self.fp.read(count).decode('latin-1')
|
|
||||||
def readbinary(self, count):
|
|
||||||
return self.fp.read(count)
|
|
||||||
def tell(self):
|
|
||||||
pos = self.fp.tell()
|
|
||||||
if self.char:
|
|
||||||
pos -= 1
|
|
||||||
return pos
|
|
||||||
def readline(self):
|
def readline(self):
|
||||||
s = b""
|
s = self.char or b""
|
||||||
if self.char:
|
|
||||||
c = self.char
|
|
||||||
self.char = None
|
self.char = None
|
||||||
else:
|
|
||||||
c = self.fp.read(1)
|
c = self.fp.read(1)
|
||||||
while c not in b"\r\n":
|
while c not in b"\r\n":
|
||||||
s = s + c
|
s = s + c
|
||||||
c = self.fp.read(1)
|
c = self.fp.read(1)
|
||||||
if c == b"\r":
|
|
||||||
self.char = self.fp.read(1)
|
self.char = self.fp.read(1)
|
||||||
if self.char == b"\n":
|
# line endings can be 1 or 2 of \r \n, in either order
|
||||||
|
if self.char in b"\r\n":
|
||||||
self.char = None
|
self.char = None
|
||||||
return s.decode('latin-1') + "\n"
|
|
||||||
|
|
||||||
class PSFpWrapper:
|
|
||||||
""" Wrapper for a filepointer that has been opened in universal mode """
|
|
||||||
def __init__(self,fp):
|
|
||||||
self.fp = fp
|
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
|
||||||
""" Delegate everything that we're not wrapping """
|
|
||||||
return getattr(self.fp, attr)
|
|
||||||
|
|
||||||
def read(self, count):
|
|
||||||
return self.fp.read(count).decode('latin-1')
|
|
||||||
|
|
||||||
def readbinary(self, count):
|
|
||||||
return self.fp.read(count)
|
|
||||||
|
|
||||||
|
|
||||||
|
return s.decode('latin-1')
|
||||||
|
|
||||||
def _accept(prefix):
|
def _accept(prefix):
|
||||||
return prefix[:4] == b"%!PS" or i32(prefix) == 0xC6D3D0C5
|
return prefix[:4] == b"%!PS" or i32(prefix) == 0xC6D3D0C5
|
||||||
|
@ -209,6 +179,8 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
format = "EPS"
|
format = "EPS"
|
||||||
format_description = "Encapsulated Postscript"
|
format_description = "Encapsulated Postscript"
|
||||||
|
|
||||||
|
mode_map = { 1:"L", 2:"LAB", 3:"RGB" }
|
||||||
|
|
||||||
def _open(self):
|
def _open(self):
|
||||||
(length, offset) = self._find_offset(self.fp)
|
(length, offset) = self._find_offset(self.fp)
|
||||||
|
|
||||||
|
@ -216,8 +188,8 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
# convert line endings and decode to latin-1.
|
# convert line endings and decode to latin-1.
|
||||||
try:
|
try:
|
||||||
if bytes is str:
|
if bytes is str:
|
||||||
# Python2, need the decode to latin-1 on read.
|
# Python2, no encoding conversion necessary
|
||||||
fp = PSFpWrapper(open(self.fp.name, "Ur"))
|
fp = open(self.fp.name, "Ur")
|
||||||
else:
|
else:
|
||||||
# Python3, can use bare open command.
|
# Python3, can use bare open command.
|
||||||
fp = open(self.fp.name, "Ur", encoding='latin-1')
|
fp = open(self.fp.name, "Ur", encoding='latin-1')
|
||||||
|
@ -236,18 +208,12 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
#
|
#
|
||||||
# Load EPS header
|
# Load EPS header
|
||||||
|
|
||||||
s = fp.readline()
|
s = fp.readline().strip('\r\n')
|
||||||
|
|
||||||
while s:
|
while s:
|
||||||
|
|
||||||
if len(s) > 255:
|
if len(s) > 255:
|
||||||
raise SyntaxError("not an EPS file")
|
raise SyntaxError("not an EPS file")
|
||||||
|
|
||||||
if s[-2:] == '\r\n':
|
|
||||||
s = s[:-2]
|
|
||||||
elif s[-1:] == '\n':
|
|
||||||
s = s[:-1]
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
m = split.match(s)
|
m = split.match(s)
|
||||||
except re.error as v:
|
except re.error as v:
|
||||||
|
@ -269,9 +235,7 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
m = field.match(s)
|
m = field.match(s)
|
||||||
|
|
||||||
if m:
|
if m:
|
||||||
k = m.group(1)
|
k = m.group(1)
|
||||||
|
|
||||||
|
@ -281,16 +245,16 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
self.info[k[:8]] = k[9:]
|
self.info[k[:8]] = k[9:]
|
||||||
else:
|
else:
|
||||||
self.info[k] = ""
|
self.info[k] = ""
|
||||||
elif s[0:1] == '%':
|
elif s[0] == '%':
|
||||||
# handle non-DSC Postscript comments that some
|
# handle non-DSC Postscript comments that some
|
||||||
# tools mistakenly put in the Comments section
|
# tools mistakenly put in the Comments section
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise IOError("bad EPS header")
|
raise IOError("bad EPS header")
|
||||||
|
|
||||||
s = fp.readline()
|
s = fp.readline().strip('\r\n')
|
||||||
|
|
||||||
if s[:1] != "%":
|
if s[0] != "%":
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
@ -302,44 +266,21 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
if len(s) > 255:
|
if len(s) > 255:
|
||||||
raise SyntaxError("not an EPS file")
|
raise SyntaxError("not an EPS file")
|
||||||
|
|
||||||
if s[-2:] == '\r\n':
|
|
||||||
s = s[:-2]
|
|
||||||
elif s[-1:] == '\n':
|
|
||||||
s = s[:-1]
|
|
||||||
|
|
||||||
if s[:11] == "%ImageData:":
|
if s[:11] == "%ImageData:":
|
||||||
# Encoded bitmapped image.
|
# Encoded bitmapped image.
|
||||||
[x, y, bi, mo, z3, z4, en, id] =\
|
[x, y, bi, mo, z3, z4, en, id] = s[11:].split(None, 7)
|
||||||
s[11:].split(None, 7)
|
|
||||||
|
|
||||||
x = int(x); y = int(y)
|
if int(bi) != 8:
|
||||||
|
|
||||||
bi = int(bi)
|
|
||||||
mo = int(mo)
|
|
||||||
|
|
||||||
en = int(en)
|
|
||||||
|
|
||||||
if en == 1:
|
|
||||||
decoder = "eps_binary"
|
|
||||||
elif en == 2:
|
|
||||||
decoder = "eps_hex"
|
|
||||||
else:
|
|
||||||
break
|
break
|
||||||
if bi != 8:
|
try:
|
||||||
break
|
self.mode = self.mode_map[int(mo)]
|
||||||
if mo == 1:
|
except:
|
||||||
self.mode = "L"
|
|
||||||
elif mo == 2:
|
|
||||||
self.mode = "LAB"
|
|
||||||
elif mo == 3:
|
|
||||||
self.mode = "RGB"
|
|
||||||
else:
|
|
||||||
break
|
break
|
||||||
|
|
||||||
self.size = (x,y)
|
self.size = int(x), int(y)
|
||||||
return
|
return
|
||||||
|
|
||||||
s = fp.readline()
|
s = fp.readline().strip('\r\n')
|
||||||
if not s:
|
if not s:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user