Added single pixel and component retrieval.

This commit is contained in:
jevans 2014-09-13 12:36:03 -04:00
commit 27b67113bc
2 changed files with 27 additions and 7 deletions

View file

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

View file

@ -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]