diff --git a/dcimg.py b/dcimg.py index a221d8e..083bb46 100644 --- a/dcimg.py +++ b/dcimg.py @@ -140,14 +140,23 @@ class DCIMGFile(object): ('bytes_per_img', ' 0 @@ -440,7 +453,7 @@ def _has_4px_data(self): footer_size = int(self._sess_footer['footer_size']) offset_to_4px = int(self._sess_footer2['offset_to_4px']) - return footer_size == offset_to_4px + 4 * self.byte_depth * self.nfrms + return footer_size == offset_to_4px + 8 * self.nfrms def __getitem__(self, item): """Allow to access image data using NumPy's basic indexing.""" @@ -513,13 +526,15 @@ def __getitem__(self, item): condition_y = False if self.fmt_version == self.FMT_OLD and self._has_4px_data: condition_y = starty == 0 or stopy == 0 - elif self.fmt_version == self.FMT_NEW: + elif self.fmt_version == self.FMT_NEW and self._has_4px_data: if stepy > 0: condition_y = starty <= target_line <= stopy elif stepy < 0: condition_y = stopy <= target_line <= starty - if condition_y and ((0 <= startx < 4) or stopx < 4): + n = 8 // self.byte_depth + + if condition_y and ((0 <= startx < n) or stopx < n): if a.size == 1: if self.first_4px_correction_enabled: a = self._4px[myitem[0].start, startx] @@ -529,16 +544,16 @@ def __getitem__(self, item): if startx < stopx: newstartx = 0 - if stopx > 4: - newstopx = int(math.ceil((4 - startx) / abs(stepx))) + if stopx > n: + newstopx = int(math.ceil((n - startx) / abs(stepx))) else: newstopx = (stopx - startx) // abs(stepx) else: newstopx = a.shape[-1] - if a.shape[-1] < 4: + if a.shape[-1] < n: newstartx = 0 else: - newstartx = (a.shape[-1] - 4 // abs(stepx)) + newstartx = (a.shape[-1] - n // abs(stepx)) if newstartx == newstopx: return np.empty([0]) @@ -563,7 +578,7 @@ def __getitem__(self, item): if self.first_4px_correction_enabled: _range = sorted((startx, stopx)) _4start = max(0, _range[0]) - _4stop = min(4, _range[1]) + _4stop = min(8 // self.byte_depth, _range[1]) _4px = self._4px[item[0], _4start:_4stop:abs(stepx)] if stepx < 0: diff --git a/tests/test_dcimg.py b/tests/test_dcimg.py index 7974df3..b08447e 100644 --- a/tests/test_dcimg.py +++ b/tests/test_dcimg.py @@ -84,7 +84,7 @@ def setUpClass(cls): _4px = (65000 - np.arange(nfrms * 4)).reshape((nfrms, 4)) f = DCIMGFileOverride4px() - f._sess_header = np.copy(_sess_header) + f._sess_header = _sess_header f.mma = np.arange(np.prod(f.shape), dtype=np.uint16).reshape(f.shape) f.mma.flags.writeable = False f.deep_copy_enabled = True @@ -97,7 +97,13 @@ def setUpClass(cls): cls.f = f f = DCIMGFile() - f._sess_header = np.copy(_sess_header) + _sess_header = np.zeros(1, dtype=DCIMGFile.NEW_SESSION_HEADER_DTYPE) + _sess_header['nfrms'][0] = nfrms + _sess_header['ysize'][0] = 2048 + _sess_header['xsize'][0] = 2048 + _sess_header['byte_depth'][0] = 2 + _sess_header['frame_footer_size'][0] = 32 + f._sess_header = _sess_header f.mma = np.arange(np.prod(f.shape), dtype=np.uint16).reshape(f.shape) f.mma.flags.writeable = False f.deep_copy_enabled = True