-
Notifications
You must be signed in to change notification settings - Fork 45
/
anonymize-slide.py
executable file
·563 lines (487 loc) · 18.2 KB
/
anonymize-slide.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/usr/bin/python
#
# anonymize-slide.py - Delete the label from a whole-slide image.
#
# Copyright (c) 2007-2013 Carnegie Mellon University
# Copyright (c) 2011 Google, Inc.
# Copyright (c) 2014 Benjamin Gilbert
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA.
#
from configparser import RawConfigParser
from io import StringIO
from glob import glob
from optparse import OptionParser
import os
import string
import struct
import sys
PROG_DESCRIPTION = '''
Delete the slide label from an MRXS, NDPI, or SVS whole-slide image.
'''.strip()
PROG_VERSION = '1.1.1'
DEBUG = False
# TIFF types
ASCII = 2
SHORT = 3
LONG = 4
FLOAT = 11
DOUBLE = 12
LONG8 = 16
# TIFF tags
IMAGE_DESCRIPTION = 270
STRIP_OFFSETS = 273
STRIP_BYTE_COUNTS = 279
NDPI_MAGIC = 65420
NDPI_SOURCELENS = 65421
# Format headers
LZW_CLEARCODE = '\x80'
JPEG_SOI = '\xff\xd8'
UTF8_BOM = '\xef\xbb\xbf'
# MRXS
MRXS_HIERARCHICAL = 'HIERARCHICAL'
MRXS_NONHIER_ROOT_OFFSET = 41
class UnrecognizedFile(Exception):
pass
class TiffFile(file):
def __init__(self, path):
file.__init__(self, path, 'r+b')
# Check header, decide endianness
endian = self.read(2)
if endian == 'II':
self._fmt_prefix = '<'
elif endian == 'MM':
self._fmt_prefix = '>'
else:
raise UnrecognizedFile
# Check TIFF version
self._bigtiff = False
self._ndpi = False
version = self.read_fmt('H')
if version == 42:
pass
elif version == 43:
self._bigtiff = True
magic2, reserved = self.read_fmt('HH')
if magic2 != 8 or reserved != 0:
raise UnrecognizedFile
else:
raise UnrecognizedFile
# Read directories
self.directories = []
while True:
in_pointer_offset = self.tell()
directory_offset = self.read_fmt('D')
if directory_offset == 0:
break
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')
def _convert_format(self, fmt):
# Format strings can have special characters:
# y: 16-bit signed on little TIFF, 64-bit signed on BigTIFF
# Y: 16-bit unsigned on little TIFF, 64-bit unsigned on BigTIFF
# z: 32-bit signed on little TIFF, 64-bit signed on BigTIFF
# Z: 32-bit unsigned on little TIFF, 64-bit unsigned on BigTIFF
# D: 32-bit unsigned on little TIFF, 64-bit unsigned on BigTIFF/NDPI
if self._bigtiff:
fmt = fmt.translate(string.maketrans('yYzZD', 'qQqQQ'))
elif self._ndpi:
fmt = fmt.translate(string.maketrans('yYzZD', 'hHiIQ'))
else:
fmt = fmt.translate(string.maketrans('yYzZD', 'hHiII'))
return self._fmt_prefix + fmt
def fmt_size(self, fmt):
return struct.calcsize(self._convert_format(fmt))
def near_pointer(self, base, offset):
# If NDPI, return the value whose low-order 32-bits are equal to
# @offset and which is within 4 GB of @base and below it.
# Otherwise, return offset.
if self._ndpi and offset < base:
seg_size = 1 << 32
offset += ((base - offset) // seg_size) * seg_size
return offset
def read_fmt(self, fmt, force_list=False):
fmt = self._convert_format(fmt)
vals = struct.unpack(fmt, self.read(struct.calcsize(fmt)))
if len(vals) == 1 and not force_list:
return vals[0]
else:
return vals
def write_fmt(self, fmt, *args):
fmt = self._convert_format(fmt)
self.write(struct.pack(fmt, *args))
class TiffDirectory(object):
def __init__(self, fh, number, in_pointer_offset):
self.entries = {}
count = fh.read_fmt('Y')
for _ in range(count):
entry = TiffEntry(fh)
self.entries[entry.tag] = entry
self._in_pointer_offset = in_pointer_offset
self._out_pointer_offset = fh.tell()
self._fh = fh
self._number = number
def delete(self, expected_prefix=None):
# Get strip offsets/lengths
try:
offsets = self.entries[STRIP_OFFSETS].value()
lengths = self.entries[STRIP_BYTE_COUNTS].value()
except KeyError:
raise IOError('Directory is not stripped')
# Wipe strips
for offset, length in zip(offsets, lengths):
offset = self._fh.near_pointer(self._out_pointer_offset, offset)
if DEBUG:
print('Zeroing', offset, 'for', length)
self._fh.seek(offset)
if expected_prefix:
buf = self._fh.read(len(expected_prefix))
if buf != expected_prefix:
raise IOError('Unexpected data in image strip')
self._fh.seek(offset)
self._fh.write('\0' * length)
# Remove directory
if DEBUG:
print('Deleting directory', self._number)
self._fh.seek(self._out_pointer_offset)
out_pointer = self._fh.read_fmt('D')
self._fh.seek(self._in_pointer_offset)
self._fh.write_fmt('D', out_pointer)
class TiffEntry(object):
def __init__(self, fh):
self.start = fh.tell()
self.tag, self.type, self.count, self.value_offset = \
fh.read_fmt('HHZZ')
self._fh = fh
def value(self):
if self.type == ASCII:
item_fmt = 'c'
elif self.type == SHORT:
item_fmt = 'H'
elif self.type == LONG:
item_fmt = 'I'
elif self.type == LONG8:
item_fmt = 'Q'
elif self.type == FLOAT:
item_fmt = 'f'
elif self.type == DOUBLE:
item_fmt = 'd'
else:
raise ValueError('Unsupported type')
fmt = '%d%s' % (self.count, item_fmt)
len = self._fh.fmt_size(fmt)
if len <= self._fh.fmt_size('Z'):
# Inline value
self._fh.seek(self.start + self._fh.fmt_size('HHZ'))
else:
# Out-of-line value
self._fh.seek(self._fh.near_pointer(self.start, self.value_offset))
items = self._fh.read_fmt(fmt, force_list=True)
if self.type == ASCII:
if items[-1] != '\0':
raise ValueError('String not null-terminated')
return ''.join(items[:-1])
else:
return items
class MrxsFile(object):
def __init__(self, filename):
# Split filename
dirname, ext = os.path.splitext(filename)
if ext != '.mrxs':
raise UnrecognizedFile
# Parse slidedat
self._slidedatfile = os.path.join(dirname, 'Slidedat.ini')
self._dat = RawConfigParser()
self._dat.optionxform = str
try:
with open(self._slidedatfile, 'rb') as fh:
self._have_bom = (fh.read(len(UTF8_BOM)) == UTF8_BOM)
if not self._have_bom:
fh.seek(0)
self._dat.readfp(fh)
except IOError:
raise UnrecognizedFile
# Get file paths
self._indexfile = os.path.join(dirname,
self._dat.get(MRXS_HIERARCHICAL, 'INDEXFILE'))
self._datafiles = [os.path.join(dirname,
self._dat.get('DATAFILE', 'FILE_%d' % i))
for i in range(self._dat.getint('DATAFILE', 'FILE_COUNT'))]
# Build levels
self._make_levels()
def _make_levels(self):
self._levels = {}
self._level_list = []
layer_count = self._dat.getint(MRXS_HIERARCHICAL, 'NONHIER_COUNT')
for layer_id in range(layer_count):
level_count = self._dat.getint(MRXS_HIERARCHICAL,
'NONHIER_%d_COUNT' % layer_id)
for level_id in range(level_count):
level = MrxsNonHierLevel(self._dat, layer_id, level_id,
len(self._level_list))
self._levels[(level.layer_name, level.name)] = level
self._level_list.append(level)
@classmethod
def _read_int32(cls, f):
buf = f.read(4)
if len(buf) != 4:
raise IOError('Short read')
return struct.unpack('<i', buf)[0]
@classmethod
def _assert_int32(cls, f, value):
v = cls._read_int32(f)
if v != value:
raise ValueError('%d != %d' % (v, value))
def _get_data_location(self, record):
with open(self._indexfile, 'rb') as fh:
fh.seek(MRXS_NONHIER_ROOT_OFFSET)
# seek to record
table_base = self._read_int32(fh)
fh.seek(table_base + record * 4)
# seek to list head
list_head = self._read_int32(fh)
fh.seek(list_head)
# seek to data page
self._assert_int32(fh, 0)
page = self._read_int32(fh)
fh.seek(page)
# check pagesize
self._assert_int32(fh, 1)
# read rest of prologue
self._read_int32(fh)
self._assert_int32(fh, 0)
self._assert_int32(fh, 0)
# read values
position = self._read_int32(fh)
size = self._read_int32(fh)
fileno = self._read_int32(fh)
return (self._datafiles[fileno], position, size)
def _zero_record(self, record):
path, offset, length = self._get_data_location(record)
with open(path, 'r+b') as fh:
fh.seek(0, 2)
do_truncate = (fh.tell() == offset + length)
if DEBUG:
if do_truncate:
print('Truncating', path, 'to', offset)
else:
print('Zeroing', path, 'at', offset, 'for', length)
fh.seek(offset)
buf = fh.read(len(JPEG_SOI))
if buf != JPEG_SOI:
raise IOError('Unexpected data in nonhier image')
if do_truncate:
fh.truncate(offset)
else:
fh.seek(offset)
fh.write('\0' * length)
def _delete_index_record(self, record):
if DEBUG:
print('Deleting record', record)
with open(self._indexfile, 'r+b') as fh:
entries_to_move = len(self._level_list) - record - 1
if entries_to_move == 0:
return
# get base of table
fh.seek(MRXS_NONHIER_ROOT_OFFSET)
table_base = self._read_int32(fh)
# read tail of table
fh.seek(table_base + (record + 1) * 4)
buf = fh.read(entries_to_move * 4)
if len(buf) != entries_to_move * 4:
raise IOError('Short read')
# overwrite the target record
fh.seek(table_base + record * 4)
fh.write(buf)
def _hier_keys_for_level(self, level):
ret = []
for k, _ in self._dat.items(MRXS_HIERARCHICAL):
if k == level.key_prefix or k.startswith(level.key_prefix + '_'):
ret.append(k)
return ret
def _rename_section(self, old, new):
if self._dat.has_section(old):
if DEBUG:
print('[%s] -> [%s]' % (old, new))
self._dat.add_section(new)
for k, v in self._dat.items(old):
self._dat.set(new, k, v)
self._dat.remove_section(old)
elif DEBUG:
print('[%s] does not exist' % old)
def _delete_section(self, section):
if DEBUG:
print('Deleting [%s]' % section)
self._dat.remove_section(section)
def _set_key(self, section, key, value):
if DEBUG:
prev = self._dat.get(section, key)
print('[%s] %s: %s -> %s' % (section, key, prev, value))
self._dat.set(section, key, value)
def _rename_key(self, section, old, new):
if DEBUG:
print('[%s] %s -> %s' % (section, old, new))
v = self._dat.get(section, old)
self._dat.remove_option(section, old)
self._dat.set(section, new, v)
def _delete_key(self, section, key):
if DEBUG:
print('Deleting [%s] %s' % (section, key))
self._dat.remove_option(section, key)
def _write(self):
buf = StringIO()
self._dat.write(buf)
with open(self._slidedatfile, 'wb') as fh:
if self._have_bom:
fh.write(UTF8_BOM)
fh.write(buf.getvalue().replace('\n', '\r\n'))
def delete_level(self, layer_name, level_name):
level = self._levels[(layer_name, level_name)]
record = level.record
# Zero image data
self._zero_record(record)
# Delete pointer from nonhier table in index
self._delete_index_record(record)
# Remove slidedat keys
for k in self._hier_keys_for_level(level):
self._delete_key(MRXS_HIERARCHICAL, k)
# Remove slidedat section
self._delete_section(level.section)
# Rename section and keys for subsequent levels in the layer
prev_level = level
for cur_level in self._level_list[record + 1:]:
if cur_level.layer_id != prev_level.layer_id:
break
for k in self._hier_keys_for_level(cur_level):
new_k = k.replace(cur_level.key_prefix,
prev_level.key_prefix, 1)
self._rename_key(MRXS_HIERARCHICAL, k, new_k)
self._set_key(MRXS_HIERARCHICAL, prev_level.section_key,
prev_level.section)
self._rename_section(cur_level.section, prev_level.section)
prev_level = cur_level
# Update level count within layer
count_k = 'NONHIER_%d_COUNT' % level.layer_id
count_v = self._dat.getint(MRXS_HIERARCHICAL, count_k)
self._set_key(MRXS_HIERARCHICAL, count_k, count_v - 1)
# Write slidedat
self._write()
# Refresh metadata
self._make_levels()
class MrxsNonHierLevel(object):
def __init__(self, dat, layer_id, level_id, record):
self.layer_id = layer_id
self.id = level_id
self.record = record
self.layer_name = dat.get(MRXS_HIERARCHICAL,
'NONHIER_%d_NAME' % layer_id)
self.key_prefix = 'NONHIER_%d_VAL_%d' % (layer_id, level_id)
self.name = dat.get(MRXS_HIERARCHICAL, self.key_prefix)
self.section_key = self.key_prefix + '_SECTION'
self.section = dat.get(MRXS_HIERARCHICAL, self.section_key)
def accept(filename, format):
if DEBUG:
print(filename + ':', format)
def do_aperio_svs(filename):
with TiffFile(filename) as fh:
# Check for SVS file
try:
desc0 = fh.directories[0].entries[IMAGE_DESCRIPTION].value()
if not desc0.startswith('Aperio'):
raise UnrecognizedFile
except KeyError:
raise UnrecognizedFile
accept(filename, 'SVS')
# Find and delete label
for directory in fh.directories:
lines = directory.entries[IMAGE_DESCRIPTION].value().splitlines()
if len(lines) >= 2 and lines[1].startswith('label '):
directory.delete(expected_prefix=LZW_CLEARCODE)
break
else:
raise IOError("No label in SVS file")
def do_hamamatsu_ndpi(filename):
with TiffFile(filename) as fh:
# Check for NDPI file
if NDPI_MAGIC not in fh.directories[0].entries:
raise UnrecognizedFile
accept(filename, 'NDPI')
# Find and delete macro image
for directory in fh.directories:
if directory.entries[NDPI_SOURCELENS].value()[0] == -1:
directory.delete(expected_prefix=JPEG_SOI)
break
else:
raise IOError("No label in NDPI file")
def do_3dhistech_mrxs(filename):
mrxs = MrxsFile(filename)
try:
mrxs.delete_level('Scan data layer', 'ScanDataLayer_SlideBarcode')
except KeyError:
raise IOError('No label in MRXS file')
format_handlers = [
do_aperio_svs,
do_hamamatsu_ndpi,
do_3dhistech_mrxs,
]
def _main():
global DEBUG
parser = OptionParser(usage='%prog [options] file [file...]',
description=PROG_DESCRIPTION, version=PROG_VERSION)
parser.add_option('-d', '--debug', action='store_true',
help='show debugging information')
opts, args = parser.parse_args()
if not args:
parser.error('specify a file')
DEBUG = opts.debug
if sys.platform == 'win32':
# The shell expects us to do wildcard expansion
filenames = []
for arg in args:
filenames.extend(glob(arg) or [arg])
else:
filenames = args
exit_code = 0
for filename in filenames:
try:
for handler in format_handlers:
try:
handler(filename)
break
except UnrecognizedFile:
pass
else:
raise IOError('Unrecognized file type')
except Exception as e:
if DEBUG:
raise
sys.stderr.write('%s: %s %s' % (filename, str(e), os.linesep))
exit_code = 1
sys.exit(exit_code)
if __name__ == '__main__':
_main()