diff --git a/docs/source/detailed_installation.rst b/docs/source/detailed_installation.rst index 06c6b79..b737843 100644 --- a/docs/source/detailed_installation.rst +++ b/docs/source/detailed_installation.rst @@ -137,8 +137,17 @@ platforms. Testing ''''''' -If you wish to run the tests (strongly recommended :-), you can either run them -from within python as follows ... :: +There are two environment variables you may wish to set before running the +tests. + + * **OPJ_DATA_ROOT** - points to directory for OpenJPEG test data + * **FORMAT_CORPUS_ROOT** - points to directory for format-corpus repository (see https://github.com/openplanets/format-corpus) + +Setting these two environment variables is not required, as any tests using +either of them will be skipped. + +In order to run the tests, you can either run them from within +python as follows ... :: >>> import glymur >>> glymur.runtests() diff --git a/glymur/codestream.py b/glymur/codestream.py index df1575c..c4d8b4b 100644 --- a/glymur/codestream.py +++ b/glymur/codestream.py @@ -102,7 +102,14 @@ class Codestream(object): while True: read_buffer = fptr.read(2) - marker_id, = struct.unpack('>H', read_buffer) + try: + marker_id, = struct.unpack('>H', read_buffer) + except struct.error: + # Treat this as a warning. + msg = "Marker had length {0} instead of expected length of 2 " + msg += "bytes. Codestream parsing terminated." + warnings.warn(msg.format(len(read_buffer))) + break if marker_id == 0xff90 and header_only: # Start-of-tile (SOT) means that we are out of the main header diff --git a/glymur/test/test_format_corpus.py b/glymur/test/test_format_corpus.py new file mode 100644 index 0000000..2f7347b --- /dev/null +++ b/glymur/test/test_format_corpus.py @@ -0,0 +1,51 @@ +""" +These tests deal with JPX/JP2/J2K images in the format-corpus repository. +""" +#pylint: disable-all + +import os +import sys + +if sys.hexversion < 0x02070000: + import unittest2 as unittest +else: + import unittest + +import warnings + +from glymur import Jp2k +import glymur + +try: + data_root = os.environ['FORMAT_CORPUS_ROOT'] +except KeyError: + data_root = None +except: + raise + + +@unittest.skipIf(sys.hexversion < 0x03020000, + "Requires features introduced in 3.2 (assertWarns)") +@unittest.skipIf(data_root is None, + "FORMAT_CORPUS_ROOT environment variable not set") +class TestSuite(unittest.TestCase): + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_balloon_trunc1(self): + # Has one byte shaved off of EOC marker. + jfile = os.path.join(data_root, + 'jp2k-test/byteCorruption/balloon_trunc1.jp2') + j2k = Jp2k(jfile) + with self.assertWarns(UserWarning): + c = j2k.get_codestream(header_only=False) + + # The last segment is truncated, so there should not be an EOC marker. + self.assertNotEqual(c.segment[-1].marker_id, 'EOC') + +if __name__ == "__main__": + unittest.main()