forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecompress.mm
1029 lines (864 loc) · 43.7 KB
/
Decompress.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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import <Foundation/Foundation.h>
//#import <OsiriX/DCMObject.h>
//#import <OsiriX/DCM.h>
//#import <OsiriX/DCMTransferSyntax.h>
//#import <OsiriX/DCMPixelDataAttribute.h>
//#import <OsiriX/DCMAbstractSyntaxUID.h>
#import "DefaultsOsiriX.h"
#import "AppController.h"
#import "QTKit/QTMovie.h"
#import "DCMPix.h"
#import <WebKit/WebKit.h>
#include <mingpp.h>
#import "N2Debug.h"
#import <Quartz/Quartz.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 "dcdicdir.h"
#include "dcdatset.h"
#include "dcmetinf.h"
#include "dcfilefo.h"
#include "dcdebug.h"
#include "dcuid.h"
#include "dcdict.h"
#include "dcdeftag.h"
#include "Binaries/dcmtk-source/dcmjpls/djdecode.h" //JPEG-LS
#include "Binaries/dcmtk-source/dcmjpls/djencode.h" //JPEG-LS
extern "C"
{
extern short Papy3Init ();
void exitOsiriX(void)
{
[NSException raise: @"JPEG error exception raised" format: @"JPEG error exception raised - See Console.app for error message"];
}
}
enum DCM_CompressionQuality {DCMLosslessQuality = 0, DCMHighQuality, DCMMediumQuality, DCMLowQuality};
NSLock *PapyrusLock = 0L;
NSThread *mainThread = 0L;
BOOL NEEDTOREBUILD = NO;
NSMutableDictionary *DATABASECOLUMNS = 0L;
short Altivec = 0;
short UseOpenJpeg = 1, Use_kdu_IfAvailable = 1;
extern void dcmtkSetJPEGColorSpace( int);
/*
void myunlink(const char * path) {
NSLog(@"Unlinking %s", path);
unlink(path);
NSLog(@"... Unlinked %s", path);
}
*/
#define myunlink unlink
// WHY THIS EXTERNAL APPLICATION FOR COMPRESS OR DECOMPRESSION?
// Because if a file is corrupted, it will not crash the OsiriX application, but only this small task.
// Always modify this function in sync with compressionForModality in Decompress.mm / BrowserController.m
int compressionForModality( NSArray *array, NSArray *arrayLow, int limit, NSString* mod, int* quality, int resolution)
{
NSArray *s;
if( resolution < limit)
s = arrayLow;
else
s = array;
if( [mod isEqualToString: @"SR"]) // No compression for DICOM SR
return compression_none;
for( NSDictionary *dict in s)
{
if( [mod rangeOfString: [dict valueForKey: @"modality"]].location != NSNotFound)
{
int compression = compression_none;
if( [[dict valueForKey: @"compression"] intValue] == compression_sameAsDefault)
dict = [s objectAtIndex: 0];
compression = [[dict valueForKey: @"compression"] intValue];
if( quality)
{
if( compression == compression_JPEG2000 || compression == compression_JPEGLS)
*quality = [[dict valueForKey: @"quality"] intValue];
else
*quality = 0;
}
return compression;
}
}
if( [s count] == 0)
return compression_none;
if( quality)
*quality = [[[s objectAtIndex: 0] valueForKey: @"quality"] intValue];
return [[[s objectAtIndex: 0] valueForKey: @"compression"] intValue];
}
void createSwfMovie(NSArray* inputFiles, NSString* path, float frameRate);
int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// To avoid:
// http://lists.apple.com/archives/quicktime-api/2007/Aug/msg00008.html
// _NXCreateWindow: error setting window property (1002)
// _NXTermWindow: error releasing window (1002)
[NSApplication sharedApplication];
// argv[ 1] : in path
// argv[ 2] : what
if( argv[ 1] && argv[ 2])
{
// register global JPEG decompression codecs
DJDecoderRegistration::registerCodecs();
DJLSDecoderRegistration::registerCodecs();
// register global JPEG compression codecs
DJEncoderRegistration::registerCodecs(
ECC_lossyRGB,
EUC_never,
OFFalse,
OFFalse,
0,
0,
0,
OFTrue,
ESS_444,
OFFalse,
OFFalse,
0,
0,
0.0,
0.0,
0,
0,
0,
0,
OFTrue,
OFTrue,
OFFalse,
OFFalse,
OFTrue);
DJLSEncoderRegistration::registerCodecs();
// register RLE compression codec
DcmRLEEncoderRegistration::registerCodecs();
// register RLE decompression codec
DcmRLEDecoderRegistration::registerCodecs();
NSString *path = [NSString stringWithUTF8String:argv[1]];
NSString *what = [NSString stringWithUTF8String:argv[2]];
NSInteger fileListFirstItemIndex = 3;
NSMutableDictionary* dict = [DefaultsOsiriX getDefaults];
[dict addEntriesFromDictionary: [[NSUserDefaults standardUserDefaults] persistentDomainForName:@"com.rossetantoine.osirix"]];
if ([what isEqualToString:@"SettingsPlist"])
{
@try
{
[dict addEntriesFromDictionary:[NSMutableDictionary dictionaryWithContentsOfFile:[NSString stringWithUTF8String:argv[fileListFirstItemIndex]]]];
what = [NSString stringWithUTF8String:argv[4]];
fileListFirstItemIndex += 2;
}
@catch (NSException* e)
{ // ignore evtl failures
NSLog(@"Decompress failed reading settings plist at %s: %@", argv[fileListFirstItemIndex], e);
}
}
dcmtkSetJPEGColorSpace( [[dict objectForKey:@"UseJPEGColorSpace"] intValue]);
// BOOL useDCMTKForJP2K = [[dict objectForKey:@"useDCMTKForJP2K"] intValue];
#pragma mark compress
if( [what isEqualToString:@"compress"])
{
UseOpenJpeg = [[dict objectForKey:@"UseOpenJpegForJPEG2000"] intValue];
Use_kdu_IfAvailable = [[dict objectForKey:@"UseKDUForJPEG2000"] intValue];
NSArray *compressionSettings = [dict valueForKey: @"CompressionSettings"];
NSArray *compressionSettingsLowRes = [dict valueForKey: @"CompressionSettingsLowRes"];
int limit = [[dict objectForKey: @"CompressionResolutionLimit"] intValue];
NSString *destDirec;
if( [path isEqualToString: @"sameAsDestination"])
destDirec = nil;
else
destDirec = path;
int i;
for( i = fileListFirstItemIndex; i < argc; i++)
{
NSString *curFile = [NSString stringWithUTF8String:argv[ i]];
OFBool status = YES;
NSString *curFileDest;
if( destDirec)
curFileDest = [destDirec stringByAppendingPathComponent: [curFile lastPathComponent]];
else
curFileDest = [curFile stringByAppendingString: @" temp"];
if( [[curFile pathExtension] isEqualToString: @"zip"] || [[curFile pathExtension] isEqualToString: @"osirixzip"])
{
NSString *tempCurFileDest = [[curFileDest stringByDeletingLastPathComponent] stringByAppendingPathComponent: [NSString stringWithFormat: @".%@", [curFileDest lastPathComponent]]];
myunlink([tempCurFileDest fileSystemRepresentation]);
myunlink([curFileDest fileSystemRepresentation]);
NSTask *t = [[[NSTask alloc] init] autorelease];
@try
{
[t setLaunchPath: @"/usr/bin/unzip"];
[t setCurrentDirectoryPath: @"/tmp/"];
NSArray *args = [NSArray arrayWithObjects: @"-o", @"-d", tempCurFileDest, curFile, nil];
[t setArguments: args];
[t launch];
while( [t isRunning])
[NSThread sleepForTimeInterval: 0.1];
//[t waitUntilExit]; // <- This is VERY DANGEROUS : the main runloop is continuing...
}
@catch ( NSException *e)
{
NSLog( @"***** unzipFile exception: %@", e);
}
[[NSFileManager defaultManager] moveItemAtPath: tempCurFileDest toPath: curFileDest error: nil];
myunlink([curFile fileSystemRepresentation]);
}
else
{
DcmFileFormat fileformat;
OFCondition cond = fileformat.loadFile( [curFile UTF8String]);
// if we can't read it stop
if( cond.good())
{
DcmDataset *dataset = fileformat.getDataset();
// DcmItem *metaInfo = fileformat.getMetaInfo();
DcmXfer original_xfer(dataset->getOriginalXfer());
const char *string = NULL;
// NSString *sopClassUID = nil;
// if (dataset->findAndGetString(DCM_SOPClassUID, string, OFFalse).good() && string != NULL)
// sopClassUID = [NSString stringWithCString:string encoding: NSASCIIStringEncoding];
{
delete dataset->remove( DcmTagKey( 0x0009, 0x1110)); // "GEIIS" The problematic private group, containing a *always* JPEG compressed PixelData
NSString *modality;
if (dataset->findAndGetString(DCM_Modality, string, OFFalse).good() && string != NULL)
modality = [NSString stringWithCString:string encoding: NSASCIIStringEncoding];
else
modality = @"OT";
int resolution = 0;
unsigned short rows = 0;
if (dataset->findAndGetUint16( DCM_Rows, rows, OFFalse).good())
{
if( resolution == 0 || resolution > rows)
resolution = rows;
}
unsigned short columns = 0;
if (dataset->findAndGetUint16( DCM_Columns, columns, OFFalse).good())
{
if( resolution == 0 || resolution > columns)
resolution = columns;
}
int quality, compression = compressionForModality( compressionSettings, compressionSettingsLowRes, limit, modality, &quality, resolution);
BOOL alreadyCompressed = NO;
if (original_xfer.isEncapsulated())
{
switch( compression)
{
case compression_JPEGLS:
if( original_xfer.getXfer() == EXS_JPEGLSLossless || original_xfer.getXfer() == EXS_JPEGLSLossy)
alreadyCompressed = YES;
break;
case compression_JPEG2000:
if( original_xfer.getXfer() == EXS_JPEG2000 || original_xfer.getXfer() == EXS_JPEG2000LosslessOnly)
alreadyCompressed = YES;
break;
case compression_JPEG:
if( original_xfer.getXfer() == EXS_JPEGProcess14SV1TransferSyntax)
alreadyCompressed = YES;
break;
}
}
if( alreadyCompressed == NO)
{
// if( useDCMTKForJP2K == NO && compression == compression_JPEG2000)
// {
// [DCMPixelDataAttribute setUse_kdu_IfAvailable: [[dict objectForKey:@"UseKDUForJPEG2000"] intValue]];
// DCMObject *dcmObject = [[DCMObject alloc] initWithContentsOfFile: curFile decodingPixelData: NO];
//
// BOOL succeed = NO;
//
// // See - (BOOL) needToCompressFile: (NSString*) path in BrowserControllerDCMTKCategory for these exceptions
// if( [DCMAbstractSyntaxUID isImageStorage: [dcmObject attributeValueWithName:@"SOPClassUID"]] == YES && [[dcmObject attributeValueWithName:@"SOPClassUID"] isEqualToString:[DCMAbstractSyntaxUID pdfStorageClassUID]] == NO && [DCMAbstractSyntaxUID isStructuredReport: [dcmObject attributeValueWithName:@"SOPClassUID"]] == NO)
// {
// @try
// {
// DCMTransferSyntax *tsx = [DCMTransferSyntax JPEG2000LossyTransferSyntax];
// succeed = [dcmObject writeToFile: curFileDest withTransferSyntax: tsx quality: quality AET:@"OsiriX" atomically:YES];
// }
// @catch (NSException *e)
// {
// NSLog( @"dcmObject writeToFile failed: %@", e);
// }
// }
// else
// {
// succeed = [[NSData dataWithContentsOfFile: curFile] writeToFile: curFileDest atomically: YES];
// }
//
// [dcmObject release];
//
// if( succeed)
// {
// myunlink([curFile fileSystemRepresentation]);
// if( destDirec == nil)
// [[NSFileManager defaultManager] moveItemAtPath: curFileDest toPath: curFile error: nil];
// }
// else
// {
// myunlink([curFileDest fileSystemRepresentation]);
//
// if ([[dict objectForKey: @"DecompressMoveIfFail"] boolValue])
// {
// [[NSFileManager defaultManager] moveItemAtPath: curFile toPath: curFileDest error: nil];
// }
// else if( destDirec)
// {
// myunlink([curFile fileSystemRepresentation]);
// NSLog( @"failed to compress file: %@, the file is deleted", curFile);
// }
// else
// NSLog( @"failed to compress file: %@", curFile);
// }
// }
// else
if( compression == compression_JPEG || compression == compression_JPEG2000 || compression == compression_JPEGLS)
{
DcmRepresentationParameter *params = nil;
E_TransferSyntax tSyntax;
DJ_RPLossless losslessParams(6,0);
DJ_RPLossy JP2KParams( quality);
DJ_RPLossy JP2KParamsLossLess( DCMLosslessQuality);
if( compression == compression_JPEG)
{
params = &losslessParams;
tSyntax = EXS_JPEGProcess14SV1TransferSyntax;
}
else if( compression == compression_JPEGLS)
{
if( quality == DCMLosslessQuality)
{
params = &JP2KParamsLossLess;
tSyntax = EXS_JPEGLSLossless;
}
else
{
params = &JP2KParams;
tSyntax = EXS_JPEGLSLossy;
}
}
else if( compression == compression_JPEG2000)
{
if( quality == DCMLosslessQuality)
{
params = &JP2KParamsLossLess;
tSyntax = EXS_JPEG2000LosslessOnly;
}
else
{
params = &JP2KParams;
tSyntax = EXS_JPEG2000;
}
}
else
{
params = &JP2KParamsLossLess;
tSyntax = EXS_JPEG2000LosslessOnly;
NSLog( @" ****** UNKNOW compression Decompress.mm");
}
// this causes the lossless JPEG version of the dataset to be created
DcmXfer oxferSyn( tSyntax);
dataset->chooseRepresentation(tSyntax, params);
// check if everything went well
if (dataset->canWriteXfer(tSyntax))
{
// force the meta-header UIDs to be re-generated when storing the file
// since the UIDs in the data set may have changed
//only need to do this for lossy
//delete metaInfo->remove(DCM_MediaStorageSOPClassUID);
//delete metaInfo->remove(DCM_MediaStorageSOPInstanceUID);
// store in lossless JPEG format
fileformat.loadAllDataIntoMemory();
{
NSString *tempCurFileDest = [[curFileDest stringByDeletingLastPathComponent] stringByAppendingPathComponent: [NSString stringWithFormat: @".%@", [curFileDest lastPathComponent]]];
myunlink([tempCurFileDest fileSystemRepresentation]);
myunlink([curFileDest fileSystemRepresentation]);
cond = fileformat.saveFile( [tempCurFileDest UTF8String], tSyntax);
status = (cond.good()) ? YES : NO;
[[NSFileManager defaultManager] moveItemAtPath: tempCurFileDest toPath: curFileDest error: nil];
}
if( status == NO)
{
myunlink([curFileDest fileSystemRepresentation]);
if ([[dict objectForKey: @"DecompressMoveIfFail"] boolValue])
{
[[NSFileManager defaultManager] moveItemAtPath: curFile toPath: curFileDest error: nil];
}
else if( destDirec)
{
myunlink([curFile fileSystemRepresentation]);
NSLog( @"failed to compress file: %@, the file is deleted", curFile);
}
else
NSLog( @"failed to compress file: %@", curFile);
}
else
{
myunlink([curFile fileSystemRepresentation]);
if( destDirec == nil)
[[NSFileManager defaultManager] moveItemAtPath: curFileDest toPath: curFile error: nil];
}
}
}
else
{
if( destDirec)
{
myunlink([curFileDest fileSystemRepresentation]);
[[NSFileManager defaultManager] moveItemAtPath: curFile toPath: curFileDest error: nil];
myunlink([curFile fileSystemRepresentation]);
}
}
}
else
{
if( destDirec)
{
myunlink([curFileDest fileSystemRepresentation]);
[[NSFileManager defaultManager] moveItemAtPath: curFile toPath: curFileDest error: nil];
myunlink([curFile fileSystemRepresentation]);
}
}
}
}
else if ([[dict objectForKey: @"DecompressMoveIfFail"] boolValue])
{
myunlink([curFileDest fileSystemRepresentation]);
[[NSFileManager defaultManager] moveItemAtPath: curFile toPath: curFileDest error: nil];
}
else NSLog( @"compress : cannot read file: %@", curFile);
}
}
}
if( [what isEqualToString: @"testDICOMDIR"])
{
NSLog( @"-- Testing DICOMDIR: %@", [NSString stringWithUTF8String: argv[ 1]]);
DcmDicomDir dcmdir( [[NSString stringWithUTF8String: argv[ 1]] fileSystemRepresentation]);
DcmDirectoryRecord& record = dcmdir.getRootRecord();
for (unsigned int i = 0; i < record.card();)
{
DcmElement* element = record.getElement(i);
OFString ofstr;
element->getOFStringArray(ofstr).good();
i += 10;
}
NSLog( @"-- Testing DICOMDIR done");
// *(long*) 0x00 = 0xDEADBEEF;
}
# pragma mark testFiles
if( [what isEqualToString: @"testFiles"])
{
Papy3Init();
//[DCMPixelDataAttribute setUse_kdu_IfAvailable: [[dict objectForKey:@"UseKDUForJPEG2000"] intValue]];
UseOpenJpeg = [[dict objectForKey:@"UseOpenJpegForJPEG2000"] intValue];
Use_kdu_IfAvailable = [[dict objectForKey:@"UseKDUForJPEG2000"] intValue];
int i;
for( i = fileListFirstItemIndex; i < argc ; i++)
{
NSString *curFile = [NSString stringWithUTF8String: argv[ i]];
// Simply try to load and generate the image... will it crash?
DCMPix *dcmPix = [[DCMPix alloc] initWithPath: curFile :0 :1 :nil :0 :0 isBonjour: NO imageObj: nil];
if( dcmPix)
{
[dcmPix CheckLoad];
//*(long*)0 = 0xDEADBEEF; // Dead Beef ? WTF ??? Will it unlock the matrix....
[dcmPix release];
}
else NSLog( @"dcmPix == nil");
}
}
# pragma mark decompressList
if( [what isEqualToString:@"decompressList"])
{
NSString *destDirec;
if( [path isEqualToString: @"sameAsDestination"])
destDirec = nil;
else
destDirec = path;
UseOpenJpeg = [[dict objectForKey:@"UseOpenJpegForJPEG2000"] intValue];
Use_kdu_IfAvailable = [[dict objectForKey:@"UseKDUForJPEG2000"] intValue];
int i;
for( i = fileListFirstItemIndex; i < argc ; i++)
{
NSString *curFile = [NSString stringWithUTF8String:argv[ i]];
NSString *curFileDest;
if( destDirec)
curFileDest = [destDirec stringByAppendingPathComponent: [curFile lastPathComponent]];
else
curFileDest = [curFile stringByAppendingString: @" temp"];
OFBool status = NO;
if( [[curFile pathExtension] isEqualToString: @"zip"] || [[curFile pathExtension] isEqualToString: @"osirixzip"])
{
NSString *tempCurFileDest = [[curFileDest stringByDeletingLastPathComponent] stringByAppendingPathComponent: [NSString stringWithFormat: @".%@", [curFileDest lastPathComponent]]];
myunlink([tempCurFileDest fileSystemRepresentation]);
myunlink([curFileDest fileSystemRepresentation]);
NSTask *t = [[[NSTask alloc] init] autorelease];
@try
{
[t setLaunchPath: @"/usr/bin/unzip"];
[t setCurrentDirectoryPath: @"/tmp/"];
NSArray *args = [NSArray arrayWithObjects: @"-o", @"-d", tempCurFileDest, curFile, nil];
[t setArguments: args];
[t launch];
while( [t isRunning])
[NSThread sleepForTimeInterval: 0.1];
//[t waitUntilExit]; // <- This is VERY DANGEROUS : the main runloop is continuing...
}
@catch ( NSException *e)
{
NSLog( @"***** unzipFile exception: %@", e);
}
[[NSFileManager defaultManager] moveItemAtPath: tempCurFileDest toPath: curFileDest error: nil];
myunlink([curFile fileSystemRepresentation]);
}
else
{
OFCondition cond;
const char *fname = (const char *)[curFile UTF8String];
DcmFileFormat fileformat;
cond = fileformat.loadFile(fname);
if (cond.good())
{
DcmXfer filexfer(fileformat.getDataset()->getOriginalXfer());
//hopefully dcmtk willsupport jpeg2000 compression and decompression in the future: November 7th 2010 : I did it !
// if( useDCMTKForJP2K == NO && (filexfer.getXfer() == EXS_JPEG2000LosslessOnly || filexfer.getXfer() == EXS_JPEG2000))
// {
// [DCMPixelDataAttribute setUse_kdu_IfAvailable: [[dict objectForKey:@"UseKDUForJPEG2000"]; intValue]];
// DCMObject *dcmObject = [[DCMObject alloc] initWithContentsOfFile: curFile decodingPixelData: NO];
// @try
// {
// status = [dcmObject writeToFile: curFileDest withTransferSyntax:[DCMTransferSyntax ImplicitVRLittleEndianTransferSyntax] quality:1 AET:@"OsiriX" atomically:YES]; //ImplicitVRLittleEndianTransferSyntax
// }
// @catch (NSException *e)
// {
// NSLog( @"dcmObject writeToFile failed: %@", e);
// }
// [dcmObject release];
//
// if( status == NO)
// {
// myunlink([curFileDest fileSystemRepresentation]);
//
// if( destDirec)
// {
// myunlink([curFile fileSystemRepresentation]);
// NSLog( @"failed to decompress file: %@, the file is deleted", curFile);
// }
// else
// NSLog( @"failed to decompress file: %@", curFile);
// }
// }
// else
if( filexfer.getXfer() != EXS_LittleEndianExplicit || filexfer.getXfer() != EXS_LittleEndianImplicit)
{
DcmDataset *dataset = fileformat.getDataset();
delete dataset->remove( DcmTagKey( 0x0009, 0x1110)); // "GEIIS" The problematic private group, containing a *always* JPEG compressed PixelData
// decompress data set if compressed
dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL);
// check if everything went well
if (dataset->canWriteXfer(EXS_LittleEndianExplicit))
{
fileformat.loadAllDataIntoMemory();
NSString *tempCurFileDest = [[curFileDest stringByDeletingLastPathComponent] stringByAppendingPathComponent: [NSString stringWithFormat: @".%@", [curFileDest lastPathComponent]]];
myunlink([tempCurFileDest fileSystemRepresentation]);
myunlink([curFileDest fileSystemRepresentation]);
cond = fileformat.saveFile( [tempCurFileDest UTF8String], EXS_LittleEndianExplicit);
status = (cond.good()) ? YES : NO;
[[NSFileManager defaultManager] moveItemAtPath: tempCurFileDest toPath: curFileDest error: nil];
}
else status = NO;
// if( status == NO) // Try DCM Framework...
// {
// NSLog( @"********* Failed to open with dcmtk, try DCMFramework");
//
// myunlink([curFileDest fileSystemRepresentation]);
//
// DCMObject *dcmObject = [[DCMObject alloc] initWithContentsOfFile: curFile decodingPixelData: NO];
// @try
// {
// status = [dcmObject writeToFile: curFileDest withTransferSyntax:[DCMTransferSyntax ImplicitVRLittleEndianTransferSyntax] quality:1 AET:@"OsiriX" atomically:YES]; //ImplicitVRLittleEndianTransferSyntax
// }
// @catch (NSException *e)
// {
// NSLog( @"******** dcmObject writeToFile failed: %@", e);
// }
// [dcmObject release];
// }
if( status == NO)
{
myunlink([curFileDest fileSystemRepresentation]);
if( destDirec)
{
myunlink([curFile fileSystemRepresentation]);
NSLog( @"failed to decompress file: %@, the file is deleted", curFile);
}
else
NSLog( @"failed to decompress file: %@", curFile);
}
}
else
{
if( destDirec)
{
myunlink([curFileDest fileSystemRepresentation]);
[[NSFileManager defaultManager] moveItemAtPath: curFile toPath: curFileDest error: nil];
myunlink([curFile fileSystemRepresentation]);
}
status = NO;
}
}
}
if( status)
{
myunlink([curFile fileSystemRepresentation]);
if( destDirec == nil)
[[NSFileManager defaultManager] moveItemAtPath: curFileDest toPath: curFile error: nil];
}
}
}
# pragma mark writeMovie
if( [what isEqualToString: @"writeMovie"])
{
if( ![path hasSuffix:@".swf"])
{
NSLog( @"******** writeMovie Decompress - not available");
}
else
{ // SWF!!
NSString* inputDir = [NSString stringWithUTF8String:argv[fileListFirstItemIndex++]];
NSArray* inputFiles = [inputDir stringsByAppendingPaths:[[NSFileManager defaultManager] contentsOfDirectoryAtPath:inputDir error:NULL]];
float frameRate = 0;
if( fileListFirstItemIndex < argc)
frameRate = [[NSString stringWithUTF8String: argv[ fileListFirstItemIndex]] floatValue];
createSwfMovie(inputFiles, path, frameRate);
[[NSFileManager defaultManager] removeItemAtPath: inputDir error: nil];
}
}
# pragma mark pdfFromURL
if( [what isEqualToString: @"pdfFromURL"])
{
@try
{
WebView *webView = [[[WebView alloc] initWithFrame: NSMakeRect(0,0,1,1) frameName: @"myFrame" groupName: @"myGroup"] autorelease];
NSWindow *w = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0,0,1,1) styleMask:NSBorderlessWindowMask backing:NSBackingStoreNonretained defer:NO] autorelease];
[w setContentView:webView];
WebPreferences *webPrefs = [WebPreferences standardPreferences];
[webPrefs setLoadsImagesAutomatically: YES];
[webPrefs setAllowsAnimatedImages: YES];
[webPrefs setAllowsAnimatedImageLooping: NO];
[webPrefs setJavaEnabled: NO];
[webPrefs setPlugInsEnabled: NO];
[webPrefs setJavaScriptEnabled: YES];
[webPrefs setJavaScriptCanOpenWindowsAutomatically: NO];
[webPrefs setShouldPrintBackgrounds: YES];
[webView setApplicationNameForUserAgent: @"OsiriX"];
[webView setPreferences: webPrefs];
[webView setMaintainsBackForwardList: NO];
NSURL *theURL = [NSURL fileURLWithPath: path];
if( theURL)
{
NSURLRequest *request = [NSURLRequest requestWithURL: theURL];
[[webView mainFrame] loadRequest: request];
NSTimeInterval timeout = [NSDate timeIntervalSinceReferenceDate] + 10;
while( [[webView mainFrame] dataSource] == nil || [[[webView mainFrame] dataSource] isLoading] == YES || [[[webView mainFrame] provisionalDataSource] isLoading] == YES)
{
[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.1]];
if( [NSDate timeIntervalSinceReferenceDate] > timeout)
break;
}
NSPrintInfo *sharedInfo = [NSPrintInfo sharedPrintInfo];
NSMutableDictionary *sharedDict = [sharedInfo dictionary];
NSMutableDictionary *printInfoDict = [NSMutableDictionary dictionaryWithDictionary: sharedDict];
[printInfoDict setObject: NSPrintSaveJob forKey: NSPrintJobDisposition];
[[NSFileManager defaultManager] removeItemAtPath: [path stringByAppendingPathExtension: @"pdf"] error: nil];
[printInfoDict setObject: [path stringByAppendingPathExtension: @"pdf"] forKey: NSPrintSavePath];
NSPrintInfo *printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
[printInfo setBottomMargin: 30];
[printInfo setTopMargin: 30];
[printInfo setLeftMargin: 24];
[printInfo setRightMargin: 24];
[printInfo setHorizontalPagination: NSAutoPagination];
[printInfo setVerticalPagination: NSAutoPagination];
[printInfo setVerticallyCentered:NO];
NSView *viewToPrint = [[[webView mainFrame] frameView] documentView];
NSPrintOperation *printOp = [NSPrintOperation printOperationWithView: viewToPrint printInfo: printInfo];
[printOp setShowsPrintPanel: NO];
[printOp setShowsProgressPanel: NO];
[printOp runOperation];
//jf remove empty last PDF page
@try
{
NSURL *pdfURL = [theURL URLByAppendingPathExtension:@"pdf"];
PDFDocument *pdf = [[[PDFDocument alloc]initWithURL:pdfURL]autorelease];
NSUInteger pdfPageCount = [pdf pageCount];
if (pdfPageCount > 1)
{
NSUInteger pdfLastPageIndex = pdfPageCount - 1;
PDFPage *pdfLastPage = [pdf pageAtIndex:pdfLastPageIndex];
NSUInteger pdfLastPageCharCount = [pdfLastPage numberOfCharacters];
if (pdfLastPageCharCount < 2)
{
[pdf removePageAtIndex:pdfLastPageIndex];
[pdf writeToURL:pdfURL];
}
}
}
@catch ( NSException *e) {
N2LogException( e);
}
}
}
@catch (NSException * e)
{
N2LogExceptionWithStackTrace(e);
}
return 0;
}
// deregister JPEG codecs
//DJDecoderRegistration::cleanup(); We dont care: we are just a small app : our memory will be killed by the system. Dont loose time here !
//DJEncoderRegistration::cleanup(); We dont care: we are just a small app : our memory will be killed by the system. Dont loose time here !
// deregister RLE codecs
//DcmRLEDecoderRegistration::cleanup(); We dont care: we are just a small app : our memory will be killed by the system. Dont loose time here !
//DcmRLEEncoderRegistration::cleanup(); We dont care: we are just a small app : our memory will be killed by the system. Dont loose time here !
}
// [pool release]; We dont care: we are just a small app : our memory will be killed by the system. Dont loose time here !
return 0;
}
void createSwfMovie(NSArray* inputFiles, NSString* path, float frameRate) {
if (path)
[[NSFileManager defaultManager] removeItemAtPath:path error:NULL];
Ming_init();
Ming_setSWFCompression(9); // 9 = maximum compression
SWFMovie* swf = new SWFMovie(7);
swf->setBackground(0x88, 0x88, 0x88);
if( frameRate < 1)
frameRate = 10;
swf->setRate(frameRate);
BOOL sizeSet = NO;
NSSize swfSize;
NSString* as = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"SWFInit" ofType:@"as"] encoding:NSUTF8StringEncoding error:NULL];
//NSLog(@"AS:\n%@", as);
SWFAction* action = new SWFAction([as cStringUsingEncoding:NSISOLatin1StringEncoding]);
// int len, res = action->compile(7, &len);
// NSLog(@"compile ret:%d len:%d", res, len);
swf->add(action);
const CGFloat ControllerHeight = 14, PlayPauseWidth = 0;
const CGFloat ControllerPadTop = 1, ControllerPadBottom = 1;
const CGFloat MarkHeight = ControllerHeight-ControllerPadTop-ControllerPadBottom, MarkWidth = MarkHeight-2;
const CGFloat ControllerPadLeft = MarkWidth/2, ControllerPadRight = MarkWidth/2+PlayPauseWidth;
NSRect ControllerRect;
NSRect ControllerNavigationRect;
const CGFloat ControllerBarThickness = 2, ControllerBarPadLeft = -ControllerBarThickness/2, ControllerBarPadRight = -ControllerBarThickness/2;
// movie controller left of mark
SWFShape* squareS = new SWFShape();
squareS->setRightFillStyle(SWFFillStyle::SolidFillStyle(0,0,0,0));
squareS->movePenTo(0,0);
squareS->drawLine(1,0);
squareS->drawLine(0,MarkHeight);
squareS->drawLine(-1,0);
squareS->drawLine(0,-MarkHeight);
SWFButton* leftBarB = new SWFButton();
leftBarB->addShape(squareS, SWFBUTTON_HIT|SWFBUTTON_UP|SWFBUTTON_DOWN|SWFBUTTON_OVER);
leftBarB->addAction(new SWFAction("leftBarMouseDown();"), SWFBUTTON_MOUSEDOWN);
SWFDisplayItem* leftBarDI = swf->add(leftBarB);
leftBarDI->setName("leftBarDI");
// movie controller right of mark
squareS = new SWFShape();
squareS->setRightFillStyle(SWFFillStyle::SolidFillStyle(0,0,0,0));
squareS->movePenTo(0,0);
squareS->drawLine(-1,0);
squareS->drawLine(0,MarkHeight);
squareS->drawLine(1,0);
squareS->drawLine(0,-MarkHeight);
SWFButton* rightBarB = new SWFButton();
rightBarB->addShape(squareS, SWFBUTTON_HIT|SWFBUTTON_UP|SWFBUTTON_DOWN|SWFBUTTON_OVER);
rightBarB->addAction(new SWFAction("rightBarMouseDown();"), SWFBUTTON_MOUSEDOWN);
SWFDisplayItem* rightBarDI = swf->add(rightBarB);
rightBarDI->setName("rightBarDI");
// movie controller mark
SWFShape* markS = new SWFShape();
markS->setRightFillStyle(SWFFillStyle::SolidFillStyle(64,64,64,191));
markS->movePenTo(-MarkWidth/2, -MarkHeight/2);
markS->drawLine(MarkWidth,0);
markS->drawLine(0,MarkHeight);
markS->drawLine(-MarkWidth,0);
markS->drawLine(0,-MarkHeight);
SWFButton* markB = new SWFButton();
markB->addShape(markS, SWFBUTTON_HIT|SWFBUTTON_UP|SWFBUTTON_DOWN|SWFBUTTON_OVER);
markB->addAction(new SWFAction("markMouseDown();"), SWFBUTTON_MOUSEDOWN);
SWFDisplayItem* markDI = swf->add(markB);
markDI->setName("markDI");
SWFBitmap* bitmap[inputFiles.count];
SWFDisplayItem* displayItem[inputFiles.count];
//#pragma omp parallel for default(private)
for (int i = 0; i < inputFiles.count; ++i) {
NSString* imgPath = [inputFiles objectAtIndex:i];
// NSLog(@"%@", imgPath);
bitmap[i] = new SWFBitmap(imgPath.UTF8String, NULL);
if (!bitmap[i])
NSLog(@"SWF creation FAILED: could not read %@", imgPath);
NSSize bitmapSize = NSMakeSize(bitmap[i]->getWidth(), bitmap[i]->getHeight());
if (!sizeSet) {
swfSize = NSMakeSize(bitmapSize.width, bitmapSize.height+ControllerHeight); // 15 is the controller height
swf->setDimension(swfSize.width, swfSize.height);
sizeSet = YES;
}
SWFShape* shape = new SWFShape();
shape->setRightFillStyle(SWFFillStyle::BitmapFillStyle(bitmap[i], SWFFILL_CLIPPED_BITMAP));
shape->drawLine(bitmapSize.width,0);
shape->drawLine(0,bitmapSize.height);
shape->drawLine(-bitmapSize.width,0);
shape->drawLine(0,-bitmapSize.height);
displayItem[i] = swf->add(shape);
displayItem[i]->moveTo(0,0);
displayItem[i]->scaleTo(0);
}
// controller
ControllerRect = NSMakeRect(0, swfSize.height-ControllerHeight, swfSize.width, ControllerHeight);
ControllerNavigationRect = NSMakeRect(ControllerRect.origin.x+ControllerPadLeft, ControllerRect.origin.y+ControllerPadTop, ControllerRect.size.width-ControllerPadLeft-ControllerPadRight, ControllerRect.size.height-ControllerPadTop-ControllerPadBottom);
swf->add(new SWFAction([[NSString stringWithFormat:@"_root.ControllerOriginX = %f; _root.ControllerWidth = %f;", ControllerNavigationRect.origin.x, ControllerNavigationRect.size.width] cStringUsingEncoding:NSISOLatin1StringEncoding]));
NSRect ControllerBarRect = NSMakeRect(ControllerNavigationRect.origin.x+ControllerBarPadLeft, (ControllerNavigationRect.origin.y*2+ControllerNavigationRect.size.height-ControllerBarThickness)/2, ControllerNavigationRect.size.width-ControllerBarPadLeft-ControllerBarPadRight, ControllerBarThickness);
SWFShape* controllerBar = new SWFShape();
controllerBar->setRightFillStyle(SWFFillStyle::SolidFillStyle(191,191,191,127));
controllerBar->movePenTo(0, 0);
controllerBar->drawLine(1,0);
controllerBar->drawLine(0,1);
controllerBar->drawLine(-1,0);
controllerBar->drawLine(0,-1);
controllerBar->setRightFillStyle(SWFFillStyle::SolidFillStyle(191,191,191,127));
SWFDisplayItem* controllerBarDisplayItem = swf->add(controllerBar);
controllerBarDisplayItem->moveTo(ControllerBarRect.origin.x, ControllerBarRect.origin.y);
controllerBarDisplayItem->scaleTo(ControllerBarRect.size.width, ControllerBarRect.size.height);
// Click in image to play/stop
SWFShape* button = new SWFShape();
button->setRightFillStyle(SWFFillStyle::SolidFillStyle(0,50,0,0));
button->drawLine(swfSize.width,0);
button->drawLine(0,swfSize.height-ControllerHeight);
button->drawLine(-swfSize.width,0);
button->drawLine(0,-swfSize.height-ControllerHeight);
SWFButton* playStop = new SWFButton();
playStop->addShape( button, SWFBUTTON_HIT|SWFBUTTON_UP|SWFBUTTON_DOWN|SWFBUTTON_OVER);
playStop->addAction(new SWFAction("if( playing == 1) playing = 0; else playing = 1; if( playing) play(); else stop();"), SWFBUTTON_MOUSEDOWN);