-
-
Notifications
You must be signed in to change notification settings - Fork 663
/
gdcmPixmapReader.cxx
1369 lines (1280 loc) · 48.2 KB
/
gdcmPixmapReader.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 "gdcmPixmapReader.h"
#include "gdcmExplicitDataElement.h"
#include "gdcmImplicitDataElement.h"
#include "gdcmValue.h"
#include "gdcmFileMetaInformation.h"
#include "gdcmElement.h"
#include "gdcmPhotometricInterpretation.h"
#include "gdcmSegmentedPaletteColorLookupTable.h"
#include "gdcmTransferSyntax.h"
#include "gdcmLookupTable.h"
#include "gdcmAttribute.h"
#include "gdcmIconImage.h"
#include "gdcmPrivateTag.h"
#include "gdcmJPEGCodec.h"
#include "gdcmImageHelper.h"
namespace gdcm
{
PixmapReader::PixmapReader():PixelData(new Pixmap)
{
}
PixmapReader::~PixmapReader()
= default;
const Pixmap& PixmapReader::GetPixmap() const
{
return *PixelData;
}
Pixmap& PixmapReader::GetPixmap()
{
return *PixelData;
}
//void PixmapReader::SetPixmap(Pixmap const &img)
//{
// PixelData = img;
//}
bool PixmapReader::Read()
{
if( !Reader::Read() )
{
// cemra_bug/IM-0001-0066.dcm
// will return from the parser with an error
// but a partial Pixel Data can be seen
return false;
}
const FileMetaInformation &header = F->GetHeader();
const DataSet &ds = F->GetDataSet();
const TransferSyntax &ts = header.GetDataSetTransferSyntax();
// Need to set the type of image we are dealing with:
PixelData->SetTransferSyntax( ts );
bool res = false;
/* Does it really make sense to check for Media Storage SOP Class UID?
* I need then to check consistency with 0008 0016 Instance SOP Class UID
* ... I don't think there is an end.
* I'd rather go the old way check a bunch of tags (From Image Plane
* Module).
*/
MediaStorage ms = header.GetMediaStorage();
bool isImage = MediaStorage::IsImage( ms );
if( isImage )
{
// I cannot leave this here, since ELSCINT1 / LOSSLESS RICE declares CT Image Storage,
// when in fact this is a private Media Storage (no Pixel Data element)
//assert( ds.FindDataElement( Tag(0x7fe0,0x0010 ) ) );
assert( ts != TransferSyntax::TS_END && ms != MediaStorage::MS_END );
// Good it's the easy case. It's declared as an Image:
//PixelData->SetCompressionFromTransferSyntax( ts );
res = ReadImage(ms);
}
//else if( ms == MediaStorage::MRSpectroscopyStorage )
// {
// res = ReadImage(ms);
// }
else
{
MediaStorage ms2 = ds.GetMediaStorage();
//assert( !ds.FindDataElement( Tag(0x7fe0,0x0010 ) ) );
if( ms == MediaStorage::MediaStorageDirectoryStorage && ms2 == MediaStorage::MS_END )
{
gdcmDebugMacro( "DICOM file is not an Image file but a : " <<
MediaStorage::GetMSString(ms) << " SOP Class UID" );
res = false;
}
else if( ms == ms2 && ms != MediaStorage::MS_END )
{
gdcmDebugMacro( "DICOM file is not an Image file but a : " <<
MediaStorage::GetMSString(ms) << " SOP Class UID" );
res = false;
}
else
{
if( ms2 != MediaStorage::MS_END )
{
bool isImage2 = MediaStorage::IsImage( ms2 );
if( isImage2 )
{
gdcmDebugMacro( "After all it might be a DICOM file "
"(Mallinckrodt-like)" );
//PixelData->SetCompressionFromTransferSyntax( ts );
//PixelData->SetCompressionType( Compression::RAW );
res = ReadImage(ms2);
}
else
{
ms2.SetFromFile( *F );
if( MediaStorage::IsImage( ms2 ) )
{
res = ReadImage(ms2);
}
else
{
gdcmDebugMacro( "DICOM file is not an Image file but a : " <<
MediaStorage::GetMSString(ms2) << " SOP Class UID" );
res = false;
}
}
}
else if( ts == TransferSyntax::ImplicitVRBigEndianACRNEMA || header.IsEmpty() )
{
// Those transfer syntax have a high probability of being ACR NEMA
gdcmDebugMacro( "Looks like an ACR-NEMA file" );
// Hopefully all ACR-NEMA are RAW:
//PixelData->SetCompressionType( Compression::RAW );
res = ReadACRNEMAImage();
}
else // there is a Unknown Media Storage Syntax
{
assert( ts != TransferSyntax::TS_END && ms == MediaStorage::MS_END );
// god damit I don't know what to do...
gdcmWarningMacro( "Attempting to read this file as a DICOM file"
"\nDesperate attempt" );
MediaStorage ms3;
ms3.SetFromFile( GetFile() );
if( ms3 != MediaStorage::MS_END )
{
res = ReadImage(ms3);
}
else
{
// Giving up
res = false;
}
}
}
}
//if(res) PixelData->Print( std::cout );
return res;
}
// PICKER-16-MONO2-Nested_icon.dcm
static void DoIconImage(const DataSet& rootds, Pixmap& image)
{
const Tag ticonimage(0x0088,0x0200);
IconImage &pixeldata = image.GetIconImage();
if( rootds.FindDataElement( ticonimage ) )
{
const DataElement &iconimagesq = rootds.GetDataElement( ticonimage );
//const SequenceOfItems* sq = iconimagesq.GetSequenceOfItems();
SmartPointer<SequenceOfItems> sq = iconimagesq.GetValueAsSQ();
// Is SQ empty ?
if( !sq || sq->IsEmpty() ) return;
SequenceOfItems::ConstIterator it = sq->Begin();
const DataSet &ds = it->GetNestedDataSet();
// D 0028|0011 [US] [Columns] [512]
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0011) );
Attribute<0x0028,0x0011> at = { 0 };
at.SetFromDataSet( ds );
pixeldata.SetDimension(0, at.GetValue() );
}
// D 0028|0010 [US] [Rows] [512]
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0010) );
Attribute<0x0028,0x0010> at = { 0 };
at.SetFromDataSet( ds );
pixeldata.SetDimension(1, at.GetValue() );
}
PixelFormat pf;
// 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...
}
pixeldata.SetPixelFormat( pf );
// D 0028|0004 [CS] [Photometric Interpretation] [MONOCHROME2 ]
const Tag tphotometricinterpretation(0x0028, 0x0004);
PhotometricInterpretation pi = PhotometricInterpretation::MONOCHROME2;
if( ds.FindDataElement( tphotometricinterpretation ) )
{
const ByteValue *photometricinterpretation = ds.GetDataElement( tphotometricinterpretation ).GetByteValue();
std::string photometricinterpretation_str(
photometricinterpretation->GetPointer(),
photometricinterpretation->GetLength() );
pi = PhotometricInterpretation::GetPIType(
photometricinterpretation_str.c_str());
}
assert( pi != PhotometricInterpretation::UNKNOWN);
pixeldata.SetPhotometricInterpretation( pi );
//
if ( pi == PhotometricInterpretation::PALETTE_COLOR )
{
SmartPointer<LookupTable> lut = new LookupTable;
const Tag testseglut(0x0028, (0x1221 + 0));
if( ds.FindDataElement( testseglut ) )
{
assert(0 && "Please report this image");
lut = new SegmentedPaletteColorLookupTable;
}
//SmartPointer<SegmentedPaletteColorLookupTable> lut = new SegmentedPaletteColorLookupTable;
lut->Allocate( pf.GetBitsAllocated() );
// for each red, green, blue:
for(int i=0; i<3; ++i)
{
// (0028,1101) US 0\0\16
// (0028,1102) US 0\0\16
// (0028,1103) US 0\0\16
const Tag tdescriptor(0x0028, (uint16_t)(0x1101 + i));
//const Tag tdescriptor(0x0028, 0x3002);
Element<VR::US,VM::VM3> el_us3;
// Now pass the byte array to a DICOMizer:
el_us3.SetFromDataElement( ds[tdescriptor] ); //.GetValue() );
lut->InitializeLUT( LookupTable::LookupTableType(i),
el_us3[0], el_us3[1], el_us3[2] );
// (0028,1201) OW
// (0028,1202) OW
// (0028,1203) OW
const Tag tlut(0x0028, (uint16_t)(0x1201 + i));
//const Tag tlut(0x0028, 0x3006);
// Segmented LUT
// (0028,1221) OW
// (0028,1222) OW
// (0028,1223) OW
const Tag seglut(0x0028, (uint16_t)(0x1221 + i));
if( ds.FindDataElement( tlut ) )
{
const ByteValue *lut_raw = ds.GetDataElement( tlut ).GetByteValue();
assert( lut_raw );
// LookupTableType::RED == 0
lut->SetLUT( LookupTable::LookupTableType(i),
(const unsigned char*)lut_raw->GetPointer(), lut_raw->GetLength() );
//assert( pf.GetBitsAllocated() == el_us3.GetValue(2) );
unsigned long check =
(el_us3.GetValue(0) ? el_us3.GetValue(0) : 65536)
* el_us3.GetValue(2) / 8;
if(!( check == lut_raw->GetLength() || 2 * check == lut_raw->GetLength()
|| check + 1 == lut_raw->GetLength() )) {
gdcmErrorMacro( "Icon Sequence is invalid. Giving up" );
pixeldata.Clear();
return;
}
}
else if( ds.FindDataElement( seglut ) )
{
const ByteValue *lut_raw = ds.GetDataElement( seglut ).GetByteValue();
assert( lut_raw );
lut->SetLUT( LookupTable::LookupTableType(i),
(const unsigned char*)lut_raw->GetPointer(), lut_raw->GetLength() );
//assert( pf.GetBitsAllocated() == el_us3.GetValue(2) );
//unsigned long check =
// (el_us3.GetValue(0) ? el_us3.GetValue(0) : 65536)
// * el_us3.GetValue(2) / 8;
//assert( check == lut_raw->GetLength() ); (void)check;
}
else
{
gdcmWarningMacro( "Icon Sequence is incomplete. Giving up" );
pixeldata.Clear();
return;
}
}
pixeldata.SetLUT(*lut);
}
const Tag tpixeldata = Tag(0x7fe0, 0x0010);
if( !ds.FindDataElement( tpixeldata ) )
{
gdcmWarningMacro( "Icon Sequence is incomplete. Giving up" );
pixeldata.Clear();
return;
}
const DataElement& de = ds.GetDataElement( tpixeldata );
pixeldata.SetDataElement( de );
// Pass TransferSyntax:
// Warning This is legal for the Icon to be uncompress in a compressed image
// We need to set the appropriate TS here:
const ByteValue *bv = de.GetByteValue();
if( bv )
pixeldata.SetTransferSyntax( TransferSyntax::ImplicitVRLittleEndian );
else
pixeldata.SetTransferSyntax( image.GetTransferSyntax() );
}
}
// GE_DLX-8-MONO2-Multiframe.dcm
static void DoCurves(const DataSet& ds, Pixmap& pixeldata)
{
unsigned int numcurves;
if( (numcurves = Curve::GetNumberOfCurves( ds )) )
{
pixeldata.SetNumberOfCurves( numcurves );
Tag curve(0x5000,0x0000);
bool finished = false;
unsigned int idxcurves = 0;
while( !finished )
{
const DataElement &de = ds.FindNextDataElement( curve );
// Are we done:
if( de.GetTag().GetGroup() > 0x50FF ) // last possible curve curve
{
finished = true;
}
else if( de.GetTag().IsPrivate() ) // GEMS owns some 0x5003
{
// Move on to the next public one:
curve.SetGroup( (uint16_t)(de.GetTag().GetGroup() + 1) );
curve.SetElement( 0 );
}
else
{
// Yay! this is an curve element
Curve &ov = pixeldata.GetCurve(idxcurves);
++idxcurves; // move on to the next one
curve = de.GetTag();
uint16_t currentcurve = curve.GetGroup();
assert( !(currentcurve % 2) ); // 0x6001 is not an curve...
// Now loop on all element from this current group:
DataElement de2 = de;
while( de2.GetTag().GetGroup() == currentcurve )
{
ov.Update(de2);
curve.SetElement( (uint16_t)(de2.GetTag().GetElement() + 1) );
de2 = ds.FindNextDataElement( curve );
// Next element:
//curve.SetElement( curve.GetElement() + 1 );
}
// If we exit the loop we have pass the current curve and potentially point to the next one:
//curve.SetElement( curve.GetElement() + 1 );
//ov.Print( std::cout );
}
}
//std::cout << "Num of curves: " << numcurves << std::endl;
assert( idxcurves == numcurves );
}
}
static unsigned int GetNumberOfOverlaysInternal(DataSet const & ds, std::vector<uint16_t> & overlaylist)
{
Tag overlay(0x6000,0x0000); // First possible overlay
bool finished = false;
unsigned int numoverlays = 0;
while( !finished )
{
const DataElement &de = ds.FindNextDataElement( overlay );
if( de.GetTag().GetGroup() > 0x60FF ) // last possible curve
{
finished = true;
}
else if( de.GetTag().IsPrivate() )
{
// Move on to the next public one:
overlay.SetGroup( (uint16_t)(de.GetTag().GetGroup() + 1) );
overlay.SetElement( 0 ); // reset just in case...
}
else
{
// Yeah this is a potential overlay element, let's check this is not a broken LEADTOOL image,
// or prova0001.dcm:
// (5000,0000) UL 0 # 4, 1 GenericGroupLength
// (6000,0000) UL 0 # 4, 1 GenericGroupLength
// (6001,0000) UL 28 # 4, 1 PrivateGroupLength
// (6001,0010) LT [PAPYRUS 3.0] # 12, 1 PrivateCreator
// (6001,1001) LT (no value available) # 0, 0 Unknown Tag & Data
/*
* FIXME:
* In order to support : gdcmData/SIEMENS_GBS_III-16-ACR_NEMA_1.acr
* gdcmDataExtra/gdcmSampleData/images_of_interest/XA_GE_JPEG_02_with_Overlays.dcm
* I cannot simply check for overlay_group,3000 this would not work
* I would need a strong euristick
*/
// Store found tag in overlay:
overlay = de.GetTag();
// heuristic based on either the Overlay Data or the Col/Row info
Tag toverlaydata(overlay.GetGroup(),0x3000 );
Tag toverlayrows(overlay.GetGroup(),0x0010 );
Tag toverlaycols(overlay.GetGroup(),0x0011 );
Tag toverlaybitpos(overlay.GetGroup(),0x0102 );
if( ds.FindDataElement( toverlaydata ) )
{
// ok so far so good...
const DataElement& overlaydata = ds.GetDataElement( toverlaydata );
//const DataElement& overlaydata = ds.GetDataElement(Tag(overlay.GetGroup(),0x0010));
if( !overlaydata.IsEmpty() )
{
++numoverlays;
overlaylist.push_back( overlay.GetGroup() );
}
}
else if( ds.FindDataElement( toverlayrows ) && ds.FindDataElement( toverlaycols )
&& ds.FindDataElement( toverlaybitpos ) )
{
// Overlay Pixel are in Unused Pixel
assert( !ds.FindDataElement( toverlaydata ) );
const DataElement& overlayrows = ds.GetDataElement( toverlayrows );
const DataElement& overlaycols = ds.GetDataElement( toverlaycols );
assert( ds.FindDataElement( toverlaybitpos ) );
const DataElement& overlaybitpos = ds.GetDataElement( toverlaybitpos );
if( !overlayrows.IsEmpty() && !overlaycols.IsEmpty() && !overlaybitpos.IsEmpty() )
{
++numoverlays;
overlaylist.push_back( overlay.GetGroup() );
}
}
// Move on to the next possible one:
overlay.SetGroup( (uint16_t)(overlay.GetGroup() + 2) );
// reset to element 0x0 just in case...
overlay.SetElement( 0 );
}
}
// at most one out of two :
assert( numoverlays < 0x00ff / 2 );
// PS 3.3 - 2004:
// C.9.2 Overlay plane module
// Each Overlay Plane is one bit deep. Sixteen separate Overlay Planes may be associated with an
// Image or exist as Standalone Overlays in a Series
assert( numoverlays <= 16 );
assert( numoverlays == overlaylist.size() );
return numoverlays;
}
static bool DoOverlays(const DataSet& ds, Pixmap& pixeldata)
{
unsigned int numoverlays;
std::vector<uint16_t> overlaylist;
std::vector<bool> updateoverlayinfo;
if( (numoverlays = GetNumberOfOverlaysInternal( ds, overlaylist )) )
{
updateoverlayinfo.resize(numoverlays, false);
pixeldata.SetNumberOfOverlays( numoverlays );
for( unsigned int idxoverlays = 0; idxoverlays < numoverlays; ++idxoverlays )
{
Overlay &ov = pixeldata.GetOverlay(idxoverlays);
uint16_t currentoverlay = overlaylist[idxoverlays];
Tag overlay(0x6000,0x0000);
overlay.SetGroup( currentoverlay );
const DataElement &de = ds.FindNextDataElement( overlay );
assert( !(currentoverlay % 2) ); // 0x6001 is not an overlay...
// Now loop on all element from this current group:
DataElement de2 = de;
while( de2.GetTag().GetGroup() == currentoverlay )
{
ov.Update(de2);
overlay.SetElement( (uint16_t)(de2.GetTag().GetElement() + 1) );
de2 = ds.FindNextDataElement( overlay );
}
// Let's decode it:
std::ostringstream unpack;
ov.Decompress( unpack );
std::string s = unpack.str();
//size_t l = s.size();
// The following line will fail with images like XA_GE_JPEG_02_with_Overlays.dcm
// since the overlays are stored in the unused bit of the PixelData
if( !ov.IsEmpty() )
{
//assert( unpack.str().size() / 8 == ((ov.GetRows() * ov.GetColumns()) + 7 ) / 8 );
assert( ov.IsInPixelData( ) == false );
}
else if( pixeldata.GetPixelFormat().GetSamplesPerPixel() == 1 )
{
assert( ov.IsEmpty() );
gdcmDebugMacro( "This image does not contains Overlay in the 0x60xx tags. "
<< "Instead the overlay is stored in the unused bit of the Pixel Data."
<< std::endl );
ov.IsInPixelData( true );
// make sure Overlay is valid
if( ov.GetBitsAllocated() != pixeldata.GetPixelFormat().GetBitsAllocated() )
{
gdcmWarningMacro( "Bits Allocated are wrong. Correcting." );
ov.SetBitsAllocated( pixeldata.GetPixelFormat().GetBitsAllocated() );
}
if( !ov.GrabOverlayFromPixelData(ds) )
{
gdcmWarningMacro( "Could not extract Overlay from Pixel Data" );
//throw Exception("TODO: Could not extract Overlay Data");
}
updateoverlayinfo[idxoverlays] = true;
}
else
{
// http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.9.2.html
// Overlay data stored in unused bit planes of Pixel Data (7FE0,0010)
// with Samples Per Pixel (0028,0002) of 1 was previously described in
// DICOM.
gdcmWarningMacro( "Overlay was found, while PI is: " << pixeldata.GetPhotometricInterpretation() << " skipping.");
}
}
//std::cout << "Num of Overlays: " << numoverlays << std::endl;
}
// Now is good time to do some cleanup (eg. DX_GE_FALCON_SNOWY-VOI.dcm).
const PixelFormat &pf = pixeldata.GetPixelFormat();
// Yes I am using a call in the for() loop, because I internally modify the
// number of overlays:
for( size_t ov_idx = pixeldata.GetNumberOfOverlays(); ov_idx != 0; --ov_idx )
{
size_t ov = ov_idx - 1;
const Overlay& o = pixeldata.GetOverlay(ov);
if( o.IsInPixelData() )
{
unsigned short obp = o.GetBitPosition();
if( obp < pf.GetBitsStored() )
{
pixeldata.RemoveOverlay( ov );
updateoverlayinfo.erase( updateoverlayinfo.begin() + ov );
gdcmWarningMacro( "Invalid BitPosition: " << obp << " for overlay #" <<
ov << " removing it." );
}
else if( obp > pf.GetBitsAllocated() )
{
pixeldata.RemoveOverlay( ov );
updateoverlayinfo.erase( updateoverlayinfo.begin() + ov );
gdcmWarningMacro( "Invalid BitPosition: " << obp << " for overlay #" <<
ov << " removing it." );
}
}
}
for( size_t ov = 0;
ov < pixeldata.GetNumberOfOverlays() && updateoverlayinfo[ov] ; ++ov )
{
Overlay& o = pixeldata.GetOverlay(ov);
// We need to update information
if( o.GetBitsAllocated() == 16 )
{
o.SetBitsAllocated( 1 );
o.SetBitPosition( 0 );
}
else
{
gdcmErrorMacro( "Overlay #" << ov << " is not supported" );
return false;
}
}
return true;
}
bool PixmapReader::ReadImage(MediaStorage const &ms)
{
return ReadImageInternal(ms);
}
bool PixmapReader::ReadImageInternal(MediaStorage const &ms, bool handlepixeldata )
{
const DataSet &ds = F->GetDataSet();
std::string conversion;
bool isacrnema = false;
const Tag trecognitioncode(0x0008,0x0010);
if( ds.FindDataElement( trecognitioncode ) && !ds.GetDataElement( trecognitioncode ).IsEmpty() )
{
// PHILIPS_Gyroscan-12-MONO2-Jpeg_Lossless.dcm
// PHILIPS_Gyroscan-12-Jpeg_Extended_Process_2_4.dcm
gdcmDebugMacro( "Mixture of ACR NEMA and DICOM file" );
isacrnema = true;
const char *str = ds.GetDataElement( trecognitioncode ).GetByteValue()->GetPointer();
assert( strncmp( str, "ACR-NEMA", strlen( "ACR-NEMA" ) ) == 0 ||
strncmp( str, "ACRNEMA", strlen( "ACRNEMA" ) ) == 0 ||
strncmp( str, "MIPS 2.0", strlen( "MIPS 2.0" ) ) == 0 );
(void)str;//warning removal
}
std::vector<unsigned int> vdims = ImageHelper::GetDimensionsValue(*F);
unsigned int numberofframes = vdims[2];
// What should I do when numberofframes == 0 ?
if( numberofframes > 1 )
{
PixelData->SetNumberOfDimensions(3);
PixelData->SetDimension(2, numberofframes );
}
else
{
gdcmDebugMacro( "NumberOfFrames was specified with a value of: "
<< numberofframes );
PixelData->SetNumberOfDimensions(2);
}
// 2. What are the col & rows:
PixelData->SetDimension(0, vdims[0] );
PixelData->SetDimension(1, vdims[1] );
// 3. Pixel Format ?
PixelFormat pf;
// D 0028|0002 [US] [Samples per Pixel] [1]
{
Attribute<0x0028,0x0002> at = { 1 }; // By default assume 1 Samples Per Pixel
at.SetFromDataSet( ds );
pf.SetSamplesPerPixel( at.GetValue() );
}
if( ms == MediaStorage::MRSpectroscopyStorage )
{
pf.SetScalarType( PixelFormat::FLOAT32 );
}
else
{
assert( MediaStorage::IsImage( ms ) );
// D 0028|0100 [US] [Bits Allocated] [16]
//pf.SetBitsAllocated(
// ReadUSFromTag( Tag(0x0028, 0x0100), ss, conversion ) );
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0100) );
Attribute<0x0028,0x0100> at = { 0 };
at.SetFromDataSet( ds );
pf.SetBitsAllocated( at.GetValue() );
//assert( at.GetValue() == ReadUSFromTag( Tag(0x0028, 0x0100), ss, conversion ) );
}
// D 0028|0101 [US] [Bits Stored] [12]
//pf.SetBitsStored(
// ReadUSFromTag( Tag(0x0028, 0x0101), ss, conversion ) );
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0101) );
Attribute<0x0028,0x0101> at = { 0 };
at.SetFromDataSet( ds );
pf.SetBitsStored( at.GetValue() );
//assert( at.GetValue() == ReadUSFromTag( Tag(0x0028, 0x0101), ss, conversion ) );
}
// D 0028|0102 [US] [High Bit] [11]
//pf.SetHighBit(
// ReadUSFromTag( Tag(0x0028, 0x0102), ss, conversion ) );
{
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0102) );
Attribute<0x0028,0x0102> at = { 0 };
at.SetFromDataSet( ds );
pf.SetHighBit( at.GetValue() );
//assert( at.GetValue() == ReadUSFromTag( Tag(0x0028, 0x0102), ss, conversion ) );
}
// D 0028|0103 [US] [Pixel Representation] [0]
//Tag tpixelrep(0x0028, 0x0103);
//if( ds.FindDataElement( tpixelrep ) && !ds.GetDataElement( tpixelrep ).IsEmpty() )
{
//pf.SetPixelRepresentation(
// ReadUSFromTag( Tag(0x0028, 0x0103), ss, conversion ) );
//const DataElement& de = ds.GetDataElement( Tag(0x0028, 0x0103) );
Attribute<0x0028,0x0103> at = { 0 };
at.SetFromDataSet( ds );
pf.SetPixelRepresentation( at.GetValue() );
//assert( at.GetValue() == ReadUSFromTag( Tag(0x0028, 0x0103), ss, conversion ) );
}
// else
// {
// gdcmWarningMacro( "Pixel Representation was not found. Default to Unsigned Pixel Representation" );
// pf.SetPixelRepresentation( 0 );
// }
}
// 5. Photometric Interpretation
// D 0028|0004 [CS] [Photometric Interpretation] [MONOCHROME2 ]
const Tag tphotometricinterpretation(0x0028, 0x0004);
const ByteValue *photometricinterpretation
= ImageHelper::GetPointerFromElement( tphotometricinterpretation, *F );
PhotometricInterpretation pi = PhotometricInterpretation::UNKNOWN;
if( photometricinterpretation )
{
std::string photometricinterpretation_str(
photometricinterpretation->GetPointer(),
photometricinterpretation->GetLength() );
pi = PhotometricInterpretation::GetPIType( photometricinterpretation_str.c_str() );
// http://www.dominator.com/assets/003/5278.pdf
// JPEG 2000 lossless YUV_RCT
if( pi == PhotometricInterpretation::PI_END )
{
gdcmWarningMacro( "Discarding suspicious PhotometricInterpretation found: "
<< photometricinterpretation_str );
}
}
// try again harder:
if( !photometricinterpretation || pi == PhotometricInterpretation::PI_END )
{
if( pf.GetSamplesPerPixel() == 1 )
{
gdcmWarningMacro( "No PhotometricInterpretation found, default to MONOCHROME2" );
pi = PhotometricInterpretation::MONOCHROME2;
}
else if( pf.GetSamplesPerPixel() == 3 )
{
gdcmWarningMacro( "No PhotometricInterpretation found, default to RGB" );
pi = PhotometricInterpretation::RGB;
}
else if( pf.GetSamplesPerPixel() == 4 )
{
gdcmWarningMacro( "No PhotometricInterpretation found, default to ARGB" );
pi = PhotometricInterpretation::ARGB;
}
else
{
gdcmWarningMacro( "Impossible value for Samples Per Pixel: " << pf.GetSamplesPerPixel() );
return false;
}
}
assert( pi != PhotometricInterpretation::PI_END );
if( !pf.GetSamplesPerPixel() || (pi.GetSamplesPerPixel() != pf.GetSamplesPerPixel()) )
{
if( pi != PhotometricInterpretation::UNKNOWN )
{
pf.SetSamplesPerPixel( pi.GetSamplesPerPixel() );
}
else if ( isacrnema )
{
assert ( pf.GetSamplesPerPixel() == 0 );
assert ( pi == PhotometricInterpretation::UNKNOWN );
pf.SetSamplesPerPixel( 1 );
pi = PhotometricInterpretation::MONOCHROME2;
}
else
{
gdcmWarningMacro( "Cannot recognize image type. Does not looks like"
"ACR-NEMA and is missing both Sample Per Pixel AND PhotometricInterpretation."
"Please report" );
return false;
}
}
assert ( pf.GetSamplesPerPixel() != 0 );
// Very important to set the PixelFormat here before PlanarConfiguration
PixelData->SetPixelFormat( pf );
pf = PixelData->GetPixelFormat();
if( !pf.IsValid() )
{
return false;
}
if( pi == PhotometricInterpretation::UNKNOWN ) return false;
PixelData->SetPhotometricInterpretation( pi );
// 4. Planar Configuration
// D 0028|0006 [US] [Planar Configuration] [1]
const Tag planarconfiguration = Tag(0x0028, 0x0006);
// FIXME: Whatif planaconfiguration is send in a grayscale image... it would be empty...
// well hopefully :(
if( ds.FindDataElement( planarconfiguration ) && !ds.GetDataElement( planarconfiguration ).IsEmpty() )
{
const DataElement& de = ds.GetDataElement( planarconfiguration );
Attribute<0x0028,0x0006> at = { 0 };
at.SetFromDataElement( de );
//unsigned int pc = ReadUSFromTag( planarconfiguration, ss, conversion );
unsigned int pc = at.GetValue();
if( pc && PixelData->GetPixelFormat().GetSamplesPerPixel() != 3 )
{
gdcmDebugMacro( "Cannot have PlanarConfiguration=1, when Sample Per Pixel != 3" );
pc = 0;
}
PixelData->SetPlanarConfiguration( pc );
}
// Do the Palette Color:
// 1. Modality LUT Sequence
bool modlut = ds.FindDataElement(Tag(0x0028,0x3000) );
if( modlut )
{
gdcmWarningMacro( "Modality LUT (0028,3000) are not handled. Image will not be displayed properly" );
}
// 2. LUTData (0028,3006)
// technically I do not need to warn about LUTData since either modality lut XOR VOI LUT need to
// be sent to require a LUT Data...
bool lutdata = ds.FindDataElement(Tag(0x0028,0x3006) );
if( lutdata )
{
gdcmWarningMacro( "LUT Data (0028,3006) are not handled. Image will not be displayed properly" );
}
// 3. VOILUTSequence (0028,3010)
bool voilut = ds.FindDataElement(Tag(0x0028,0x3010) );
if( voilut )
{
gdcmWarningMacro( "VOI LUT (0028,3010) are not handled. Image will not be displayed properly" );
}
// (0028,0120) US 32767 # 2, 1 PixelPaddingValue
bool pixelpaddingvalue = ds.FindDataElement(Tag(0x0028,0x0120));
// PS 3.3 - 2008 / C.7.5.1.1.2 Pixel Padding Value and Pixel Padding Range Limit
if(pixelpaddingvalue)
{
// Technically if Pixel Padding Value is 0 on MONOCHROME2 image, then appearance should be fine...
bool vizissue = true;
if( pf.GetPixelRepresentation() == 0 )
{
Element<VR::US,VM::VM1> ppv;
if( !ds.GetDataElement(Tag(0x0028,0x0120) ).IsEmpty() )
{
ppv.SetFromDataElement( ds.GetDataElement(Tag(0x0028,0x0120)) ); //.GetValue() );
if( pi == PhotometricInterpretation::MONOCHROME2 && ppv.GetValue() == 0 )
{
vizissue = false;
}
}
}
else if( pf.GetPixelRepresentation() == 1 )
{
gdcmDebugMacro( "TODO" );
}
// test if there is any viz issue:
if( vizissue )
{
gdcmDebugMacro( "Pixel Padding Value (0028,0120) is not handled. Image will not be displayed properly" );
}
}
// 4. Palette Color Lookup Table Descriptor
if ( pi == PhotometricInterpretation::PALETTE_COLOR )
{
//const DataElement& modlutsq = ds.GetDataElement( Tag(0x0028,0x3000) );
//const SequenceOfItems* sq = modlutsq.GetSequenceOfItems();
//SequenceOfItems::ConstIterator it = sq->Begin();
//const DataSet &ds = it->GetNestedDataSet();
SmartPointer<LookupTable> lut = new LookupTable;
const Tag testseglut(0x0028, (0x1221 + 0));
if( ds.FindDataElement( testseglut ) )
{
lut = new SegmentedPaletteColorLookupTable;
}
//SmartPointer<SegmentedPaletteColorLookupTable> lut = new SegmentedPaletteColorLookupTable;
lut->Allocate( pf.GetBitsAllocated() );
// for each red, green, blue:
for(int i=0; i<3; ++i)
{
// (0028,1101) US 0\0\16
// (0028,1102) US 0\0\16
// (0028,1103) US 0\0\16
const Tag tdescriptor(0x0028, (uint16_t)(0x1101 + i));
//const Tag tdescriptor(0x0028, 0x3002);
Element<VR::US,VM::VM3> el_us3 = {{ 0, 0, 0}};
// Now pass the byte array to a DICOMizer:
el_us3.SetFromDataElement( ds[tdescriptor] ); //.GetValue() );
lut->InitializeLUT( LookupTable::LookupTableType(i),
el_us3[0], el_us3[1], el_us3[2] );
// (0028,1201) OW
// (0028,1202) OW
// (0028,1203) OW
const Tag tlut(0x0028, (uint16_t)(0x1201 + i));
//const Tag tlut(0x0028, 0x3006);
// Segmented LUT
// (0028,1221) OW
// (0028,1222) OW
// (0028,1223) OW
const Tag seglut(0x0028, (uint16_t)(0x1221 + i));
if( ds.FindDataElement( tlut ) )
{
const ByteValue *lut_raw = ds.GetDataElement( tlut ).GetByteValue();
if( lut_raw )
{
// LookupTableType::RED == 0
lut->SetLUT( LookupTable::LookupTableType(i),
(const unsigned char*)lut_raw->GetPointer(), lut_raw->GetLength() );
//assert( pf.GetBitsAllocated() == el_us3.GetValue(2) );
}
else
{
lut->Clear();
}
unsigned long check =
(el_us3.GetValue(0) ? el_us3.GetValue(0) : 65536)
* el_us3.GetValue(2) / 8;
assert( !lut->Initialized() || check == lut_raw->GetLength() ); (void)check;
}
else if( ds.FindDataElement( seglut ) )
{
const ByteValue *lut_raw = ds.GetDataElement( seglut ).GetByteValue();
if( lut_raw )
{
lut->SetLUT( LookupTable::LookupTableType(i),
(const unsigned char*)lut_raw->GetPointer(), lut_raw->GetLength() );
//assert( pf.GetBitsAllocated() == el_us3.GetValue(2) );
}
else
{
lut->Clear();
}
//unsigned long check =
// (el_us3.GetValue(0) ? el_us3.GetValue(0) : 65536)
// * el_us3.GetValue(2) / 8;
//assert( check == lut_raw->GetLength() ); (void)check;
}
else
{
gdcmAssertAlwaysMacro(0);
}
}
if( ! lut->Initialized() ) return false;
PixelData->SetLUT(*lut);
}
// TODO
//assert( pi.GetSamplesPerPixel() == pf.GetSamplesPerPixel() );
// 5.5 Do IconImage if any
assert( PixelData->GetIconImage().IsEmpty() );
DoIconImage(ds, *PixelData);
// 6. Do the Curves if any
DoCurves(ds, *PixelData);
// 7. Do the Overlays if any
if( !DoOverlays(ds, *PixelData) )
{
return false;
}
// 8. Do the PixelData
if( handlepixeldata )
{
if( ms == MediaStorage::MRSpectroscopyStorage )
{
const Tag spectdata = Tag(0x5600, 0x0020);
if( !ds.FindDataElement( spectdata ) )
{
gdcmWarningMacro( "No Spectroscopy Data Found" );
return false;
}
const DataElement& xde = ds.GetDataElement( spectdata );
//bool need = PixelData->GetTransferSyntax() == TransferSyntax::ImplicitVRBigEndianPrivateGE;
//PixelData->SetNeedByteSwap( need );
PixelData->SetDataElement( xde );