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):