Skip to content

Commit

Permalink
Open a jp2 file multiple tiles to allow multithreaded reads.
Browse files Browse the repository at this point in the history
  • Loading branch information
manthey committed Oct 8, 2019
1 parent bd3d6c6 commit 6357f70
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion server/tilesource/openjpeg.py
Original file line number Diff line number Diff line change
@@ -93,6 +93,7 @@ def __init__(self, path, **kwargs):
self._openjpeg = glymur.Jp2k(largeImagePath)
except glymur.jp2box.InvalidJp2kError:
raise TileSourceException('File cannot be opened via Glymur and OpenJPEG.')
self._openjpegHandles = [self._openjpeg]
try:
self.sizeY, self.sizeX = self._openjpeg.shape[:2]
except IndexError:
@@ -202,8 +203,17 @@ def getTile(self, x, y, z, pilImageAllowed=False, **kwargs):
raise TileSourceException('x is outside layer')
if y < 0 or y0 >= self.sizeY:
raise TileSourceException('y is outside layer')
# possible open the file multiple times so multiple threads can access
# it concurrently.
with self._openjpegLock:
tile = self._openjpeg[y0:y1:step, x0:x1:step]
if not len(self._openjpegHandles):
self._openjpegHandles.append(glymur.Jp2k(self._largeImagePath))
openjpegHandle = self._openjpegHandles.pop()
try:
tile = openjpegHandle[y0:y1:step, x0:x1:step]
finally:
with self._openjpegLock:
self._openjpegHandles.append(openjpegHandle)
mode = 'L'
if len(tile.shape) == 3:
mode = ['L', 'LA', 'RGB', 'RGBA'][tile.shape[2] - 1]

0 comments on commit 6357f70

Please sign in to comment.