1 /*=========================================================================
4 Module: $RCSfile: PrintFile.cxx,v $
6 Date: $Date: 2007/09/26 08:07:42 $
7 Version: $Revision: 1.86 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmDocument.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmSQItem.h"
22 #include "gdcmDataEntry.h"
24 #include "gdcmFileHelper.h"
25 #include "gdcmDebug.h"
26 #include "gdcmDirList.h"
27 #include "gdcmGlobal.h"
28 #include "gdcmDictSet.h"
29 #include "gdcmArgMgr.h"
30 #include "gdcmOrientation.h"
33 // TODO : code factorization, for 'single file' an 'whole directory' processing
35 void ShowLutData(GDCM_NAME_SPACE::File *f);
37 // Nothing is written yet to get LUT Data user friendly
38 // The following is to be moved into a PixelReadConvert method
39 // Let here, waiting for a clever idea on the way to do it.
41 void ShowLutData(GDCM_NAME_SPACE::File *f)
43 GDCM_NAME_SPACE::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
46 GDCM_NAME_SPACE::SQItem *sqi= modLutSeq->GetFirstSQItem();
49 std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
50 if ( /*lutDescriptor == GDCM_UNFOUND*/ 0 )
52 std::cout << "LUT Descriptor is missing" << std::endl;
55 int length; // LUT length in Bytes
56 int deb; // Subscript of the first Lut Value
57 int nbits; // Lut item size (in Bits)
59 int nbRead; // nb of items in LUT descriptor (must be = 3)
61 nbRead = sscanf( lutDescriptor.c_str(),
63 &length, &deb, &nbits );
64 std::cout << "length " << length
70 std::cout << "Wrong LUT descriptor" << std::endl;
72 //LUT Data (CTX dependent)
73 GDCM_NAME_SPACE::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
76 int BitsAllocated = f->GetBitsAllocated();
77 if ( BitsAllocated <= 8 )
80 if ( ( nbits == 16 ) && ( BitsAllocated == 8 ) )
82 // when LUT item size is different than pixel size
83 mult = 2; // high byte must be = low byte
87 // See PS 3.3-2003 C.11.1.1.2 p 619
90 uint8_t *lut = b->GetBinArea();
91 for( int i=0; i < length; ++i )
93 std::cout << i+deb << " : \t"
94 << (int) (lut[i*mult + 1]) << std::endl;
99 uint16_t *lut = (uint16_t *)(b->GetBinArea());
100 for( int i=0; i < length; ++i )
102 std::cout << i+deb << " : \t"
103 << (int) (((uint16_t *)lut)[i])
109 std::cout << "No LUT Data DataEntry (0x0028,0x3006) found?!? "
113 std::cout << "No First SQ Item within (0x0028,0x3000) ?!? "
117 std::cout << "No LUT Data SeqEntry (0x0028,0x3000) found "
121 int main(int argc, char *argv[])
125 " \n PrintFile : \n ",
126 " Display the header of a ACR-NEMA/PAPYRUS/DICOM File ",
127 " usage: PrintFile {filein=inputFileName|dirin=inputDirectoryName}[level=n]",
128 " [forceload=listOfElementsToForceLoad] [rec] [noex] ",
129 " [4DLoc= ][dict= privateDirectory] ",
130 " [ { [noshadowseq] | [noshadow][noseq] } ] ",
131 " [debug] [warning] ",
132 " level = 0,1,2 : depending on the amount of details user wants to see",
133 " rec : user wants to parse recursively the directory ",
134 " noex : user doen't want extra 'user friendly' info ",
135 " 4DLoc: group-elem(in hexa, no space) of the DataEntry holdind 4thDim",
136 " listOfElementsToForceLoad : group-elem,g2-e2,... (in hexa, no space)",
137 " of Elements to load whatever their length ",
138 " privateDirectory : source file full path name of Shadow Group elems ",
139 " noshadowseq: user doesn't want to load Private Sequences ",
140 " noshadow : user doesn't want to load Private groups (odd number) ",
141 " noseq : user doesn't want to load Sequences ",
142 " debug : user wants to run the program in 'debug mode' ",
143 " warning : user wants to be warned about any oddity in the File ",
144 " showlut :user wants to display the Palette Color (as an int array) ",
147 // Initialize Arguments Manager
148 GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
150 if (argc == 1 || am->ArgMgrDefined("usage") )
152 am->ArgMgrUsage(usage); // Display 'usage'
157 const char *fileName = am->ArgMgrGetString("filein");
158 const char *dirName = am->ArgMgrGetString("dirin");
160 if ( (fileName == 0 && dirName == 0) ||
161 (fileName != 0 && dirName != 0) )
163 std::cerr << std::endl
164 << "Either 'filein=' or 'dirin=' must be present;"
165 << std::endl << "Not both" << std::endl;
166 am->ArgMgrUsage(usage); // Display 'usage'
171 bool noex = ( 0 != am->ArgMgrDefined("noex") );
172 bool rec = ( 0 != am->ArgMgrDefined("rec") );
174 if (am->ArgMgrDefined("debug"))
175 GDCM_NAME_SPACE::Debug::DebugOn();
177 if (am->ArgMgrDefined("warning"))
178 GDCM_NAME_SPACE::Debug::WarningOn();
180 int loadMode = GDCM_NAME_SPACE::LD_ALL;
181 if ( am->ArgMgrDefined("noshadowseq") )
182 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
185 if ( am->ArgMgrDefined("noshadow") )
186 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
187 if ( am->ArgMgrDefined("noseq") )
188 loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
191 int level = am->ArgMgrGetInt("level", 1);
194 uint16_t *elemsToForceLoad
195 = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
198 uint16_t *FourthDimLoc;
199 if ( am->ArgMgrDefined("4DLoc") )
201 FourthDimLoc = am->ArgMgrGetXInt16Enum("4DLoc", &nbP);
205 std::cout << "4DLoc must have 2 and only 2 components!" << std::endl;
211 bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
213 bool ddict = am->ArgMgrDefined("dict") ? true : false;
214 const char *dict = 0;
218 dict = am->ArgMgrGetString("dict",0);
221 /* if unused Param we give up */
222 if ( am->ArgMgrPrintUnusedLabels() )
224 am->ArgMgrUsage(usage);
229 delete am; // we don't need Argument Manager any longer
231 // ----------- End Arguments Manager ---------
236 GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);
239 if ( fileName != 0 ) // ====== Deal with a single file ======
241 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
242 f->SetLoadMode(loadMode);
243 f->SetFileName( fileName );
245 for (int ri=0; ri<forceLoadNb; ri++)
247 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
248 (uint32_t)elemsToForceLoad[2*ri+1] );
250 // TODO : find why such a polution
251 // To avoid polluting the output with messages
252 // 'Last system error was : No such file or directory'
257 bool res = f->Load();
258 // GDCM_NAME_SPACE::File::IsReadable() is no usable here, because we deal with
259 // any kind of gdcm-Parsable *document*
260 // not only GDCM_NAME_SPACE::File (as opposed to GDCM_NAME_SPACE::DicomDir)
263 std::cout << "Cannot process file [" << fileName << "]" << std::endl;
264 std::cout << "Either it doesn't exist, or it's read protected "
266 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
268 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
275 f->SetFourthDimensionLocation(FourthDimLoc[0],FourthDimLoc[1]);
278 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
279 fh->SetPrintLevel( level );
283 std::cout << "\n\n" << std::endl;
285 std::cout <<std::endl;
286 std::cout <<" dataSize " << fh->GetImageDataSize() << std::endl;
287 std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
290 int nX,nY,nZ,nT,sPP,planarConfig;
291 std::string pixelType;
296 std::cout << " DIMX=" << nX << " DIMY=" << nY
297 << " DIMZ=" << nZ << " DIMT=" << nT
300 pixelType = f->GetPixelType();
301 sPP = f->GetSamplesPerPixel();
302 std::cout << " pixelType= [" << pixelType
303 << "] SamplesPerPixel= [" << sPP
308 planarConfig = f->GetPlanarConfiguration();
309 std::cout << " PlanarConfiguration= [" << planarConfig
312 std::cout << " PhotometricInterpretation= ["
313 << f->GetEntryString(0x0028,0x0004)
316 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
317 std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents
319 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
322 if ( f->GetDataEntry(0x0002,0x0010) )
323 if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() )
325 std::cout << "Transfer Syntax not loaded. " << std::endl
326 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
332 std::string transferSyntaxName = f->GetTransferSyntaxName();
333 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
335 std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
336 std::cout << " ------" << std::endl;
338 std::cout << "\n\n" << std::endl;
339 std::cout << "X spacing " << f->GetXSpacing() << std::endl;
340 std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
341 std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
343 //------------------------------
345 // Let's get and print some usefull fields about 'Orientation'
346 // ------------------------------------------------------------
348 std::string strPatientPosition =
349 f->GetEntryString(0x0018,0x5100);
350 if ( strPatientPosition != GDCM_NAME_SPACE::GDCM_UNFOUND
351 && strPatientPosition != "" )
352 std::cout << "PatientPosition (0x0010,0x5100)= ["
353 << strPatientPosition << "]" << std::endl;
355 std::string strViewPosition =
356 f->GetEntryString(0x0018,0x5101);
357 if ( strViewPosition != GDCM_NAME_SPACE::GDCM_UNFOUND
358 && strViewPosition != "" )
359 std::cout << "View Position (0x0018,0x5101)= ["
360 << strViewPosition << "]" << std::endl;
362 std::string strPatientOrientation =
363 f->GetEntryString(0x0020,0x0020);
364 if ( strPatientOrientation != GDCM_NAME_SPACE::GDCM_UNFOUND
365 && strPatientOrientation != "")
366 std::cout << "PatientOrientation (0x0020,0x0020)= ["
367 << strPatientOrientation << "]" << std::endl;
369 std::string strImageOrientationPatient =
370 f->GetEntryString(0x0020,0x0037);
371 if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND
372 && strImageOrientationPatient != "" )
373 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
374 << strImageOrientationPatient << "]" << std::endl;
376 std::string strImageOrientationRET =
377 f->GetEntryString(0x0020,0x0035);
378 if ( strImageOrientationRET != GDCM_NAME_SPACE::GDCM_UNFOUND
379 && strImageOrientationRET != "" )
380 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
381 << strImageOrientationRET << "]" << std::endl;
383 std::string strImagePositionPatient =
384 f->GetEntryString(0x0020,0x0032);
385 if ( strImagePositionPatient != GDCM_NAME_SPACE::GDCM_UNFOUND
386 && strImagePositionPatient != "" )
387 std::cout << "ImagePositionPatient (0x0020,0x0032)= ["
388 << strImagePositionPatient << "]" << std::endl;
390 std::string strImagePositionPatientRET =
391 f->GetEntryString(0x0020,0x0030);
392 if ( strImagePositionPatientRET != GDCM_NAME_SPACE::GDCM_UNFOUND
393 && strImagePositionPatientRET != "" )
394 std::cout << "ImagePositionPatientRET (0x0020,0x0030)= ["
395 << strImagePositionPatientRET << "]" << std::endl;
398 /*bool riop = */f->GetImageOrientationPatient(iop);
400 /*bool ripp = */f->GetImagePositionPatient(ipp);
402 std::cout << "Image Position (0x0020,0x0032|0x0030) : "
403 << ipp[0] << " , " << ipp[1] << " , "<< ipp[2]
405 std::cout << "Image Orientation (0x0020,0x0037|0x0035) : "
406 << iop[0] << " , " << iop[1] << " , "<< iop[2] << " , "
407 << iop[3] << " , " << iop[4] << " , "<< iop[5]
411 // Let's compute 'user friendly' results about 'Orientation'
412 // ---------------------------------------------------------
414 GDCM_NAME_SPACE::Orientation *o = GDCM_NAME_SPACE::Orientation::New();
416 if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND ||
417 strImageOrientationRET != GDCM_NAME_SPACE::GDCM_UNFOUND )
420 GDCM_NAME_SPACE::OrientationType orient = o->GetOrientationType( f );
422 std::cout << "TypeOrientation = " << orient << " (-> "
423 << o->GetOrientationTypeString(orient) << " )" << std::endl;
426 std::string ori = o->GetOrientation ( f );
428 std::cout << "Orientation [" << ori << "]" << std::endl;
431 //------------------------------
434 // Display the LUT as an int array (for debugging purpose)
435 if ( f->HasLUT() && showlut )
437 uint8_t* lutrgba = fh->GetLutRGBA();
440 std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
442 // Nothing is written yet to get LUT Data user friendly
443 // The following is to be moved into a PixelRedaConvert method
445 GDCM_NAME_SPACE::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
448 GDCM_NAME_SPACE::SQItem *sqi= modLutSeq->GetFirstSQItem();
451 std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
452 int length; // LUT length in Bytes
453 int deb; // Subscript of the first Lut Value
454 int nbits; // Lut item size (in Bits)
455 int nbRead; // nb of items in LUT descriptor (must be = 3)
457 nbRead = sscanf( lutDescriptor.c_str(),
459 &length, &deb, &nbits );
462 std::cout << "Wrong LUT descriptor" << std::endl;
464 GDCM_NAME_SPACE::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
467 if ( b->GetLength() != 0 )
469 std::cout << "---------------------------------------"
470 << " We should never reach this point "
472 //LoadEntryBinArea(b); //LUT Data (CTX dependent)
478 std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
482 if ( fh->GetLutItemSize() == 8 )
484 for (int i=0;i<fh->GetLutItemNumber();i++)
485 std::cout << i << " : \t"
486 << (int)(lutrgba[i*4]) << " "
487 << (int)(lutrgba[i*4+1]) << " "
488 << (int)(lutrgba[i*4+2]) << std::endl;
490 else // LutItemSize assumed to be = 16
492 uint16_t* lutrgba16 = (uint16_t*)lutrgba;
493 for (int i=0;i<fh->GetLutItemNumber();i++)
494 std::cout << i << " : \t"
495 << (int)(lutrgba16[i*4]) << " "
496 << (int)(lutrgba16[i*4+1]) << " "
497 << (int)(lutrgba16[i*4+2]) << std::endl;
503 std::cout << "Try LUT Data "<< std::endl;
507 // Parsability of the GDCM_NAME_SPACE::Document already checked, after Load() !
509 if ( f->IsReadable() )
511 std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
513 else if ( f->GetSeqEntry(0x0041,0x1010) )
515 std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
518 else if ( f->GetSeqEntry(0x0004,0x1220) )
520 std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
525 std::cout <<std::endl<<fileName<<" doesn't look like an image file "
529 std::cout<<std::flush;
533 else // ====== Deal with a Directory ======
535 std::cout << "dirName [" << dirName << "]" << std::endl;
537 GDCM_NAME_SPACE::DirList dirList(dirName,rec); // gets recursively (or not) the file list
538 GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
539 GDCM_NAME_SPACE::File *f;
542 for( GDCM_NAME_SPACE::DirListType::iterator it = fileList.begin();
543 it != fileList.end();
546 std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
548 f = GDCM_NAME_SPACE::File::New();
549 f->SetLoadMode(loadMode);
550 f->SetFileName( it->c_str() );
552 for (int ri=0; ri<forceLoadNb; ri++)
554 printf("%04x,%04x\n",elemsToForceLoad[2*ri],
555 elemsToForceLoad[2*ri+1]);
556 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
557 (uint32_t)elemsToForceLoad[2*ri+1]);
563 std::cout << "Cannot process file [" << it->c_str() << "]"
565 std::cout << "Either it doesn't exist, or it's read protected "
567 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
569 std::cout << "use 'PrintFile filein=... debug' "
570 << "to try to guess the pb"
576 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
577 fh->SetPrintLevel( level );
580 //------------------------------
583 // Lets's get and print some usefull fields about 'Orientation'
584 // ------------------------------------------------------------
586 std::string strPatientPosition =
587 f->GetEntryString(0x0018,0x5100);
588 if ( strPatientPosition != GDCM_NAME_SPACE::GDCM_UNFOUND
589 && strPatientPosition != "" )
590 std::cout << "PatientPosition (0x0010,0x5100)= ["
591 << strPatientPosition << "]" << std::endl;
593 std::string strViewPosition =
594 f->GetEntryString(0x0018,0x5101);
595 if ( strViewPosition != GDCM_NAME_SPACE::GDCM_UNFOUND
596 && strViewPosition != "" )
597 std::cout << "strViewPosition (0x0010,0x5101)= ["
598 << strViewPosition << "]" << std::endl;
600 std::string strPatientOrientation =
601 f->GetEntryString(0x0020,0x0020);
602 if ( strPatientOrientation != GDCM_NAME_SPACE::GDCM_UNFOUND
603 && strPatientOrientation != "")
604 std::cout << "PatientOrientation (0x0020,0x0020)= ["
605 << strPatientOrientation << "]" << std::endl;
607 std::string strImageOrientationPatient =
608 f->GetEntryString(0x0020,0x0037);
609 if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND
610 && strImageOrientationPatient != "" )
611 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
612 << strImageOrientationPatient << "]" << std::endl;
614 std::string strImageOrientationRET =
615 f->GetEntryString(0x0020,0x0035);
616 if ( strImageOrientationRET != GDCM_NAME_SPACE::GDCM_UNFOUND
617 && strImageOrientationRET != "" )
619 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
620 << strImageOrientationRET << "]" << std::endl;
623 // Let's compute 'user friendly' results about 'Orientation'
624 // ---------------------------------------------------------
626 GDCM_NAME_SPACE::Orientation *o = GDCM_NAME_SPACE::Orientation::New();
629 if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND ||
630 strImageOrientationRET != GDCM_NAME_SPACE::GDCM_UNFOUND )
633 GDCM_NAME_SPACE::OrientationType orient = o->GetOrientationType( f );
635 std::cout << "TypeOrientation = " << orient << " (-> "
636 << o->GetOrientationTypeString(orient) << " )" << std::endl;
639 std::string ori = o->GetOrientation ( f );
641 std::cout << "Orientation [" << ori << "]" << std::endl;
644 //-------------------------------
647 std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
649 std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
650 std::cout << "\n\n" << std::endl;
654 std::cout<<std::flush;