From 27b67113bc2a2809f4be9ea3fb24c14b1ef4e9b4 Mon Sep 17 00:00:00 2001 From: jevans Date: Sat, 13 Sep 2014 12:36:03 -0400 Subject: [PATCH] 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]