Skip to content

Commit

Permalink
Merge 8b806e4 into 9dddca0
Browse files Browse the repository at this point in the history
  • Loading branch information
jleclanche committed Jan 6, 2017
2 parents 9dddca0 + 8b806e4 commit 370c1f5
Show file tree
Hide file tree
Showing 94 changed files with 412 additions and 570 deletions.
24 changes: 3 additions & 21 deletions PIL/BdfFontFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
# See the README file for information on usage and redistribution.
#

from __future__ import print_function

from PIL import Image
from PIL import FontFile
from PIL import Image, FontFile
from .exceptions import InvalidFileType


# --------------------------------------------------------------------
Expand Down Expand Up @@ -89,14 +87,12 @@ def bdf_char(f):
# Font file plugin for the X11 BDF format.

class BdfFontFile(FontFile.FontFile):

def __init__(self, fp):

FontFile.FontFile.__init__(self)

s = fp.readline()
if s[:13] != b"STARTFONT 2.1":
raise SyntaxError("not a valid BDF file")
raise InvalidFileType("not a valid BDF file")

props = {}
comments = []
Expand All @@ -111,20 +107,6 @@ def __init__(self, fp):
if s.find(b"LogicalFontDescription") < 0:
comments.append(s[i+1:-1].decode('ascii'))

# font = props["FONT"].split("-")

# font[4] = bdf_slant[font[4].upper()]
# font[11] = bdf_spacing[font[11].upper()]

# ascent = int(props["FONT_ASCENT"])
# descent = int(props["FONT_DESCENT"])

# fontname = ";".join(font[1:])

# print("#", fontname)
# for i in comments:
# print("#", i)

while True:
c = bdf_char(fp)
if not c:
Expand Down
7 changes: 4 additions & 3 deletions PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
# See the README file for information on usage and redistribution.
#


from PIL import Image, ImageFile, ImagePalette, _binary
import math
from PIL import Image, ImageFile, ImagePalette, _binary
from .exceptions import InvalidFileType, PILReadError


__version__ = "0.7"

Expand Down Expand Up @@ -199,7 +200,7 @@ def _open(self):
head_data = self.fp.read(14)
# choke if the file does not have the required magic bytes
if head_data[0:2] != b"BM":
raise SyntaxError("Not a BMP file")
raise InvalidFileType("Not a BMP file")
# read the start position of the BMP image data (u32)
offset = i32(head_data[10:14])
# load bitmap information (offset=raster info)
Expand Down
4 changes: 2 additions & 2 deletions PIL/BufrStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#

from PIL import Image, ImageFile
from .exceptions import InvalidFileType

_handler = None

Expand Down Expand Up @@ -37,11 +38,10 @@ class BufrStubImageFile(ImageFile.StubImageFile):
format_description = "BUFR"

def _open(self):

offset = self.fp.tell()

if not _accept(self.fp.read(8)):
raise SyntaxError("Not a BUFR file")
raise InvalidFileType("Not a BUFR file")

self.fp.seek(offset)

Expand Down
14 changes: 3 additions & 11 deletions PIL/CurImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# See the README file for information on usage and redistribution.
#

from __future__ import print_function

from PIL import Image, BmpImagePlugin, _binary
from .exceptions import InvalidFileType


__version__ = "0.1"

Expand Down Expand Up @@ -49,7 +49,7 @@ def _open(self):
# check magic
s = self.fp.read(6)
if not _accept(s):
raise SyntaxError("not a CUR file")
raise InvalidFileType("not a CUR file")

# pick the largest cursor in the file
m = b""
Expand All @@ -59,14 +59,6 @@ def _open(self):
m = s
elif i8(s[0]) > i8(m[0]) and i8(s[1]) > i8(m[1]):
m = s
# print("width", i8(s[0]))
# print("height", i8(s[1]))
# print("colors", i8(s[2]))
# print("reserved", i8(s[3]))
# print("hotspot x", i16(s[4:]))
# print("hotspot y", i16(s[6:]))
# print("bytes", i32(s[8:]))
# print("offset", i32(s[12:]))
if not m:
raise TypeError("No cursors were found")

