1 /*=========================================================================
4 Module: $RCSfile: PrintFile.cxx,v $
6 Date: $Date: 2006/03/29 16:15:01 $
7 Version: $Revision: 1.80 $
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::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::File *f)
43 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
46 gdcm::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::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] ",
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 " 4DLoc: group-elem(in hexa, no space) of the DataEntry holdind 4thDim",
134 " listOfElementsToForceLoad : group-elem,g2-e2,... (in hexa, no space)",
135 " of Elements to load whatever their length ",
136 " privateDirectory : source file full path name of Shadow Group elems ",
137 " noshadowseq: user doesn't want to load Private Sequences ",
138 " noshadow : user doesn't want to load Private groups (odd number) ",
139 " noseq : user doesn't want to load Sequences ",
140 " debug : user wants to run the program in 'debug mode' ",
141 " warning : user wants to be warned about any oddity in the File ",
142 " showlut :user wants to display the Palette Color (as an int array) ",
145 // Initialize Arguments Manager
146 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
148 if (argc == 1 || am->ArgMgrDefined("usage") )
150 am->ArgMgrUsage(usage); // Display 'usage'
155 const char *fileName = am->ArgMgrGetString("filein");
156 const char *dirName = am->ArgMgrGetString("dirin");
158 if ( (fileName == 0 && dirName == 0) ||
159 (fileName != 0 && dirName != 0) )
161 std::cerr << std::endl
162 << "Either 'filein=' or 'dirin=' must be present;"
163 << std::endl << "Not both" << std::endl;
164 am->ArgMgrUsage(usage); // Display 'usage'
169 if (am->ArgMgrDefined("debug"))
170 gdcm::Debug::DebugOn();
172 if (am->ArgMgrDefined("warning"))
173 gdcm::Debug::WarningOn();
175 int loadMode = gdcm::LD_ALL;
176 if ( am->ArgMgrDefined("noshadowseq") )
177 loadMode |= gdcm::LD_NOSHADOWSEQ;
180 if ( am->ArgMgrDefined("noshadow") )
181 loadMode |= gdcm::LD_NOSHADOW;
182 if ( am->ArgMgrDefined("noseq") )
183 loadMode |= gdcm::LD_NOSEQ;
186 int level = am->ArgMgrGetInt("level", 1);
189 uint16_t *elemsToForceLoad
190 = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
193 uint16_t *FourthDimLoc;
194 if ( am->ArgMgrDefined("4DLoc") )
196 FourthDimLoc = am->ArgMgrGetXInt16Enum("4DLoc", &nbP);
200 std::cout << "4DLoc must have 2 and only 2 components!" << std::endl;
206 bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
208 bool ddict = am->ArgMgrDefined("dict") ? true : false;
209 const char *dict = 0;
213 dict = am->ArgMgrGetString("dict",0);
216 /* if unused Param we give up */
217 if ( am->ArgMgrPrintUnusedLabels() )
219 am->ArgMgrUsage(usage);
224 delete am; // we don't need Argument Manager any longer
226 // ----------- End Arguments Manager ---------
231 gdcm::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);
234 if ( fileName != 0 ) // ====== Deal with a single file ======
236 gdcm::File *f = gdcm::File::New();
237 f->SetLoadMode(loadMode);
238 f->SetFileName( fileName );
240 for (int ri=0; ri<forceLoadNb; ri++)
242 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
243 (uint32_t)elemsToForceLoad[2*ri+1] );
245 // TODO : find why such a polution
246 // To avoid polluting the output with messages
247 // 'Last system error was : No such file or directory'
252 bool res = f->Load();
253 // gdcm::File::IsReadable() is no usable here, because we deal with
254 // any kind of gdcm-Parsable *document*
255 // not only gdcm::File (as opposed to gdcm::DicomDir)
258 std::cout << "Cannot process file [" << fileName << "]" << std::endl;
259 std::cout << "Either it doesn't exist, or it's read protected "
261 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
263 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
269 f->SetFourthDimensionLocation(FourthDimLoc[0],FourthDimLoc[1]);
271 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
272 fh->SetPrintLevel( level );
276 std::cout << "\n\n" << std::endl;
278 std::cout <<std::endl;
279 std::cout <<" dataSize " << fh->GetImageDataSize() << std::endl;
280 std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
282 int nX,nY,nZ,nT,sPP,planarConfig;
283 std::string pixelType;
288 std::cout << " DIMX=" << nX << " DIMY=" << nY
289 << " DIMZ=" << nZ << " DIMT=" << nT
292 pixelType = f->GetPixelType();
293 sPP = f->GetSamplesPerPixel();
294 std::cout << " pixelType= [" << pixelType
295 << "] SamplesPerPixel= [" << sPP
300 planarConfig = f->GetPlanarConfiguration();
301 std::cout << " PlanarConfiguration= [" << planarConfig
304 std::cout << " PhotometricInterpretation= ["
305 << f->GetEntryString(0x0028,0x0004)
308 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
309 std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents
311 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
314 if ( f->GetDataEntry(0x0002,0x0010) )
315 if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() )
317 std::cout << "Transfer Syntax not loaded. " << std::endl
318 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
324 std::string transferSyntaxName = f->GetTransferSyntaxName();
325 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
327 std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
328 std::cout << " ------" << std::endl;
330 //std::cout << "\n\n" << std::endl;
331 //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
332 //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
333 //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
335 //------------------------------
336 // Lets's get and print some usefull fields about 'Orientation'
337 // ------------------------------------------------------------
339 std::string strPatientPosition =
340 f->GetEntryString(0x0018,0x5100);
341 if ( strPatientPosition != gdcm::GDCM_UNFOUND
342 && strPatientPosition != "" )
343 std::cout << "PatientPosition (0x0010,0x5100)= ["
344 << strPatientPosition << "]" << std::endl;
346 std::string strViewPosition =
347 f->GetEntryString(0x0018,0x5101);
348 if ( strViewPosition != gdcm::GDCM_UNFOUND
349 && strViewPosition != "" )
350 std::cout << "strViewPosition (0x0010,0x5101)= ["
351 << strViewPosition << "]" << std::endl;
353 std::string strPatientOrientation =
354 f->GetEntryString(0x0020,0x0020);
355 if ( strPatientOrientation != gdcm::GDCM_UNFOUND
356 && strPatientOrientation != "")
357 std::cout << "PatientOrientation (0x0020,0x0020)= ["
358 << strPatientOrientation << "]" << std::endl;
360 std::string strImageOrientationPatient =
361 f->GetEntryString(0x0020,0x0037);
362 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
363 && strImageOrientationPatient != "" )
364 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
365 << strImageOrientationPatient << "]" << std::endl;
367 std::string strImageOrientationRET =
368 f->GetEntryString(0x0020,0x0035);
369 if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
370 && strImageOrientationRET != "" )
371 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
372 << strImageOrientationRET << "]" << std::endl;
374 // Let's compute 'user friendly' results about 'Orientation'
375 // ---------------------------------------------------------
377 gdcm::Orientation *o = gdcm::Orientation::New();
379 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
380 strImageOrientationRET != gdcm::GDCM_UNFOUND )
383 gdcm::OrientationType orient = o->GetOrientationType( f );
385 std::cout << "TypeOrientation = " << orient << " (-> "
386 << o->GetOrientationTypeString(orient) << " )" << std::endl;
389 std::string ori = o->GetOrientation ( f );
391 std::cout << "Orientation [" << ori << "]" << std::endl;
394 //------------------------------
397 // Display the LUT as an int array (for debugging purpose)
398 if ( f->HasLUT() && showlut )
400 uint8_t* lutrgba = fh->GetLutRGBA();
403 std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
405 // Nothing is written yet to get LUT Data user friendly
406 // The following is to be moved into a PixelRedaConvert method
408 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
411 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
414 std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
415 int length; // LUT length in Bytes
416 int deb; // Subscript of the first Lut Value
417 int nbits; // Lut item size (in Bits)
418 int nbRead; // nb of items in LUT descriptor (must be = 3)
420 nbRead = sscanf( lutDescriptor.c_str(),
422 &length, &deb, &nbits );
425 std::cout << "Wrong LUT descriptor" << std::endl;
427 gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
430 if ( b->GetLength() != 0 )
432 std::cout << "---------------------------------------"
433 << " We should never reach this point "
435 //LoadEntryBinArea(b); //LUT Data (CTX dependent)
441 std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
445 if ( fh->GetLutItemSize() == 8 )
447 for (int i=0;i<fh->GetLutItemNumber();i++)
448 std::cout << i << " : \t"
449 << (int)(lutrgba[i*4]) << " "
450 << (int)(lutrgba[i*4+1]) << " "
451 << (int)(lutrgba[i*4+2]) << std::endl;
453 else // LutItemSize assumed to be = 16
455 uint16_t* lutrgba16 = (uint16_t*)lutrgba;
456 for (int i=0;i<fh->GetLutItemNumber();i++)
457 std::cout << i << " : \t"
458 << (int)(lutrgba16[i*4]) << " "
459 << (int)(lutrgba16[i*4+1]) << " "
460 << (int)(lutrgba16[i*4+2]) << std::endl;
466 std::cout << "Try LUT Data "<< std::endl;
470 // Parsability of the gdcm::Document already checked, after Load() !
472 if ( f->IsReadable() )
474 std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
476 else if ( f->GetSeqEntry(0x0041,0x1010) )
478 std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
481 else if ( f->GetSeqEntry(0x0004,0x1220) )
483 std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
488 std::cout <<std::endl<<fileName<<" doesn't look like an image file "
492 std::cout<<std::flush;
496 else // ====== Deal with a Directory ======
498 std::cout << "dirName [" << dirName << "]" << std::endl;
499 gdcm::DirList dirList(dirName,1); // gets recursively the file list
500 gdcm::DirListType fileList = dirList.GetFilenames();
504 for( gdcm::DirListType::iterator it = fileList.begin();
505 it != fileList.end();
508 std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
510 f = gdcm::File::New();
511 f->SetLoadMode(loadMode);
512 f->SetFileName( it->c_str() );
514 for (int ri=0; ri<forceLoadNb; ri++)
516 printf("%04x,%04x\n",elemsToForceLoad[2*ri],
517 elemsToForceLoad[2*ri+1]);
518 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
519 (uint32_t)elemsToForceLoad[2*ri+1]);
525 std::cout << "Cannot process file [" << it->c_str() << "]"
527 std::cout << "Either it doesn't exist, or it's read protected "
529 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
531 std::cout << "use 'PrintFile filein=... debug' "
532 << "to try to guess the pb"
538 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
539 fh->SetPrintLevel( level );
542 //------------------------------
544 // Lets's get and print some usefull fields about 'Orientation'
545 // ------------------------------------------------------------
547 std::string strPatientPosition =
548 f->GetEntryString(0x0018,0x5100);
549 if ( strPatientPosition != gdcm::GDCM_UNFOUND
550 && strPatientPosition != "" )
551 std::cout << "PatientPosition (0x0010,0x5100)= ["
552 << strPatientPosition << "]" << std::endl;
554 std::string strViewPosition =
555 f->GetEntryString(0x0018,0x5101);
556 if ( strViewPosition != gdcm::GDCM_UNFOUND
557 && strViewPosition != "" )
558 std::cout << "strViewPosition (0x0010,0x5101)= ["
559 << strViewPosition << "]" << std::endl;
561 std::string strPatientOrientation =
562 f->GetEntryString(0x0020,0x0020);
563 if ( strPatientOrientation != gdcm::GDCM_UNFOUND
564 && strPatientOrientation != "")
565 std::cout << "PatientOrientation (0x0020,0x0020)= ["
566 << strPatientOrientation << "]" << std::endl;
568 std::string strImageOrientationPatient =
569 f->GetEntryString(0x0020,0x0037);
570 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
571 && strImageOrientationPatient != "" )
572 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
573 << strImageOrientationPatient << "]" << std::endl;
575 std::string strImageOrientationRET =
576 f->GetEntryString(0x0020,0x0035);
577 if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
578 && strImageOrientationRET != "" )
580 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
581 << strImageOrientationRET << "]" << std::endl;
584 // Let's compute 'user friendly' results about 'Orientation'
585 // ---------------------------------------------------------
587 gdcm::Orientation *o = gdcm::Orientation::New();
590 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
591 strImageOrientationRET != gdcm::GDCM_UNFOUND )
594 gdcm::OrientationType orient = o->GetOrientationType( f );
596 std::cout << "TypeOrientation = " << orient << " (-> "
597 << o->GetOrientationTypeString(orient) << " )" << std::endl;
600 std::string ori = o->GetOrientation ( f );
602 std::cout << "Orientation [" << ori << "]" << std::endl;
605 //-------------------------------
608 std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
610 std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
611 std::cout << "\n\n" << std::endl;
615 std::cout<<std::flush;