From c45a2dac8f0971552380090429f22b93fad7b884 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 13 Dec 2016 13:03:22 +0530 Subject: [PATCH] Preserve order of tests --- src/calibre/utils/imghdr.py | 55 ++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/src/calibre/utils/imghdr.py b/src/calibre/utils/imghdr.py index eb35b7177b..c03cd01e8b 100644 --- a/src/calibre/utils/imghdr.py +++ b/src/calibre/utils/imghdr.py @@ -91,7 +91,16 @@ def identify(src): # ---------------------------------# -def test_for_jpeg(h): +tests = [] + + +def test(f): + tests.append(f) + return f + + +@test +def 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)""" @@ -145,18 +154,21 @@ def jpeg_dimensions(stream): return -1, -1 -def test_for_png(h): +@test +def png(h): if h[:8] == b"\211PNG\r\n\032\n": return 'png' -def test_for_gif(h): +@test +def gif(h): """GIF ('87 and '89 variants)""" if h[:6] in (b'GIF87a', b'GIF89a'): return 'gif' -def test_for_tiff(h): +@test +def 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': @@ -164,69 +176,80 @@ def test_for_tiff(h): return 'tiff' -def test_for_webp(h): +@test +def webp(h): if h[:4] == b'RIFF' and h[8:12] == b'WEBP': return 'webp' -def test_for_rgb(h): +@test +def rgb(h): """SGI image library""" if h[:2] == b'\001\332': return 'rgb' -def test_for_pbm(h): +@test +def 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' -def test_for_pgm(h): +@test +def 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' -def test_for_ppm(h): +@test +def 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' -def test_for_rast(h): +@test +def rast(h): """Sun raster file""" if h[:4] == b'\x59\xA6\x6A\x95': return 'rast' -def test_for_xbm(h): +@test +def xbm(h): """X bitmap (X10 or X11)""" s = b'#define ' if h[:len(s)] == s: return 'xbm' -def test_for_bmp(h): +@test +def bmp(h): if h[:2] == b'BM': return 'bmp' -def test_for_emf(h): +@test +def emf(h): if h[:4] == b'\x01\0\0\0' and h[40:44] == b' EMF': return 'emf' -def test_for_jpeg2000(h): +@test +def jpeg2000(h): if h[:12] == b'\x00\x00\x00\x0cjP \r\n\x87\n': return 'jpeg2000' -def test_for_svg(h): +@test +def svg(h): if h[:4] == b'