Skip to content

Commit

Permalink
Autodetect NDPI in TIFF parser so we don't choke on SVS files
Browse files Browse the repository at this point in the history
  • Loading branch information
bgilbert committed Aug 22, 2014
1 parent 3e9159e commit 9a15322
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions anonymize-slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class UnrecognizedFile(Exception):


class TiffFile(file):
def __init__(self, path, ndpi=False):
def __init__(self, path):
file.__init__(self, path, 'r+b')

# Check header, decide endianness
Expand All @@ -74,7 +74,7 @@ def __init__(self, path, ndpi=False):
self._ndpi = False
version = self.read_fmt('H')
if version == 42:
self._ndpi = ndpi
pass
elif version == 43:
self._bigtiff = True
magic2, reserved = self.read_fmt('HH')
Expand All @@ -93,6 +93,14 @@ def __init__(self, path, ndpi=False):
self.seek(directory_offset)
directory = TiffDirectory(self, len(self.directories),
in_pointer_offset)
if not self.directories and not self._bigtiff:
# Check for NDPI. Because we don't know we have an NDPI file
# until after reading the first directory, we will choke if
# the first directory is beyond 4 GB.
if NDPI_MAGIC in directory.entries:
if DEBUG:
print 'Enabling NDPI mode.'
self._ndpi = True
self.directories.append(directory)
if not self.directories:
raise IOError('No directories')
Expand Down Expand Up @@ -246,7 +254,7 @@ def do_aperio_svs(filename):


def do_hamamatsu_ndpi(filename):
with TiffFile(filename, ndpi=True) as fh:
with TiffFile(filename) as fh:
# Check for NDPI file
if NDPI_MAGIC not in fh.directories[0].entries:
raise UnrecognizedFile
Expand All @@ -262,8 +270,8 @@ def do_hamamatsu_ndpi(filename):


format_handlers = [
do_hamamatsu_ndpi,
do_aperio_svs,
do_hamamatsu_ndpi,
]


Expand Down

0 comments on commit 9a15322

Please sign in to comment.