Expand Down
5 changes: 3 additions & 2 deletions PIL/DcxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

from PIL import Image, _binary
from PIL.PcxImagePlugin import PcxImageFile
from .exceptions import InvalidFileType


__version__ = "0.2"

Expand All @@ -44,11 +46,10 @@ class DcxImageFile(PcxImageFile):
format_description = "Intel DCX"

def _open(self):

# Header
s = self.fp.read(4)
if i32(s) != MAGIC:
raise SyntaxError("not a DCX file")
raise InvalidFileType("Invalid DCS header")

# Component directory
self._offset = []
Expand Down
17 changes: 9 additions & 8 deletions PIL/EpsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
# See the README file for information on usage and redistribution.
#

import re
import io
import re
import sys
from PIL import Image, ImageFile, _binary
from .exceptions import InvalidFileType, PILReadError


__version__ = "0.5"

Expand Down Expand Up @@ -235,12 +237,12 @@ def _open(self):

while s:
if len(s) > 255:
raise SyntaxError("not an EPS file")
raise PILReadError("Size too large: %r" % (len(s)))

try:
m = split.match(s)
except re.error as v:
raise SyntaxError("not an EPS file")
raise InvalidFileType("not an EPS file")

if m:
k, v = m.group(1, 2)
Expand Down Expand Up @@ -273,7 +275,7 @@ def _open(self):
# tools mistakenly put in the Comments section
pass
else:
raise IOError("bad EPS header")
raise PILReadError("bad EPS header")

s = fp.readline().strip('\r\n')

Expand All @@ -284,9 +286,8 @@ def _open(self):
# Scan for an "ImageData" descriptor

while s[:1] == "%":

if len(s) > 255:
raise SyntaxError("not an EPS file")
raise PILReadError("Size too large: %r" % (len(s)))

if s[:11] == "%ImageData:":
# Encoded bitmapped image.
Expand All @@ -307,7 +308,7 @@ def _open(self):
break

if not box:
raise IOError("cannot determine EPS bounding box")
raise PILReadError("cannot determine EPS bounding box")

def _find_offset(self, fp):

Expand All @@ -327,7 +328,7 @@ def _find_offset(self, fp):
offset = i32(s[4:8])
length = i32(s[8:12])
else:
raise SyntaxError("not an EPS file")
raise InvalidFileType("not an EPS file")

return (length, offset)

Expand Down
4 changes: 3 additions & 1 deletion PIL/FitsStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#

from PIL import Image, ImageFile
from .exceptions import InvalidFileType


_handler = None

Expand Down Expand Up @@ -41,7 +43,7 @@ def _open(self):
offset = self.fp.tell()

if not _accept(self.fp.read(6)):
raise SyntaxError("Not a FITS file")
raise InvalidFileType("Not a FITS file")

# FIXME: add more sanity checks here; mandatory header items
# include SIMPLE, BITPIX, NAXIS, etc.
Expand Down
4 changes: 3 additions & 1 deletion PIL/FliImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@


from PIL import Image, ImageFile, ImagePalette, _binary
from .exceptions import InvalidFileType


__version__ = "0.2"

Expand Down Expand Up @@ -50,7 +52,7 @@ def _open(self):
if not (magic in [0xAF11, 0xAF12] and
i16(s[14:16]) in [0, 3] and # flags
s[20:22] == b"\x00\x00"): # reserved
raise SyntaxError("not an FLI/FLC file")
raise InvalidFileType("not an FLI/FLC file")

# image characteristics
self.mode = "P"
Expand Down
3 changes: 0 additions & 3 deletions PIL/FontFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# See the README file for information on usage and redistribution.
#

from __future__ import print_function

import os
from PIL import Image, _binary

