From 161833ee5c062dab7c21f5260cea04be1459c5a6 Mon Sep 17 00:00:00 2001 From: John Evans Date: Tue, 18 Mar 2014 12:08:16 -0400 Subject: [PATCH] Checking for bad wavelet transform while parsing. #195 Being more intelligent about printing in such cases. --- glymur/codestream.py | 11 ++++++++--- glymur/test/test_codestream.py | 11 +++++++++++ glymur/test/test_printing.py | 9 +++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/glymur/codestream.py b/glymur/codestream.py index 35e3fee..756277d 100644 --- a/glymur/codestream.py +++ b/glymur/codestream.py @@ -52,9 +52,9 @@ _PROGRESSION_ORDER_DISPLAY = _keydefaultdict(_factory, PCRL: 'PCRL', CPRL: 'CPRL'}) -_WAVELET_TRANSFORM_DISPLAY = { - WAVELET_XFORM_9X7_IRREVERSIBLE: '9-7 irreversible', - WAVELET_XFORM_5X3_REVERSIBLE: '5-3 reversible'} +_WAVELET_TRANSFORM_DISPLAY = _keydefaultdict(_factory, + { WAVELET_XFORM_9X7_IRREVERSIBLE: '9-7 irreversible', + WAVELET_XFORM_5X3_REVERSIBLE: '5-3 reversible'}) # Need a catch-all list of valid markers. # See table A-1 in ISO/IEC FCD15444-1. @@ -390,6 +390,11 @@ class Codestream(object): msg = "Invalid progression order in COD segment: {0}." warnings.warn(msg.format(spcod[0])) + if spcod[8] not in [WAVELET_XFORM_9X7_IRREVERSIBLE, + WAVELET_XFORM_5X3_REVERSIBLE]: + msg = "Invalid wavelet transform in COD segment: {0}." + warnings.warn(msg.format(spcod[8])) + sop = (scod & 2) > 0 eph = (scod & 4) > 0 diff --git a/glymur/test/test_codestream.py b/glymur/test/test_codestream.py index 28ea0f8..9aba31c 100644 --- a/glymur/test/test_codestream.py +++ b/glymur/test/test_codestream.py @@ -51,6 +51,17 @@ class TestCodestreamOpjData(unittest.TestCase): def tearDown(self): pass + def test_bad_wavelet_transform(self): + """Should warn if wavelet transform is bad. Issue129""" + filename = opj_data_file('input/nonregression/edf_c2_10025.jp2') + if sys.hexversion < 0x03000000: + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + j = Jp2k(filename) + else: + with self.assertWarns(UserWarning): + j = Jp2k(filename) + def test_invalid_progression_order(self): """Should still be able to parse even if prog order is invalid.""" jfile = opj_data_file('input/nonregression/2977.pdf.asan.67.2198.jp2') diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index 1e38c55..df73f3f 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -653,6 +653,15 @@ class TestPrintingOpjDataRoot(unittest.TestCase): def tearDown(self): pass + def test_bad_wavelet_transform(self): + """Should still be able to print if wavelet xform is bad, issue195""" + filename = opj_data_file('input/nonregression/edf_c2_10025.jp2') + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + j = Jp2k(filename) + with patch('sys.stdout', new=StringIO()) as fake_out: + print(j) + def test_invalid_progression_order(self): """Should still be able to print even if prog order is invalid.""" jfile = opj_data_file('input/nonregression/2977.pdf.asan.67.2198.jp2')