-
Notifications
You must be signed in to change notification settings - Fork 0
/
QtDcmMoveScu.cpp
1043 lines (853 loc) · 33.4 KB
/
QtDcmMoveScu.cpp
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
/*
QtDcm is a C++ Qt based library for communication and conversion of Dicom images.
Copyright (C) 2011 Alexandre Abadie <Alexandre.Abadie@univ-rennes1.fr>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define QT_NO_CAST_TO_ASCII
#define INCLUDE_CSTDLIB
#define INCLUDE_CSTDIO
#define INCLUDE_CSTRING
#define INCLUDE_CSTDARG
// From Dcmtk:
#include <dcmtk/config/osconfig.h> /* make sure OS specific configuration is included first */
#include "dcmtk/ofstd/ofstdinc.h"
#include "dcmtk/ofstd/ofstd.h"
#include "dcmtk/ofstd/ofconapp.h"
#include <dcmtk/ofstd/ofstream.h>
#include <dcmtk/dcmdata/dctk.h>
#include <dcmtk/dcmdata/dcfilefo.h>
#include "dcmtk/dcmnet/dfindscu.h"
#include <dcmtk/dcmdata/dcistrmz.h> /* for dcmZlibExpectRFC1950Encoding */
// For dcm images
#include <dcmtk/dcmimgle/dcmimage.h>
#include "dcmtk/dcmdata/dcrledrg.h" /* for DcmRLEDecoderRegistration */
#include "dcmtk/dcmjpeg/djdecode.h" /* for dcmjpeg decoders */
#include "dcmtk/dcmjpeg/dipijpeg.h" /* for dcmimage JPEG plugin */
// For color images
#include <dcmtk/dcmimage/diregist.h>
#include "dcmtk/ofstd/ofstdinc.h"
#include "dcmtk/dcmnet/dimse.h"
#include "dcmtk/dcmnet/diutil.h"
#include "dcmtk/dcmdata/dcdict.h"
#include "dcmtk/dcmdata/dcuid.h" /* for dcmtk version name */
#ifdef WITH_OPENSSL
#include "dcmtk/dcmtls/tlstrans.h"
#include "dcmtk/dcmtls/tlslayer.h"
#endif
#include <QtDcmPreferences.h>
#include <QtDcmServer.h>
#include <QtDcmManager.h>
#include <QtDcmMoveScu.h>
class QtDcmMoveScuPrivate
{
public:
QList<QString> series;
QList<QString> filenames;
QString outputDir;
QString importDir;
QString currentSerie;
QtDcmMoveScu::mode mode;
QString imageId;
};
QtDcmMoveScu::QtDcmMoveScu ( QObject * parent ) : d ( new QtDcmMoveScuPrivate )
{
progressTotal = 0;
progressSerie = 0;
step = 0;
maxPDU = ASC_DEFAULTMAXPDU;
useMetaheader = OFTrue;
networkTransferSyntax = EXS_Unknown;
writeTransferSyntax = EXS_Unknown;
groupLength = EGL_recalcGL;
sequenceType = EET_ExplicitLength;
paddingType = EPD_withoutPadding;
filepad = 0;
itempad = 0;
bitPreserving = OFFalse;
ignore = OFFalse;
correctUIDPadding = OFFalse;
repeatCount = 1;
abortAssociation = OFFalse;
moveDestination = NULL;
cancelAfterNResponses = -1;
queryModel = QMPatientRoot;
ignorePendingDatasets = OFTrue;
outputDirectory = ".";
acseTimeout = 30;
overrideKeys = NULL;
useStoreSCP = true;
overrideKeys = NULL;
blockMode = DIMSE_BLOCKING;
d->mode = QtDcmMoveScu::IMPORT;
}
QtDcmMoveScu::~QtDcmMoveScu()
{
delete d;
d = NULL;
}
void QtDcmMoveScu::setMode ( QtDcmMoveScu::mode mode )
{
d->mode = mode;
}
QtDcmMoveScu::mode QtDcmMoveScu::getMode()
{
return d->mode;
}
void QtDcmMoveScu::setImageId ( QString id )
{
d->imageId = id;
}
void QtDcmMoveScu::setSeries ( QList<QString> series )
{
d->series = series;
}
void QtDcmMoveScu::setOutputDir ( QString dir )
{
d->outputDir = dir;
}
void QtDcmMoveScu::setImportDir ( QString dir )
{
d->importDir = dir;
}
void QtDcmMoveScu::onStopMove()
{
if ( this->isRunning() )
{
ASC_dropNetwork ( &net );
this->exit();
}
}
void QtDcmMoveScu::run()
{
OFCondition cond;
step = ( int ) ( 100.0 / d->series.size() );
progressTotal = 0;
for ( int i = 0; i < d->series.size(); i++ )
{
d->currentSerie = d->series.at ( i );
QDir serieDir ( d->outputDir + QDir::separator() + d->series.at ( i ) );
if ( !serieDir.exists() )
QDir ( d->outputDir ).mkdir ( d->series.at ( i ) );
outputDirectory = QString ( d->outputDir + QDir::separator() + d->currentSerie ).toUtf8().constData();
if ( d->mode == IMPORT )
{
cond = this->move ( d->series.at ( i ) );
emit serieMoved ( serieDir.absolutePath(), d->series.at ( i ), i );
emit updateProgress ( ( int ) ( 100.0 * ( i+1 ) / d->series.size() ) );
progressTotal += step;
}
else
{
cond = this->move ( d->imageId );
}
}
exit();
}
OFCondition QtDcmMoveScu::move ( QString uid )
{
OFString temp_str;
params = NULL;
assoc = NULL;
net = NULL;
QuerySyntax querySyntax[3] =
{
{ UID_FINDPatientRootQueryRetrieveInformationModel,
UID_MOVEPatientRootQueryRetrieveInformationModel },
{ UID_FINDStudyRootQueryRetrieveInformationModel,
UID_MOVEStudyRootQueryRetrieveInformationModel },
{ UID_RETIRED_FINDPatientStudyOnlyQueryRetrieveInformationModel,
UID_RETIRED_MOVEPatientStudyOnlyQueryRetrieveInformationModel }
};
OFCondition cond;
if ( d->mode == IMPORT )
{
this->addOverrideKey ( QString ( "QueryRetrieveLevel=" ) + QString ( "" "SERIES" "" ) );
this->addOverrideKey ( QString ( "SeriesInstanceUID=" + uid ) );
}
else
{
this->addOverrideKey ( QString ( "QueryRetrieveLevel=" ) + QString ( "" "IMAGE" "" ) );
this->addOverrideKey ( QString ( "SOPInstanceUID=" + uid ) );
}
cond = ASC_initializeNetwork ( NET_ACCEPTORREQUESTOR, QtDcmPreferences::instance()->getPort().toInt(), acseTimeout, &net );
if ( cond.bad() )
{
qDebug() << "Cannot create network: " << DimseCondition::dump ( temp_str, cond ).c_str();
return cond;
}
cond = ASC_createAssociationParameters ( ¶ms, maxPDU );
if ( cond.bad() )
{
qDebug() << "Cannot create association: " << DimseCondition::dump ( temp_str, cond ).c_str();
return cond;
}
ASC_setAPTitles ( params, QtDcmPreferences::instance()->getAetitle().toUtf8().data(), QtDcmManager::instance()->getCurrentPacs()->getAetitle().toUtf8().data(), QtDcmManager::instance()->getCurrentPacs()->getAetitle().toUtf8().data() );
ASC_setPresentationAddresses ( params, QtDcmPreferences::instance()->getHostname().toUtf8().data(), QString ( QtDcmManager::instance()->getCurrentPacs()->getHostname() + ":"
+ QtDcmManager::instance()->getCurrentPacs()->getPort() ).toUtf8().data() );
cond = addPresentationContext ( params, 1, querySyntax[queryModel].findSyntax, networkTransferSyntax );
cond = addPresentationContext ( params, 3, querySyntax[queryModel].moveSyntax, networkTransferSyntax );
if ( cond.bad() )
{
qDebug() << "Wrong presentation context:" << DimseCondition::dump ( temp_str, cond ).c_str();
return cond;
}
cond = ASC_requestAssociation ( net, params, &assoc );
if ( cond.bad() )
{
if ( cond == DUL_ASSOCIATIONREJECTED )
{
T_ASC_RejectParameters rej;
ASC_getRejectParameters ( params, &rej );
ASC_printRejectParameters ( temp_str, &rej );
qDebug() << "Association Rejected:" << QString ( temp_str.c_str() );
return cond;
}
else
{
qDebug() << "Association Request Failed:" << DimseCondition::dump ( temp_str,cond ).c_str();
return cond;
}
}
if ( ASC_countAcceptedPresentationContexts ( params ) == 0 )
{
qDebug() << "No Acceptable Presentation Contexts";
return cond;
}
cond = EC_Normal;
cond = this->cmove ( assoc, NULL );
overrideKeys->clear();
this->addOverrideKey ( QString ( "QueryRetrieveLevel=" ) + QString ( "" "STUDY" "" ) );
if ( cond == EC_Normal )
{
if ( abortAssociation )
{
qDebug() << "Aborting Association";
cond = ASC_abortAssociation ( assoc );
ASC_dropNetwork ( &net );
if ( cond.bad() )
{
qDebug() << "Association Abort Failed: " << DimseCondition::dump ( temp_str,cond ).c_str();
return cond;
}
}
else
{
/* release association */
qDebug() << "Releasing Association";
// cond = ASC_releaseAssociation(assoc); //Problem with error message Illegal Key
ASC_dropNetwork ( &net );
if ( cond.bad() )
{
qDebug() << "Association Release Failed:" << DimseCondition::dump ( temp_str,cond ).c_str();
return cond;
}
}
}
else if ( cond == DUL_PEERREQUESTEDRELEASE )
{
qDebug() << "Protocol Error: Peer requested release (Aborting)";
cond = ASC_abortAssociation ( assoc );
ASC_dropNetwork ( &net );
if ( cond.bad() )
{
qDebug() << "Association Abort Failed: " << DimseCondition::dump ( temp_str,cond ).c_str();
return cond;
}
return cond;
}
else if ( cond == DUL_PEERABORTEDASSOCIATION )
{
qDebug() << "Peer Aborted Association";
return cond;
}
else
{
qDebug() << "Move SCU Failed: Aborting Association";
cond = ASC_abortAssociation ( assoc );
ASC_dropNetwork ( &net );
if ( cond.bad() )
{
qDebug() << "Association Abort Failed: " << DimseCondition::dump ( temp_str,cond ).c_str();
return cond;
}
return cond;
}
return cond;
}
void QtDcmMoveScu::addOverrideKey ( QString key )
{
char * s = key.toUtf8().data();
unsigned int g = 0xffff;
unsigned int e = 0xffff;
int n = 0;
OFString dicName, valStr;
OFString msg;
char msg2[200];
// try to parse group and element number
n = sscanf ( s, "%x,%x=", &g, &e );
OFString toParse = s;
size_t eqPos = toParse.find ( '=' );
if ( n < 2 ) // if at least no tag could be parsed
{
// if value is given, extract it (and extrect dictname)
if ( eqPos != OFString_npos )
{
dicName = toParse.substr ( 0, eqPos ).c_str();
valStr = toParse.substr ( eqPos + 1, toParse.length() );
}
else
// no value given, just dictionary name
dicName = s; // only dictionary name given (without value)
// try to lookup in dictionary
DcmTagKey key ( 0xffff, 0xffff );
const DcmDataDictionary& globalDataDict = dcmDataDict.rdlock();
const DcmDictEntry *dicent = globalDataDict.findEntry ( dicName.c_str() );
dcmDataDict.unlock();
if ( dicent != NULL )
{
// found dictionary name, copy group and element number
key = dicent->getKey();
g = key.getGroup();
e = key.getElement();
}
else
{
// not found in dictionary
msg = "bad key format or dictionary name not found in dictionary: ";
msg += dicName;
qDebug() << QString ( msg.c_str() );
}
} // tag could be parsed, copy value if it exists
else
{
if ( eqPos != OFString_npos )
valStr = toParse.substr ( eqPos + 1, toParse.length() );
}
DcmTag tag ( g, e );
if ( tag.error() != EC_Normal )
{
sprintf ( msg2, "unknown tag: (%04x,%04x)", g, e );
qDebug() << QString ( msg2 );
}
DcmElement *elem = newDicomElement ( tag );
if ( elem == NULL )
{
sprintf ( msg2, "cannot create element for tag: (%04x,%04x)", g, e );
qDebug() << QString ( msg2 );
}
if ( valStr.length() > 0 )
{
if ( elem->putString ( valStr.c_str() ).bad() )
{
sprintf ( msg2, "cannot put tag value: (%04x,%04x)=\"", g, e );
msg = msg2;
msg += valStr;
msg += "\"";
qDebug() << QString ( msg.c_str() );
}
}
if ( overrideKeys == NULL )
overrideKeys = new DcmDataset;
if ( overrideKeys->insert ( elem, OFTrue ).bad() )
{
sprintf ( msg2, "cannot insert tag: (%04x,%04x)", g, e );
qDebug() << QString ( msg2 );
}
}
OFCondition QtDcmMoveScu::addPresentationContext ( T_ASC_Parameters *params, T_ASC_PresentationContextID pid, const char* abstractSyntax, E_TransferSyntax preferredTransferSyntax )
{
const char* transferSyntaxes[] = { NULL, NULL, NULL };
int numTransferSyntaxes = 0;
switch ( preferredTransferSyntax )
{
case EXS_LittleEndianImplicit:
/* we only support Little Endian Implicit */
transferSyntaxes[0] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 1;
break;
case EXS_LittleEndianExplicit:
/* we prefer Little Endian Explicit */
transferSyntaxes[0] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[1] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 3;
break;
case EXS_BigEndianExplicit:
/* we prefer Big Endian Explicit */
transferSyntaxes[0] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 3;
break;
default:
/* We prefer explicit transfer syntaxes.
* If we are running on a Little Endian machine we prefer
* LittleEndianExplicitTransferSyntax to BigEndianTransferSyntax.
*/
if ( gLocalByteOrder == EBO_LittleEndian ) /* defined in dcxfer.h */
{
transferSyntaxes[0] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[1] = UID_BigEndianExplicitTransferSyntax;
}
else
{
transferSyntaxes[0] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
}
transferSyntaxes[2] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 3;
break;
}
return ASC_addPresentationContext (
params, pid, abstractSyntax,
transferSyntaxes, numTransferSyntaxes );
}
OFCondition QtDcmMoveScu::acceptSubAssoc ( T_ASC_Network * aNet, T_ASC_Association ** assoc )
{
const char* knownAbstractSyntaxes[] =
{
UID_VerificationSOPClass
};
const char* transferSyntaxes[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
int numTransferSyntaxes;
OFCondition cond = ASC_receiveAssociation ( aNet, assoc, ASC_DEFAULTMAXPDU );
if ( cond.good() )
{
switch ( EXS_Unknown )
{
case EXS_LittleEndianImplicit:
/* we only support Little Endian Implicit */
transferSyntaxes[0] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 1;
break;
case EXS_LittleEndianExplicit:
/* we prefer Little Endian Explicit */
transferSyntaxes[0] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[1] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 3;
break;
case EXS_BigEndianExplicit:
/* we prefer Big Endian Explicit */
transferSyntaxes[0] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 3;
break;
case EXS_JPEGProcess14SV1TransferSyntax:
/* we prefer JPEGLossless:Hierarchical-1stOrderPrediction (default lossless) */
transferSyntaxes[0] = UID_JPEGProcess14SV1TransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
case EXS_JPEGProcess1TransferSyntax:
/* we prefer JPEGBaseline (default lossy for 8 bit images) */
transferSyntaxes[0] = UID_JPEGProcess1TransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
case EXS_JPEGProcess2_4TransferSyntax:
/* we prefer JPEGExtended (default lossy for 12 bit images) */
transferSyntaxes[0] = UID_JPEGProcess2_4TransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
case EXS_JPEG2000LosslessOnly:
/* we prefer JPEG2000 Lossless */
transferSyntaxes[0] = UID_JPEG2000LosslessOnlyTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
case EXS_JPEG2000:
/* we prefer JPEG2000 Lossy */
transferSyntaxes[0] = UID_JPEG2000TransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
case EXS_JPEGLSLossless:
/* we prefer JPEG-LS Lossless */
transferSyntaxes[0] = UID_JPEGLSLosslessTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
case EXS_JPEGLSLossy:
/* we prefer JPEG-LS Lossy */
transferSyntaxes[0] = UID_JPEGLSLossyTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
case EXS_MPEG2MainProfileAtMainLevel:
/* we prefer MPEG2 MP@ML */
transferSyntaxes[0] = UID_MPEG2MainProfileAtMainLevelTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
case EXS_RLELossless:
/* we prefer RLE Lossless */
transferSyntaxes[0] = UID_RLELosslessTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
#ifdef WITH_ZLIB
case EXS_DeflatedLittleEndianExplicit:
/* we prefer Deflated Explicit VR Little Endian */
transferSyntaxes[0] = UID_DeflatedExplicitVRLittleEndianTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[2] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[3] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 4;
break;
#endif
default:
if ( OFFalse )
{
/* we accept all supported transfer syntaxes
* (similar to "AnyTransferSyntax" in "storescp.cfg")
*/
transferSyntaxes[0] = UID_JPEG2000TransferSyntax;
transferSyntaxes[1] = UID_JPEG2000LosslessOnlyTransferSyntax;
transferSyntaxes[2] = UID_JPEGProcess2_4TransferSyntax;
transferSyntaxes[3] = UID_JPEGProcess1TransferSyntax;
transferSyntaxes[4] = UID_JPEGProcess14SV1TransferSyntax;
transferSyntaxes[5] = UID_JPEGLSLossyTransferSyntax;
transferSyntaxes[6] = UID_JPEGLSLosslessTransferSyntax;
transferSyntaxes[7] = UID_RLELosslessTransferSyntax;
transferSyntaxes[8] = UID_MPEG2MainProfileAtMainLevelTransferSyntax;
transferSyntaxes[9] = UID_DeflatedExplicitVRLittleEndianTransferSyntax;
if ( gLocalByteOrder == EBO_LittleEndian )
{
transferSyntaxes[10] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[11] = UID_BigEndianExplicitTransferSyntax;
}
else
{
transferSyntaxes[10] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[11] = UID_LittleEndianExplicitTransferSyntax;
}
transferSyntaxes[12] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 13;
}
else
{
/* We prefer explicit transfer syntaxes.
* If we are running on a Little Endian machine we prefer
* LittleEndianExplicitTransferSyntax to BigEndianTransferSyntax.
*/
if ( gLocalByteOrder == EBO_LittleEndian ) /* defined in dcxfer.h */
{
transferSyntaxes[0] = UID_LittleEndianExplicitTransferSyntax;
transferSyntaxes[1] = UID_BigEndianExplicitTransferSyntax;
}
else
{
transferSyntaxes[0] = UID_BigEndianExplicitTransferSyntax;
transferSyntaxes[1] = UID_LittleEndianExplicitTransferSyntax;
}
transferSyntaxes[2] = UID_LittleEndianImplicitTransferSyntax;
numTransferSyntaxes = 3;
}
break;
}
/* accept the Verification SOP Class if presented */
cond = ASC_acceptContextsWithPreferredTransferSyntaxes (
( *assoc )->params,
knownAbstractSyntaxes, DIM_OF ( knownAbstractSyntaxes ),
transferSyntaxes, numTransferSyntaxes );
if ( cond.good() )
{
/* the array of Storage SOP Class UIDs comes from dcuid.h */
cond = ASC_acceptContextsWithPreferredTransferSyntaxes (
( *assoc )->params,
dcmAllStorageSOPClassUIDs, numberOfAllDcmStorageSOPClassUIDs,
transferSyntaxes, numTransferSyntaxes );
}
}
if ( cond.good() ) cond = ASC_acknowledgeAssociation ( *assoc );
if ( cond.bad() )
{
ASC_dropAssociation ( *assoc );
ASC_destroyAssociation ( assoc );
}
return cond;
}
OFCondition QtDcmMoveScu::echoSCP ( T_ASC_Association * assoc, T_DIMSE_Message * msg, T_ASC_PresentationContextID presID )
{
OFCondition cond = DIMSE_sendEchoResponse ( assoc, presID, &msg->msg.CEchoRQ, STATUS_Success, NULL );
if ( cond.bad() )
{
qDebug() << "Echo SCP Failed";
}
return cond;
}
OFCondition QtDcmMoveScu::storeSCP ( T_ASC_Association *assoc, T_DIMSE_Message *msg, T_ASC_PresentationContextID presID, void* subOpCallbackData )
{
QtDcmMoveScu * self = ( QtDcmMoveScu* ) subOpCallbackData;
OFCondition cond = EC_Normal;
T_DIMSE_C_StoreRQ *req;
char imageFile[2048];
req = &msg->msg.CStoreRQ;
if ( OFFalse )
{
#ifdef _WIN32
tmpnam ( imageFile );
#else
strcpy ( imageFile, NULL_DEVICE_NAME );
#endif
}
else
{
sprintf ( imageFile, "%s.%s",
dcmSOPClassUIDToModality ( req->AffectedSOPClassUID ),
req->AffectedSOPInstanceUID );
}
self->assoc = assoc;
self->imageFile = imageFile;
DcmFileFormat dcmff;
self->file = &dcmff;
// store SourceApplicationEntityTitle in metaheader
if ( assoc && assoc->params )
{
const char *aet = assoc->params->DULparams.callingAPTitle;
if ( aet ) dcmff.getMetaInfo()->putAndInsertString ( DCM_SourceApplicationEntityTitle, aet );
}
DcmDataset *dset = dcmff.getDataset();
if ( OFFalse )
{
cond = DIMSE_storeProvider ( assoc, presID, req, imageFile, OFFalse,
NULL, storeSCPCallback, ( void * ) subOpCallbackData, DIMSE_BLOCKING, 0 );
}
else
{
cond = DIMSE_storeProvider ( assoc, presID, req, ( char * ) NULL, OFFalse,
&dset, storeSCPCallback, ( void * ) subOpCallbackData, DIMSE_BLOCKING, 0 );
}
return cond;
}
void QtDcmMoveScu::storeSCPCallback ( void *callbackData, T_DIMSE_StoreProgress *progress, T_DIMSE_C_StoreRQ *req, char *imageFile, DcmDataset **imageDataSet, T_DIMSE_C_StoreRSP *rsp, DcmDataset **statusDetail )
{
QtDcmMoveScu* self = ( QtDcmMoveScu* ) callbackData;
DIC_UI sopClass;
DIC_UI sopInstance;
if ( progress->state == DIMSE_StoreEnd )
{
*statusDetail = NULL;
if ( ( imageDataSet != NULL ) && ( *imageDataSet != NULL ) && !self->bitPreserving && !self->ignore )
{
/* create full path name for the output file */
OFString ofname;
OFStandard::combineDirAndFilename ( ofname, self->outputDirectory, self->imageFile, OFTrue /* allowEmptyDirName */ );
E_TransferSyntax xfer = self->writeTransferSyntax;
if ( xfer == EXS_Unknown ) xfer = ( *imageDataSet )->getOriginalXfer();
OFCondition cond = self->file->saveFile ( ofname.c_str(), xfer, self->sequenceType, self->groupLength,
self->paddingType, OFstatic_cast ( Uint32, self->filepad ), OFstatic_cast ( Uint32, self->itempad ),
( self->useMetaheader ) ? EWM_fileformat : EWM_dataset );
if ( QFile ( ofname.c_str() ).exists() )
emit self->previewSlice ( QString ( ofname.c_str() ) );
if ( ( rsp->DimseStatus == STATUS_Success ) && !self->ignore )
{
/* which SOP class and SOP instance ? */
if ( !DU_findSOPClassAndInstanceInDataSet ( *imageDataSet, sopClass, sopInstance, self->correctUIDPadding ) )
{
rsp->DimseStatus = STATUS_STORE_Error_CannotUnderstand;
}
else if ( strcmp ( sopClass, req->AffectedSOPClassUID ) != 0 )
{
rsp->DimseStatus = STATUS_STORE_Error_DataSetDoesNotMatchSOPClass;
}
else if ( strcmp ( sopInstance, req->AffectedSOPInstanceUID ) != 0 )
{
rsp->DimseStatus = STATUS_STORE_Error_DataSetDoesNotMatchSOPClass;
}
}
}
}
return;
}
OFCondition QtDcmMoveScu::subOpSCP ( T_ASC_Association **subAssoc, void * subOpCallbackData )
{
T_DIMSE_Message msg;
T_ASC_PresentationContextID presID;
if ( !ASC_dataWaiting ( *subAssoc, 0 ) ) /* just in case */
return DIMSE_NODATAAVAILABLE;
OFCondition cond = DIMSE_receiveCommand ( *subAssoc, DIMSE_BLOCKING, 0, &presID, &msg, NULL );
if ( cond == EC_Normal )
{
switch ( msg.CommandField )
{
case DIMSE_C_STORE_RQ:
cond = storeSCP ( *subAssoc, &msg, presID, subOpCallbackData );
break;
case DIMSE_C_ECHO_RQ:
cond = echoSCP ( *subAssoc, &msg, presID );
break;
default:
cond = DIMSE_BADCOMMANDTYPE;
break;
}
}
/* clean up on association termination */
if ( cond == DUL_PEERREQUESTEDRELEASE )
{
cond = ASC_acknowledgeRelease ( *subAssoc );
ASC_dropSCPAssociation ( *subAssoc );
ASC_destroyAssociation ( subAssoc );
return cond;
}
else if ( cond == DUL_PEERABORTEDASSOCIATION )
{
}
else if ( cond != EC_Normal )
{
OFString temp_str;
qDebug() << "DIMSE failure (aborting sub-association): " << DimseCondition::dump ( temp_str, cond ).c_str();
/* some kind of error so abort the association */
cond = ASC_abortAssociation ( *subAssoc );
}
if ( cond != EC_Normal )
{
ASC_dropAssociation ( *subAssoc );
ASC_destroyAssociation ( subAssoc );
}
return cond;
}
void QtDcmMoveScu::subOpCallback ( void * caller, T_ASC_Network *aNet, T_ASC_Association **subAssoc )
{
if ( !caller )
return;
QtDcmMoveScu * self = ( QtDcmMoveScu* ) caller;
// if ( self->getMode() == QtDcmMoveScu::IMPORT )
if ( self->slicesCount )
emit self->updateProgress ( self->progressTotal + ( int ) ( ( ( float ) ( self->step * ( self->progressSerie ) / self->slicesCount ) ) ) );
if ( aNet == NULL ) return; /* help no net ! */
if ( *subAssoc == NULL )
{
/* negotiate association */
acceptSubAssoc ( aNet, subAssoc );
}
else
{
/* be a service class provider */
subOpSCP ( subAssoc, caller );
}
self->progressSerie ++;
}
void QtDcmMoveScu::moveCallback ( void *caller, T_DIMSE_C_MoveRQ * req, int responseCount, T_DIMSE_C_MoveRSP * rsp )
{
if ( !caller )
return;
QtDcmMoveScu * self = ( QtDcmMoveScu* ) caller;
if ( responseCount == 1 )
self->progressSerie = 0;
self->slicesCount = rsp->NumberOfRemainingSubOperations + rsp->NumberOfFailedSubOperations + rsp->NumberOfWarningSubOperations + rsp->NumberOfCompletedSubOperations;
OFCondition cond = EC_Normal;
OFString temp_str;
DIMSE_dumpMessage ( temp_str, *rsp, DIMSE_INCOMING );
qDebug() << "Move Response " << responseCount << ":";
qDebug() << QString ( temp_str.c_str() );
}
void QtDcmMoveScu::substituteOverrideKeys ( DcmDataset *dset )
{
if ( overrideKeys == NULL )
{
return; /* nothing to do */
}
/* copy the override keys */
DcmDataset keys ( *overrideKeys );
/* put the override keys into dset replacing existing tags */
unsigned long elemCount = keys.card();
for ( unsigned long i = 0; i < elemCount; i++ )
{
DcmElement *elem = keys.remove ( ( unsigned long ) 0 );
dset->insert ( elem, OFTrue );
}
}
OFCondition QtDcmMoveScu::moveSCU ( T_ASC_Association * assoc, const char *fname )
{
T_DIMSE_C_MoveRQ req;
T_DIMSE_C_MoveRSP rsp;
DIC_US msgId = assoc->nextMsgID++;
DcmDataset *rspIds = NULL;
const char *sopClass;
DcmDataset *statusDetail = NULL;
QuerySyntax querySyntax[3] =
{
{ UID_FINDPatientRootQueryRetrieveInformationModel,
UID_MOVEPatientRootQueryRetrieveInformationModel },
{ UID_FINDStudyRootQueryRetrieveInformationModel,
UID_MOVEStudyRootQueryRetrieveInformationModel },
{ UID_RETIRED_FINDPatientStudyOnlyQueryRetrieveInformationModel,
UID_RETIRED_MOVEPatientStudyOnlyQueryRetrieveInformationModel }
};
DcmFileFormat file;
if ( fname != NULL )
{
if ( file.loadFile ( fname ).bad() )
{
qDebug() << "bad DICOM file: " << QString ( fname );
return DIMSE_BADDATA;
}
}
/* replace specific keys by those in overrideKeys */
substituteOverrideKeys ( file.getDataset() );
sopClass = querySyntax[queryModel].moveSyntax;
/* which presentation context should be used */
presId = ASC_findAcceptedPresentationContextID ( assoc, sopClass );
if ( presId == 0 ) return DIMSE_NOVALIDPRESENTATIONCONTEXTID;