Expand Down Expand Up @@ -90,7 +88,6 @@ def compile(self):
x = xx
s = src[0] + x0, src[1] + y0, src[2] + x0, src[3] + y0
self.bitmap.paste(im.crop(src), s)
# print(chr(i), dst, s)
self.metrics[i] = d, dst, s

def save(self, filename):
Expand Down
17 changes: 6 additions & 11 deletions PIL/FpxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
# See the README file for information on usage and redistribution.
#

from __future__ import print_function

import olefile
from PIL import Image, ImageFile, _binary
from .exceptions import InvalidFileType, PILReadError

import olefile

__version__ = "0.1"

Expand Down Expand Up @@ -65,10 +64,10 @@ def _open(self):
try:
self.ole = olefile.OleFileIO(self.fp)
except IOError:
raise SyntaxError("not an FPX file; invalid OLE file")
raise InvalidFileType("not an FPX file; invalid OLE file")

if self.ole.root.clsid != "56616700-C154-11CE-8553-00AA00A1F95B":
raise SyntaxError("not an FPX file; bad root CLSID")
raise InvalidFileType("not an FPX file; bad root CLSID")

self._open_index(1)

Expand Down Expand Up @@ -116,8 +115,6 @@ def _open_index(self, index=1):
if id in prop:
self.jpeg[i] = prop[id]

# print(len(self.jpeg), "tables loaded")

self._open_subimage(1, self.maxid)

def _open_subimage(self, index=1, subimage=0):
Expand Down Expand Up @@ -145,10 +142,8 @@ def _open_subimage(self, index=1, subimage=0):
offset = i32(s, 28)
length = i32(s, 32)

# print(size, self.mode, self.rawmode)

if size != self.size:
raise IOError("subimage mismatch")
raise PILReadError("subimage mismatch")

# get tile descriptors
fp.seek(28 + offset)
Expand Down Expand Up @@ -203,7 +198,7 @@ def _open_subimage(self, index=1, subimage=0):
self.tile_prefix = self.jpeg[jpeg_tables]

else:
raise IOError("unknown/invalid compression")
raise PILReadError("unknown/invalid compression")

x = x + xtile
if x >= xsize:
Expand Down
20 changes: 13 additions & 7 deletions PIL/GbrImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
# the color depth field. This is currently unsupported by Pillow.

from PIL import Image, ImageFile, _binary
from .exceptions import InvalidFileType


i32 = _binary.i32be

Expand All @@ -42,28 +44,32 @@ class GbrImageFile(ImageFile.ImageFile):
format_description = "GIMP brush file"

def _open(self):
header_size = i32(self.fp.read(4))
version = i32(self.fp.read(4))
s = self.fp.read(4)
t = self.fp.read(4)
if len(s) < 4 or len(t) < 4:
raise InvalidFileType("not a GIMP brush")
header_size = i32(s)
version = i32(t)
if header_size < 20:
raise SyntaxError("not a GIMP brush")
raise InvalidFileType("not a GIMP brush")
if version not in (1, 2):
raise SyntaxError("Unsupported GIMP brush version: %s" % version)
raise NotImplementedError("Unsupported GIMP brush version: %s" % version)

width = i32(self.fp.read(4))
height = i32(self.fp.read(4))
color_depth = i32(self.fp.read(4))
if width <= 0 or height <= 0:
raise SyntaxError("not a GIMP brush")
raise InvalidFileType("not a GIMP brush")
if color_depth not in (1, 4):
raise SyntaxError("Unsupported GIMP brush color depth: %s" % color_depth)
raise NotImplementedError("Unsupported GIMP brush color depth: %s" % color_depth)

if version == 1:
comment_length = header_size-20
else:
comment_length = header_size-28
magic_number = self.fp.read(4)
if magic_number != b'GIMP':
raise SyntaxError("not a GIMP brush, bad magic number")
raise InvalidFileType("not a GIMP brush, bad magic number")
self.info['spacing'] = i32(self.fp.read(4))

comment = self.fp.read(comment_length)[:-1]
Expand Down
Loading

0 comments on commit 370c1f5

Please sign in to comment.