1 /*=========================================================================
4 Module: $RCSfile: PrintFile.cxx,v $
6 Date: $Date: 2005/09/30 17:42:17 $
7 Version: $Revision: 1.60 $
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 "gdcmBinEntry.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"
34 void ShowLutData(gdcm::File *f);
36 void ShowLutData(gdcm::File *f)
38 // Nothing is written yet to get LUT Data user friendly
39 // The following is to be moved into a PixelReadConvert method
40 // Let here, waiting for a clever idea on the way to do it.
42 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
45 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
48 std::string lutDescriptor = sqi->GetEntryValue(0x0028,0x3002);
49 if ( /*lutDescriptor == GDCM_UNFOUND*/ 0 )
51 //gdcmWarningMacro( "LUT Descriptor is missing" );
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 //gdcmWarningMacro( "Wrong LUT descriptor" );
71 std::cout << "Wrong LUT descriptor" << std::endl;
73 //LUT Data (CTX dependent)
74 gdcm::BinEntry *b = sqi->GetBinEntry(0x0028,0x3006);
77 int BitsAllocated = f->GetBitsAllocated();
78 if ( BitsAllocated <= 8 )
81 if ( ( nbits == 16 ) && ( BitsAllocated == 8 ) )
83 // when LUT item size is different than pixel size
84 mult = 2; // high byte must be = low byte
88 // See PS 3.3-2003 C.11.1.1.2 p 619
91 uint8_t *lut = b->GetBinArea();
92 for( int i=0; i < length; ++i )
94 std::cout << i+deb << " : \t"
95 << (int) (lut[i*mult + 1]) << std::endl;
100 uint16_t *lut = (uint16_t *)(b->GetBinArea());
101 for( int i=0; i < length; ++i )
103 std::cout << i+deb << " : \t"
104 << (int) (((uint16_t *)lut)[i])
110 std::cout << "No LUT Data BinEntry (0x0028,0x3006) found?!? "
114 std::cout << "No First SQ Item within (0x0028,0x3000) ?!? "
118 std::cout << "No LUT Data SeqEntry (0x0028,0x3000) found "
122 int main(int argc, char *argv[])
126 " \n PrintFile : \n ",
127 " Display the header of a ACR-NEMA/PAPYRUS/DICOM File ",
128 " usage: PrintFile {filein=inputFileName|dirin=inputDirectoryName}[level=n]",
129 " [forceload=listOfElementsToForceLoad] ",
130 " [dict= privateDirectory] ",
131 " [ { [noshadowseq] | [noshadow][noseq] } ] [debug] ",
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 " showlut :user wants to display the Palette Color (as an int array) ",
143 // Initialize Arguments Manager
144 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
146 if (argc == 1 || am->ArgMgrDefined("usage") )
148 am->ArgMgrUsage(usage); // Display 'usage'
153 char *fileName = am->ArgMgrGetString("filein",(char *)0);
154 char *dirName = am->ArgMgrGetString("dirin",(char *)0);
156 if ( (fileName == 0 && dirName == 0)
158 (fileName != 0 && dirName != 0) )
160 std::cout <<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 int loadMode = gdcm::LD_ALL;
172 if ( am->ArgMgrDefined("noshadowseq") )
173 loadMode |= gdcm::LD_NOSHADOWSEQ;
176 if ( am->ArgMgrDefined("noshadow") )
177 loadMode |= gdcm::LD_NOSHADOW;
178 if ( am->ArgMgrDefined("noseq") )
179 loadMode |= gdcm::LD_NOSEQ;
182 int level = am->ArgMgrGetInt("level", 1);
185 uint16_t *elemsToForceLoad
186 = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
188 bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
190 bool ddict = am->ArgMgrDefined("dict") ? true : false;
195 dict = am->ArgMgrGetString("dict",(char *)0);
198 /* if unused Param we give up */
199 if ( am->ArgMgrPrintUnusedLabels() )
201 am->ArgMgrUsage(usage);
206 delete am; // we don't need Argument Manager any longer
208 // ----------- End Arguments Manager ---------
210 std::string tabOrientation[13] = {
221 "Heart Axial invert",
222 "Heart Coronal invert",
223 "Heart Sagital invert"
228 gdcm::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);
231 if ( fileName != 0 ) // ====== Deal with a single file ======
233 // gdcm::File::IsReadable() is no usable here, because we deal with
234 // any kind of gdcm-Parsable *document*
235 // not only gdcm::File (as opposed to gdcm::DicomDir)
237 gdcm::File *f = new gdcm::File();
238 f->SetLoadMode(loadMode);
239 f->SetFileName( fileName );
241 for (int ri=0; ri<forceLoadNb; ri++)
243 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
244 (uint32_t)elemsToForceLoad[2*ri+1] );
247 bool res = f->Load();
250 std::cout << "Cannot process file [" << fileName << "]" << std::endl;
251 std::cout << "Either it doesn't exist, or it's read protected "
253 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
255 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
261 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
262 fh->SetPrintLevel( level );
266 std::cout << "\n\n" << std::endl;
268 std::cout <<std::endl;
269 std::cout <<" dataSize " << fh->GetImageDataSize() << std::endl;
270 std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
272 int nX,nY,nZ,sPP,planarConfig;
273 std::string pixelType;
277 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ
280 pixelType = f->GetPixelType();
281 sPP = f->GetSamplesPerPixel();
282 planarConfig = f->GetPlanarConfiguration();
284 std::cout << " pixelType= [" << pixelType
285 << "] SamplesPerPixel= [" << sPP
286 << "] PlanarConfiguration= [" << planarConfig
288 << " PhotometricInterpretation= ["
289 << f->GetEntryValue(0x0028,0x0004)
292 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
293 std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents
295 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
298 if ( f->GetEntryValue(0x0002,0x0010) == gdcm::GDCM_NOTLOADED )
300 std::cout << "Transfer Syntax not loaded. " << std::endl
301 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
306 std::string transferSyntaxName = f->GetTransferSyntaxName();
307 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
309 std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
310 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 // Lets's get and print some usefull fields about 'Orientation'
317 // ------------------------------------------------------------
319 std::string strPatientPosition =
320 f->GetEntryValue(0x0018,0x5100);
321 if ( strPatientPosition != gdcm::GDCM_UNFOUND
322 && strPatientPosition != "" )
323 std::cout << "PatientPosition (0x0010,0x5100)= ["
324 << strPatientPosition << "]" << std::endl;
326 std::string strPatientOrientation =
327 f->GetEntryValue(0x0020,0x0020);
328 if ( strPatientOrientation != gdcm::GDCM_UNFOUND
329 && strPatientOrientation != "")
330 std::cout << "PatientOrientation (0x0020,0x0020)= ["
331 << strPatientOrientation << "]" << std::endl;
333 std::string strImageOrientationPatient =
334 f->GetEntryValue(0x0020,0x0037);
335 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
336 && strImageOrientationPatient != "" )
337 std::cout << "ImageOrientationPatient (0x0020,0x0037)= ["
338 << strImageOrientationPatient << "]" << std::endl;
340 std::string strImageOrientationRET =
341 f->GetEntryValue(0x0020,0x0035);
342 if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
343 && strImageOrientationRET != "" )
344 std::cout << "ImageOrientationRET (0x0020,0x0035)= ["
345 << strImageOrientationRET << "]" << std::endl;
347 // Let's compute 'user friendly' results about 'Orientation'
348 // ---------------------------------------------------------
352 if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
353 strImageOrientationRET != gdcm::GDCM_UNFOUND )
356 double orient = o.TypeOrientation( f );
361 std::cout << "TypeOrientation = " << orient << " (-> "
362 << tabOrientation[k] << " )" << std::endl;
365 std::string ori = o.GetOrientation ( f );
367 std::cout << "Orientation [" << ori << "]" << std::endl;
370 // Display the LUT as an int array (for debugging purpose)
371 if ( f->HasLUT() && showlut )
373 uint8_t* lutrgba = fh->GetLutRGBA();
376 std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
378 // Nothing is written yet to get LUT Data user friendly
379 // The following is to be moved into a PixelRedaConvert method
381 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
384 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
387 std::string lutDescriptor = sqi->GetEntryValue(0x0028,0x3002);
388 int length; // LUT length in Bytes
389 int deb; // Subscript of the first Lut Value
390 int nbits; // Lut item size (in Bits)
391 int nbRead; // nb of items in LUT descriptor (must be = 3)
393 nbRead = sscanf( lutDescriptor.c_str(),
395 &length, &deb, &nbits );
398 //gdcmWarningMacro( "Wrong LUT descriptor" );
399 std::cout << "Wrong LUT descriptor" << std::endl;
401 gdcm::BinEntry *b = sqi->GetBinEntry(0x0028,0x3006);
404 if ( b->GetLength() != 0 )
406 std::cout << "---------------------------------------"
407 << " We should never reach this point "
409 //LoadEntryBinArea(b); //LUT Data (CTX dependent)
415 std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
419 if ( fh->GetLutItemSize() == 8 )
421 for (int i=0;i<fh->GetLutItemNumber();i++)
422 std::cout << i << " : \t"
423 << (int)(lutrgba[i*4]) << " "
424 << (int)(lutrgba[i*4+1]) << " "
425 << (int)(lutrgba[i*4+2]) << std::endl;
427 else // LutItemSize assumed to be = 16
429 uint16_t* lutrgba16 = (uint16_t*)lutrgba;
430 for (int i=0;i<fh->GetLutItemNumber();i++)
431 std::cout << i << " : \t"
432 << (int)(lutrgba16[i*4]) << " "
433 << (int)(lutrgba16[i*4+1]) << " "
434 << (int)(lutrgba16[i*4+2]) << std::endl;
440 std::cout << "Try LUT Data "<< std::endl;
444 //if( !f->gdcm::Document::IsReadable())
445 // Try downcast to please MSVC
446 if ( !((gdcm::Document *)f)->IsReadable() )
448 std::cout <<std::endl<<fileName<<" is NOT 'gdcm parsable'"<<std::endl;
452 std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
453 else if ( f->GetSeqEntry(0x0041,0x1010) )
455 std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
458 else if ( f->GetSeqEntry(0x0004,0x1220) )
460 std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
465 std::cout <<std::endl<<fileName<<" doesn't look like an image file "
469 std::cout<<std::flush;
474 else // ====== Deal with a Directory ======
476 std::cout << "dirName [" << dirName << "]" << std::endl;
477 gdcm::DirList dirList(dirName,1); // gets recursively the file list
478 gdcm::DirListType fileList = dirList.GetFilenames();
481 for( gdcm::DirListType::iterator it = fileList.begin();
482 it != fileList.end();
485 std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
487 f = new gdcm::File();
488 f->SetLoadMode(loadMode);
489 f->SetFileName( it->c_str() );
491 for (int ri=0; ri<forceLoadNb; ri++)
493 printf("%04x,%04x\n",elemsToForceLoad[2*ri],
494 elemsToForceLoad[2*ri+1]);
495 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
496 (uint32_t)elemsToForceLoad[2*ri+1]);
502 std::cout << "Cannot process file [" << it->c_str() << "]"
504 std::cout << "Either it doesn't exist, or it's read protected "
506 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
508 std::cout << "use 'PrintFile filein=... debug' "
509 << "to try to guess the pb"
515 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
516 fh->SetPrintLevel( level );
520 std::string ori = o.GetOrientation ( f );
521 if (ori != gdcm::GDCM_UNFOUND )
522 std::cout << "- Orientation [" << ori << "]" << std::endl;
524 double d = o.TypeOrientation( f );
529 std::cout << "TypeOrientation = " << d << " (-> "
530 << tabOrientation[k] << std::endl;
533 std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
535 std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
536 std::cout << "\n\n" << std::endl;
540 std::cout<<std::flush;