forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnonymization.mm
509 lines (403 loc) · 17.5 KB
/
Anonymization.mm
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
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#import "Anonymization.h"
#import "DCMAttributeTag.h"
#import "AnonymizationViewController.h"
#import "AnonymizationSavePanelController.h"
#import "NSFileManager+N2.h"
#import "NSDictionary+N2.h"
#import "DCMObject.h"
#import "DicomImage.h"
#import "DCMCalendarDate.h"
#import "DicomStudy.h"
#import "DicomSeries.h"
#import "BrowserController.h"
#import "AppController.h"
#import "Wait.h"
#import "DicomFile.h"
#import "DICOMToNSString.h"
#import "XMLController.h"
#import "DicomFileDCMTKCategory.h"
#import "XMLControllerDCMTKCategory.h"
#import "N2Debug.h"
static NSString *templateDicomFile = nil;
@interface AnonymizationPanelRepresentation : NSObject {
NSString* defaultsKey;
id representedObject;
id target;
SEL action;
}
@property(retain) NSString* defaultsKey;
@property(retain) id representedObject;
@property(retain) id target;
@property SEL action;
@end
@implementation AnonymizationPanelRepresentation
@synthesize defaultsKey, representedObject, target, action;
-(void)dealloc {
self.defaultsKey = NULL;
self.representedObject = NULL;
self.target = NULL;
self.action = NULL;
[super dealloc];
}
@end
@implementation Anonymization
+(DCMAttributeTag*)tagFromString:(NSString*)k {
static NSDictionary* oldKeys = [[NSDictionary alloc] initWithObjectsAndKeys:
@"PatientsName", @"Patient's Name",
@"PatientsSex", @"Patient's Sex",
@"PatientID", @"Patient's ID",
@"PatientsWeight", @"Patient's Weight",
@"PatientsAge", @"Patient's Age",
@"ClinicalTrialSponsorName", @"Trial Sponsor Name",
@"PatientsBirthDate", @"Patient's Date of Birth",
@"ClinicalTrialProtocolID", @"Trial Protocol ID",
@"InstitutionName", @"Institution Name",
@"ClinicalTrialProtocolName", @"Trial Protocol Name",
@"StudyID", @"Study ID",
@"ClinicalTrialSiteID", @"Trial Site ID",
@"StudyDate", @"Study Date",
@"ClinicalTrialSiteName", @"Trial Site Name",
@"StudyTime", @"Study Time",
@"ClinicalTrialSubjectReadingID", @"Trial Subject Reading ID",
@"AcquisitionDatetime", @"Aquisition Date/Time",
@"ClinicalTrialSubjectID", @"Trial Subject ID",
@"SeriesDate", @"Series Date",
@"ClinicalTrialTimePointID", @"Trial Time Point ID",
@"SeriesTime", @"Series Time",
@"ClinicalTrialTimePointDescription", @"Trial Time Point Description",
@"InstanceCreationDate", @"Image Date",
@"ClinicalTrialCoordinatingCenterName", @"Trial Coordinating Center Name",
@"InstanceCreationTime", @"Image Time",
@"PerformingPhysiciansName", @"Performing Physician",
@"ReferringPhysiciansName", @"Referring Physician",
@"PhysiciansofRecord", @"Physicians of Record",
@"AccessionNumber", @"AccessionNumber",
NULL];
// older versions of OsiriX stored anonymization descriptors using the spaced keys and linked those with the DICOM tags through tags in the xib views and code.
// here, through the oldKeys dictionary, we support these keys and directly translate them to standard dicom tag names.
NSString* k2 = [oldKeys objectForKey:k];
if (k2) k = k2;
DCMAttributeTag* tag = [DCMAttributeTag tagWithName:k];
if (!tag)
tag = [DCMAttributeTag tagWithTagString:k];
if (!tag)
NSLog(@"Warning: unrecognized DICOM attribute tag %@", k);
return tag;
}
+(NSArray*)tagsValuesArrayFromDictionary:(NSDictionary*)dic {
NSMutableArray* out = [[NSMutableArray alloc] initWithCapacity:dic.count];
for (NSString* k in dic) {
id v = [dic objectForKey:k];
DCMAttributeTag* tag = [self tagFromString:k];
if (!tag)
continue;
if ([v isKindOfClass:[NSNull class]])
v = NULL;
[out addObject:[NSArray arrayWithObjects: tag, v, NULL]]; // if v is null then array contains only 1 object
}
return [out autorelease];
}
+(NSDictionary*)tagsValuesDictionaryFromArray:(NSArray*)arr {
NSMutableDictionary* out = [[NSMutableDictionary alloc] initWithCapacity:arr.count];
for (NSArray* a in arr) {
DCMAttributeTag* tag = [a objectAtIndex:0];
id v = a.count>1? [a objectAtIndex:1] : @"";
NSString* k = tag.name;
if (!k) k = tag.stringValue;
[out setObject:v forKey:k];
}
return [out autorelease];
}
+(NSArray*)tagsArrayFromStringsArray:(NSArray*)strings {
NSMutableArray* out = [NSMutableArray arrayWithCapacity:strings.count];
for (NSString* s in strings) {
DCMAttributeTag* tag = [self tagFromString:s];
if (tag)
[out addObject:tag];
}
return [[out copy] autorelease];
}
+(NSArray*)stringArrayFromTagsArray:(NSArray*)tags {
NSMutableArray* out = [NSMutableArray arrayWithCapacity:tags.count];
for (DCMAttributeTag* tag in tags)
[out addObject:tag.stringValue];
return [[out copy] autorelease];
}
+(BOOL)tagsValues:(NSArray*)a1 isEqualTo:(NSArray*)a2 {
if (a1.count != a2.count)
return NO;
for (NSArray* a in a1) {
DCMAttributeTag* atag = [a objectAtIndex:0];
BOOL found = NO;
for (NSArray* b in a2) {
DCMAttributeTag* btag = [b objectAtIndex:0];
if ([atag isEqual:btag]) {
id aval = a.count>1? [a objectAtIndex:1] : @"";
id bval = b.count>1? [b objectAtIndex:1] : @"";
found = YES;
if (!(aval == bval || [aval isEqual:bval]))
return NO;
}
}
if (!found)
return NO;
}
return YES;
}
#pragma mark Panel
+(NSString*) templateDicomFile
{
return templateDicomFile;
}
+(id)showPanelClass:(Class)c forDefaultsKey:(NSString*)defaultsKey modalForWindow:(NSWindow*)window modalDelegate:(id)delegate didEndSelector:(SEL)sel representedObject:(id)representedObject {
@try
{
[templateDicomFile release];
templateDicomFile = nil;
templateDicomFile = [[[representedObject objectAtIndex: 0] objectAtIndex: [[representedObject objectAtIndex: 0] count]/2] retain];
}
@catch (NSException * e)
{
N2LogExceptionWithStackTrace(e);
}
NSArray* values = [Anonymization tagsValuesArrayFromDictionary:[[NSUserDefaultsController sharedUserDefaultsController] dictionaryForKey:defaultsKey]];
NSArray* tags = [self tagsArrayFromStringsArray:[[NSUserDefaultsController sharedUserDefaultsController] arrayForKey:[NSString stringWithFormat:@"%@All", defaultsKey]]];
AnonymizationPanelController* panelController = [[c alloc] initWithTags:tags values:values];
AnonymizationPanelRepresentation* ro = [[[AnonymizationPanelRepresentation alloc] init] autorelease];
ro.defaultsKey = defaultsKey;
ro.representedObject = representedObject;
ro.target = delegate;
ro.action = sel;
panelController.representedObject = ro;
[NSApp beginSheet:panelController.window modalForWindow:window modalDelegate:self didEndSelector:@selector(panelDidEnd:returnCode:contextInfo:) contextInfo:panelController];
[panelController.window orderFront:self];
if (!delegate)
[NSApp runModalForWindow:panelController.window];
return panelController;
}
+(AnonymizationPanelController*)showPanelForDefaultsKey:(NSString*)defaultsKey modalForWindow:(NSWindow*)window modalDelegate:(id)delegate didEndSelector:(SEL)sel representedObject:(id)representedObject {
return [self showPanelClass:[AnonymizationPanelController class] forDefaultsKey:defaultsKey modalForWindow:window modalDelegate:delegate didEndSelector:sel representedObject:representedObject];
}
+(AnonymizationSavePanelController*)showSavePanelForDefaultsKey:(NSString*)defaultsKey modalForWindow:(NSWindow*)window modalDelegate:(id)delegate didEndSelector:(SEL)sel representedObject:(id)representedObject {
return [self showPanelClass:[AnonymizationSavePanelController class] forDefaultsKey:defaultsKey modalForWindow:window modalDelegate:delegate didEndSelector:sel representedObject:representedObject];
}
+(void)panelDidEnd:(NSPanel*)panel returnCode:(NSInteger)returnCode contextInfo:(void*)contextInfo {
AnonymizationPanelController* panelController = (id)contextInfo;
AnonymizationPanelRepresentation* ro = panelController.representedObject;
if (panelController.end) { // save config
[[NSUserDefaults standardUserDefaults] setObject:[self stringArrayFromTagsArray:panelController.anonymizationViewController.tags] forKey:[NSString stringWithFormat:@"%@All", ro.defaultsKey]];
[[NSUserDefaults standardUserDefaults] setObject:[self tagsValuesDictionaryFromArray:panelController.anonymizationViewController.tagsValues] forKey:ro.defaultsKey];
}
[panel close];
[ro retain];
panelController.representedObject = ro.representedObject;
if (ro.target)
[ro.target performSelector:ro.action withObject:panelController];
else if (panelController.end)
[NSApp stopModal];
else [NSApp abortModal];
[ro release];
[panelController release];
}
#pragma mark Anonymization
+ (NSString*) cleanStringForFile: (NSString*) s
{
s = [s stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
s = [s stringByReplacingOccurrencesOfString:@":" withString:@"-"];
return s;
}
+ (void) error: (NSString*) s
{
NSRunCriticalAlertPanel( NSLocalizedString( @"Error", nil), s, NSLocalizedString( @"OK", nil), nil, nil);
}
+(NSDictionary*)anonymizeFiles:(NSArray*)files dicomImages: (NSArray*) dicomImages toPath:(NSString*)dirPath withTags:(NSArray*)intags
{
if( [files count] != [dicomImages count])
{
NSLog( @"***** anonymizeFiles [files count] != [dicomImages count]");
return nil;
}
NSMutableArray* tags = [NSMutableArray arrayWithCapacity:intags.count];
for (NSArray* intag in intags)
{
DCMAttributeTag* tag = [intag objectAtIndex:0];
id val = intag.count>1? [intag objectAtIndex:1] : NULL;
if ([val isKindOfClass:[NSDate class]])
{
if ([tag.vr isEqualToString:@"DA"]) //Date String
val = [DCMCalendarDate dicomDateWithDate:val];
else if ([tag.vr isEqualToString:@"TM"]) //Time String
val = [DCMCalendarDate dicomTimeWithDate:val];
else if ([tag.vr isEqualToString:@"DT"]) //Date Time
val = [DCMCalendarDate dicomDateTimeWithDicomDate:[DCMCalendarDate dicomDateWithDate:val] dicomTime:[DCMCalendarDate dicomTimeWithDate:val]];
}
else if ([val isKindOfClass:[NSNumber class]])
{
if ([tag.vr isEqualToString:@"DS"]) //Decimal String representing floating point
val = [val stringValue];
else if ([tag.vr isEqualToString:@"IS"]) //Integer String
val = [val stringValue];
}
[tags addObject:[NSArray arrayWithObjects: tag, val, NULL]];
}
NSMutableDictionary* filenameTranslation = [NSMutableDictionary dictionaryWithCapacity:files.count];
NSString* tempDirPath = [dirPath stringByAppendingPathComponent:@".temp"];
@try {
[[NSFileManager defaultManager] confirmDirectoryAtPath:tempDirPath];
}
@catch (NSException *exception) {
[self performSelectorOnMainThread: @selector( error:) withObject: exception.description waitUntilDone: NO];
return nil;
}
Wait *splash = nil;
if( [NSThread isMainThread])
{
splash = [[[Wait alloc] initWithString: NSLocalizedString( @"Processing...", nil)] autorelease];
[[splash progress] setMaxValue: [files count] * 2];
[splash showWindow: self];
[splash setCancel: YES];
}
NSMutableArray* producedFiles = [NSMutableArray arrayWithCapacity: files.count];
if( [[NSUserDefaults standardUserDefaults] boolForKey: @"useDCMTKForAnonymization"])
{
NSInteger fileIndex = 0;
for( NSString* filePath in files)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try
{
NSString* ext = [filePath pathExtension];
if (!ext.length) ext = @"dcm";
NSString* tempFileName = [NSString stringWithFormat:@"%d.%@", (int) fileIndex, ext];
NSString* tempFilePath = [tempDirPath stringByAppendingPathComponent:tempFileName];
[[NSFileManager defaultManager] copyItemAtPath: filePath toPath: tempFilePath byReplacingExisting: YES error: nil];
[filenameTranslation setObject:tempFilePath forKey:filePath];
++fileIndex;
[producedFiles addObject: tempFilePath];
[splash incrementBy: 1];
}
@catch (NSException * e)
{
N2LogExceptionWithStackTrace(e);
}
[pool release];
if( [splash aborted]) break;
}
// Prepare the parameters
NSMutableArray *params = [NSMutableArray arrayWithObjects:@"dcmodify", @"--ignore-errors", nil];
for( NSArray *intag in tags)
{
DCMAttributeTag* tag = [intag objectAtIndex:0];
id val = intag.count>1? [intag objectAtIndex:1] : NULL;
if( val == nil || [[val description] length] == 0)
[params addObjectsFromArray: [NSArray arrayWithObjects: @"-e", [NSString stringWithFormat: @"(%@)", tag.stringValue], nil]];
else
[params addObjectsFromArray: [NSArray arrayWithObjects: @"-i", [NSString stringWithFormat: @"(%@)=%@", tag.stringValue, [val description]], nil]];
}
[params addObjectsFromArray: producedFiles];
@try
{
NSStringEncoding encoding = [NSString encodingForDICOMCharacterSet: [[DicomFile getEncodingArrayForFile: [producedFiles lastObject]] objectAtIndex: 0]];
[XMLController modifyDicom: params encoding: encoding];
for( id loopItem in files)
[[NSFileManager defaultManager] removeFileAtPath: [loopItem stringByAppendingString:@".bak"] handler:nil];
}
@catch (NSException * e)
{
NSLog(@"**** DicomStudy setComment: %@", e);
}
}
else
{
NSInteger fileIndex = 0;
for( NSString* filePath in files)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try
{
NSString* ext = [filePath pathExtension];
if (!ext.length) ext = @"dcm";
NSString* tempFileName = [NSString stringWithFormat:@"%d.%@", (int) fileIndex, ext];
NSString* tempFilePath = [tempDirPath stringByAppendingPathComponent:tempFileName];
[DCMObject anonymizeContentsOfFile:filePath tags:tags writingToFile:tempFilePath];
[filenameTranslation setObject:tempFilePath forKey:filePath];
++fileIndex;
[producedFiles addObject: tempFilePath];
[splash incrementBy: 1];
}
@catch (NSException * e)
{
N2LogExceptionWithStackTrace(e);
}
[pool release];
if( [splash aborted]) break;
}
}
if( [producedFiles count] != [dicomImages count])
{
NSLog( @"***** anonymizeFiles [producedFiles count] != [dicomImages count]");
filenameTranslation = nil;
}
else
{
NSMutableArray* dicomSeries = [NSMutableArray array];
for (int i = 0; i < [dicomImages count]; i++)
{
DicomImage *image = [dicomImages objectAtIndex: i];
@try
{
if (![dicomSeries containsObject:image.series])
[dicomSeries addObject:image.series];
NSString* tempFilePath = [producedFiles objectAtIndex: i];
NSString* ext = [tempFilePath pathExtension];
NSString* fileDirPath = nil;
if( [image.series.study.patientID length] > 0)
fileDirPath = [dirPath stringByAppendingPathComponent: [NSString stringWithFormat: NSLocalizedString( @"Anonymized - %@", nil), [Anonymization cleanStringForFile:image.series.study.patientID]]];
else
fileDirPath = [dirPath stringByAppendingPathComponent: NSLocalizedString( @"Anonymized", nil)];
fileDirPath = [fileDirPath stringByAppendingPathComponent: [Anonymization cleanStringForFile: image.series.study.studyName]];
fileDirPath = [fileDirPath stringByAppendingPathComponent: [Anonymization cleanStringForFile: [NSString stringWithFormat:@"%@ - %@", image.series.name, image.series.id]]];
@try {
[[NSFileManager defaultManager] confirmDirectoryAtPath:fileDirPath];
}
@catch (NSException *exception) {
[self performSelectorOnMainThread: @selector( error:) withObject: exception.description waitUntilDone: NO];
break;
}
NSString* filePath;
NSInteger i = 0;
do
{
++i;
NSString* is = i ? [NSString stringWithFormat:@"-%4.4d", (int) i] : @"";
NSString* fileName = [NSString stringWithFormat:@"IM-%4.4d-%4.4d%@.%@", (int) dicomSeries.count, (int) [image.instanceNumber intValue], is, ext];
filePath = [fileDirPath stringByAppendingPathComponent:fileName];
} while ([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
[[NSFileManager defaultManager] moveItemAtPath:tempFilePath toPath:filePath error:NULL];
NSString* k = [filenameTranslation keyForObject:tempFilePath];
if (k) [filenameTranslation setObject:filePath forKey: k];
else NSLog(@"Warning: anonymization file naming error: unknown original for %@ which should have changed to %@", tempFilePath, filePath);
}
@catch (NSException * e)
{
N2LogExceptionWithStackTrace(e);
}
[splash incrementBy: 1];
}
if( tempDirPath)
[[NSFileManager defaultManager] removeItemAtPath:tempDirPath error:NULL];
}
[splash close];
return [[filenameTranslation copy] autorelease];
}
@end