Skip to content

Commit

Permalink
Unify code to check if a tile exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
manthey committed Jul 3, 2020
1 parent 2e9f2a4 commit ecfccf8
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 33 deletions.
16 changes: 16 additions & 0 deletions large_image/tilesource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,22 @@ def getInternalMetadata(self, **kwargs):
"""
return None

def _xyzInRange(self, x, y, z):
"""
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
exception if not.
"""
if z < 0 or z >= self.levels:
raise exceptions.TileSourceException('z layer does not exist')
scale = 2 ** (self.levels - 1 - z)
offsetx = x * self.tileWidth * scale
if not (0 <= offsetx < self.sizeX):
raise exceptions.TileSourceException('x is outside layer')
offsety = y * self.tileHeight * scale
if not (0 <= offsety < self.sizeY):
raise exceptions.TileSourceException('y is outside layer')

@methodcache()
def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False,
sparseFallback=False, frame=None):
Expand Down
7 changes: 1 addition & 6 deletions sources/gdal/large_image_source_gdal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,17 +645,12 @@ def _bandNumber(self, band, exc=True):
@methodcache()
def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
if not self.projection:
if z < 0 or z >= self.levels:
raise TileSourceException('z layer does not exist')
self._xyzInRange(x, y, z)
factor = int(2 ** (self.levels - 1 - z))
x0 = int(x * factor * self.tileWidth)
y0 = int(y * factor * self.tileHeight)
x1 = int(min(x0 + factor * self.tileWidth, self.sourceSizeX))
y1 = int(min(y0 + factor * self.tileHeight, self.sourceSizeY))
if x < 0 or x0 >= self.sizeX:
raise TileSourceException('x is outside layer')
if y < 0 or y0 >= self.sizeY:
raise TileSourceException('y is outside layer')
w = int(max(1, round((x1 - x0) / factor)))
h = int(max(1, round((y1 - y0) / factor)))
with self._getDatasetLock:
Expand Down
7 changes: 1 addition & 6 deletions sources/nd2/large_image_source_nd2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,12 @@ def getInternalMetadata(self, **kwargs):

@methodcache()
def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
if z < 0 or z >= self.levels:
raise TileSourceException('z layer does not exist')
self._xyzInRange(x, y, z)
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)
if x < 0 or x0 >= self.sizeX:
raise TileSourceException('x is outside layer')
if y < 0 or y0 >= self.sizeY:
raise TileSourceException('y is outside layer')
frame = kwargs.get('frame')
frame = int(frame) if frame else 0
if frame < 0 or frame >= len(self._nd2):
Expand Down
7 changes: 1 addition & 6 deletions sources/openjpeg/large_image_source_openjpeg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,12 @@ def getInternalMetadata(self, **kwargs):

@methodcache()
def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
if z < 0 or z >= self.levels:
raise TileSourceException('z layer does not exist')
self._xyzInRange(x, y, z)
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)
if x < 0 or x0 >= self.sizeX:
raise TileSourceException('x is outside layer')
if y < 0 or y0 >= self.sizeY:
raise TileSourceException('y is outside layer')
scale = None
if z < self._minlevel:
scale = int(2 ** (self._minlevel - z))
Expand Down
7 changes: 1 addition & 6 deletions sources/openslide/large_image_source_openslide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ def getInternalMetadata(self, **kwargs):

@methodcache()
def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
if z < 0:
raise TileSourceException('z layer does not exist')
self._xyzInRange(x, y, z)
try:
svslevel = self._svslevels[z]
except IndexError:
Expand All @@ -271,11 +270,7 @@ def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
# scaled by the tile size and by the z level.
scale = 2 ** (self.levels - 1 - z)
offsetx = x * self.tileWidth * scale
if not (0 <= offsetx < self.sizeX):
raise TileSourceException('x is outside layer')
offsety = y * self.tileHeight * scale
if not (0 <= offsety < self.sizeY):
raise TileSourceException('y is outside layer')
# We ask to read an area that will cover the tile at the z level. The
# scale we computed in the __init__ process for this svs level tells
# how much larger a region we need to read.
Expand Down
10 changes: 1 addition & 9 deletions sources/tiff/large_image_source_tiff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,7 @@ def getInternalMetadata(self, **kwargs):
@methodcache()
def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False,
sparseFallback=False, **kwargs):
if z < 0:
raise TileSourceException('z layer does not exist')
scale = 2 ** (self.levels - 1 - z)
offsetx = x * self.tileWidth * scale
if not (0 <= offsetx < self.sizeX):
raise TileSourceException('x is outside layer')
offsety = y * self.tileHeight * scale
if not (0 <= offsety < self.sizeY):
raise TileSourceException('y is outside layer')
self._xyzInRange(x, y, z)
try:
allowStyle = True
if self._tiffDirectories[z] is None:
Expand Down

0 comments on commit ecfccf8

Please sign in to comment.