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 # # Subroutines per image file type #
# ---------------------------------# # ---------------------------------#
tests = []
def test_for_jpeg(h):
def test_jpeg(h):
"""JPEG data in JFIF format (Changed by Kovid to mimic the file utility, """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 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)""" 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 return -1, -1
tests.append(test_jpeg)
def test_for_png(h):
def test_png(h):
if h[:8] == b"\211PNG\r\n\032\n": if h[:8] == b"\211PNG\r\n\032\n":
return 'png' return 'png'
tests.append(test_png)
def test_for_gif(h):
def test_gif(h):
"""GIF ('87 and '89 variants)""" """GIF ('87 and '89 variants)"""
if h[:6] in (b'GIF87a', b'GIF89a'): if h[:6] in (b'GIF87a', b'GIF89a'):
return 'gif' return 'gif'
tests.append(test_gif)
def test_for_tiff(h):
def test_tiff(h):
"""TIFF (can be in Motorola or Intel byte order)""" """TIFF (can be in Motorola or Intel byte order)"""
if h[:2] in (b'MM', b'II'): if h[:2] in (b'MM', b'II'):
if h[2:4] == b'\xbc\x01': if h[2:4] == b'\xbc\x01':
return 'jxr' return 'jxr'
return 'tiff' return 'tiff'
tests.append(test_tiff)
def test_for_webp(h):
def test_webp(h):
if h[:4] == b'RIFF' and h[8:12] == b'WEBP': if h[:4] == b'RIFF' and h[8:12] == b'WEBP':
return 'webp' return 'webp'
tests.append(test_webp)
def test_for_rgb(h):
def test_rgb(h):
"""SGI image library""" """SGI image library"""
if h[:2] == b'\001\332': if h[:2] == b'\001\332':
return 'rgb' return 'rgb'
tests.append(test_rgb)
def test_for_pbm(h):
def test_pbm(h):
"""PBM (portable bitmap)""" """PBM (portable bitmap)"""
if len(h) >= 3 and \ if len(h) >= 3 and \
h[0] == b'P' and h[1] in b'14' and h[2] in b' \t\n\r': h[0] == b'P' and h[1] in b'14' and h[2] in b' \t\n\r':
return 'pbm' return 'pbm'
tests.append(test_pbm)
def test_for_pgm(h):
def test_pgm(h):
"""PGM (portable graymap)""" """PGM (portable graymap)"""
if len(h) >= 3 and \ if len(h) >= 3 and \
h[0] == b'P' and h[1] in b'25' and h[2] in b' \t\n\r': h[0] == b'P' and h[1] in b'25' and h[2] in b' \t\n\r':
return 'pgm' return 'pgm'
tests.append(test_pgm)
def test_for_ppm(h):
def test_ppm(h):
"""PPM (portable pixmap)""" """PPM (portable pixmap)"""
if len(h) >= 3 and \ if len(h) >= 3 and \
h[0] == b'P' and h[1] in b'36' and h[2] in b' \t\n\r': h[0] == b'P' and h[1] in b'36' and h[2] in b' \t\n\r':
return 'ppm' return 'ppm'
tests.append(test_ppm)
def test_for_rast(h):
def test_rast(h):
"""Sun raster file""" """Sun raster file"""
if h[:4] == b'\x59\xA6\x6A\x95': if h[:4] == b'\x59\xA6\x6A\x95':
return 'rast' return 'rast'
tests.append(test_rast)
def test_for_xbm(h):
def test_xbm(h):
"""X bitmap (X10 or X11)""" """X bitmap (X10 or X11)"""
s = b'#define ' s = b'#define '
if h[:len(s)] == s: if h[:len(s)] == s:
return 'xbm' return 'xbm'
tests.append(test_xbm)
def test_for_bmp(h):
def test_bmp(h):
if h[:2] == b'BM': if h[:2] == b'BM':
return 'bmp' return 'bmp'
tests.append(test_bmp)
def test_for_emf(h):
def test_emf(h):
if h[:4] == b'\x01\0\0\0' and h[40:44] == b' EMF': if h[:4] == b'\x01\0\0\0' and h[40:44] == b' EMF':
return 'emf' return 'emf'
tests.append(test_emf)
def test_for_jpeg2000(h):
def test_jpeg2000(h):
if h[:12] == b'\x00\x00\x00\x0cjP \r\n\x87\n': if h[:12] == b'\x00\x00\x00\x0cjP \r\n\x87\n':
return 'jpeg2000' return 'jpeg2000'
tests.append(test_jpeg2000)
def test_for_svg(h):
def test_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()): 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' return 'svg'
tests.append(test_svg)
tests = tuple(v for k, v in globals().iteritems() if callable(v) and k.startswith('test_for_'))