From 3adb09e6f4ff46a150c589e66361bdb012817a1b Mon Sep 17 00:00:00 2001 From: John Evans Date: Fri, 12 Sep 2014 10:05:40 -0400 Subject: [PATCH 1/9] refactoring slice protocol tests --- glymur/test/test_jp2k.py | 100 +++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 35 deletions(-) diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 9058bbf..fcf5ea0 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -52,74 +52,92 @@ def load_tests(loader, tests, ignore): return tests -class TestJp2k(unittest.TestCase): - """These tests should be run by just about all configuration.""" - +class TestSliceProtocol(unittest.TestCase): + """ + Test slice protocol, i.e. when using [ ] to read image data. + """ def setUp(self): self.jp2file = glymur.data.nemo() - self.j2kfile = glymur.data.goodstuff() + self.j2k = Jp2k(glymur.data.goodstuff()) self.jpxfile = glymur.data.jpxfile() def tearDown(self): pass - def test_slice_protocol_negative(self): - """ - """ - j = Jp2k(self.j2kfile) - + def test_resolution_strides_cannot_differ(self): with self.assertRaises(IndexError): # Strides in x/y directions cannot differ. - d = j[::2, ::3] + self.j2k[::2, ::3] + def test_resolution_strides_must_be_powers_of_two(self): with self.assertRaises(IndexError): - # Strides in x/y direction must be powers of 2. - d = j[::3, ::3] + self.j2k[::3, ::3] - # start and stop are not supported when slicing on Jp2k object + def test_start_and_resolution_stride_not_allowed_at_same_time(self): with self.assertRaises(IndexError): - d = j[2::2, 2::2] - with self.assertRaises(IndexError): - d = j[:8:2, :8:2] - with self.assertRaises(IndexError): - d = j[2:8:2, 2:8:2] + self.j2k[2::2, 2::2] - def test_slice_protocol_3d(self): - """ - """ - j = Jp2k(self.j2kfile) - all = j.read() + def test_stop_and_resolution_stride_not_allowed_at_same_time(self): + with self.assertRaises(IndexError): + self.j2k[:8:2, :8:2] - d = j[:,:,0] + def test_integer_index_in_3d(self): + all = self.j2k.read() + + d = self.j2k[:,:,0] np.testing.assert_array_equal(all[:,:,0], d) - d = j[:,:,1] + d = self.j2k[:,:,1] np.testing.assert_array_equal(all[:,:,1], d) - d = j[:,:,2] + d = self.j2k[:,:,2] np.testing.assert_array_equal(all[:,:,2], d) - d = j[:,:,1:3] + def test_slice_in_third_dimension(self): + all = self.j2k.read() + + d = self.j2k[:,:,1:3] np.testing.assert_array_equal(all[:,:,1:3], d) - d = j[::2, ::2, 1:3] + def test_reduce_resolution_and_slice_in_third_dimension(self): + d = self.j2k[::2, ::2, 1:3] all = j.read(rlevel=1) np.testing.assert_array_equal(all[:,:,1:3], d) - def test_slice_protocol_2d(self): - """ + def test_full_resolution_upper_left_quarter(self): + all = self.jp2[:] - """ - j = Jp2k(self.j2kfile) + d = j[:728, :1296] + np.testing.assert_array_equal(all[:728, :1296], d) - d = j[:] + def test_full_resolution_lower_left_quarter(self): + all = self.jp2[:] + + d = j[728:, :1296] + np.testing.assert_array_equal(all[728:, :1296], d) + + def test_full_resolution_upper_right_quarter(self): + """ + Slice protocol should work when not reducing resolution. + """ + all = j[:] + + d = j[:728, 1296:] + np.testing.assert_array_equal(all[:728, 1296:], d) + + def test_full_resolution_lower_right_quarter(self): + all = j[:] + + d = j[728:, 1296:] + np.testing.assert_array_equal(all[728:, :1296:], d) + + def test_slice_protocol_2d_reduce_resolution(self): + d = self.j2k[:] self.assertEqual(d.shape, (800, 480, 3)) - # Stride of one. d = j[::1, ::1] self.assertEqual(d.shape, (800, 480, 3)) - # Stride of 2. d = j[::2, ::2] self.assertEqual(d.shape, (400, 240, 3)) @@ -135,6 +153,18 @@ class TestJp2k(unittest.TestCase): d = j[::32, ::32] self.assertEqual(d.shape, (25, 15, 3)) +class TestJp2k(unittest.TestCase): + """These tests should be run by just about all configuration.""" + + def setUp(self): + self.jp2file = glymur.data.nemo() + self.j2kfile = glymur.data.goodstuff() + self.jpxfile = glymur.data.jpxfile() + + def tearDown(self): + pass + + @unittest.skipIf(os.name == "nt", "Unexplained failure on windows") def test_irreversible(self): """Irreversible""" From 276a74ecaa6a0b89fbb3bd517a50099c14ed2397 Mon Sep 17 00:00:00 2001 From: John Evans Date: Fri, 12 Sep 2014 10:06:09 -0400 Subject: [PATCH 2/9] starting work to allow full resolution slicing --- glymur/jp2k.py | 65 ++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 8463eb5..b33cbdf 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -770,38 +770,47 @@ class Jp2k(Jp2kBox): else: raise IndexError("Illegal syntax.") - if isinstance(pargs[0], tuple): - ridx = pargs[0][0] - cidx = pargs[0][1] + if not isinstance(pargs[0], tuple): + msg = "Unexpected situation, slicing invoked, but not passed " + msg += "a slice or tuple." + raise RuntimeError(msg) - if ((ridx.start is not None) or - (ridx.stop is not None) or - (cidx.start is not None) or - (cidx.stop is not None)): - msg = "Only strides are supported when slicing a Jp2k object." - raise IndexError(msg) + # Assuming tuple from now on. + ridx = pargs[0][0] + cidx = pargs[0][1] - if ridx.step is None and cidx.step is None: - step = 1 - elif ridx.step != cidx.step: - msg = "Row and column strides must be the same." - raise IndexError(msg) - else: - step = ridx.step - - if np.log2(step) != np.floor(np.log2(step)): - msg = "Row and column strides must be powers of 2." - raise IndexError(msg) + if ((ridx.step is None) and (cidx.step is None)): + # Slicing with full resolution. + return self.read()[ridx, cidx] - data = self.read(rlevel=np.int(np.log2(step))) - if len(pargs[0]) == 2: - return data + if ((ridx.start is not None) or + (ridx.stop is not None) or + (cidx.start is not None) or + (cidx.stop is not None)): + msg = "Only strides are supported when slicing a Jp2k object." + raise IndexError(msg) - # Ok, 3 arguments in pargs. - if isinstance(pargs[0][2], slice): - return data[:,:,pargs[0][2]] - elif isinstance(pargs[0][2], int): - return data[:,:,pargs[0][2]] + if ridx.step is None and cidx.step is None: + step = 1 + elif ridx.step != cidx.step: + msg = "Row and column strides must be the same." + raise IndexError(msg) + else: + step = ridx.step + + if np.log2(step) != np.floor(np.log2(step)): + msg = "Row and column strides must be powers of 2." + raise IndexError(msg) + + data = self.read(rlevel=np.int(np.log2(step))) + if len(pargs[0]) == 2: + return data + + # Ok, 3 arguments in pargs. + if isinstance(pargs[0][2], slice): + return data[:,:,pargs[0][2]] + elif isinstance(pargs[0][2], int): + return data[:,:,pargs[0][2]] def read(self, **kwargs): From 6b66d4f6046681dde02f6ad7d9456311cc37c3b9 Mon Sep 17 00:00:00 2001 From: John Evans Date: Fri, 12 Sep 2014 10:06:59 -0400 Subject: [PATCH 3/9] Do not show VIM swp files in output of "git status" --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0d20b64..c9b568f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pyc +*.swp From 79a0b7edb86e3e2b03a85d372f68d94cbf16a75c Mon Sep 17 00:00:00 2001 From: John Evans Date: Fri, 12 Sep 2014 11:08:22 -0400 Subject: [PATCH 4/9] Further along. --- glymur/jp2k.py | 4 +- glymur/test/test_jp2k.py | 92 ++++++++++++++++++++++------------------ 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index b33cbdf..c9021be 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -761,6 +761,7 @@ class Jp2k(Jp2kBox): def __getitem__(self, *pargs): """ + Slicing protocol. """ if isinstance(pargs[0], slice): # Should have a slice object where start = stop = step = None @@ -778,10 +779,11 @@ class Jp2k(Jp2kBox): # Assuming tuple from now on. ridx = pargs[0][0] cidx = pargs[0][1] + bidx = pargs[0][2] if ((ridx.step is None) and (cidx.step is None)): # Slicing with full resolution. - return self.read()[ridx, cidx] + return self.read()[ridx, cidx, bidx] if ((ridx.start is not None) or (ridx.stop is not None) or diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index fcf5ea0..bbbe657 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -56,13 +56,14 @@ class TestSliceProtocol(unittest.TestCase): """ Test slice protocol, i.e. when using [ ] to read image data. """ - def setUp(self): - self.jp2file = glymur.data.nemo() - self.j2k = Jp2k(glymur.data.goodstuff()) - self.jpxfile = glymur.data.jpxfile() + @classmethod + def setUpClass(self): - def tearDown(self): - pass + self.jp2 = Jp2k(glymur.data.nemo()) + self.jp2_data = self.jp2.read() + + self.j2k = Jp2k(glymur.data.goodstuff()) + self.j2k_data = self.j2k.read() def test_resolution_strides_cannot_differ(self): with self.assertRaises(IndexError): @@ -82,75 +83,84 @@ class TestSliceProtocol(unittest.TestCase): self.j2k[:8:2, :8:2] def test_integer_index_in_3d(self): - all = self.j2k.read() d = self.j2k[:,:,0] - np.testing.assert_array_equal(all[:,:,0], d) + np.testing.assert_array_equal(self.j2k_data[:,:,0], d) d = self.j2k[:,:,1] - np.testing.assert_array_equal(all[:,:,1], d) + np.testing.assert_array_equal(self.j2k_data[:,:,1], d) d = self.j2k[:,:,2] - np.testing.assert_array_equal(all[:,:,2], d) + np.testing.assert_array_equal(self.j2k_data[:,:,2], d) def test_slice_in_third_dimension(self): - all = self.j2k.read() - - d = self.j2k[:,:,1:3] - np.testing.assert_array_equal(all[:,:,1:3], d) + actual = self.j2k[:,:,1:3] + expected = self.j2k_data[:,:,1:3] + np.testing.assert_array_equal(actual, expected) def test_reduce_resolution_and_slice_in_third_dimension(self): d = self.j2k[::2, ::2, 1:3] - all = j.read(rlevel=1) + all = self.j2k.read(rlevel=1) np.testing.assert_array_equal(all[:,:,1:3], d) - def test_full_resolution_upper_left_quarter(self): - all = self.jp2[:] + def test_full_resolution_slicing_by_quarters(self): + # upper left + np.testing.assert_array_equal(self.jp2_data[:728, :1296], + self.jp2[:728, :1296]) + # lower left + np.testing.assert_array_equal(self.jp2_data[728:, :1296], + self.jp2[728:, :1296]) - d = j[:728, :1296] - np.testing.assert_array_equal(all[:728, :1296], d) + def test_full_resolution_slicing_by_quarters_upper_right(self): + actual = self.jp2[:728, 1296:] + expected = self.jp2_data[:728, 1296:] + np.testing.assert_array_equal(actual, expected) - def test_full_resolution_lower_left_quarter(self): - all = self.jp2[:] + def test_full_resolution_slicing_by_quarters_lower_right(self): + actual = self.jp2[728:, 1296:] + expected = self.jp2_data[728:, 1296:] + np.testing.assert_array_equal(actual, expected) - d = j[728:, :1296] - np.testing.assert_array_equal(all[728:, :1296], d) + def test_full_resolution_slicing_by_halves_left(self): + actual = self.jp2[:, :1296] + expected = self.jp2_data[:, :1296] + np.testing.assert_array_equal(actual, expected) - def test_full_resolution_upper_right_quarter(self): - """ - Slice protocol should work when not reducing resolution. - """ - all = j[:] + def test_full_resolution_slicing_by_right_half(self): + actual = self.jp2[:, 1296:] + expected = self.jp2_data[:, 1296:] + np.testing.assert_array_equal(actual, expected) - d = j[:728, 1296:] - np.testing.assert_array_equal(all[:728, 1296:], d) + def test_full_resolution_slicing_by_top_half(self): + actual = self.jp2[:728, :] + expected = self.jp2_data[:728, :] + np.testing.assert_array_equal(actual, expected) - def test_full_resolution_lower_right_quarter(self): - all = j[:] - - d = j[728:, 1296:] - np.testing.assert_array_equal(all[728:, :1296:], d) + def test_full_resolution_slicing_by_bottom_half(self): + actual = self.jp2[728:, :] + expected = self.jp2_data[728:, :] + np.testing.assert_array_equal(actual, expected) def test_slice_protocol_2d_reduce_resolution(self): d = self.j2k[:] self.assertEqual(d.shape, (800, 480, 3)) - d = j[::1, ::1] + d = self.j2k[::1, ::1] self.assertEqual(d.shape, (800, 480, 3)) - d = j[::2, ::2] + d = self.j2k[::2, ::2] self.assertEqual(d.shape, (400, 240, 3)) - d = j[::4, ::4] + d = self.j2k[::4, ::4] self.assertEqual(d.shape, (200, 120, 3)) - d = j[::8, ::8] + d = self.j2k[::8, ::8] self.assertEqual(d.shape, (100, 60, 3)) - d = j[::16, ::16] + d = self.j2k[::16, ::16] self.assertEqual(d.shape, (50, 30, 3)) - d = j[::32, ::32] + d = self.j2k[::32, ::32] self.assertEqual(d.shape, (25, 15, 3)) class TestJp2k(unittest.TestCase): From f7b7b9a0df1e589ace66c9a9b5d76680fdac3cb2 Mon Sep 17 00:00:00 2001 From: John Evans Date: Fri, 12 Sep 2014 16:46:08 -0400 Subject: [PATCH 5/9] __keyitem__ parameter now assumed to be either integer or slice. No longer erroring out when mixing resolution reduction with horiz/vert slicing, but neither is it tested. --- glymur/jp2k.py | 53 +++++++++++++++++----------------------- glymur/test/test_jp2k.py | 35 +++++++++----------------- 2 files changed, 34 insertions(+), 54 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index c9021be..6919555 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -759,60 +759,51 @@ class Jp2k(Jp2kBox): return boxes - def __getitem__(self, *pargs): + def __getitem__(self, pargs): """ Slicing protocol. """ - if isinstance(pargs[0], slice): + if isinstance(pargs, slice): + # Case of jp2[:] + # # Should have a slice object where start = stop = step = None - slc = pargs[0] + slc = pargs if slc.start is None and slc.stop is None and slc.step is None: return self.read() else: raise IndexError("Illegal syntax.") - if not isinstance(pargs[0], tuple): - msg = "Unexpected situation, slicing invoked, but not passed " - msg += "a slice or tuple." - raise RuntimeError(msg) + # Assuming pargs is a tuple from now on. + rows = pargs[0] + cols = pargs[1] + if len(pargs) == 2: + bands = slice(None, None, None) + else: + bands = pargs[2] - # Assuming tuple from now on. - ridx = pargs[0][0] - cidx = pargs[0][1] - bidx = pargs[0][2] - - if ((ridx.step is None) and (cidx.step is None)): + if ((rows.step is None) and (cols.step is None)): # Slicing with full resolution. - return self.read()[ridx, cidx, bidx] + # This can be improved to take advantage of tiling. + return self.read()[rows, cols, bands] - if ((ridx.start is not None) or - (ridx.stop is not None) or - (cidx.start is not None) or - (cidx.stop is not None)): - msg = "Only strides are supported when slicing a Jp2k object." - raise IndexError(msg) - - if ridx.step is None and cidx.step is None: - step = 1 - elif ridx.step != cidx.step: + if rows.step != cols.step: msg = "Row and column strides must be the same." raise IndexError(msg) - else: - step = ridx.step + + # Ok, reduce layer step is the same in both xy directions, so just take + # one of them. + step = rows.step if np.log2(step) != np.floor(np.log2(step)): msg = "Row and column strides must be powers of 2." raise IndexError(msg) data = self.read(rlevel=np.int(np.log2(step))) - if len(pargs[0]) == 2: + if len(pargs) == 2: return data # Ok, 3 arguments in pargs. - if isinstance(pargs[0][2], slice): - return data[:,:,pargs[0][2]] - elif isinstance(pargs[0][2], int): - return data[:,:,pargs[0][2]] + return data[:, :, bands] def read(self, **kwargs): diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index bbbe657..50b153a 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -74,24 +74,11 @@ class TestSliceProtocol(unittest.TestCase): with self.assertRaises(IndexError): self.j2k[::3, ::3] - def test_start_and_resolution_stride_not_allowed_at_same_time(self): - with self.assertRaises(IndexError): - self.j2k[2::2, 2::2] - - def test_stop_and_resolution_stride_not_allowed_at_same_time(self): - with self.assertRaises(IndexError): - self.j2k[:8:2, :8:2] - def test_integer_index_in_3d(self): - d = self.j2k[:,:,0] - np.testing.assert_array_equal(self.j2k_data[:,:,0], d) - - d = self.j2k[:,:,1] - np.testing.assert_array_equal(self.j2k_data[:,:,1], d) - - d = self.j2k[:,:,2] - np.testing.assert_array_equal(self.j2k_data[:,:,2], d) + for j in [0, 1, 2]: + band = self.j2k[:, :, j] + np.testing.assert_array_equal(self.j2k_data[:, :, j], band) def test_slice_in_third_dimension(self): actual = self.j2k[:,:,1:3] @@ -103,13 +90,15 @@ class TestSliceProtocol(unittest.TestCase): all = self.j2k.read(rlevel=1) np.testing.assert_array_equal(all[:,:,1:3], d) - def test_full_resolution_slicing_by_quarters(self): - # upper left - np.testing.assert_array_equal(self.jp2_data[:728, :1296], - self.jp2[:728, :1296]) - # lower left - np.testing.assert_array_equal(self.jp2_data[728:, :1296], - self.jp2[728:, :1296]) + def test_full_resolution_slicing_by_quarters_upper_left(self): + actual = self.jp2[:728, :1296] + expected = self.jp2_data[:728, :1296] + np.testing.assert_array_equal(actual, expected) + + def test_full_resolution_slicing_by_quarters_lower_left(self): + actual = self.jp2[728:, :1296] + expected = self.jp2_data[728:, :1296] + np.testing.assert_array_equal(actual, expected) def test_full_resolution_slicing_by_quarters_upper_right(self): actual = self.jp2[:728, 1296:] From 7f6081113563cc48b2b8dcab6a76c04e9f708d8b Mon Sep 17 00:00:00 2001 From: jevans Date: Sat, 13 Sep 2014 09:31:11 -0400 Subject: [PATCH 6/9] More progress. All UTs passing on Mac/3.4.1/2.1.0 __getitem__ method needs a refactor. Need to test on JPX file with multiple images. --- glymur/jp2k.py | 49 ++++++++++++++++++++++++++++++++++------ glymur/test/test_jp2k.py | 35 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 6919555..68348ca 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -763,6 +763,15 @@ class Jp2k(Jp2kBox): """ Slicing protocol. """ + codestream = self.get_codestream(header_only=True) + if isinstance(pargs, int): + # Not a very good use of this protocol, but technically legal. + row = pargs + area = (row, 0, row + 1, codestream.segment[1].xsiz) + + # Take out the singleton row dimension. + return self.read(area=area).squeeze() + if isinstance(pargs, slice): # Case of jp2[:] # @@ -781,24 +790,50 @@ class Jp2k(Jp2kBox): else: bands = pargs[2] - if ((rows.step is None) and (cols.step is None)): - # Slicing with full resolution. - # This can be improved to take advantage of tiling. - return self.read()[rows, cols, bands] + if rows.step is None: + rows_step = 1 + else: + rows_step = rows.step - if rows.step != cols.step: + if cols.step is None: + cols_step = 1 + else: + cols_step = cols.step + + if rows_step != cols_step: msg = "Row and column strides must be the same." raise IndexError(msg) # Ok, reduce layer step is the same in both xy directions, so just take # one of them. - step = rows.step + step = rows_step if np.log2(step) != np.floor(np.log2(step)): msg = "Row and column strides must be powers of 2." raise IndexError(msg) - data = self.read(rlevel=np.int(np.log2(step))) + if rows.start is None: + rows_start = 0 + else: + rows_start = rows.start + + if rows.stop is None: + rows_stop = codestream.segment[1].ysiz + else: + rows_stop = rows.stop + + if cols.start is None: + cols_start = 0 + else: + cols_start = cols.start + + if cols.stop is None: + cols_stop = codestream.segment[1].xsiz + else: + cols_stop = cols.stop + + area = (rows_start, cols_start, rows_stop, cols_stop) + data = self.read(area=area, rlevel=np.int(np.log2(step))) if len(pargs) == 2: return data diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 50b153a..e03181d 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -70,6 +70,11 @@ class TestSliceProtocol(unittest.TestCase): # Strides in x/y directions cannot differ. self.j2k[::2, ::3] + def test_resolution_strides_cannot_differ(self): + with self.assertRaises(IndexError): + # Strides in x/y directions cannot differ. + self.j2k[::2, ::3] + def test_resolution_strides_must_be_powers_of_two(self): with self.assertRaises(IndexError): self.j2k[::3, ::3] @@ -90,6 +95,11 @@ class TestSliceProtocol(unittest.TestCase): all = self.j2k.read(rlevel=1) np.testing.assert_array_equal(all[:,:,1:3], d) + def test_retrieve_single_row(self): + actual = self.jp2[0] + expected = self.jp2_data[0] + np.testing.assert_array_equal(actual, expected) + def test_full_resolution_slicing_by_quarters_upper_left(self): actual = self.jp2[:728, :1296] expected = self.jp2_data[:728, :1296] @@ -110,6 +120,11 @@ class TestSliceProtocol(unittest.TestCase): expected = self.jp2_data[728:, 1296:] np.testing.assert_array_equal(actual, expected) + def test_full_resolution_slicing_by_quarters_center(self): + actual = self.jp2[364:1092, 648:1942] + expected = self.jp2_data[364:1092, 648:1942] + np.testing.assert_array_equal(actual, expected) + def test_full_resolution_slicing_by_halves_left(self): actual = self.jp2[:, :1296] expected = self.jp2_data[:, :1296] @@ -130,6 +145,26 @@ class TestSliceProtocol(unittest.TestCase): expected = self.jp2_data[728:, :] np.testing.assert_array_equal(actual, expected) + def test_region_rlevel1(self): + actual = self.jp2[0:201:2, 0:201:2] + expected = self.jp2.read(area=(0, 0, 201, 201), rlevel=1) + np.testing.assert_array_equal(actual, expected) + + def test_region_rlevel1_slice_start_is_none(self): + actual = self.jp2[:201:2, :201:2] + expected = self.jp2.read(area=(0, 0, 201, 201), rlevel=1) + np.testing.assert_array_equal(actual, expected) + + def test_region_rlevel1_slice_stop_is_none(self): + actual = self.jp2[201::2, 201::2] + expected = self.jp2.read(area=(201, 201, 1456, 2592), rlevel=1) + np.testing.assert_array_equal(actual, expected) + + def test_region_rlevel1(self): + actual = self.jp2[0:202:2, 0:202:2] + expected = self.jp2.read(area=(0, 0, 202, 202), rlevel=1) + np.testing.assert_array_equal(actual, expected) + def test_slice_protocol_2d_reduce_resolution(self): d = self.j2k[:] self.assertEqual(d.shape, (800, 480, 3)) From 27b67113bc2a2809f4be9ea3fb24c14b1ef4e9b4 Mon Sep 17 00:00:00 2001 From: jevans Date: Sat, 13 Sep 2014 12:36:03 -0400 Subject: [PATCH 7/9] Added single pixel and component retrieval. --- glymur/jp2k.py | 24 +++++++++++++++++------- glymur/test/test_jp2k.py | 10 ++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 68348ca..155f7ee 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -766,6 +766,7 @@ class Jp2k(Jp2kBox): codestream = self.get_codestream(header_only=True) if isinstance(pargs, int): # Not a very good use of this protocol, but technically legal. + # This retrieves a single row. row = pargs area = (row, 0, row + 1, codestream.segment[1].xsiz) @@ -773,16 +774,25 @@ class Jp2k(Jp2kBox): return self.read(area=area).squeeze() if isinstance(pargs, slice): - # Case of jp2[:] + # Case of jp2[:], i.e. retrieve the entire image. # # Should have a slice object where start = stop = step = None - slc = pargs - if slc.start is None and slc.stop is None and slc.step is None: - return self.read() - else: - raise IndexError("Illegal syntax.") + return self.read() - # Assuming pargs is a tuple from now on. + if isinstance(pargs, tuple) and all(isinstance(x, int) for x in pargs): + # Retrieve a single pixel. + # Something like jp2[r, c] + row = pargs[0] + col = pargs[1] + area = (row, col, row + 1, col + 1) + pixel = self.read(area=area).squeeze() + + if len(pargs) == 2: + return pixel + elif len(pargs) == 3: + return pixel[pargs[2]] + + # Assuming pargs is a tuple of slices from now on. rows = pargs[0] cols = pargs[1] if len(pargs) == 2: diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index e03181d..f1e9971 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -100,6 +100,16 @@ class TestSliceProtocol(unittest.TestCase): expected = self.jp2_data[0] np.testing.assert_array_equal(actual, expected) + def test_retrieve_single_pixel(self): + actual = self.jp2[0,0] + expected = self.jp2_data[0, 0] + np.testing.assert_array_equal(actual, expected) + + def test_retrieve_single_component(self): + actual = self.jp2[20,20,2] + expected = self.jp2_data[20, 20, 2] + np.testing.assert_array_equal(actual, expected) + def test_full_resolution_slicing_by_quarters_upper_left(self): actual = self.jp2[:728, :1296] expected = self.jp2_data[:728, :1296] From 18c1687b28eea70c7130a545d9eb645594d40b11 Mon Sep 17 00:00:00 2001 From: jevans Date: Sat, 13 Sep 2014 20:35:35 -0400 Subject: [PATCH 8/9] Not supporting on 1.5 --- glymur/jp2k.py | 7 +++++-- glymur/test/test_jp2k.py | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index 155f7ee..56f2db2 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -763,14 +763,17 @@ class Jp2k(Jp2kBox): """ Slicing protocol. """ + if re.match("1.", version.openjpeg_version): + msg = "Not implemented for version 1.x of OpenJPEG. Use the read " + msg += "method instead." + raise NotImplementedError(msg) + codestream = self.get_codestream(header_only=True) if isinstance(pargs, int): # Not a very good use of this protocol, but technically legal. # This retrieves a single row. row = pargs area = (row, 0, row + 1, codestream.segment[1].xsiz) - - # Take out the singleton row dimension. return self.read(area=area).squeeze() if isinstance(pargs, slice): diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index f1e9971..20dc3bc 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -52,6 +52,8 @@ def load_tests(loader, tests, ignore): return tests +@unittest.skipIf(glymur.version.openjpeg_version[0] == '1', + "Slice protocol not supported in version 1.x") class TestSliceProtocol(unittest.TestCase): """ Test slice protocol, i.e. when using [ ] to read image data. From 2c780af3f0b0bb5f2dc84b2761b7913036b8a903 Mon Sep 17 00:00:00 2001 From: jevans Date: Sun, 14 Sep 2014 17:26:10 -0400 Subject: [PATCH 9/9] added several near duplicate tests from opj suite, some refactoring Rewrite of read method tests with area parameter. They run much faster now. --- glymur/test/test_jp2k.py | 153 ++++++++++++++ glymur/test/test_opj_suite_2p1.py | 320 +++++++++++++----------------- 2 files changed, 290 insertions(+), 183 deletions(-) diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 20dc3bc..79decb7 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -199,6 +199,159 @@ class TestSliceProtocol(unittest.TestCase): d = self.j2k[::32, ::32] self.assertEqual(d.shape, (25, 15, 3)) +@unittest.skipIf(OPJ_DATA_ROOT is None, + "OPJ_DATA_ROOT environment variable not set") +@unittest.skipIf(glymur.version.openjpeg_version[0] == '1', + "Slice protocol not supported in version 1.x") +class TestSliceProtocolOpjData(unittest.TestCase): + """ + Test slice protocol, i.e. when using [ ] to read image data. + These correspond to tests for the read method with the area parameter. + """ + @classmethod + def setUpClass(self): + + jfile = opj_data_file('input/conformance/p1_04.j2k') + self.j2k = Jp2k(jfile) + self.j2k_data = self.j2k.read() + self.j2k_half_data = self.j2k.read(rlevel=1) + self.j2k_quarter_data = self.j2k.read(rlevel=2) + + def test_NR_DEC_p1_04_j2k_43_decode(self): + actual = self.j2k[:1024, :1024] + expected = self.j2k_data + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_44_decode(self): + actual = self.j2k[640:768, 512:640] + expected = self.j2k_data[640:768, 512:640] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_45_decode(self): + actual = self.j2k[896:1024, 896:1024] + expected = self.j2k_data[896:1024, 896:1024] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_46_decode(self): + actual = self.j2k[500:800, 100:300] + expected = self.j2k_data[500:800, 100:300] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_47_decode(self): + actual = self.j2k[520:600, 260:360] + expected = self.j2k_data[520:600, 260:360] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_48_decode(self): + actual = self.j2k[520:660, 260:360] + expected = self.j2k_data[520:660, 260:360] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_49_decode(self): + actual = self.j2k[520:600, 360:400] + expected = self.j2k_data[520:600, 360:400] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_50_decode(self): + actual = self.j2k[:1024:4, :1024:4] + expected = self.j2k_quarter_data[:256, :256] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_51_decode(self): + actual = self.j2k[640:768:4, 512:640:4] + expected = self.j2k_quarter_data[160:192, 128:160] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_52_decode(self): + actual = self.j2k[896:1024:4, 896:1024:4] + expected = self.j2k_quarter_data[224:352, 224:352] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_53_decode(self): + actual = self.j2k[500:800:4, 100:300:4] + expected = self.j2k_quarter_data[125:200, 25:75] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_54_decode(self): + actual = self.j2k[520:600:4, 260:360:4] + expected = self.j2k_quarter_data[130:150, 65:90] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_55_decode(self): + actual = self.j2k[520:660:4, 260:360:4] + expected = self.j2k_quarter_data[130:165, 65:90] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_56_decode(self): + actual = self.j2k[520:600:4, 360:400:4] + expected = self.j2k_quarter_data[130:150, 90:100] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_06_j2k_75_decode(self): + # Image size would be 0 x 0. + with self.assertRaises((IOError, OSError)): + self.j2k[9:12:4, 9:12:4] + + def test_NR_DEC_p0_04_j2k_85_decode(self): + actual = self.j2k[:256, :256] + expected = self.j2k_data[:256, :256] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_86_decode(self): + actual = self.j2k[:128, 128:256] + expected = self.j2k_data[:128, 128:256] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_87_decode(self): + actual = self.j2k[10:200, 50:120] + expected = self.j2k_data[10:200, 50:120] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_88_decode(self): + actual = self.j2k[150:210, 10:190] + expected = self.j2k_data[150:210, 10:190] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_89_decode(self): + actual = self.j2k[80:150, 100:200] + expected = self.j2k_data[80:150, 100:200] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_90_decode(self): + actual = self.j2k[20:50, 150:200] + expected = self.j2k_data[20:50, 150:200] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_91_decode(self): + actual = self.j2k[:256:4, :256:4] + expected = self.j2k_quarter_data[0:64, 0:64] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_92_decode(self): + actual = self.j2k[:128:4, 128:256:4] + expected = self.j2k_quarter_data[:32, 32:64] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_93_decode(self): + actual = self.j2k[10:200:4, 50:120:4] + expected = self.j2k_quarter_data[3:50, 13:30] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_94_decode(self): + actual = self.j2k[150:210:4, 10:190:4] + expected = self.j2k_quarter_data[38:53, 3:48] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_95_decode(self): + actual = self.j2k[80:150:4, 100:200:4] + expected = self.j2k_quarter_data[20:38, 25:50] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p0_04_j2k_96_decode(self): + actual = self.j2k[20:50:4, 150:200:4] + expected = self.j2k_quarter_data[5:13, 38:50] + np.testing.assert_array_equal(actual, expected) + class TestJp2k(unittest.TestCase): """These tests should be run by just about all configuration.""" diff --git a/glymur/test/test_opj_suite_2p1.py b/glymur/test/test_opj_suite_2p1.py index ebe5bf8..1b58d77 100644 --- a/glymur/test/test_opj_suite_2p1.py +++ b/glymur/test/test_opj_suite_2p1.py @@ -124,105 +124,6 @@ class TestSuite2point1(unittest.TestCase): Jp2k(jfile).read() self.assertTrue(True) - def test_NR_DEC_p1_04_j2k_43_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 1024, 1024)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata) - - def test_NR_DEC_p1_04_j2k_44_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(640, 512, 768, 640)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[640:768, 512:640]) - - def test_NR_DEC_p1_04_j2k_45_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(896, 896, 1024, 1024)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[896:1024, 896:1024]) - - def test_NR_DEC_p1_04_j2k_46_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(500, 100, 800, 300)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[500:800, 100:300]) - - def test_NR_DEC_p1_04_j2k_47_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 260, 600, 360)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[520:600, 260:360]) - - def test_NR_DEC_p1_04_j2k_48_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 260, 660, 360)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[520:660, 260:360]) - - def test_NR_DEC_p1_04_j2k_49_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 360, 600, 400)) - odata = jp2k.read() - np.testing.assert_array_equal(ssdata, odata[520:600, 360:400]) - - def test_NR_DEC_p1_04_j2k_50_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 1024, 1024), rlevel=2) - odata = jp2k.read(rlevel=2) - - np.testing.assert_array_equal(ssdata, odata[0:256, 0:256]) - - def test_NR_DEC_p1_04_j2k_51_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(640, 512, 768, 640), rlevel=2) - odata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(ssdata, odata[160:192, 128:160]) - - def test_NR_DEC_p1_04_j2k_52_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(896, 896, 1024, 1024), rlevel=2) - odata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(ssdata, odata[224:352, 224:352]) - - def test_NR_DEC_p1_04_j2k_53_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(500, 100, 800, 300), rlevel=2) - odata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(ssdata, odata[125:200, 25:75]) - - def test_NR_DEC_p1_04_j2k_54_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 260, 600, 360), rlevel=2) - odata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(ssdata, odata[130:150, 65:90]) - - def test_NR_DEC_p1_04_j2k_55_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 260, 660, 360), rlevel=2) - odata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(ssdata, odata[130:165, 65:90]) - - def test_NR_DEC_p1_04_j2k_56_decode(self): - jfile = opj_data_file('input/conformance/p1_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(520, 360, 600, 400), rlevel=2) - odata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(ssdata, odata[130:150, 90:100]) - def test_NR_DEC_p1_04_j2k_57_decode(self): jfile = opj_data_file('input/conformance/p1_04.j2k') jp2k = Jp2k(jfile) @@ -263,127 +164,180 @@ class TestSuite2point1(unittest.TestCase): with self.assertRaises(IOError): j.read() - def test_NR_DEC_p1_06_j2k_70_decode(self): +@unittest.skipIf(OPJ_DATA_ROOT is None, + "OPJ_DATA_ROOT environment variable not set") +@unittest.skipIf(re.match(r'''(1|2.0.0)''', + glymur.version.openjpeg_version) is not None, + "Only supported in 2.0.1 or higher") +class TestReadArea(unittest.TestCase): + """ + Runs tests introduced in version 2.0+ or that pass only in 2.0+ + + Specifically for read method with area parameter. + """ + @classmethod + def setUpClass(self): + + jfile = opj_data_file('input/conformance/p1_04.j2k') + self.j2k = Jp2k(jfile) + self.j2k_data = self.j2k.read() + self.j2k_half_data = self.j2k.read(rlevel=1) + self.j2k_quarter_data = self.j2k.read(rlevel=2) + jfile = opj_data_file('input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(9, 9, 12, 12), rlevel=1) - self.assertEqual(ssdata.shape, (1, 1, 3)) + self.j2k_p1_06 = Jp2k(jfile) + + def test_NR_DEC_p1_04_j2k_43_decode(self): + actual = self.j2k.read(area=(0, 0, 1024, 1024)) + expected = self.j2k_data + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_44_decode(self): + actual = self.j2k.read(area=(640, 512, 768, 640)) + expected = self.j2k_data[640:768, 512:640] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_45_decode(self): + actual = self.j2k.read(area=(896, 896, 1024, 1024)) + expected = self.j2k_data[896:1024, 896:1024] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_46_decode(self): + actual = self.j2k.read(area=(500, 100, 800, 300)) + expected = self.j2k_data[500:800, 100:300] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_47_decode(self): + actual = self.j2k.read(area=(520, 260, 600, 360)) + expected = self.j2k_data[520:600, 260:360] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_48_decode(self): + actual = self.j2k.read(area=(520, 260, 660, 360)) + expected = self.j2k_data[520:660, 260:360] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_49_decode(self): + actual = self.j2k.read(area=(520, 360, 600, 400)) + expected = self.j2k_data[520:600, 360:400] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_50_decode(self): + actual = self.j2k.read(area=(0, 0, 1024, 1024), rlevel=2) + expected = self.j2k_quarter_data + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_51_decode(self): + actual = self.j2k.read(area=(640, 512, 768, 640), rlevel=2) + expected = self.j2k_quarter_data[160:192, 128:160] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_52_decode(self): + actual = self.j2k.read(area=(896, 896, 1024, 1024), rlevel=2) + expected = self.j2k_quarter_data[224:352, 224:352] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_53_decode(self): + actual = self.j2k.read(area=(500, 100, 800, 300), rlevel=2) + expected = self.j2k_quarter_data[125:200, 25:75] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_54_decode(self): + actual = self.j2k.read(area=(520, 260, 600, 360), rlevel=2) + expected = self.j2k_quarter_data[130:150, 65:90] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_55_decode(self): + actual = self.j2k.read(area=(520, 260, 660, 360), rlevel=2) + expected = self.j2k_quarter_data[130:165, 65:90] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_04_j2k_56_decode(self): + actual = self.j2k.read(area=(520, 360, 600, 400), rlevel=2) + expected = self.j2k_quarter_data[130:150, 90:100] + np.testing.assert_array_equal(actual, expected) + + def test_NR_DEC_p1_06_j2k_70_decode(self): + actual = self.j2k_p1_06.read(area=(9, 9, 12, 12), rlevel=1) + self.assertEqual(actual.shape, (1, 1, 3)) def test_NR_DEC_p1_06_j2k_71_decode(self): - jfile = opj_data_file('input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(10, 4, 12, 10), rlevel=1) - self.assertEqual(ssdata.shape, (1, 3, 3)) + actual = self.j2k_p1_06.read(area=(10, 4, 12, 10), rlevel=1) + self.assertEqual(actual.shape, (1, 3, 3)) def test_NR_DEC_p1_06_j2k_72_decode(self): - jfile = opj_data_file('input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(3, 3, 9, 9), rlevel=1) + ssdata = self.j2k_p1_06.read(area=(3, 3, 9, 9), rlevel=1) self.assertEqual(ssdata.shape, (3, 3, 3)) def test_NR_DEC_p1_06_j2k_73_decode(self): - jfile = opj_data_file('input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(4, 4, 7, 7), rlevel=1) + ssdata = self.j2k_p1_06.read(area=(4, 4, 7, 7), rlevel=1) self.assertEqual(ssdata.shape, (2, 2, 3)) def test_NR_DEC_p1_06_j2k_74_decode(self): - jfile = opj_data_file('input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(4, 4, 5, 5), rlevel=1) + ssdata = self.j2k_p1_06.read(area=(4, 4, 5, 5), rlevel=1) self.assertEqual(ssdata.shape, (1, 1, 3)) def test_NR_DEC_p1_06_j2k_75_decode(self): # Image size would be 0 x 0. - jfile = opj_data_file('input/conformance/p1_06.j2k') - jp2k = Jp2k(jfile) with self.assertRaises((IOError, OSError)): - jp2k.read(area=(9, 9, 12, 12), rlevel=2) + self.j2k_p1_06.read(area=(9, 9, 12, 12), rlevel=2) def test_NR_DEC_p0_04_j2k_85_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 256, 256)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[0:256, 0:256], ssdata) + actual = self.j2k.read(area=(0, 0, 256, 256)) + expected = self.j2k_data[:256, :256] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_86_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 128, 128, 256)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[0:128, 128:256], ssdata) + actual = self.j2k.read(area=(0, 128, 128, 256)) + expected = self.j2k_data[:128, 128:256] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_87_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(10, 50, 200, 120)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[10:200, 50:120], ssdata) + actual = self.j2k.read(area=(10, 50, 200, 120)) + expected = self.j2k_data[10:200, 50:120] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_88_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(150, 10, 210, 190)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[150:210, 10:190], ssdata) + actual = self.j2k.read(area=(150, 10, 210, 190)) + expected = self.j2k_data[150:210, 10:190] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_89_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(80, 100, 150, 200)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[80:150, 100:200], ssdata) + actual = self.j2k.read(area=(80, 100, 150, 200)) + expected = self.j2k_data[80:150, 100:200] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_90_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(20, 150, 50, 200)) - fulldata = jp2k.read() - np.testing.assert_array_equal(fulldata[20:50, 150:200], ssdata) + actual = self.j2k.read(area=(20, 150, 50, 200)) + expected = self.j2k_data[20:50, 150:200] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_91_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 0, 256, 256), rlevel=2) - fulldata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(fulldata[0:64, 0:64], ssdata) + actual = self.j2k.read(area=(0, 0, 256, 256), rlevel=2) + expected = self.j2k_quarter_data[0:64, 0:64] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_92_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(0, 128, 128, 256), rlevel=2) - fulldata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(fulldata[0:32, 32:64], ssdata) + actual = self.j2k.read(area=(0, 128, 128, 256), rlevel=2) + expected = self.j2k_quarter_data[:32, 32:64] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_93_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(10, 50, 200, 120), rlevel=2) - fulldata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(fulldata[3:50, 13:30], ssdata) + actual = self.j2k.read(area=(10, 50, 200, 120), rlevel=2) + expected = self.j2k_quarter_data[3:50, 13:30] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_94_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(150, 10, 210, 190), rlevel=2) - fulldata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(fulldata[38:53, 3:48], ssdata) + actual = self.j2k.read(area=(150, 10, 210, 190), rlevel=2) + expected = self.j2k_quarter_data[38:53, 3:48] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_95_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(80, 100, 150, 200), rlevel=2) - fulldata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(fulldata[20:38, 25:50], ssdata) + actual = self.j2k.read(area=(80, 100, 150, 200), rlevel=2) + expected = self.j2k_quarter_data[20:38, 25:50] + np.testing.assert_array_equal(actual, expected) def test_NR_DEC_p0_04_j2k_96_decode(self): - jfile = opj_data_file('input/conformance/p0_04.j2k') - jp2k = Jp2k(jfile) - ssdata = jp2k.read(area=(20, 150, 50, 200), rlevel=2) - fulldata = jp2k.read(rlevel=2) - np.testing.assert_array_equal(fulldata[5:13, 38:50], ssdata) - - -if __name__ == "__main__": - unittest.main() + actual = self.j2k.read(area=(20, 150, 50, 200), rlevel=2) + expected = self.j2k_quarter_data[5:13, 38:50] + np.testing.assert_array_equal(actual, expected)