From 276a74ecaa6a0b89fbb3bd517a50099c14ed2397 Mon Sep 17 00:00:00 2001 From: John Evans Date: Fri, 12 Sep 2014 10:06:09 -0400 Subject: [PATCH] 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):