From a8219236aa4e9f4614ff6c8933542a9c903fa59a Mon Sep 17 00:00:00 2001 From: jevans Date: Sat, 6 Jul 2013 17:50:39 -0400 Subject: [PATCH] Erroring out appropriately when _read_openjpeg gets differing sampling If the subsampling factors are different, we should error out when using read with libopenjpeg. Closed #72 --- glymur/jp2k.py | 17 +++++++++++++---- glymur/test/test_jp2k.py | 2 +- glymur/test/test_jp2k_15.py | 5 ++--- glymur/test/test_opj_suite.py | 2 +- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 6d9d7ce..b0d4157 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -613,9 +613,19 @@ class Jp2k(Jp2kBox): Raises ------ - IOError + RuntimeError If the image has differing subsample factors. """ + # Check for differing subsample factors. + codestream = self.get_codestream(header_only=True) + dxs = np.array(codestream.segment[1].XRsiz) + dys = np.array(codestream.segment[1].YRsiz) + if np.any(dxs - dxs[0]) or np.any(dys - dys[0]): + msg = "Components must all have the same subsampling factors " + msg += "to use this method with OpenJPEG 1.5.1. Please consider " + msg += "using OPENJP2 instead." + raise RuntimeError(msg) + with ExitStack() as stack: # Set decoding parameters. @@ -653,7 +663,6 @@ class Jp2k(Jp2kBox): stack.callback(opj._cio_close, cio) ncomps = image.contents.numcomps - component = image.contents.comps[0] if component.sgnd: if component.prec <= 8: @@ -726,7 +735,7 @@ class Jp2k(Jp2kBox): Raises ------ - IOError + RuntimeError If the image has differing subsample factors. """ # Check for differing subsample factors. @@ -735,7 +744,7 @@ class Jp2k(Jp2kBox): dys = np.array(codestream.segment[1].YRsiz) if np.any(dxs - dxs[0]) or np.any(dys - dys[0]): msg = "Components must all have the same subsampling factors." - raise IOError(msg) + raise RuntimeError(msg) img_array = self._read_common(reduce=reduce, layer=layer, diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index f2da55f..8990eb8 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -319,7 +319,7 @@ class TestJp2k(unittest.TestCase): # Issue 86. filename = os.path.join(data_root, 'input/conformance/p0_05.j2k') j = Jp2k(filename) - with self.assertRaises(IOError): + with self.assertRaises(RuntimeError): j.read() @unittest.skipIf(data_root is None, diff --git a/glymur/test/test_jp2k_15.py b/glymur/test/test_jp2k_15.py index 5708cb4..90e9fd7 100644 --- a/glymur/test/test_jp2k_15.py +++ b/glymur/test/test_jp2k_15.py @@ -794,12 +794,11 @@ class TestSuite(unittest.TestCase): data = Jp2k(jfile).read() self.assertTrue(True) - @unittest.skip("Should have worked, must be investigated.") def test_NR_DEC_file_409752_jp2_40_decode(self): jfile = os.path.join(data_root, 'input/nonregression/file409752.jp2') j = Jp2k(jfile) - data = j.read() - self.assertTrue(True) + with self.assertRaises(RuntimeError) as ce: + data = j.read() if __name__ == "__main__": unittest.main() diff --git a/glymur/test/test_opj_suite.py b/glymur/test/test_opj_suite.py index 05c97dd..fe2485b 100644 --- a/glymur/test/test_opj_suite.py +++ b/glymur/test/test_opj_suite.py @@ -1068,7 +1068,7 @@ class TestSuite(unittest.TestCase): def test_NR_DEC_file_409752_jp2_40_decode(self): jfile = os.path.join(data_root, 'input/nonregression/file409752.jp2') - with self.assertRaises(IOError): + with self.assertRaises(RuntimeError): data = Jp2k(jfile).read() @unittest.skipIf(sys.hexversion < 0x03020000,