Skip to content

Commit

Permalink
Merge pull request #543 from girder/improve-xyz-in-tile
Browse files Browse the repository at this point in the history
Move more repeated code into the base class
  • Loading branch information
manthey authored Feb 16, 2021
2 parents 81892f5 + 4004b9b commit 3c31cdc
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
5 changes: 4 additions & 1 deletion large_image/tilesource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ def getInternalMetadata(self, **kwargs):
"""
return None

def _xyzInRange(self, x, y, z):
def _xyzInRange(self, x, y, z, frame=None, numFrames=None):
"""
Check if a tile at x, y, z is in range based on self.levels,
self.tileWidth, self.tileHeight, self.sizeX, and self.sizeY, Raise an
Expand All @@ -1606,6 +1606,9 @@ def _xyzInRange(self, x, y, z):
offsety = y * self.tileHeight * scale
if not (0 <= offsety < self.sizeY):
raise exceptions.TileSourceException('y is outside layer')
if frame is not None and numFrames is not None:
if frame < 0 or frame >= numFrames:
raise exceptions.TileSourceException('Frame does not exist')

@methodcache()
def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False,
Expand Down
7 changes: 2 additions & 5 deletions sources/nd2/large_image_source_nd2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,13 @@ def getInternalMetadata(self, **kwargs):

@methodcache()
def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
self._xyzInRange(x, y, z)
frame = int(kwargs.get('frame') or 0)
self._xyzInRange(x, y, z, frame, len(self._nd2))
step = int(2 ** (self.levels - 1 - z))
x0 = x * step * self.tileWidth
x1 = min((x + 1) * step * self.tileWidth, self.sizeX)
y0 = y * step * self.tileHeight
y1 = min((y + 1) * step * self.tileHeight, self.sizeY)
frame = kwargs.get('frame')
frame = int(frame) if frame else 0
if frame < 0 or frame >= len(self._nd2):
raise TileSourceException('Frame does not exist')
with self._tileLock:
if frame in self._recentFrames:
tileframe = self._recentFrames[frame]
Expand Down
1 change: 1 addition & 0 deletions sources/ometiff/large_image_source_ometiff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(self, path, **kwargs):
:param path: a filesystem path for the tile source.
"""
# Note this is the super of the parent class, not of this class.
super(TiffFileTileSource, self).__init__(path, **kwargs)

largeImagePath = self._getLargeImagePath()
Expand Down
5 changes: 1 addition & 4 deletions sources/openslide/large_image_source_openslide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,7 @@ def getInternalMetadata(self, **kwargs):
@methodcache()
def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
self._xyzInRange(x, y, z)
try:
svslevel = self._svslevels[z]
except IndexError:
raise TileSourceException('z layer does not exist')
svslevel = self._svslevels[z]
# When we read a region from the SVS, we have to ask for it in the
# SVS level 0 coordinate system. Our x and y is in tile space at the
# specifed z level, so the offset in SVS level 0 coordinates has to be
Expand Down

0 comments on commit 3c31cdc

Please sign in to comment.