diff --git a/large_image/tilesource/base.py b/large_image/tilesource/base.py index e4529d276..77a746eb5 100644 --- a/large_image/tilesource/base.py +++ b/large_image/tilesource/base.py @@ -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): diff --git a/sources/gdal/large_image_source_gdal/__init__.py b/sources/gdal/large_image_source_gdal/__init__.py index 1112db8b7..5f1342498 100644 --- a/sources/gdal/large_image_source_gdal/__init__.py +++ b/sources/gdal/large_image_source_gdal/__init__.py @@ -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: diff --git a/sources/nd2/large_image_source_nd2/__init__.py b/sources/nd2/large_image_source_nd2/__init__.py index 1aa81d7f9..5f0146fd1 100644 --- a/sources/nd2/large_image_source_nd2/__init__.py +++ b/sources/nd2/large_image_source_nd2/__init__.py @@ -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): diff --git a/sources/openjpeg/large_image_source_openjpeg/__init__.py b/sources/openjpeg/large_image_source_openjpeg/__init__.py index b4b3eeb9c..0f7c54894 100644 --- a/sources/openjpeg/large_image_source_openjpeg/__init__.py +++ b/sources/openjpeg/large_image_source_openjpeg/__init__.py @@ -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)) diff --git a/sources/openslide/large_image_source_openslide/__init__.py b/sources/openslide/large_image_source_openslide/__init__.py index 7d82c9767..e61f37367 100644 --- a/sources/openslide/large_image_source_openslide/__init__.py +++ b/sources/openslide/large_image_source_openslide/__init__.py @@ -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: @@ -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. diff --git a/sources/tiff/large_image_source_tiff/__init__.py b/sources/tiff/large_image_source_tiff/__init__.py index d9bf16bc6..a61c37cd9 100644 --- a/sources/tiff/large_image_source_tiff/__init__.py +++ b/sources/tiff/large_image_source_tiff/__init__.py @@ -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: