]> Creatis software - gdcm.git/blob - Example/PrintFile.cxx
To avoid useless warnings
[gdcm.git] / Example / PrintFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PrintFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/27 09:07:45 $
7   Version:   $Revision: 1.68 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmFile.h"
19 #include "gdcmDocument.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmSQItem.h"
22 #include "gdcmDataEntry.h"
23
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"
31 #include <iostream>
32
33 // TODO : code factorization, for 'single file' an 'whole directory' processing
34
35 void ShowLutData(gdcm::File *f);
36
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.
40
41 void ShowLutData(gdcm::File *f)
42 {  
43    gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
44    if ( modLutSeq !=0 )
45    {
46       gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
47       if ( sqi != 0 )
48       {
49          std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
50          if (   /*lutDescriptor   == GDCM_UNFOUND*/ 0 )
51          {
52             //gdcmWarningMacro( "LUT Descriptor is missing" );
53             std::cout << "LUT Descriptor is missing" << std::endl;
54             return;
55          }
56          int length;   // LUT length in Bytes
57          int deb;      // Subscript of the first Lut Value
58          int nbits;    // Lut item size (in Bits)
59
60          int nbRead;    // nb of items in LUT descriptor (must be = 3)
61
62          nbRead = sscanf( lutDescriptor.c_str(),
63                            "%d\\%d\\%d",
64                               &length, &deb, &nbits );
65          std::cout << "length " << length 
66                   << " deb " << deb 
67                   << " nbits " << nbits
68                   << std::endl;
69          if ( nbRead != 3 )
70          {
71                //gdcmWarningMacro( "Wrong LUT descriptor" );
72                std::cout << "Wrong LUT descriptor" << std::endl;
73          }
74          //LUT Data (CTX dependent)    
75          gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006); 
76          if ( b != 0 )
77          { 
78             int BitsAllocated = f->GetBitsAllocated();
79             if ( BitsAllocated <= 8 )
80             { 
81                int mult;
82                if ( ( nbits == 16 ) && ( BitsAllocated == 8 ) )
83                {
84                // when LUT item size is different than pixel size
85                   mult = 2; // high byte must be = low byte
86                }
87                else
88                {
89                // See PS 3.3-2003 C.11.1.1.2 p 619
90                   mult = 1;
91                }
92                uint8_t *lut = b->GetBinArea();
93                for( int i=0; i < length; ++i )
94                {
95                   std::cout << i+deb << " : \t"
96                               << (int) (lut[i*mult + 1]) << std::endl;
97                }
98             }
99             else
100             {
101                uint16_t *lut = (uint16_t *)(b->GetBinArea());  
102                for( int i=0; i < length; ++i )
103                {
104                   std::cout << i+deb << " : \t"
105                               << (int) (((uint16_t *)lut)[i])
106                               << std::endl;
107                }             
108             }
109          }  
110          else
111             std::cout << "No LUT Data DataEntry (0x0028,0x3006) found?!? " 
112                         << std::endl;
113       }
114       else
115          std::cout << "No First SQ Item within (0x0028,0x3000) ?!? " 
116                      << std::endl;      
117    }
118    else
119       std::cout << "No LUT Data SeqEntry (0x0028,0x3000) found " 
120                   << std::endl;
121 }
122
123 int main(int argc, char *argv[])
124 {
125
126    START_USAGE(usage)
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] } ] [debug]  ",
133    "      level = 0,1,2 : depending on the amount of details user wants to see",
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    "      showlut :user wants to display the Palette Color (as an int array)  ",
142    FINISH_USAGE
143
144    // Initialize Arguments Manager   
145    gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
146   
147    if (argc == 1 || am->ArgMgrDefined("usage") )
148    {
149       am->ArgMgrUsage(usage); // Display 'usage'
150       delete am;
151       return 1;
152    }
153
154    char *fileName = am->ArgMgrGetString("filein",(char *)0);
155    char *dirName  = am->ArgMgrGetString("dirin",(char *)0);
156
157    if ( (fileName == 0 && dirName == 0) ||
158         (fileName != 0 && dirName != 0) )
159    {
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'  
164       delete am;
165       return 1;
166    }
167
168    if (am->ArgMgrDefined("debug"))
169       gdcm::Debug::DebugOn();
170  
171    int loadMode = gdcm::LD_ALL;
172    if ( am->ArgMgrDefined("noshadowseq") )
173       loadMode |= gdcm::LD_NOSHADOWSEQ;
174    else 
175    {
176    if ( am->ArgMgrDefined("noshadow") )
177          loadMode |= gdcm::LD_NOSHADOW;
178       if ( am->ArgMgrDefined("noseq") )
179          loadMode |= gdcm::LD_NOSEQ;
180    }
181
182    int level = am->ArgMgrGetInt("level", 1);
183
184    int forceLoadNb;
185    uint16_t *elemsToForceLoad 
186                            = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
187
188    bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
189
190    bool ddict = am->ArgMgrDefined("dict") ? true : false;
191    char *dict = 0;
192
193    if (ddict)
194    {
195      dict = am->ArgMgrGetString("dict",(char *)0);
196    }
197
198    /* if unused Param we give up */
199    if ( am->ArgMgrPrintUnusedLabels() )
200    {
201       am->ArgMgrUsage(usage);
202       delete am;
203       return 1;
204    } 
205
206    delete am;  // we don't need Argument Manager any longer
207
208    // ----------- End Arguments Manager ---------
209
210  
211    if (ddict)
212    {
213       gdcm::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);   
214    }
215
216    if ( fileName != 0 ) // ====== Deal with a single file ======
217    { 
218       // gdcm::File::IsReadable() is no usable here, because we deal with
219       // any kind of gdcm-Parsable *document* 
220       // not only gdcm::File (as opposed to gdcm::DicomDir)
221
222       gdcm::File *f = gdcm::File::New();
223       f->SetLoadMode(loadMode);
224       f->SetFileName( fileName );
225
226       for (int ri=0; ri<forceLoadNb; ri++)
227       {
228          f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri], 
229                                 (uint32_t)elemsToForceLoad[2*ri+1] ); 
230       }
231
232       bool res = f->Load();
233       if ( !res )
234       {
235          std::cout << "Cannot process file [" << fileName << "]" << std::endl;
236          std::cout << "Either it doesn't exist, or it's read protected " 
237                    << std::endl;
238          std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
239                    << std::endl;
240          std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
241                    << std::endl;
242          f->Delete();
243          return 0;
244       }
245
246       gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
247       fh->SetPrintLevel( level );
248
249       fh->Print();
250
251       std::cout << "\n\n" << std::endl; 
252
253       std::cout <<std::endl;
254       std::cout <<" dataSize    " << fh->GetImageDataSize()    << std::endl;
255       std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
256
257       int nX,nY,nZ,sPP,planarConfig;
258       std::string pixelType;
259       nX=f->GetXSize();
260       nY=f->GetYSize();
261       nZ=f->GetZSize();
262       std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ 
263                 << std::endl;
264
265       pixelType    = f->GetPixelType();
266       sPP          = f->GetSamplesPerPixel();
267       if (sPP == 3)
268          planarConfig = f->GetPlanarConfiguration();
269
270       std::cout << " pixelType= ["            << pixelType 
271                 << "] SamplesPerPixel= ["     << sPP
272                 << "] ";
273       if (sPP == 3)
274          std::cout << " PlanarConfiguration= [" << planarConfig 
275                 << "] "<< std::endl;
276  
277       std::cout << " PhotometricInterpretation= [" 
278                 << f->GetEntryString(0x0028,0x0004)
279                 << "] "<< std::endl;
280
281       int numberOfScalarComponents=f->GetNumberOfScalarComponents();
282       std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents 
283                 <<std::endl
284                 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
285                 << std::endl;
286
287       if ( f->GetDataEntry(0x0002,0x0010) )
288          if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() ) 
289          {
290             std::cout << "Transfer Syntax not loaded. " << std::endl
291                      << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
292                   << std::endl;
293             f->Delete();
294             return 0;
295          }
296   
297       std::string transferSyntaxName = f->GetTransferSyntaxName();
298       std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" 
299                 << std::endl;
300       std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
301       std::cout << " ------" << std::endl;
302
303       //std::cout << "\n\n" << std::endl; 
304       //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
305       //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
306       //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
307
308 //------------------------------
309       // Lets's get and print some usefull fields about 'Orientation'
310       // ------------------------------------------------------------
311
312       std::string strPatientPosition = 
313                                       f->GetEntryString(0x0018,0x5100);
314       if ( strPatientPosition != gdcm::GDCM_UNFOUND 
315         && strPatientPosition != "" )  
316             std::cout << "PatientPosition (0x0010,0x5100)= [" 
317                       << strPatientPosition << "]" << std::endl;
318  
319       std::string strViewPosition = 
320                                       f->GetEntryString(0x0018,0x5101);
321       if ( strViewPosition != gdcm::GDCM_UNFOUND 
322         && strViewPosition != "" )  
323             std::cout << "strViewPosition (0x0010,0x5101)= [" 
324                       << strViewPosition << "]" << std::endl;
325       
326      std::string strPatientOrientation = 
327                                       f->GetEntryString(0x0020,0x0020);
328       if ( strPatientOrientation != gdcm::GDCM_UNFOUND
329         && strPatientOrientation != "")  
330          std::cout << "PatientOrientation (0x0020,0x0020)= [" 
331                    << strPatientOrientation << "]" << std::endl;
332
333       std::string strImageOrientationPatient = 
334                                       f->GetEntryString(0x0020,0x0037);  
335       if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
336         && strImageOrientationPatient != "" )  
337          std::cout << "ImageOrientationPatient (0x0020,0x0037)= [" 
338                    << strImageOrientationPatient << "]" << std::endl;
339
340       std::string strImageOrientationRET = 
341                                       f->GetEntryString(0x0020,0x0035);
342       if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
343         && strImageOrientationRET != "" )  
344          std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
345                    << strImageOrientationRET << "]" << std::endl;
346
347       // Let's compute 'user friendly' results about 'Orientation'
348       // ---------------------------------------------------------
349  
350       gdcm::Orientation o;
351
352       if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
353            strImageOrientationRET     != gdcm::GDCM_UNFOUND )
354       {
355   
356          gdcm::OrientationType orient = o.GetOrientationType( f );
357  
358          std::cout << "TypeOrientation = " << orient << " (-> " 
359                    << o.GetOrientationTypeString(orient) << " )" << std::endl;
360       }
361
362       std::string ori = o.GetOrientation ( f );
363       if (ori != "\\" )
364          std::cout << "Orientation [" << ori << "]" << std::endl;
365  
366 //------------------------------
367
368
369       // Display the LUT as an int array (for debugging purpose)
370       if ( f->HasLUT() && showlut )
371       {
372          uint8_t* lutrgba = fh->GetLutRGBA();
373          if ( lutrgba == 0 )
374          {
375             std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
376  
377            // Nothing is written yet to get LUT Data user friendly
378            // The following is to be moved into a PixelRedaConvert method
379   
380             gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
381             if ( modLutSeq !=0 )
382             {
383                gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
384                if ( !sqi )
385                {
386                   std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
387                   int length;   // LUT length in Bytes
388                   int deb;      // Subscript of the first Lut Value
389                   int nbits;    // Lut item size (in Bits)
390                   int nbRead;   // nb of items in LUT descriptor (must be = 3)
391
392                   nbRead = sscanf( lutDescriptor.c_str(),
393                                     "%d\\%d\\%d",
394                                      &length, &deb, &nbits );
395                   if ( nbRead != 3 )
396                   {
397                       //gdcmWarningMacro( "Wrong LUT descriptor" );
398                       std::cout << "Wrong LUT descriptor" << std::endl;
399                   }                                                  
400                   gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
401                   if ( b != 0 )
402                   {
403                      if ( b->GetLength() != 0 )
404                      {
405                         std::cout << "---------------------------------------"
406                                << " We should never reach this point      "
407                                << std::endl;
408                         //LoadEntryBinArea(b);    //LUT Data (CTX dependent)
409                      }   
410                  }
411               }      
412             }
413             else
414                std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
415         }
416          else
417          {
418             if ( fh->GetLutItemSize() == 8 )
419             {
420                for (int i=0;i<fh->GetLutItemNumber();i++)
421                   std::cout << i << " : \t"
422                          << (int)(lutrgba[i*4])   << " "
423                          << (int)(lutrgba[i*4+1]) << " "
424                          << (int)(lutrgba[i*4+2]) << std::endl;
425             }
426             else // LutItemSize assumed to be = 16
427             {
428                uint16_t* lutrgba16 = (uint16_t*)lutrgba;
429                for (int i=0;i<fh->GetLutItemNumber();i++)
430                   std::cout << i << " : \t"
431                          << (int)(lutrgba16[i*4])   << " "
432                          << (int)(lutrgba16[i*4+1]) << " "
433                          << (int)(lutrgba16[i*4+2]) << std::endl;
434             }
435          }
436       }
437       else if (showlut)
438       {
439          std::cout << "Try LUT Data "<< std::endl;
440          ShowLutData(f);
441       }
442
443       //if( !f->gdcm::Document::IsReadable())
444       // Try downcast to please MSVC
445       if ( !((gdcm::Document *)f)->IsReadable() )
446       {
447          std::cout <<std::endl<<fileName<<" is NOT 'gdcm parsable'"<<std::endl;
448       }
449      
450       if ( f->IsReadable() )
451       {
452          std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
453       }
454       else if ( f->GetSeqEntry(0x0041,0x1010) )
455       {
456          std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
457                    <<std::endl;
458       }
459       else if ( f->GetSeqEntry(0x0004,0x1220) )
460       {
461          std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
462                    <<std::endl;
463       }
464       else 
465       {
466          std::cout <<std::endl<<fileName<<" doesn't look like an image file "
467              <<std::endl; 
468       }
469  
470       std::cout<<std::flush;
471       f->Delete();
472       delete fh;
473    }
474    else  // ====== Deal with a Directory ======
475    {
476       std::cout << "dirName [" << dirName << "]" << std::endl;
477       gdcm::DirList dirList(dirName,1); // gets recursively the file list
478       gdcm::DirListType fileList = dirList.GetFilenames();
479       gdcm::File *f;
480       bool res;
481
482       for( gdcm::DirListType::iterator it  = fileList.begin();
483                                  it != fileList.end();
484                                  ++it )
485       {
486          std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
487                    << std::endl;
488          f = gdcm::File::New();
489          f->SetLoadMode(loadMode);
490          f->SetFileName( it->c_str() );
491
492          for (int ri=0; ri<forceLoadNb; ri++)
493          {
494             printf("%04x,%04x\n",elemsToForceLoad[2*ri], 
495                                  elemsToForceLoad[2*ri+1]);
496             f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri], 
497                                    (uint32_t)elemsToForceLoad[2*ri+1]); 
498          }
499          res = f->Load();
500
501          if ( !res )
502          {
503             std::cout << "Cannot process file [" << it->c_str() << "]" 
504                       << std::endl;
505             std::cout << "Either it doesn't exist, or it's read protected " 
506                       << std::endl;
507             std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
508                       << std::endl;
509             std::cout << "use 'PrintFile filein=... debug' "
510                       << "to try to guess the pb"
511                       << std::endl;
512             f->Delete();
513             continue;
514          }
515
516          gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
517          fh->SetPrintLevel( level );
518          fh->Print();
519
520 //------------------------------
521
522          // Lets's get and print some usefull fields about 'Orientation'
523          // ------------------------------------------------------------
524
525          std::string strPatientPosition = 
526                                        f->GetEntryString(0x0018,0x5100);
527          if ( strPatientPosition != gdcm::GDCM_UNFOUND 
528          && strPatientPosition != "" )  
529                std::cout << "PatientPosition (0x0010,0x5100)= [" 
530                         << strPatientPosition << "]" << std::endl;
531     
532          std::string strViewPosition = 
533                                        f->GetEntryString(0x0018,0x5101);
534          if ( strViewPosition != gdcm::GDCM_UNFOUND 
535          && strViewPosition != "" )  
536                std::cout << "strViewPosition (0x0010,0x5101)= [" 
537                         << strViewPosition << "]" << std::endl;
538          
539          std::string strPatientOrientation = 
540                                        f->GetEntryString(0x0020,0x0020);
541          if ( strPatientOrientation != gdcm::GDCM_UNFOUND
542          && strPatientOrientation != "")  
543             std::cout << "PatientOrientation (0x0020,0x0020)= [" 
544                      << strPatientOrientation << "]" << std::endl;
545
546          std::string strImageOrientationPatient = 
547                                        f->GetEntryString(0x0020,0x0037);  
548          if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
549          && strImageOrientationPatient != "" )  
550             std::cout << "ImageOrientationPatient (0x0020,0x0037)= [" 
551                      << strImageOrientationPatient << "]" << std::endl;
552
553          std::string strImageOrientationRET = 
554                                        f->GetEntryString(0x0020,0x0035);
555          if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
556          && strImageOrientationRET != "" )
557          {
558             std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
559                      << strImageOrientationRET << "]" << std::endl;
560          }
561
562          // Let's compute 'user friendly' results about 'Orientation'
563          // ---------------------------------------------------------
564     
565          gdcm::Orientation o;
566
567          if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
568             strImageOrientationRET     != gdcm::GDCM_UNFOUND )
569          {
570      
571             gdcm::OrientationType orient = o.GetOrientationType( f );
572     
573             std::cout << "TypeOrientation = " << orient << " (-> " 
574                      << o.GetOrientationTypeString(orient) << " )" << std::endl;
575          }
576
577          std::string ori = o.GetOrientation ( f );
578          if (ori != "\\" )
579             std::cout << "Orientation [" << ori << "]" << std::endl;
580
581 //------------------------------- 
582
583         
584          if (f->IsReadable())
585             std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
586          else
587             std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
588          std::cout << "\n\n" << std::endl;
589          f->Delete();
590          fh->Delete();
591       }
592       std::cout<<std::flush;
593    }
594    return 0;
595 }