1 /*=========================================================================
4 Module: $RCSfile: PrintFile.cxx,v $
6 Date: $Date: 2005/11/28 10:55:00 $
7 Version: $Revision: 1.74 $
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 char *fileName = am->ArgMgrGetString("filein",(char *)0);
155 char *dirName = am->ArgMgrGetString("dirin",(char *)0);
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;
198 dict = am->ArgMgrGetString("dict",(char *)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] );
231 bool res = f->Load();
232 // gdcm::File::IsReadable() is no usable here, because we deal with
233 // any kind of gdcm-Parsable *document*
234 // not only gdcm::File (as opposed to gdcm::DicomDir)
237 std::cout << "Cannot process file [" << fileName << "]" << std::endl;
238 std::cout << "Either it doesn't exist, or it's read protected "
240 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
242 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
248 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
249 fh->SetPrintLevel( level );
253 std::cout << "\n\n" << std::endl;
255 std::cout <<std::endl;
256 std::cout <<" dataSize " << fh->GetImageDataSize() << std::endl;
257 std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
259 int nX,nY,nZ,sPP,planarConfig;
260 std::string pixelType;
264 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ
267 pixelType = f->GetPixelType();
268 sPP = f->GetSamplesPerPixel();
269 std::cout << " pixelType= [" << pixelType
270 << "] SamplesPerPixel= [" << sPP
275 planarConfig = f->GetPlanarConfiguration();
276 std::cout << " PlanarConfiguration= [" << planarConfig
279 std::cout << " PhotometricInterpretation= ["
280 << f->GetEntryString(0x0028,0x0004)
283 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
284 std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents
286 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
289 if ( f->GetDataEntry(0x0002,0x0010) )
290 if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() )
292 std::cout << "Transfer Syntax not loaded. " << std::endl
293 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
299 std::string transferSyntaxName = f->GetTransferSyntaxName();
300 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
302 std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
303 std::cout << " ------" << std::endl;
305 //std::cout << "\n\n" << std::endl;
306 //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
307 //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
308 //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
310 //------------------------------
311 // Lets's get and print some usefull fields about 'Orientation'
312 // ------------------------------------------------------------
314 std::string strPatientPosition =
315 f->GetEntryString(0x0018,0x5100);
316 if ( strPatientPosition != gdcm::GDCM_UNFOUND
317 && strPatientPosition != "" )
318 std::cout << "PatientPosition (0x0010,0x5100)= ["
319 << strPatientPosition << "]" << std::endl;
321 std::string strViewPosition =
322 f->GetEntryString(0x0018,0x5101);
323 if ( strViewPosition != gdcm::GDCM_UNFOUND
324 && strViewPosition != "" )
325 std::cout << "strViewPosition (0x0010,0x5101)= ["
326 << strViewPosition << "]" << std::endl;
328 std::string strPatientOrientation =
329 f->GetEntryString(0x0020,0x0020);
330 if ( strPatientOrientation != gdcm::GDCM_UNFOUND
331 && strPatientOrientation != "")
332 std::cout << "PatientOrientation (0x0020,0x0020)= ["
333 << strPatientOrientation << "]" << std::endl;
335 std::string strImageOrientationPatient =
336 f->GetEntryString(0x0020,0x0037);
337 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
338 && strImageOrientationPatient != "" )
339 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
340 << strImageOrientationPatient << "]" << std::endl;
342 std::string strImageOrientationRET =
343 f->GetEntryString(0x0020,0x0035);
344 if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
345 && strImageOrientationRET != "" )
346 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
347 << strImageOrientationRET << "]" << std::endl;
349 // Let's compute 'user friendly' results about 'Orientation'
350 // ---------------------------------------------------------
352 gdcm::Orientation *o = gdcm::Orientation::New();
354 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
355 strImageOrientationRET != gdcm::GDCM_UNFOUND )
358 gdcm::OrientationType orient = o->GetOrientationType( f );
360 std::cout << "TypeOrientation = " << orient << " (-> "
361 << o->GetOrientationTypeString(orient) << " )" << std::endl;
364 std::string ori = o->GetOrientation ( f );
366 std::cout << "Orientation [" << ori << "]" << std::endl;
368 o->gdcm::Orientation::Delete();
369 //------------------------------
372 // Display the LUT as an int array (for debugging purpose)
373 if ( f->HasLUT() && showlut )
375 uint8_t* lutrgba = fh->GetLutRGBA();
378 std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
380 // Nothing is written yet to get LUT Data user friendly
381 // The following is to be moved into a PixelRedaConvert method
383 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
386 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
389 std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
390 int length; // LUT length in Bytes
391 int deb; // Subscript of the first Lut Value
392 int nbits; // Lut item size (in Bits)
393 int nbRead; // nb of items in LUT descriptor (must be = 3)
395 nbRead = sscanf( lutDescriptor.c_str(),
397 &length, &deb, &nbits );
400 std::cout << "Wrong LUT descriptor" << std::endl;
402 gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
405 if ( b->GetLength() != 0 )
407 std::cout << "---------------------------------------"
408 << " We should never reach this point "
410 //LoadEntryBinArea(b); //LUT Data (CTX dependent)
416 std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
420 if ( fh->GetLutItemSize() == 8 )
422 for (int i=0;i<fh->GetLutItemNumber();i++)
423 std::cout << i << " : \t"
424 << (int)(lutrgba[i*4]) << " "
425 << (int)(lutrgba[i*4+1]) << " "
426 << (int)(lutrgba[i*4+2]) << std::endl;
428 else // LutItemSize assumed to be = 16
430 uint16_t* lutrgba16 = (uint16_t*)lutrgba;
431 for (int i=0;i<fh->GetLutItemNumber();i++)
432 std::cout << i << " : \t"
433 << (int)(lutrgba16[i*4]) << " "
434 << (int)(lutrgba16[i*4+1]) << " "
435 << (int)(lutrgba16[i*4+2]) << std::endl;
441 std::cout << "Try LUT Data "<< std::endl;
445 // Parsability of the gdcm::Document already checked, after Load() !
447 if ( f->IsReadable() )
449 std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
451 else if ( f->GetSeqEntry(0x0041,0x1010) )
453 std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
456 else if ( f->GetSeqEntry(0x0004,0x1220) )
458 std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
463 std::cout <<std::endl<<fileName<<" doesn't look like an image file "
467 std::cout<<std::flush;
471 else // ====== Deal with a Directory ======
473 std::cout << "dirName [" << dirName << "]" << std::endl;
474 gdcm::DirList dirList(dirName,1); // gets recursively the file list
475 gdcm::DirListType fileList = dirList.GetFilenames();
479 for( gdcm::DirListType::iterator it = fileList.begin();
480 it != fileList.end();
483 std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
485 f = gdcm::File::New();
486 f->SetLoadMode(loadMode);
487 f->SetFileName( it->c_str() );
489 for (int ri=0; ri<forceLoadNb; ri++)
491 printf("%04x,%04x\n",elemsToForceLoad[2*ri],
492 elemsToForceLoad[2*ri+1]);
493 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
494 (uint32_t)elemsToForceLoad[2*ri+1]);
500 std::cout << "Cannot process file [" << it->c_str() << "]"
502 std::cout << "Either it doesn't exist, or it's read protected "
504 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
506 std::cout << "use 'PrintFile filein=... debug' "
507 << "to try to guess the pb"
513 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
514 fh->SetPrintLevel( level );
517 //------------------------------
519 // Lets's get and print some usefull fields about 'Orientation'
520 // ------------------------------------------------------------
522 std::string strPatientPosition =
523 f->GetEntryString(0x0018,0x5100);
524 if ( strPatientPosition != gdcm::GDCM_UNFOUND
525 && strPatientPosition != "" )
526 std::cout << "PatientPosition (0x0010,0x5100)= ["
527 << strPatientPosition << "]" << std::endl;
529 std::string strViewPosition =
530 f->GetEntryString(0x0018,0x5101);
531 if ( strViewPosition != gdcm::GDCM_UNFOUND
532 && strViewPosition != "" )
533 std::cout << "strViewPosition (0x0010,0x5101)= ["
534 << strViewPosition << "]" << std::endl;
536 std::string strPatientOrientation =
537 f->GetEntryString(0x0020,0x0020);
538 if ( strPatientOrientation != gdcm::GDCM_UNFOUND
539 && strPatientOrientation != "")
540 std::cout << "PatientOrientation (0x0020,0x0020)= ["
541 << strPatientOrientation << "]" << std::endl;
543 std::string strImageOrientationPatient =
544 f->GetEntryString(0x0020,0x0037);
545 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
546 && strImageOrientationPatient != "" )
547 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
548 << strImageOrientationPatient << "]" << std::endl;
550 std::string strImageOrientationRET =
551 f->GetEntryString(0x0020,0x0035);
552 if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
553 && strImageOrientationRET != "" )
555 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
556 << strImageOrientationRET << "]" << std::endl;
559 // Let's compute 'user friendly' results about 'Orientation'
560 // ---------------------------------------------------------
562 gdcm::Orientation *o = gdcm::Orientation::New();
565 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
566 strImageOrientationRET != gdcm::GDCM_UNFOUND )
569 gdcm::OrientationType orient = o->GetOrientationType( f );
571 std::cout << "TypeOrientation = " << orient << " (-> "
572 << o->GetOrientationTypeString(orient) << " )" << std::endl;
575 std::string ori = o->GetOrientation ( f );
577 std::cout << "Orientation [" << ori << "]" << std::endl;
579 o->gdcm::Orientation::Delete();
581 //-------------------------------
584 std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
586 std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
587 std::cout << "\n\n" << std::endl;
591 std::cout<<std::flush;