1 /*=========================================================================
4 Module: $RCSfile: PrintFile.cxx,v $
6 Date: $Date: 2006/01/26 15:52:43 $
7 Version: $Revision: 1.79 $
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 " [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 " listOfElementsToForceLoad : group-elem,g2-e2,... (in hexa, no space)",
134 " of Elements to load whatever their length ",
135 " privateDirectory : source file full path name of Shadow Group elems ",
136 " noshadowseq: user doesn't want to load Private Sequences ",
137 " noshadow : user doesn't want to load Private groups (odd number) ",
138 " noseq : user doesn't want to load Sequences ",
139 " debug : user wants to run the program in 'debug mode' ",
140 " warning : user wants to be warned about any oddity in the File ",
141 " showlut :user wants to display the Palette Color (as an int array) ",
144 // Initialize Arguments Manager
145 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
147 if (argc == 1 || am->ArgMgrDefined("usage") )
149 am->ArgMgrUsage(usage); // Display 'usage'
154 const char *fileName = am->ArgMgrGetString("filein");
155 const char *dirName = am->ArgMgrGetString("dirin");
157 if ( (fileName == 0 && dirName == 0) ||
158 (fileName != 0 && dirName != 0) )
160 std::cerr << std::endl
161 << "Either 'filein=' or 'dirin=' must be present;"
162 << std::endl << "Not both" << std::endl;
163 am->ArgMgrUsage(usage); // Display 'usage'
168 if (am->ArgMgrDefined("debug"))
169 gdcm::Debug::DebugOn();
171 if (am->ArgMgrDefined("warning"))
172 gdcm::Debug::WarningOn();
174 int loadMode = gdcm::LD_ALL;
175 if ( am->ArgMgrDefined("noshadowseq") )
176 loadMode |= gdcm::LD_NOSHADOWSEQ;
179 if ( am->ArgMgrDefined("noshadow") )
180 loadMode |= gdcm::LD_NOSHADOW;
181 if ( am->ArgMgrDefined("noseq") )
182 loadMode |= gdcm::LD_NOSEQ;
185 int level = am->ArgMgrGetInt("level", 1);
188 uint16_t *elemsToForceLoad
189 = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
191 bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
193 bool ddict = am->ArgMgrDefined("dict") ? true : false;
194 const char *dict = 0;
198 dict = am->ArgMgrGetString("dict",0);
201 /* if unused Param we give up */
202 if ( am->ArgMgrPrintUnusedLabels() )
204 am->ArgMgrUsage(usage);
209 delete am; // we don't need Argument Manager any longer
211 // ----------- End Arguments Manager ---------
216 gdcm::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);
219 if ( fileName != 0 ) // ====== Deal with a single file ======
221 gdcm::File *f = gdcm::File::New();
222 f->SetLoadMode(loadMode);
223 f->SetFileName( fileName );
225 for (int ri=0; ri<forceLoadNb; ri++)
227 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
228 (uint32_t)elemsToForceLoad[2*ri+1] );
230 // TODO : find why such a polution
231 // To avoid polluting the output with messages
232 // 'Last system error was : No such file or directory'
237 bool res = f->Load();
238 // gdcm::File::IsReadable() is no usable here, because we deal with
239 // any kind of gdcm-Parsable *document*
240 // not only gdcm::File (as opposed to gdcm::DicomDir)
243 std::cout << "Cannot process file [" << fileName << "]" << std::endl;
244 std::cout << "Either it doesn't exist, or it's read protected "
246 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
248 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
254 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
255 fh->SetPrintLevel( level );
259 std::cout << "\n\n" << std::endl;
261 std::cout <<std::endl;
262 std::cout <<" dataSize " << fh->GetImageDataSize() << std::endl;
263 std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
265 int nX,nY,nZ,sPP,planarConfig;
266 std::string pixelType;
270 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ
273 pixelType = f->GetPixelType();
274 sPP = f->GetSamplesPerPixel();
275 std::cout << " pixelType= [" << pixelType
276 << "] SamplesPerPixel= [" << sPP
281 planarConfig = f->GetPlanarConfiguration();
282 std::cout << " PlanarConfiguration= [" << planarConfig
285 std::cout << " PhotometricInterpretation= ["
286 << f->GetEntryString(0x0028,0x0004)
289 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
290 std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents
292 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
295 if ( f->GetDataEntry(0x0002,0x0010) )
296 if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() )
298 std::cout << "Transfer Syntax not loaded. " << std::endl
299 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
305 std::string transferSyntaxName = f->GetTransferSyntaxName();
306 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
308 std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
309 std::cout << " ------" << std::endl;
311 //std::cout << "\n\n" << std::endl;
312 //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
313 //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
314 //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
316 //------------------------------
317 // Lets's get and print some usefull fields about 'Orientation'
318 // ------------------------------------------------------------
320 std::string strPatientPosition =
321 f->GetEntryString(0x0018,0x5100);
322 if ( strPatientPosition != gdcm::GDCM_UNFOUND
323 && strPatientPosition != "" )
324 std::cout << "PatientPosition (0x0010,0x5100)= ["
325 << strPatientPosition << "]" << std::endl;
327 std::string strViewPosition =
328 f->GetEntryString(0x0018,0x5101);
329 if ( strViewPosition != gdcm::GDCM_UNFOUND
330 && strViewPosition != "" )
331 std::cout << "strViewPosition (0x0010,0x5101)= ["
332 << strViewPosition << "]" << std::endl;
334 std::string strPatientOrientation =
335 f->GetEntryString(0x0020,0x0020);
336 if ( strPatientOrientation != gdcm::GDCM_UNFOUND
337 && strPatientOrientation != "")
338 std::cout << "PatientOrientation (0x0020,0x0020)= ["
339 << strPatientOrientation << "]" << std::endl;
341 std::string strImageOrientationPatient =
342 f->GetEntryString(0x0020,0x0037);
343 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
344 && strImageOrientationPatient != "" )
345 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
346 << strImageOrientationPatient << "]" << std::endl;
348 std::string strImageOrientationRET =
349 f->GetEntryString(0x0020,0x0035);
350 if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
351 && strImageOrientationRET != "" )
352 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
353 << strImageOrientationRET << "]" << std::endl;
355 // Let's compute 'user friendly' results about 'Orientation'
356 // ---------------------------------------------------------
358 gdcm::Orientation *o = gdcm::Orientation::New();
360 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
361 strImageOrientationRET != gdcm::GDCM_UNFOUND )
364 gdcm::OrientationType orient = o->GetOrientationType( f );
366 std::cout << "TypeOrientation = " << orient << " (-> "
367 << o->GetOrientationTypeString(orient) << " )" << std::endl;
370 std::string ori = o->GetOrientation ( f );
372 std::cout << "Orientation [" << ori << "]" << std::endl;
375 //------------------------------
378 // Display the LUT as an int array (for debugging purpose)
379 if ( f->HasLUT() && showlut )
381 uint8_t* lutrgba = fh->GetLutRGBA();
384 std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
386 // Nothing is written yet to get LUT Data user friendly
387 // The following is to be moved into a PixelRedaConvert method
389 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
392 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
395 std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
396 int length; // LUT length in Bytes
397 int deb; // Subscript of the first Lut Value
398 int nbits; // Lut item size (in Bits)
399 int nbRead; // nb of items in LUT descriptor (must be = 3)
401 nbRead = sscanf( lutDescriptor.c_str(),
403 &length, &deb, &nbits );
406 std::cout << "Wrong LUT descriptor" << std::endl;
408 gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
411 if ( b->GetLength() != 0 )
413 std::cout << "---------------------------------------"
414 << " We should never reach this point "
416 //LoadEntryBinArea(b); //LUT Data (CTX dependent)
422 std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
426 if ( fh->GetLutItemSize() == 8 )
428 for (int i=0;i<fh->GetLutItemNumber();i++)
429 std::cout << i << " : \t"
430 << (int)(lutrgba[i*4]) << " "
431 << (int)(lutrgba[i*4+1]) << " "
432 << (int)(lutrgba[i*4+2]) << std::endl;
434 else // LutItemSize assumed to be = 16
436 uint16_t* lutrgba16 = (uint16_t*)lutrgba;
437 for (int i=0;i<fh->GetLutItemNumber();i++)
438 std::cout << i << " : \t"
439 << (int)(lutrgba16[i*4]) << " "
440 << (int)(lutrgba16[i*4+1]) << " "
441 << (int)(lutrgba16[i*4+2]) << std::endl;
447 std::cout << "Try LUT Data "<< std::endl;
451 // Parsability of the gdcm::Document already checked, after Load() !
453 if ( f->IsReadable() )
455 std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
457 else if ( f->GetSeqEntry(0x0041,0x1010) )
459 std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
462 else if ( f->GetSeqEntry(0x0004,0x1220) )
464 std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
469 std::cout <<std::endl<<fileName<<" doesn't look like an image file "
473 std::cout<<std::flush;
477 else // ====== Deal with a Directory ======
479 std::cout << "dirName [" << dirName << "]" << std::endl;
480 gdcm::DirList dirList(dirName,1); // gets recursively the file list
481 gdcm::DirListType fileList = dirList.GetFilenames();
485 for( gdcm::DirListType::iterator it = fileList.begin();
486 it != fileList.end();
489 std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
491 f = gdcm::File::New();
492 f->SetLoadMode(loadMode);
493 f->SetFileName( it->c_str() );
495 for (int ri=0; ri<forceLoadNb; ri++)
497 printf("%04x,%04x\n",elemsToForceLoad[2*ri],
498 elemsToForceLoad[2*ri+1]);
499 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
500 (uint32_t)elemsToForceLoad[2*ri+1]);
506 std::cout << "Cannot process file [" << it->c_str() << "]"
508 std::cout << "Either it doesn't exist, or it's read protected "
510 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
512 std::cout << "use 'PrintFile filein=... debug' "
513 << "to try to guess the pb"
519 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
520 fh->SetPrintLevel( level );
523 //------------------------------
525 // Lets's get and print some usefull fields about 'Orientation'
526 // ------------------------------------------------------------
528 std::string strPatientPosition =
529 f->GetEntryString(0x0018,0x5100);
530 if ( strPatientPosition != gdcm::GDCM_UNFOUND
531 && strPatientPosition != "" )
532 std::cout << "PatientPosition (0x0010,0x5100)= ["
533 << strPatientPosition << "]" << std::endl;
535 std::string strViewPosition =
536 f->GetEntryString(0x0018,0x5101);
537 if ( strViewPosition != gdcm::GDCM_UNFOUND
538 && strViewPosition != "" )
539 std::cout << "strViewPosition (0x0010,0x5101)= ["
540 << strViewPosition << "]" << std::endl;
542 std::string strPatientOrientation =
543 f->GetEntryString(0x0020,0x0020);
544 if ( strPatientOrientation != gdcm::GDCM_UNFOUND
545 && strPatientOrientation != "")
546 std::cout << "PatientOrientation (0x0020,0x0020)= ["
547 << strPatientOrientation << "]" << std::endl;
549 std::string strImageOrientationPatient =
550 f->GetEntryString(0x0020,0x0037);
551 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
552 && strImageOrientationPatient != "" )
553 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
554 << strImageOrientationPatient << "]" << std::endl;
556 std::string strImageOrientationRET =
557 f->GetEntryString(0x0020,0x0035);
558 if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
559 && strImageOrientationRET != "" )
561 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
562 << strImageOrientationRET << "]" << std::endl;
565 // Let's compute 'user friendly' results about 'Orientation'
566 // ---------------------------------------------------------
568 gdcm::Orientation *o = gdcm::Orientation::New();
571 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
572 strImageOrientationRET != gdcm::GDCM_UNFOUND )
575 gdcm::OrientationType orient = o->GetOrientationType( f );
577 std::cout << "TypeOrientation = " << orient << " (-> "
578 << o->GetOrientationTypeString(orient) << " )" << std::endl;
581 std::string ori = o->GetOrientation ( f );
583 std::cout << "Orientation [" << ori << "]" << std::endl;
586 //-------------------------------
589 std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
591 std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
592 std::cout << "\n\n" << std::endl;
596 std::cout<<std::flush;