forked from horosproject/horos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrowserControllerDCMTKCategory.mm
259 lines (199 loc) · 8.46 KB
/
BrowserControllerDCMTKCategory.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
/*=========================================================================
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.
---------------------------------------------------------------------------
This file is part of the Horos Project.
Current contributors to the project include Alex Bettarini and Danny Weissman.
Horos is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
Horos 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 Horos. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
This file is part of the Horos Project.
Current contributors to the project include Alex Bettarini and Danny Weissman.
Horos is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
Horos 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 Horos. If not, see <http://www.gnu.org/licenses/>.
=========================================================================*/
#import "BrowserControllerDCMTKCategory.h"
#import <OsiriX/DCMObject.h>
#import <OsiriX/DCM.h>
#import <OsiriX/DCMTransferSyntax.h>
#import <OsiriX/DCMAbstractSyntaxUID.h>
#import "AppController.h"
#import "DCMPix.h"
#import "WaitRendering.h"
#import "DicomDatabase+DCMTK.h"
#undef verify
#include "osconfig.h" /* make sure OS specific configuration is included first */
#include "djdecode.h" /* for dcmjpeg decoders */
#include "djencode.h" /* for dcmjpeg encoders */
#include "dcrledrg.h" /* for DcmRLEDecoderRegistration */
#include "dcrleerg.h" /* for DcmRLEEncoderRegistration */
#include "djrploss.h"
#include "djrplol.h"
#include "dcpixel.h"
#include "dcrlerp.h"
#include "dcdatset.h"
#include "dcmetinf.h"
#include "dcfilefo.h"
#include "dcdebug.h"
#include "dcuid.h"
#include "dcdict.h"
#include "dcdeftag.h"
extern NSRecursiveLock *PapyrusLock;
@implementation BrowserController (BrowserControllerDCMTKCategory)
+ (NSString*) compressionString: (NSString*) string
{
if( [string isEqualToString: @"1.2.840.10008.1.2"])
return NSLocalizedString( @"Uncompressed", nil);
if( [string isEqualToString: @"1.2.840.10008.1.2.1"])
return NSLocalizedString( @"Uncompressed", nil);
if( [string isEqualToString: @"1.2.840.10008.1.2.2"])
return NSLocalizedString( @"Uncompressed BigEndian", nil);
if( dcmFindNameOfUID( [string UTF8String]))
return [NSString stringWithFormat:@"%s", dcmFindNameOfUID( [string UTF8String])];
else
return NSLocalizedString( @"Unknown UID", nil);
}
#ifndef OSIRIX_LIGHT
static int uniqueID = 1;
static NSString *uniqueSync = @"uniqueSync";
- (NSData*) getDICOMFile:(NSString*) file inSyntax:(NSString*) syntax quality: (int) quality
{
OFCondition cond;
OFBool status = NO;
DcmFileFormat fileformat;
cond = fileformat.loadFile( [file UTF8String]);
if (cond.good())
{
DcmDataset *dataset = fileformat.getDataset();
DcmXfer filexfer( dataset->getOriginalXfer());
DcmXfer xfer( [syntax UTF8String]);
if( filexfer.getXfer() == xfer.getXfer())
return [NSData dataWithContentsOfFile: file];
if( filexfer.getXfer() == EXS_JPEG2000 && xfer.getXfer() == EXS_JPEG2000LosslessOnly)
return [NSData dataWithContentsOfFile: file];
if( filexfer.getXfer() == EXS_JPEG2000LosslessOnly && xfer.getXfer() == EXS_JPEG2000)
return [NSData dataWithContentsOfFile: file];
if( filexfer.getXfer() == EXS_JPEGLSLossy && xfer.getXfer() == EXS_JPEGLSLossless)
return [NSData dataWithContentsOfFile: file];
if( filexfer.getXfer() == EXS_JPEGLSLossless && xfer.getXfer() == EXS_JPEGLSLossy)
return [NSData dataWithContentsOfFile: file];
// ------
NSString *tmpWADOFile = nil;
@synchronized( uniqueSync)
{
tmpWADOFile = [NSString stringWithFormat: @"/tmp/wado-recompress-%d.dcm", uniqueID++];
}
[[NSFileManager defaultManager] removeItemAtPath: tmpWADOFile error: nil];
if( [[NSUserDefaults standardUserDefaults] boolForKey: @"useDCMTKForJP2K"])
{
// DcmItem *metaInfo = fileformat.getMetaInfo();
DcmRepresentationParameter *params = nil;
DJ_RPLossy lossyParams( 90);
DJ_RPLossy JP2KParams( quality);
DJ_RPLossy JP2KParamsLossLess( quality);
DcmRLERepresentationParameter rleParams;
DJ_RPLossless losslessParams(6,0);
if( xfer.getXfer() == EXS_JPEGProcess14SV1TransferSyntax)
params = &losslessParams;
else if( xfer.getXfer() == EXS_JPEGProcess2_4TransferSyntax)
params = &lossyParams;
else if( xfer.getXfer() == EXS_RLELossless)
params = &rleParams;
else if( xfer.getXfer() == EXS_JPEG2000LosslessOnly)
params = &JP2KParamsLossLess;
else if( xfer.getXfer() == EXS_JPEG2000)
params = &JP2KParams;
else if( xfer.getXfer() == EXS_JPEGLSLossless)
params = &JP2KParamsLossLess;
else if( xfer.getXfer() == EXS_JPEGLSLossy)
params = &JP2KParams;
// this causes the lossless JPEG version of the dataset to be created
dataset->chooseRepresentation( xfer.getXfer(), params);
// check if everything went well
if (dataset->canWriteXfer( xfer.getXfer()))
{
// force the meta-header UIDs to be re-generated when storing the file
// since the UIDs in the data set may have changed
//delete metaInfo->remove(DCM_MediaStorageSOPClassUID);
//delete metaInfo->remove(DCM_MediaStorageSOPInstanceUID);
fileformat.loadAllDataIntoMemory();
cond = fileformat.saveFile( [tmpWADOFile fileSystemRepresentation], xfer.getXfer());
status = (cond.good()) ? YES : NO;
if( status == NO)
NSLog( @"getDICOMFile:(NSString*) file inSyntax:(NSString*) syntax quality: (int) quality failed");
}
}
// else
// {
// DCMObject *dcmObject = nil;
// @try
// {
// dcmObject = [[DCMObject alloc] initWithContentsOfFile: file decodingPixelData: NO];
// status = [dcmObject writeToFile: tmpWADOFile withTransferSyntax: [[[DCMTransferSyntax alloc] initWithTS: syntax] autorelease] quality: quality AET:@"Horos" atomically:YES];
// }
// @catch (NSException *e)
// {
// NSLog( @"dcmObject writeToFile failed: %@", e);
// }
// [dcmObject release];
// }
// if( status == NO || [[NSFileManager defaultManager] fileExistsAtPath: tmpWADOFile] == NO)
// {
// DCMObject *dcmObject = nil;
// @try
// {
// dcmObject = [[DCMObject alloc] initWithContentsOfFile: file decodingPixelData: NO];
// status = [dcmObject writeToFile: tmpWADOFile withTransferSyntax: [DCMTransferSyntax ExplicitVRLittleEndianTransferSyntax] quality: quality AET:@"Horos" atomically:YES];
//
// }
// @catch (NSException *e)
// {
// NSLog( @"dcmObject writeToFile failed: %@", e);
// }
// [dcmObject release];
// }
NSData *data = [NSData dataWithContentsOfFile: tmpWADOFile];
[[NSFileManager defaultManager] removeItemAtPath: tmpWADOFile error: nil];
if( data == nil)
data = [NSData dataWithContentsOfFile: file]; // Original file
return data;
}
return nil;
}
-(BOOL)needToCompressFile:(NSString*)path { // __deprecated
return [DicomDatabase fileNeedsDecompression:path];
}
-(BOOL)compressDICOMWithJPEG:(NSArray*)paths { // __deprecated
return [_database compressFilesAtPaths:paths];
}
-(BOOL)compressDICOMWithJPEG:(NSArray*)paths to:(NSString*)dest { // __deprecated
return [_database compressFilesAtPaths:paths intoDirAtPath:dest];
}
-(BOOL)decompressDICOMList:(NSArray*)files to:(NSString*)dest { // __deprecated
return [_database decompressFilesAtPaths:files intoDirAtPath:dest];
}
-(BOOL)testFiles:(NSArray*)files {
return [DicomDatabase testFiles:files];
}
#endif
@end