-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathgdcmImageHelper.cxx
2943 lines (2802 loc) · 107 KB
/
gdcmImageHelper.cxx
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
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmImageHelper.h"
#include "gdcmMediaStorage.h"
#include "gdcmFile.h"
#include "gdcmDataSet.h"
#include "gdcmDataElement.h"
#include "gdcmItem.h"
#include "gdcmSequenceOfItems.h"
#include "gdcmGlobal.h"
#include "gdcmDictEntry.h"
#include "gdcmDicts.h"
#include "gdcmAttribute.h"
#include "gdcmImage.h"
#include "gdcmDirectionCosines.h"
#include "gdcmSegmentedPaletteColorLookupTable.h"
#include "gdcmByteValue.h"
#include <cmath> // fabs
/* TODO:
*
* (0028,9145) SQ (Sequence with undefined length #=1) # u/l, 1 PixelValueTransformationSequence
* (fffe,e000) na (Item with undefined length #=3) # u/l, 1 Item
* (0028,1052) DS [0.00000] # 8, 1 RescaleIntercept
* (0028,1053) DS [1.00000] # 8, 1 RescaleSlope
* (0028,1054) LO [US] # 2, 1 RescaleType
* (fffe,e00d) na (ItemDelimitationItem) # 0, 0 ItemDelimitationItem
* (fffe,e0dd) na (SequenceDelimitationItem) # 0, 0 SequenceDelimitationItem
*
* Same goes for window level...
*/
namespace gdcm
{
bool ImageHelper::ForceRescaleInterceptSlope = false;
bool ImageHelper::PMSRescaleInterceptSlope = true;
bool ImageHelper::ForcePixelSpacing = false;
static bool GetOriginValueFromSequence(const DataSet& ds, const Tag& tfgs, std::vector<double> &ori)
{
if( !ds.FindDataElement( tfgs ) ) return false;
//const SequenceOfItems * sqi = ds.GetDataElement( tfgs ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi = ds.GetDataElement( tfgs ).GetValueAsSQ();
if( !(sqi && sqi->GetNumberOfItems() > 0) ) return false;
// Get first item:
const Item &item = sqi->GetItem(1);
const DataSet & subds = item.GetNestedDataSet();
// Plane position Sequence
const Tag tpms(0x0020,0x9113);
if( !subds.FindDataElement(tpms) ) return false;
//const SequenceOfItems * sqi2 = subds.GetDataElement( tpms ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi2 = subds.GetDataElement( tpms ).GetValueAsSQ();
if( sqi2 && !sqi2->IsEmpty() )
{
const Item &item2 = sqi2->GetItem(1);
const DataSet & subds2 = item2.GetNestedDataSet();
//
const Tag tps(0x0020,0x0032);
if( !subds2.FindDataElement(tps) ) return false;
const DataElement &de = subds2.GetDataElement( tps );
//assert( bv );
Attribute<0x0020,0x0032> at;
at.SetFromDataElement( de );
//at.Print( std::cout );
ori.push_back( at.GetValue(0) );
ori.push_back( at.GetValue(1) );
ori.push_back( at.GetValue(2) );
return true;
}
return false;
}
static bool GetDirectionCosinesValueFromSequence(const DataSet& ds, const Tag& tfgs, std::vector<double> &dircos)
{
if( !ds.FindDataElement( tfgs ) ) return false;
//const SequenceOfItems * sqi = ds.GetDataElement( tfgs ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi = ds.GetDataElement( tfgs ).GetValueAsSQ();
if( !(sqi && sqi->GetNumberOfItems() > 0) ) return false;
// Get first item:
const Item &item = sqi->GetItem(1);
const DataSet & subds = item.GetNestedDataSet();
// Plane position Sequence
const Tag tpms(0x0020,0x9116);
if( !subds.FindDataElement(tpms) ) return false;
//const SequenceOfItems * sqi2 = subds.GetDataElement( tpms ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi2 = subds.GetDataElement( tpms ).GetValueAsSQ();
if( !(sqi2 && sqi2->GetNumberOfItems()) ) return false;
assert( sqi2 && sqi2->GetNumberOfItems() );
// Take it from the first item
const Item &item2 = sqi2->GetItem(1);
const DataSet & subds2 = item2.GetNestedDataSet();
//
const Tag tps(0x0020,0x0037);
if( !subds2.FindDataElement(tps) ) return false;
const DataElement &de = subds2.GetDataElement( tps );
//assert( bv );
Attribute<0x0020,0x0037> at;
at.SetFromDataElement( de );
dircos.push_back( at.GetValue(0) );
dircos.push_back( at.GetValue(1) );
dircos.push_back( at.GetValue(2) );
dircos.push_back( at.GetValue(3) );
dircos.push_back( at.GetValue(4) );
dircos.push_back( at.GetValue(5) );
return true;
}
static bool GetInterceptSlopeValueFromSequence(const DataSet& ds, const Tag& tfgs, std::vector<double> &intslope)
{
if( !ds.FindDataElement( tfgs ) ) return false;
//const SequenceOfItems * sqi = ds.GetDataElement( tfgs ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi = ds.GetDataElement( tfgs ).GetValueAsSQ();
if( !(sqi && sqi->GetNumberOfItems() > 0) ) return false;
// Get first item:
const Item &item = sqi->GetItem(1);
const DataSet & subds = item.GetNestedDataSet();
// (0028,9145) SQ (Sequence with undefined length) # u/l,1 Pixel Value Transformation Sequence
const Tag tpms(0x0028,0x9145);
if( !subds.FindDataElement(tpms) ) return false;
//const SequenceOfItems * sqi2 = subds.GetDataElement( tpms ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi2 = subds.GetDataElement( tpms ).GetValueAsSQ();
assert( sqi2 );
const Item &item2 = sqi2->GetItem(1);
const DataSet & subds2 = item2.GetNestedDataSet();
{
// (0028,1052) DS [0] # 2,1 Rescale Intercept
const Tag tps(0x0028,0x1052);
if( !subds2.FindDataElement(tps) ) return false;
const DataElement &de = subds2.GetDataElement( tps );
//assert( bv );
Attribute<0x0028,0x1052> at;
at.SetFromDataElement( de );
//at.Print( std::cout );
intslope.push_back( at.GetValue() );
}
{
// (0028,1053) DS [5.65470085470085] # 16,1 Rescale Slope
const Tag tps(0x0028,0x1053);
if( !subds2.FindDataElement(tps) ) return false;
const DataElement &de = subds2.GetDataElement( tps );
//assert( bv );
Attribute<0x0028,0x1053> at;
at.SetFromDataElement( de );
//at.Print( std::cout );
intslope.push_back( at.GetValue() );
}
assert( intslope.size() == 2 );
return true;
}
static bool ComputeZSpacingFromIPP(const DataSet &ds, double &zspacing)
{
// first we need to get the direction cosines:
const Tag t1(0x5200,0x9229);
const Tag t2(0x5200,0x9230);
std::vector<double> cosines;
// For some reason TOSHIBA-EnhancedCT.dcm is storing the direction cosines in the per-frame section
// and not the shared one... oh well
bool b1 = GetDirectionCosinesValueFromSequence(ds, t1, cosines)
|| GetDirectionCosinesValueFromSequence(ds,t2,cosines);
if(!b1)
{
cosines.resize( 6 );
bool b2 = ImageHelper::GetDirectionCosinesFromDataSet(ds, cosines);
if( b2 )
{
gdcmWarningMacro( "Image Orientation (Patient) cannot be stored here!. Continuing" );
b1 = b2;
}
else
{
gdcmErrorMacro( "Image Orientation (Patient) was not found" );
cosines[0] = 1;
cosines[1] = 0;
cosines[2] = 0;
cosines[3] = 0;
cosines[4] = 1;
cosines[5] = 0;
}
}
assert( b1 && cosines.size() == 6 ); // yeah we really need that
const Tag tfgs(0x5200,0x9230);
if( !ds.FindDataElement( tfgs ) ) return false;
//const SequenceOfItems * sqi = ds.GetDataElement( tfgs ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi = ds.GetDataElement( tfgs ).GetValueAsSQ();
if( !sqi ) return false;
assert( sqi );
double normal[3];
DirectionCosines dc( cosines.data() );
dc.Cross( normal );
// For each item
SequenceOfItems::SizeType nitems = sqi->GetNumberOfItems();
if( nitems > 1 ) {
std::vector<double> dircos_subds2; dircos_subds2.resize(6);
std::vector<double> distances;
for(SequenceOfItems::SizeType i0 = 1; i0 <= nitems; ++i0)
{
const Item &item = sqi->GetItem(i0);
const DataSet & subds = item.GetNestedDataSet();
// (0020,9113) SQ (Sequence with undefined length #=1) # u/l, 1 PlanePositionSequence
const Tag tpms(0x0020,0x9113);
if( !subds.FindDataElement(tpms) ) return false;
//const SequenceOfItems * sqi2 = subds.GetDataElement( tpms ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi2 = subds.GetDataElement( tpms ).GetValueAsSQ();
assert( sqi2 );
const Item &item2 = sqi2->GetItem(1);
const DataSet & subds2 = item2.GetNestedDataSet();
// Check Image Orientation (Patient)
if( ImageHelper::GetDirectionCosinesFromDataSet(subds2, dircos_subds2) )
{
assert( dircos_subds2 == cosines );
}
// (0020,0032) DS [-82.5\-82.5\1153.75] # 20, 3 ImagePositionPatient
const Tag tps(0x0020,0x0032);
if( !subds2.FindDataElement(tps) ) return false;
const DataElement &de = subds2.GetDataElement( tps );
Attribute<0x0020,0x0032> ipp;
ipp.SetFromDataElement(de);
double dist = 0;
for (int i = 0; i < 3; ++i) dist += normal[i]*ipp[i];
distances.push_back( dist );
}
assert( distances.size() == nitems );
double meanspacing = 0;
double prev = distances[0];
for(unsigned int i = 1; i < nitems; ++i)
{
const double current = distances[i] - prev;
meanspacing += current;
prev = distances[i];
}
bool timeseries = false;
if( nitems > 1 )
{
meanspacing /= (double)(nitems - 1);
if( meanspacing == 0.0 )
{
// Could be a time series. Assume time spacing of 1. for now:
gdcmDebugMacro( "Assuming time series for Z-spacing" );
meanspacing = 1.0;
timeseries = true;
}
}
zspacing = meanspacing;
if( nitems > 1 )
assert( zspacing != 0.0 ); // technically this should not happen
if( !timeseries )
{
// Check spacing is consistent:
const double ZTolerance = 1e-3; // ??? FIXME
prev = distances[0];
for(unsigned int i = 1; i < nitems; ++i)
{
const double current = distances[i] - prev;
if( fabs(current - zspacing) > ZTolerance )
{
// For now simply gives up
gdcmErrorMacro( "This Enhanced Multiframe is not supported for now. Sorry" );
return false;
}
prev = distances[i];
}
}
} else {
// single slice, this is not an error to not find the zspacing in this case.
zspacing = 1.0;
const Tag tfgs0(0x5200,0x9229);
if( !ds.FindDataElement( tfgs0 ) ) return true;
SmartPointer<SequenceOfItems> sqi0 = ds.GetDataElement( tfgs0 ).GetValueAsSQ();
if( !(sqi0 && sqi0->GetNumberOfItems() > 0) ) return true;
// Get first item:
const Item &item = sqi0->GetItem(1);
const DataSet & subds = item.GetNestedDataSet();
// <entry group="0028" element="9110" vr="SQ" vm="1" name="Pixel Measures Sequence"/>
const Tag tpms(0x0028,0x9110);
if( !subds.FindDataElement(tpms) ) return true;
//const SequenceOfItems * sqi2 = subds.GetDataElement( tpms ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi2 = subds.GetDataElement( tpms ).GetValueAsSQ();
assert( sqi2 );
const Item &item2 = sqi2->GetItem(1);
const DataSet & subds2 = item2.GetNestedDataSet();
// <entry group="0028" element="0030" vr="DS" vm="2" name="Pixel Spacing"/>
const Tag tps(0x0018,0x0088);
if( !subds2.FindDataElement(tps) ) return true;
const DataElement &de = subds2.GetDataElement( tps );
Attribute<0x0018,0x0088> at;
at.SetFromDataElement( de );
zspacing = at.GetValue();
}
return true;
}
// EnhancedMRImageStorage & EnhancedCTImageStorage
static bool GetSpacingValueFromSequence(const DataSet& ds, const Tag& tfgs, std::vector<double> &sp)
{
// (0028,9110) SQ (Sequence with undefined length #=1) # u/l, 1 PixelMeasuresSequence
// (fffe,e000) na (Item with undefined length #=2) # u/l, 1 Item
// (0018,0050) DS [0.5] # 4, 1 SliceThickness
// (0028,0030) DS [0.322\0.322] # 12, 2 PixelSpacing
// <entry group="5200" element="9229" vr="SQ" vm="1" name="Shared Functional Groups Sequence"/>
//const Tag tfgs(0x5200,0x9229);
//const Tag tfgs(0x5200,0x9230);
//assert( ds.FindDataElement( tfgs ) );
if( !ds.FindDataElement( tfgs ) ) return false;
//const SequenceOfItems * sqi = ds.GetDataElement( tfgs ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi = ds.GetDataElement( tfgs ).GetValueAsSQ();
if( !(sqi && sqi->GetNumberOfItems() > 0) ) return false;
// Get first item:
const Item &item = sqi->GetItem(1);
const DataSet & subds = item.GetNestedDataSet();
// <entry group="0028" element="9110" vr="SQ" vm="1" name="Pixel Measures Sequence"/>
const Tag tpms(0x0028,0x9110);
if( !subds.FindDataElement(tpms) ) return false;
//const SequenceOfItems * sqi2 = subds.GetDataElement( tpms ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi2 = subds.GetDataElement( tpms ).GetValueAsSQ();
assert( sqi2 );
const Item &item2 = sqi2->GetItem(1);
const DataSet & subds2 = item2.GetNestedDataSet();
// <entry group="0028" element="0030" vr="DS" vm="2" name="Pixel Spacing"/>
const Tag tps(0x0028,0x0030);
if( !subds2.FindDataElement(tps) ) return false;
const DataElement &de = subds2.GetDataElement( tps );
//assert( bv );
Attribute<0x0028,0x0030> at;
at.SetFromDataElement( de );
//at.Print( std::cout );
sp.push_back( at.GetValue(1) );
sp.push_back( at.GetValue(0) );
// BUG ! Check for instance:
// gdcmData/BRTUM001.dcm
// Slice Thickness is 5.0 while the Zspacing should be 6.0 !
#if 0
// Do the 3rd dimension zspacing:
// <entry group="0018" element="0050" vr="DS" vm="1" name="Slice Thickness"/>
const Tag tst(0x0018,0x0050);
if( !subds2.FindDataElement(tst) ) return false;
const DataElement &de2 = subds2.GetDataElement( tst );
Attribute<0x0018,0x0050> at2;
at2.SetFromDataElement( de2 );
//at2.Print( std::cout );
sp.push_back( at2.GetValue(0) );
#endif
double zspacing;
bool b = ComputeZSpacingFromIPP(ds, zspacing);
if( !b ) return false;
sp.push_back( zspacing );
return true;
}
static SmartPointer<SequenceOfItems> InsertOrReplaceSQ( DataSet & ds, const Tag &tag )
{
SmartPointer<SequenceOfItems> sqi;
if( !ds.FindDataElement( tag ) )
{
sqi = new SequenceOfItems;
DataElement de( tag );
de.SetVR( VR::SQ );
de.SetValue( *sqi );
assert( de.GetVL().IsUndefined() );
de.SetVLToUndefined();
ds.Insert( de );
}
sqi = ds.GetDataElement( tag ).GetValueAsSQ();
sqi->SetLengthToUndefined();
DataElement de_dup = ds.GetDataElement( tag );
de_dup.SetValue( *sqi );
ds.Replace( de_dup );
return sqi;
}
// UltrasoundMultiframeImageStorage
static bool GetUltraSoundSpacingValueFromSequence(const DataSet& ds, std::vector<double> &sp)
{
/*
(0018,6011) SQ (Sequence with explicit length #=1) # 196, 1 SequenceOfUltrasoundRegions
(fffe,e000) na (Item with explicit length #=15) # 188, 1 Item
(0018,6012) US 1 # 2, 1 RegionSpatialFormat
(0018,6014) US 1 # 2, 1 RegionDataType
(0018,6016) UL 0 # 4, 1 RegionFlags
(0018,6018) UL 0 # 4, 1 RegionLocationMinX0
(0018,601a) UL 0 # 4, 1 RegionLocationMinY0
(0018,601c) UL 479 # 4, 1 RegionLocationMaxX1
(0018,601e) UL 479 # 4, 1 RegionLocationMaxY1
(0018,6020) SL 0 # 4, 1 ReferencePixelX0
(0018,6022) SL 0 # 4, 1 ReferencePixelY0
(0018,6024) US 3 # 2, 1 PhysicalUnitsXDirection
(0018,6026) US 3 # 2, 1 PhysicalUnitsYDirection
(0018,6028) FD 0 # 8, 1 ReferencePixelPhysicalValueX
(0018,602a) FD 0 # 8, 1 ReferencePixelPhysicalValueY
(0018,602c) FD 0.002 # 8, 1 PhysicalDeltaX
(0018,602e) FD 0.002 # 8, 1 PhysicalDeltaY
(fffe,e00d) na (ItemDelimitationItem for re-encoding) # 0, 0 ItemDelimitationItem
(fffe,e0dd) na (SequenceDelimitationItem for re-encod.) # 0, 0 SequenceDelimitationItem
*/
const Tag tsqusreg(0x0018,0x6011);
if( !ds.FindDataElement( tsqusreg ) ) return false;
//const SequenceOfItems * sqi = ds.GetDataElement( tsqusreg ).GetSequenceOfItems();
SmartPointer<SequenceOfItems> sqi = ds.GetDataElement( tsqusreg ).GetValueAsSQ();
if( !sqi ) return false;
assert( sqi );
// Get first item:
const Item &item = sqi->GetItem(1);
const DataSet & subds = item.GetNestedDataSet();
// (0018,602c) FD 0.002 # 8, 1 PhysicalDeltaX
// (0018,602e) FD 0.002 # 8, 1 PhysicalDeltaY
Attribute<0x0018,0x602c> at1;
Attribute<0x0018,0x602e> at2;
const DataElement &de1 = subds.GetDataElement( at1.GetTag() );
at1.SetFromDataElement( de1 );
assert( at1.GetNumberOfValues() == 1 );
const DataElement &de2 = subds.GetDataElement( at2.GetTag() );
at2.SetFromDataElement( de2 );
assert( at2.GetNumberOfValues() == 1 );
sp.push_back( at1.GetValue() );
sp.push_back( at2.GetValue() );
return true;
}
static void SetUltraSoundSpacingValueFromSequence(DataSet& ds, std::vector<double> const &spacing)
{
/*
(0018,6011) SQ (Sequence with explicit length #=1) # 196, 1 SequenceOfUltrasoundRegions
(fffe,e000) na (Item with explicit length #=15) # 188, 1 Item
(0018,6012) US 1 # 2, 1 RegionSpatialFormat
(0018,6014) US 1 # 2, 1 RegionDataType
(0018,6016) UL 0 # 4, 1 RegionFlags
(0018,6018) UL 0 # 4, 1 RegionLocationMinX0
(0018,601a) UL 0 # 4, 1 RegionLocationMinY0
(0018,601c) UL 479 # 4, 1 RegionLocationMaxX1
(0018,601e) UL 479 # 4, 1 RegionLocationMaxY1
(0018,6020) SL 0 # 4, 1 ReferencePixelX0
(0018,6022) SL 0 # 4, 1 ReferencePixelY0
(0018,6024) US 3 # 2, 1 PhysicalUnitsXDirection
(0018,6026) US 3 # 2, 1 PhysicalUnitsYDirection
(0018,6028) FD 0 # 8, 1 ReferencePixelPhysicalValueX
(0018,602a) FD 0 # 8, 1 ReferencePixelPhysicalValueY
(0018,602c) FD 0.002 # 8, 1 PhysicalDeltaX
(0018,602e) FD 0.002 # 8, 1 PhysicalDeltaY
(fffe,e00d) na (ItemDelimitationItem for re-encoding) # 0, 0 ItemDelimitationItem
(fffe,e0dd) na (SequenceDelimitationItem for re-encod.) # 0, 0 SequenceDelimitationItem
*/
const Tag tsqusreg(0x0018,0x6011);
SmartPointer<SequenceOfItems> sqi = InsertOrReplaceSQ( ds, tsqusreg);
if( !sqi->GetNumberOfItems() )
{
Item item; //( Tag(0xfffe,0xe000) );
sqi->AddItem( item );
}
Item &item1 = sqi->GetItem(1);
item1.SetVLToUndefined();
DataSet &subds = item1.GetNestedDataSet();
// (0018,602c) FD 0.002 # 8, 1 PhysicalDeltaX
// (0018,602e) FD 0.002 # 8, 1 PhysicalDeltaY
Attribute<0x0018,0x602c> at1;
at1.SetValue( spacing[0] );
Attribute<0x0018,0x602e> at2;
at2.SetValue( spacing[1] );
subds.Replace( at1.GetAsDataElement() );
subds.Replace( at2.GetAsDataElement() );
}
/* Enhanced stuff looks like:
(0020,9113) SQ (Sequence with undefined length #=1) # u/l, 1 PlanePositionSequence
(fffe,e000) na (Item with undefined length #=1) # u/l, 1 Item
(0020,0032) DS [73.5100815890831\-129.65028828174\189.777023529388] # 50, 3 ImagePositionPatient
(fffe,e00d) na (ItemDelimitationItem) # 0, 0 ItemDelimitationItem
(fffe,e0dd) na (SequenceDelimitationItem) # 0, 0 SequenceDelimitationItem
(0020,9116) SQ (Sequence with undefined length #=1) # u/l, 1 PlaneOrientationSequence
(fffe,e000) na (Item with undefined length #=1) # u/l, 1 Item
(0020,0037) DS [0.01604138687252\0.99942564964294\-0.0298495516180\0.0060454937629... # 102, 6 ImageOrientationPatient
(fffe,e00d) na (ItemDelimitationItem) # 0, 0 ItemDelimitationItem
(fffe,e0dd) na (SequenceDelimitationItem) # 0, 0 SequenceDelimitationItem
(0028,9110) SQ (Sequence with undefined length #=1) # u/l, 1 PixelMeasuresSequence
(fffe,e000) na (Item with undefined length #=2) # u/l, 1 Item
(0018,0050) DS [1] # 2, 1 SliceThickness
(0028,0030) DS [0.83333331346511\0.83333331346511] # 34, 2 PixelSpacing
(fffe,e00d) na (ItemDelimitationItem) # 0, 0 ItemDelimitationItem
(fffe,e0dd) na (SequenceDelimitationItem) # 0, 0 SequenceDelimitationItem
*/
std::vector<double> ImageHelper::GetOriginValue(File const & f)
{
std::vector<double> ori;
MediaStorage ms;
ms.SetFromFile(f);
const DataSet& ds = f.GetDataSet();
if( ms == MediaStorage::EnhancedCTImageStorage
|| ms == MediaStorage::EnhancedMRImageStorage
|| ms == MediaStorage::EnhancedMRColorImageStorage
|| ms == MediaStorage::EnhancedPETImageStorage
|| ms == MediaStorage::OphthalmicTomographyImageStorage
|| ms == MediaStorage::MultiframeSingleBitSecondaryCaptureImageStorage
|| ms == MediaStorage::MultiframeGrayscaleByteSecondaryCaptureImageStorage
|| ms == MediaStorage::MultiframeGrayscaleWordSecondaryCaptureImageStorage
|| ms == MediaStorage::MultiframeTrueColorSecondaryCaptureImageStorage
|| ms == MediaStorage::XRay3DAngiographicImageStorage
|| ms == MediaStorage::XRay3DCraniofacialImageStorage
|| ms == MediaStorage::SegmentationStorage
|| ms == MediaStorage::IVOCTForProcessing
|| ms == MediaStorage::IVOCTForPresentation
|| ms == MediaStorage::BreastTomosynthesisImageStorage
|| ms == MediaStorage::BreastProjectionXRayImageStorageForPresentation
|| ms == MediaStorage::BreastProjectionXRayImageStorageForProcessing
|| ms == MediaStorage::LegacyConvertedEnhancedMRImageStorage
|| ms == MediaStorage::LegacyConvertedEnhancedCTImageStorage
|| ms == MediaStorage::LegacyConvertedEnhancedPETImageStorage )
{
const Tag t1(0x5200,0x9229);
const Tag t2(0x5200,0x9230);
if( GetOriginValueFromSequence(ds,t1, ori)
|| GetOriginValueFromSequence(ds, t2, ori) )
{
assert( ori.size() == 3 );
return ori;
}
ori.resize( 3 );
gdcmWarningMacro( "Could not find Origin" );
return ori;
}
if( ms == MediaStorage::NuclearMedicineImageStorage )
{
const Tag t1(0x0054,0x0022);
if( ds.FindDataElement( t1 ) )
{
SmartPointer<SequenceOfItems> sqi = ds.GetDataElement( t1 ).GetValueAsSQ();
if( sqi && sqi->GetNumberOfItems() >= 1)
{
// Get first item:
const Item &item = sqi->GetItem(1);
const DataSet & subds = item.GetNestedDataSet();
const Tag timagepositionpatient(0x0020, 0x0032);
assert( subds.FindDataElement( timagepositionpatient ) );
Attribute<0x0020,0x0032> at = {{0,0,0}}; // default value if empty
at.SetFromDataSet( subds );
ori.resize( at.GetNumberOfValues() );
for( unsigned int i = 0; i < at.GetNumberOfValues(); ++i )
{
ori[i] = at.GetValue(i);
}
return ori;
}
}
ori.resize( 3 );
gdcmWarningMacro( "Could not find Origin" );
return ori;
}
ori.resize( 3 );
// else
const Tag timagepositionpatient(0x0020, 0x0032);
if( ms != MediaStorage::SecondaryCaptureImageStorage && ds.FindDataElement( timagepositionpatient ) )
{
const DataElement& de = ds.GetDataElement( timagepositionpatient );
Attribute<0x0020,0x0032> at = {{0,0,0}}; // default value if empty
at.SetFromDataElement( de );
for( unsigned int i = 0; i < at.GetNumberOfValues(); ++i )
{
ori[i] = at.GetValue(i);
}
}
else
{
ori[0] = 0;
ori[1] = 0;
ori[2] = 0;
}
assert( ori.size() == 3 );
return ori;
}
bool ImageHelper::GetDirectionCosinesFromDataSet(DataSet const & ds, std::vector<double> & dircos)
{
// \precondition: this dataset is not a secondary capture !
// else
const Tag timageorientationpatient(0x0020, 0x0037);
if( ds.FindDataElement( timageorientationpatient ) /*&& !ds.GetDataElement( timageorientationpatient ).IsEmpty()*/ )
{
const DataElement& de = ds.GetDataElement( timageorientationpatient );
Attribute<0x0020,0x0037> at = {{1,0,0,0,1,0}}; // default value if empty
at.SetFromDataElement( de );
for( unsigned int i = 0; i < at.GetNumberOfValues(); ++i )
{
dircos[i] = at.GetValue(i);
}
DirectionCosines dc( dircos.data() );
if( !dc.IsValid() )
{
dc.Normalize();
if( dc.IsValid() )
{
gdcmWarningMacro( "DirectionCosines was not normalized. Fixed" );
const double * p = dc;
dircos = std::vector<double>(p, p + 6);
//return dircos;
}
else
{
// PAPYRUS_CR_InvalidIOP.dcm
gdcmWarningMacro( "Could not get DirectionCosines. Will be set to unit vector." );
//dircos[0] = 1;
//dircos[1] = 0;
//dircos[2] = 0;
//dircos[3] = 0;
//dircos[4] = 1;
//dircos[5] = 0;
return false;
}
}
return true;
}
return false;
}
std::vector<double> ImageHelper::GetDirectionCosinesValue(File const & f)
{
std::vector<double> dircos;
MediaStorage ms;
ms.SetFromFile(f);
const DataSet& ds = f.GetDataSet();
if( ms == MediaStorage::EnhancedCTImageStorage
|| ms == MediaStorage::EnhancedMRImageStorage
|| ms == MediaStorage::EnhancedMRColorImageStorage
|| ms == MediaStorage::EnhancedPETImageStorage
|| ms == MediaStorage::MultiframeSingleBitSecondaryCaptureImageStorage
|| ms == MediaStorage::MultiframeGrayscaleByteSecondaryCaptureImageStorage
|| ms == MediaStorage::MultiframeGrayscaleWordSecondaryCaptureImageStorage
|| ms == MediaStorage::MultiframeTrueColorSecondaryCaptureImageStorage
|| ms == MediaStorage::XRay3DAngiographicImageStorage
|| ms == MediaStorage::XRay3DCraniofacialImageStorage
|| ms == MediaStorage::SegmentationStorage
|| ms == MediaStorage::IVOCTForPresentation
|| ms == MediaStorage::IVOCTForProcessing
|| ms == MediaStorage::BreastTomosynthesisImageStorage
|| ms == MediaStorage::BreastProjectionXRayImageStorageForPresentation
|| ms == MediaStorage::BreastProjectionXRayImageStorageForProcessing
|| ms == MediaStorage::LegacyConvertedEnhancedMRImageStorage
|| ms == MediaStorage::LegacyConvertedEnhancedCTImageStorage
|| ms == MediaStorage::LegacyConvertedEnhancedPETImageStorage )
{
const Tag t1(0x5200,0x9229);
const Tag t2(0x5200,0x9230);
if( GetDirectionCosinesValueFromSequence(ds,t1, dircos)
|| GetDirectionCosinesValueFromSequence(ds, t2, dircos) )
{
assert( dircos.size() == 6 );
return dircos;
}
else
{
dircos.resize( 6 );
bool b2 = ImageHelper::GetDirectionCosinesFromDataSet(ds, dircos);
if( b2 )
{
gdcmWarningMacro( "Image Orientation (Patient) cannot be stored here!. Continuing" );
}
else
{
gdcmErrorMacro( "Image Orientation (Patient) was not found" );
dircos[0] = 1;
dircos[1] = 0;
dircos[2] = 0;
dircos[3] = 0;
dircos[4] = 1;
dircos[5] = 0;
}
return dircos;
}
}
if( ms == MediaStorage::NuclearMedicineImageStorage )
{
const Tag t1(0x0054,0x0022);
if( ds.FindDataElement( t1 ) )
{
SmartPointer<SequenceOfItems> sqi = ds.GetDataElement( t1 ).GetValueAsSQ();
if( sqi && sqi->GetNumberOfItems() >= 1 )
{
// Get first item:
const Item &item = sqi->GetItem(1);
const DataSet & subds = item.GetNestedDataSet();
dircos.resize( 6 );
bool b2 = ImageHelper::GetDirectionCosinesFromDataSet(subds, dircos);
if( b2 )
{
}
else
{
gdcmErrorMacro( "Image Orientation (Patient) was not found" );
dircos[0] = 1;
dircos[1] = 0;
dircos[2] = 0;
dircos[3] = 0;
dircos[4] = 1;
dircos[5] = 0;
}
return dircos;
}
}
}
dircos.resize( 6 );
if( ms == MediaStorage::SecondaryCaptureImageStorage || !GetDirectionCosinesFromDataSet(ds, dircos) )
{
dircos[0] = 1;
dircos[1] = 0;
dircos[2] = 0;
dircos[3] = 0;
dircos[4] = 1;
dircos[5] = 0;
}
assert( dircos.size() == 6 );
return dircos;
}
void ImageHelper::SetForceRescaleInterceptSlope(bool b)
{
ForceRescaleInterceptSlope = b;
}
bool ImageHelper::GetForceRescaleInterceptSlope()
{
return ForceRescaleInterceptSlope;
}
void ImageHelper::SetPMSRescaleInterceptSlope(bool b)
{
PMSRescaleInterceptSlope = b;
}
bool ImageHelper::GetPMSRescaleInterceptSlope()
{
return PMSRescaleInterceptSlope;
}
void ImageHelper::SetForcePixelSpacing(bool b)
{
ForcePixelSpacing = b;
}
bool ImageHelper::GetForcePixelSpacing()
{
return ForcePixelSpacing;
}
bool GetRescaleInterceptSlopeValueFromDataSet(const DataSet& ds, std::vector<double> & interceptslope)
{
Attribute<0x0028,0x1052> at1;
bool intercept = ds.FindDataElement(at1.GetTag());
if( intercept )
{
if( !ds.GetDataElement(at1.GetTag()).IsEmpty() )
{
at1.SetFromDataElement( ds.GetDataElement(at1.GetTag()) );
interceptslope[0] = at1.GetValue();
}
}
Attribute<0x0028,0x1053> at2;
bool slope = ds.FindDataElement(at2.GetTag());
if ( slope )
{
if( !ds.GetDataElement(at2.GetTag()).IsEmpty() )
{
at2.SetFromDataElement( ds.GetDataElement(at2.GetTag()) );
interceptslope[1] = at2.GetValue();
if( interceptslope[1] == 0 )
{
// come' on ! WTF
gdcmDebugMacro( "Cannot have slope == 0. Defaulting to 1.0 instead" );
interceptslope[1] = 1;
}
}
}
return intercept || slope;
}
/// This function returns pixel information about an image from its dataset
/// That includes samples per pixel and bit depth (in that order)
/// Returns a PixelFormat
PixelFormat ImageHelper::GetPixelFormatValue(const File& f)
{
// D 0028|0011 [US] [Columns] [512]
//[10/20/10 9:05:07 AM] Mathieu Malaterre:
PixelFormat pf;
const DataSet& ds = f.GetDataSet();
// D 0028|0100 [US] [Bits Allocated] [16]
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0100) );
Attribute<0x0028,0x0100> at = { 0 };
at.SetFromDataSet( ds );
pf.SetBitsAllocated( at.GetValue() );
}
// D 0028|0101 [US] [Bits Stored] [12]
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0101) );
Attribute<0x0028,0x0101> at = { 0 };
at.SetFromDataSet( ds );
pf.SetBitsStored( at.GetValue() );
}
// D 0028|0102 [US] [High Bit] [11]
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0102) );
Attribute<0x0028,0x0102> at = { 0 };
at.SetFromDataSet( ds );
pf.SetHighBit( at.GetValue() );
}
// D 0028|0103 [US] [Pixel Representation] [0]
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0103) );
Attribute<0x0028,0x0103> at = { 0 };
at.SetFromDataSet( ds );
pf.SetPixelRepresentation( at.GetValue() );
}
// (0028,0002) US 1 # 2, 1 SamplesPerPixel
{
//if( ds.FindDataElement( Tag(0x0028, 0x0002) ) )
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0002) );
Attribute<0x0028,0x0002> at = { 1 };
at.SetFromDataSet( ds );
pf.SetSamplesPerPixel( at.GetValue() );
}
// else pf will default to 1...
}
return pf;
}
/// This function checks tags (0x0028, 0x0010) and (0x0028, 0x0011) for the
/// rows and columns of the image in pixels (as opposed to actual distances).
/// Also checks 0054, 0081 for the number of z slices for a 3D image
/// If that tag is not present, default the z dimension to 1
std::vector<unsigned int> ImageHelper::GetDimensionsValue(const File& f)
{
DataSet const & ds = f.GetDataSet();
MediaStorage ms;
ms.SetFromFile(f);
std::vector<unsigned int> theReturn(3);
#if 0
if( ms == MediaStorage::VLWholeSlideMicroscopyImageStorage )
{
{
Attribute<0x0048,0x0006> at = { 0 };
at.SetFromDataSet( ds );
theReturn[0] = at.GetValue();
}
{
Attribute<0x0048,0x0007> at = { 0 };
at.SetFromDataSet( ds );
theReturn[1] = at.GetValue();
}
theReturn[2] = 1;
}
else
#endif
{
{
Attribute<0x0028,0x0011> at = { 0 }; // Columns
at.SetFromDataSet( ds );
theReturn[0] = at.GetValue();
}
{
Attribute<0x0028,0x0010> at = { 0 }; // Rows
at.SetFromDataSet( ds );
theReturn[1] = at.GetValue();
}
{
Attribute<0x0028,0x0008> at = { 0 };
at.SetFromDataSet( ds );
int numberofframes = at.GetValue();
theReturn[2] = 1;
if( numberofframes > 1 )
{
theReturn[2] = at.GetValue();
}
}
// ACR-NEMA legacy
{
Attribute<0x0028,0x0005> at = { 0 };
if( ds.FindDataElement( at.GetTag() ) )
{
const DataElement &de = ds.GetDataElement( at.GetTag() );
// SIEMENS_MAGNETOM-12-MONO2-Uncompressed.dcm picks VR::SS instead...
if( at.GetVR().Compatible( de.GetVR() ) )
{
at.SetFromDataSet( ds );
int imagedimensions = at.GetValue();
if( imagedimensions == 3 )
{
Attribute<0x0028,0x0012> at2 = { 0 };
at2.SetFromDataSet( ds );
theReturn[2] = at2.GetValue();
}
}
else
{
gdcmWarningMacro( "Sorry cannot read attribute (wrong VR): " << at.GetTag() );
}
}
}
}
return theReturn;
}
void ImageHelper::SetDimensionsValue(File& f, const Pixmap & img)
{
const unsigned int *dims = img.GetDimensions();
MediaStorage ms;
ms.SetFromFile(f);
DataSet& ds = f.GetDataSet();
assert( MediaStorage::IsImage( ms ) );
{
Attribute<0x0028,0x0010> rows;
rows.SetValue( (uint16_t)dims[1] );
ds.Replace( rows.GetAsDataElement() );
Attribute<0x0028,0x0011> columns;
columns.SetValue( (uint16_t)dims[0] );
ds.Replace( columns.GetAsDataElement() );
Attribute<0x0028,0x0008> numframes = { 0 };
numframes.SetValue( dims[2] );
if( img.GetNumberOfDimensions() == 3 && dims[2] >= 1 )
{
if( ms.MediaStorage::GetModalityDimension() > 2 )
ds.Replace( numframes.GetAsDataElement() );
else if( ms.MediaStorage::GetModalityDimension() == 2 && dims[2] == 1 )
ds.Remove( numframes.GetTag() );
else
{
gdcmErrorMacro( "MediaStorage does not allow 3rd dimension. But value is: " << dims[2] );
gdcmAssertAlwaysMacro( "Could not set third dimension" );
}
}
else if( img.GetNumberOfDimensions() == 2 && dims[2] == 1 )
{
// This is a MF instances, need to set Number of Frame to 1 when Required
if (ms.MediaStorage::GetModalityDimension() > 2)
{
// Only include Multi-Frame when required (not Conditional):
if( ms == MediaStorage::XRayAngiographicImageStorage // A.14.3 XA Image IOD Module Table: Multi-frame C.7.6.6 C - Required if pixel data is Multi - frame Cine data
)
{
ds.Remove(numframes.GetTag());
}
else
{
ds.Replace(numframes.GetAsDataElement());
}
}
else if( ms.MediaStorage::GetModalityDimension() == 2 /*&& dims[2] == 1*/ )
ds.Remove( numframes.GetTag() );
}
else // cleanup
ds.Remove( numframes.GetTag() );
}
// cleanup pass:
if( ms == MediaStorage::EnhancedCTImageStorage
|| ms == MediaStorage::EnhancedMRImageStorage
|| ms == MediaStorage::EnhancedMRColorImageStorage
|| ms == MediaStorage::EnhancedPETImageStorage
|| ms == MediaStorage::MultiframeSingleBitSecondaryCaptureImageStorage
|| ms == MediaStorage::MultiframeGrayscaleByteSecondaryCaptureImageStorage
|| ms == MediaStorage::MultiframeGrayscaleWordSecondaryCaptureImageStorage
|| ms == MediaStorage::MultiframeTrueColorSecondaryCaptureImageStorage
|| ms == MediaStorage::XRay3DAngiographicImageStorage
|| ms == MediaStorage::XRay3DCraniofacialImageStorage
|| ms == MediaStorage::SegmentationStorage
|| ms == MediaStorage::IVOCTForProcessing