This commit is contained in:
Kovid Goyal 2016-12-13 12:54:05 +05:30
parent bf39b865a3
commit f1248b88d0

View File

@ -90,10 +90,8 @@ def identify(src):
# Subroutines per image file type #
# ---------------------------------#
tests = []
def test_jpeg(h):
def test_for_jpeg(h):
"""JPEG data in JFIF format (Changed by Kovid to mimic the file utility,
the original code was failing with some jpegs that included ICC_PROFILE
data, for example: http://nationalpostnews.files.wordpress.com/2013/03/budget.jpeg?w=300&h=1571)"""
@ -146,116 +144,89 @@ def jpeg_dimensions(stream):
return -1, -1
tests.append(test_jpeg)
def test_png(h):
def test_for_png(h):
if h[:8] == b"\211PNG\r\n\032\n":
return 'png'
tests.append(test_png)
def test_gif(h):
def test_for_gif(h):
"""GIF ('87 and '89 variants)"""
if h[:6] in (b'GIF87a', b'GIF89a'):
return 'gif'
tests.append(test_gif)
def test_tiff(h):
def test_for_tiff(h):
"""TIFF (can be in Motorola or Intel byte order)"""
if h[:2] in (b'MM', b'II'):
if h[2:4] == b'\xbc\x01':
return 'jxr'
return 'tiff'
tests.append(test_tiff)
def test_webp(h):
def test_for_webp(h):
if h[:4] == b'RIFF' and h[8:12] == b'WEBP':
return 'webp'
tests.append(test_webp)
def test_rgb(h):
def test_for_rgb(h):
"""SGI image library"""
if h[:2] == b'\001\332':
return 'rgb'
tests.append(test_rgb)
def test_pbm(h):
def test_for_pbm(h):
"""PBM (portable bitmap)"""
if len(h) >= 3 and \
h[0] == b'P' and h[1] in b'14' and h[2] in b' \t\n\r':
return 'pbm'
tests.append(test_pbm)
def test_pgm(h):
def test_for_pgm(h):
"""PGM (portable graymap)"""
if len(h) >= 3 and \
h[0] == b'P' and h[1] in b'25' and h[2] in b' \t\n\r':
return 'pgm'
tests.append(test_pgm)
def test_ppm(h):
def test_for_ppm(h):
"""PPM (portable pixmap)"""
if len(h) >= 3 and \
h[0] == b'P' and h[1] in b'36' and h[2] in b' \t\n\r':
return 'ppm'
tests.append(test_ppm)
def test_rast(h):
def test_for_rast(h):
"""Sun raster file"""
if h[:4] == b'\x59\xA6\x6A\x95':
return 'rast'
tests.append(test_rast)
def test_xbm(h):
def test_for_xbm(h):
"""X bitmap (X10 or X11)"""
s = b'#define '
if h[:len(s)] == s:
return 'xbm'
tests.append(test_xbm)
def test_bmp(h):
def test_for_bmp(h):
if h[:2] == b'BM':
return 'bmp'
tests.append(test_bmp)
def test_emf(h):
def test_for_emf(h):
if h[:4] == b'\x01\0\0\0' and h[40:44] == b' EMF':
return 'emf'
tests.append(test_emf)
def test_jpeg2000(h):
def test_for_jpeg2000(h):
if h[:12] == b'\x00\x00\x00\x0cjP \r\n\x87\n':
return 'jpeg2000'
tests.append(test_jpeg2000)
def test_svg(h):
def test_for_svg(h):
if h[:4] == b'<svg' or (h[:2] == b'<?' and h[2:5].tobytes().lower() == b'xml' and b'<svg' in h.tobytes()):
return 'svg'
tests.append(test_svg)
tests = tuple(v for k, v in globals().iteritems() if callable(v) and k.startswith('test_for_'))