1 /*=========================================================================
4 Module: $RCSfile: PrintFile.cxx,v $
6 Date: $Date: 2005/11/07 10:38:56 $
7 Version: $Revision: 1.70 $
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 //gdcmWarningMacro( "LUT Descriptor is missing" );
53 std::cout << "LUT Descriptor is missing" << std::endl;
56 int length; // LUT length in Bytes
57 int deb; // Subscript of the first Lut Value
58 int nbits; // Lut item size (in Bits)
60 int nbRead; // nb of items in LUT descriptor (must be = 3)
62 nbRead = sscanf( lutDescriptor.c_str(),
64 &length, &deb, &nbits );
65 std::cout << "length " << length
71 //gdcmWarningMacro( "Wrong LUT descriptor" );
72 std::cout << "Wrong LUT descriptor" << std::endl;
74 //LUT Data (CTX dependent)
75 gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
78 int BitsAllocated = f->GetBitsAllocated();
79 if ( BitsAllocated <= 8 )
82 if ( ( nbits == 16 ) && ( BitsAllocated == 8 ) )
84 // when LUT item size is different than pixel size
85 mult = 2; // high byte must be = low byte
89 // See PS 3.3-2003 C.11.1.1.2 p 619
92 uint8_t *lut = b->GetBinArea();
93 for( int i=0; i < length; ++i )
95 std::cout << i+deb << " : \t"
96 << (int) (lut[i*mult + 1]) << std::endl;
101 uint16_t *lut = (uint16_t *)(b->GetBinArea());
102 for( int i=0; i < length; ++i )
104 std::cout << i+deb << " : \t"
105 << (int) (((uint16_t *)lut)[i])
111 std::cout << "No LUT Data DataEntry (0x0028,0x3006) found?!? "
115 std::cout << "No First SQ Item within (0x0028,0x3000) ?!? "
119 std::cout << "No LUT Data SeqEntry (0x0028,0x3000) found "
123 int main(int argc, char *argv[])
127 " \n PrintFile : \n ",
128 " Display the header of a ACR-NEMA/PAPYRUS/DICOM File ",
129 " usage: PrintFile {filein=inputFileName|dirin=inputDirectoryName}[level=n]",
130 " [forceload=listOfElementsToForceLoad] ",
131 " [dict= privateDirectory] ",
132 " [ { [noshadowseq] | [noshadow][noseq] } ] ",
133 " [debug] [warning] ",
134 " level = 0,1,2 : depending on the amount of details user wants to see",
135 " listOfElementsToForceLoad : group-elem,g2-e2,... (in hexa, no space)",
136 " of Elements to load whatever their length ",
137 " privateDirectory : source file full path name of Shadow Group elems ",
138 " noshadowseq: user doesn't want to load Private Sequences ",
139 " noshadow : user doesn't want to load Private groups (odd number) ",
140 " noseq : user doesn't want to load Sequences ",
141 " debug : user wants to run the program in 'debug mode' ",
142 " warning : user wants to be warned about any oddity in the File ",
143 " showlut :user wants to display the Palette Color (as an int array) ",
146 // Initialize Arguments Manager
147 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
149 if (argc == 1 || am->ArgMgrDefined("usage") )
151 am->ArgMgrUsage(usage); // Display 'usage'
156 char *fileName = am->ArgMgrGetString("filein",(char *)0);
157 char *dirName = am->ArgMgrGetString("dirin",(char *)0);
159 if ( (fileName == 0 && dirName == 0) ||
160 (fileName != 0 && dirName != 0) )
162 std::cerr << std::endl
163 << "Either 'filein=' or 'dirin=' must be present;"
164 << std::endl << "Not both" << std::endl;
165 am->ArgMgrUsage(usage); // Display 'usage'
170 if (am->ArgMgrDefined("debug"))
171 gdcm::Debug::DebugOn();
173 if (am->ArgMgrDefined("warning"))
174 gdcm::Debug::WarningOn();
176 int loadMode = gdcm::LD_ALL;
177 if ( am->ArgMgrDefined("noshadowseq") )
178 loadMode |= gdcm::LD_NOSHADOWSEQ;
181 if ( am->ArgMgrDefined("noshadow") )
182 loadMode |= gdcm::LD_NOSHADOW;
183 if ( am->ArgMgrDefined("noseq") )
184 loadMode |= gdcm::LD_NOSEQ;
187 int level = am->ArgMgrGetInt("level", 1);
190 uint16_t *elemsToForceLoad
191 = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
193 bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
195 bool ddict = am->ArgMgrDefined("dict") ? true : false;
200 dict = am->ArgMgrGetString("dict",(char *)0);
203 /* if unused Param we give up */
204 if ( am->ArgMgrPrintUnusedLabels() )
206 am->ArgMgrUsage(usage);
211 delete am; // we don't need Argument Manager any longer
213 // ----------- End Arguments Manager ---------
218 gdcm::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);
221 if ( fileName != 0 ) // ====== Deal with a single file ======
223 // gdcm::File::IsReadable() is no usable here, because we deal with
224 // any kind of gdcm-Parsable *document*
225 // not only gdcm::File (as opposed to gdcm::DicomDir)
227 gdcm::File *f = gdcm::File::New();
228 f->SetLoadMode(loadMode);
229 f->SetFileName( fileName );
231 for (int ri=0; ri<forceLoadNb; ri++)
233 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
234 (uint32_t)elemsToForceLoad[2*ri+1] );
237 bool res = f->Load();
240 std::cout << "Cannot process file [" << fileName << "]" << std::endl;
241 std::cout << "Either it doesn't exist, or it's read protected "
243 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
245 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
251 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
252 fh->SetPrintLevel( level );
256 std::cout << "\n\n" << std::endl;
258 std::cout <<std::endl;
259 std::cout <<" dataSize " << fh->GetImageDataSize() << std::endl;
260 std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
262 int nX,nY,nZ,sPP,planarConfig;
263 std::string pixelType;
267 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ
270 pixelType = f->GetPixelType();
271 sPP = f->GetSamplesPerPixel();
272 std::cout << " pixelType= [" << pixelType
273 << "] SamplesPerPixel= [" << sPP
278 planarConfig = f->GetPlanarConfiguration();
279 std::cout << " PlanarConfiguration= [" << planarConfig
282 std::cout << " PhotometricInterpretation= ["
283 << f->GetEntryString(0x0028,0x0004)
286 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
287 std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents
289 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
292 if ( f->GetDataEntry(0x0002,0x0010) )
293 if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() )
295 std::cout << "Transfer Syntax not loaded. " << std::endl
296 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
302 std::string transferSyntaxName = f->GetTransferSyntaxName();
303 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
305 std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
306 std::cout << " ------" << std::endl;
308 //std::cout << "\n\n" << std::endl;
309 //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
310 //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
311 //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
313 //------------------------------
314 // Lets's get and print some usefull fields about 'Orientation'
315 // ------------------------------------------------------------
317 std::string strPatientPosition =
318 f->GetEntryString(0x0018,0x5100);
319 if ( strPatientPosition != gdcm::GDCM_UNFOUND
320 && strPatientPosition != "" )
321 std::cout << "PatientPosition (0x0010,0x5100)= ["
322 << strPatientPosition << "]" << std::endl;
324 std::string strViewPosition =
325 f->GetEntryString(0x0018,0x5101);
326 if ( strViewPosition != gdcm::GDCM_UNFOUND
327 && strViewPosition != "" )
328 std::cout << "strViewPosition (0x0010,0x5101)= ["
329 << strViewPosition << "]" << std::endl;
331 std::string strPatientOrientation =
332 f->GetEntryString(0x0020,0x0020);
333 if ( strPatientOrientation != gdcm::GDCM_UNFOUND
334 && strPatientOrientation != "")
335 std::cout << "PatientOrientation (0x0020,0x0020)= ["
336 << strPatientOrientation << "]" << std::endl;
338 std::string strImageOrientationPatient =
339 f->GetEntryString(0x0020,0x0037);
340 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
341 && strImageOrientationPatient != "" )
342 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
343 << strImageOrientationPatient << "]" << std::endl;
345 std::string strImageOrientationRET =
346 f->GetEntryString(0x0020,0x0035);
347 if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
348 && strImageOrientationRET != "" )
349 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
350 << strImageOrientationRET << "]" << std::endl;
352 // Let's compute 'user friendly' results about 'Orientation'
353 // ---------------------------------------------------------
357 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
358 strImageOrientationRET != gdcm::GDCM_UNFOUND )
361 gdcm::OrientationType orient = o.GetOrientationType( f );
363 std::cout << "TypeOrientation = " << orient << " (-> "
364 << o.GetOrientationTypeString(orient) << " )" << std::endl;
367 std::string ori = o.GetOrientation ( f );
369 std::cout << "Orientation [" << ori << "]" << std::endl;
371 //------------------------------
374 // Display the LUT as an int array (for debugging purpose)
375 if ( f->HasLUT() && showlut )
377 uint8_t* lutrgba = fh->GetLutRGBA();
380 std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
382 // Nothing is written yet to get LUT Data user friendly
383 // The following is to be moved into a PixelRedaConvert method
385 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
388 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
391 std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
392 int length; // LUT length in Bytes
393 int deb; // Subscript of the first Lut Value
394 int nbits; // Lut item size (in Bits)
395 int nbRead; // nb of items in LUT descriptor (must be = 3)
397 nbRead = sscanf( lutDescriptor.c_str(),
399 &length, &deb, &nbits );
402 //gdcmWarningMacro( "Wrong LUT descriptor" );
403 std::cout << "Wrong LUT descriptor" << std::endl;
405 gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
408 if ( b->GetLength() != 0 )
410 std::cout << "---------------------------------------"
411 << " We should never reach this point "
413 //LoadEntryBinArea(b); //LUT Data (CTX dependent)
419 std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
423 if ( fh->GetLutItemSize() == 8 )
425 for (int i=0;i<fh->GetLutItemNumber();i++)
426 std::cout << i << " : \t"
427 << (int)(lutrgba[i*4]) << " "
428 << (int)(lutrgba[i*4+1]) << " "
429 << (int)(lutrgba[i*4+2]) << std::endl;
431 else // LutItemSize assumed to be = 16
433 uint16_t* lutrgba16 = (uint16_t*)lutrgba;
434 for (int i=0;i<fh->GetLutItemNumber();i++)
435 std::cout << i << " : \t"
436 << (int)(lutrgba16[i*4]) << " "
437 << (int)(lutrgba16[i*4+1]) << " "
438 << (int)(lutrgba16[i*4+2]) << std::endl;
444 std::cout << "Try LUT Data "<< std::endl;
448 //if( !f->gdcm::Document::IsReadable())
449 // Try downcast to please MSVC
450 if ( !((gdcm::Document *)f)->IsReadable() )
452 std::cout <<std::endl<<fileName<<" is NOT 'gdcm parsable'"<<std::endl;
455 if ( f->IsReadable() )
457 std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
459 else if ( f->GetSeqEntry(0x0041,0x1010) )
461 std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
464 else if ( f->GetSeqEntry(0x0004,0x1220) )
466 std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
471 std::cout <<std::endl<<fileName<<" doesn't look like an image file "
475 std::cout<<std::flush;
479 else // ====== Deal with a Directory ======
481 std::cout << "dirName [" << dirName << "]" << std::endl;
482 gdcm::DirList dirList(dirName,1); // gets recursively the file list
483 gdcm::DirListType fileList = dirList.GetFilenames();
487 for( gdcm::DirListType::iterator it = fileList.begin();
488 it != fileList.end();
491 std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
493 f = gdcm::File::New();
494 f->SetLoadMode(loadMode);
495 f->SetFileName( it->c_str() );
497 for (int ri=0; ri<forceLoadNb; ri++)
499 printf("%04x,%04x\n",elemsToForceLoad[2*ri],
500 elemsToForceLoad[2*ri+1]);
501 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
502 (uint32_t)elemsToForceLoad[2*ri+1]);
508 std::cout << "Cannot process file [" << it->c_str() << "]"
510 std::cout << "Either it doesn't exist, or it's read protected "
512 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
514 std::cout << "use 'PrintFile filein=... debug' "
515 << "to try to guess the pb"
521 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
522 fh->SetPrintLevel( level );
525 //------------------------------
527 // Lets's get and print some usefull fields about 'Orientation'
528 // ------------------------------------------------------------
530 std::string strPatientPosition =
531 f->GetEntryString(0x0018,0x5100);
532 if ( strPatientPosition != gdcm::GDCM_UNFOUND
533 && strPatientPosition != "" )
534 std::cout << "PatientPosition (0x0010,0x5100)= ["
535 << strPatientPosition << "]" << std::endl;
537 std::string strViewPosition =
538 f->GetEntryString(0x0018,0x5101);
539 if ( strViewPosition != gdcm::GDCM_UNFOUND
540 && strViewPosition != "" )
541 std::cout << "strViewPosition (0x0010,0x5101)= ["
542 << strViewPosition << "]" << std::endl;
544 std::string strPatientOrientation =
545 f->GetEntryString(0x0020,0x0020);
546 if ( strPatientOrientation != gdcm::GDCM_UNFOUND
547 && strPatientOrientation != "")
548 std::cout << "PatientOrientation (0x0020,0x0020)= ["
549 << strPatientOrientation << "]" << std::endl;
551 std::string strImageOrientationPatient =
552 f->GetEntryString(0x0020,0x0037);
553 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
554 && strImageOrientationPatient != "" )
555 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
556 << strImageOrientationPatient << "]" << std::endl;
558 std::string strImageOrientationRET =
559 f->GetEntryString(0x0020,0x0035);
560 if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
561 && strImageOrientationRET != "" )
563 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
564 << strImageOrientationRET << "]" << std::endl;
567 // Let's compute 'user friendly' results about 'Orientation'
568 // ---------------------------------------------------------
572 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
573 strImageOrientationRET != gdcm::GDCM_UNFOUND )
576 gdcm::OrientationType orient = o.GetOrientationType( f );
578 std::cout << "TypeOrientation = " << orient << " (-> "
579 << o.GetOrientationTypeString(orient) << " )" << std::endl;
582 std::string ori = o.GetOrientation ( f );
584 std::cout << "Orientation [" << ori << "]" << std::endl;
586 //-------------------------------
590 std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
592 std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
593 std::cout << "\n\n" << std::endl;
597 std::cout<<std::flush;