]> Creatis software - gdcm.git/blob - Example/PrintFile.cxx
77a4920fdb770bf1e4850d8afecb0a03d8a42dd4
[gdcm.git] / Example / PrintFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PrintFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/28 10:55:00 $
7   Version:   $Revision: 1.74 $
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             std::cout << "LUT Descriptor is missing" << std::endl;
53             return;
54          }
55          int length;   // LUT length in Bytes
56          int deb;      // Subscript of the first Lut Value
57          int nbits;    // Lut item size (in Bits)
58
59          int nbRead;    // nb of items in LUT descriptor (must be = 3)
60
61          nbRead = sscanf( lutDescriptor.c_str(),
62                            "%d\\%d\\%d",
63                               &length, &deb, &nbits );
64          std::cout << "length " << length 
65                   << " deb " << deb 
66                   << " nbits " << nbits
67                   << std::endl;
68          if ( nbRead != 3 )
69          {
70             std::cout << "Wrong LUT descriptor" << std::endl;
71          }
72          //LUT Data (CTX dependent)    
73          gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006); 
74          if ( b != 0 )
75          { 
76             int BitsAllocated = f->GetBitsAllocated();
77             if ( BitsAllocated <= 8 )
78             { 
79                int mult;
80                if ( ( nbits == 16 ) && ( BitsAllocated == 8 ) )
81                {
82                // when LUT item size is different than pixel size
83                   mult = 2; // high byte must be = low byte
84                }
85                else
86                {
87                // See PS 3.3-2003 C.11.1.1.2 p 619
88                   mult = 1;
89                }
90                uint8_t *lut = b->GetBinArea();
91                for( int i=0; i < length; ++i )
92                {
93                   std::cout << i+deb << " : \t"
94                               << (int) (lut[i*mult + 1]) << std::endl;
95                }
96             }
97             else
98             {
99                uint16_t *lut = (uint16_t *)(b->GetBinArea());  
100                for( int i=0; i < length; ++i )
101                {
102                   std::cout << i+deb << " : \t"
103                               << (int) (((uint16_t *)lut)[i])
104                               << std::endl;
105                }             
106             }
107          }  
108          else
109             std::cout << "No LUT Data DataEntry (0x0028,0x3006) found?!? " 
110                         << std::endl;
111       }
112       else
113          std::cout << "No First SQ Item within (0x0028,0x3000) ?!? " 
114                      << std::endl;      
115    }
116    else
117       std::cout << "No LUT Data SeqEntry (0x0028,0x3000) found " 
118                   << std::endl;
119 }
120
121 int main(int argc, char *argv[])
122 {
123
124    START_USAGE(usage)
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)  ",
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    if (am->ArgMgrDefined("warning"))
172       gdcm::Debug::WarningOn();
173        
174    int loadMode = gdcm::LD_ALL;
175    if ( am->ArgMgrDefined("noshadowseq") )
176       loadMode |= gdcm::LD_NOSHADOWSEQ;
177    else 
178    {
179    if ( am->ArgMgrDefined("noshadow") )
180          loadMode |= gdcm::LD_NOSHADOW;
181       if ( am->ArgMgrDefined("noseq") )
182          loadMode |= gdcm::LD_NOSEQ;
183    }
184
185    int level = am->ArgMgrGetInt("level", 1);
186
187    int forceLoadNb;
188    uint16_t *elemsToForceLoad 
189                            = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
190
191    bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
192
193    bool ddict = am->ArgMgrDefined("dict") ? true : false;
194    char *dict = 0;
195
196    if (ddict)
197    {
198      dict = am->ArgMgrGetString("dict",(char *)0);
199    }
200
201    /* if unused Param we give up */
202    if ( am->ArgMgrPrintUnusedLabels() )
203    {
204       am->ArgMgrUsage(usage);
205       delete am;
206       return 1;
207    } 
208
209    delete am;  // we don't need Argument Manager any longer
210
211    // ----------- End Arguments Manager ---------
212
213  
214    if (ddict)
215    {
216       gdcm::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);   
217    }
218
219    if ( fileName != 0 ) // ====== Deal with a single file ======
220    { 
221       gdcm::File *f = gdcm::File::New();
222       f->SetLoadMode(loadMode);
223       f->SetFileName( fileName );
224
225       for (int ri=0; ri<forceLoadNb; ri++)
226       {
227          f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri], 
228                                 (uint32_t)elemsToForceLoad[2*ri+1] ); 
229       }
230
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)
235       if ( !res )
236       {
237          std::cout << "Cannot process file [" << fileName << "]" << std::endl;
238          std::cout << "Either it doesn't exist, or it's read protected " 
239                    << std::endl;
240          std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
241                    << std::endl;
242          std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
243                    << std::endl;
244          f->Delete();
245          return 0;
246       }
247
248       gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
249       fh->SetPrintLevel( level );
250
251       fh->Print();
252
253       std::cout << "\n\n" << std::endl; 
254
255       std::cout <<std::endl;
256       std::cout <<" dataSize    " << fh->GetImageDataSize()    << std::endl;
257       std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
258
259       int nX,nY,nZ,sPP,planarConfig;
260       std::string pixelType;
261       nX=f->GetXSize();
262       nY=f->GetYSize();
263       nZ=f->GetZSize();
264       std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ 
265                 << std::endl;
266
267       pixelType    = f->GetPixelType();
268       sPP          = f->GetSamplesPerPixel();
269       std::cout << " pixelType= ["            << pixelType 
270                 << "] SamplesPerPixel= ["     << sPP
271                 << "] ";
272
273       if (sPP == 3)
274       {
275          planarConfig = f->GetPlanarConfiguration();
276          std::cout << " PlanarConfiguration= [" << planarConfig 
277                 << "] "<< std::endl;
278       } 
279       std::cout << " PhotometricInterpretation= [" 
280                 << f->GetEntryString(0x0028,0x0004)
281                 << "] "<< std::endl;
282
283       int numberOfScalarComponents=f->GetNumberOfScalarComponents();
284       std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents 
285                 <<std::endl
286                 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
287                 << std::endl;
288
289       if ( f->GetDataEntry(0x0002,0x0010) )
290          if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() ) 
291          {
292             std::cout << "Transfer Syntax not loaded. " << std::endl
293                      << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
294                   << std::endl;
295             f->Delete();
296             return 0;
297          }
298   
299       std::string transferSyntaxName = f->GetTransferSyntaxName();
300       std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" 
301                 << std::endl;
302       std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
303       std::cout << " ------" << std::endl;
304
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;
309
310 //------------------------------
311       // Lets's get and print some usefull fields about 'Orientation'
312       // ------------------------------------------------------------
313
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;
320  
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;
327       
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;
334
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;
341
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;
348
349       // Let's compute 'user friendly' results about 'Orientation'
350       // ---------------------------------------------------------
351  
352       gdcm::Orientation *o = gdcm::Orientation::New();
353
354       if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
355            strImageOrientationRET     != gdcm::GDCM_UNFOUND )
356       {
357   
358          gdcm::OrientationType orient = o->GetOrientationType( f );
359  
360          std::cout << "TypeOrientation = " << orient << " (-> " 
361                    << o->GetOrientationTypeString(orient) << " )" << std::endl;
362       }
363
364       std::string ori = o->GetOrientation ( f );
365       if (ori != "\\" )
366          std::cout << "Orientation [" << ori << "]" << std::endl;
367  
368        o->gdcm::Orientation::Delete(); 
369 //------------------------------
370
371
372       // Display the LUT as an int array (for debugging purpose)
373       if ( f->HasLUT() && showlut )
374       {
375          uint8_t* lutrgba = fh->GetLutRGBA();
376          if ( lutrgba == 0 )
377          {
378             std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
379  
380            // Nothing is written yet to get LUT Data user friendly
381            // The following is to be moved into a PixelRedaConvert method
382   
383             gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
384             if ( modLutSeq !=0 )
385             {
386                gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
387                if ( !sqi )
388                {
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)
394
395                   nbRead = sscanf( lutDescriptor.c_str(),
396                                     "%d\\%d\\%d",
397                                      &length, &deb, &nbits );
398                   if ( nbRead != 3 )
399                   {
400                       std::cout << "Wrong LUT descriptor" << std::endl;
401                   }                                                  
402                   gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
403                   if ( b != 0 )
404                   {
405                      if ( b->GetLength() != 0 )
406                      {
407                         std::cout << "---------------------------------------"
408                                << " We should never reach this point      "
409                                << std::endl;
410                         //LoadEntryBinArea(b);    //LUT Data (CTX dependent)
411                      }   
412                  }
413               }      
414             }
415             else
416                std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
417         }
418          else
419          {
420             if ( fh->GetLutItemSize() == 8 )
421             {
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;
427             }
428             else // LutItemSize assumed to be = 16
429             {
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;
436             }
437          }
438       }
439       else if (showlut)
440       {
441          std::cout << "Try LUT Data "<< std::endl;
442          ShowLutData(f);
443       }
444
445       // Parsability of the gdcm::Document already checked, after Load() !
446       
447       if ( f->IsReadable() )
448       {
449          std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
450       }
451       else if ( f->GetSeqEntry(0x0041,0x1010) )
452       {
453          std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
454                    <<std::endl;
455       }
456       else if ( f->GetSeqEntry(0x0004,0x1220) )
457       {
458          std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
459                    <<std::endl;
460       }
461       else 
462       {
463          std::cout <<std::endl<<fileName<<" doesn't look like an image file "
464              <<std::endl; 
465       }
466  
467       std::cout<<std::flush;
468       f->Delete();
469       fh->Delete();
470    }
471    else  // ====== Deal with a Directory ======
472    {
473       std::cout << "dirName [" << dirName << "]" << std::endl;
474       gdcm::DirList dirList(dirName,1); // gets recursively the file list
475       gdcm::DirListType fileList = dirList.GetFilenames();
476       gdcm::File *f;
477       bool res;
478
479       for( gdcm::DirListType::iterator it  = fileList.begin();
480                                  it != fileList.end();
481                                  ++it )
482       {
483          std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
484                    << std::endl;
485          f = gdcm::File::New();
486          f->SetLoadMode(loadMode);
487          f->SetFileName( it->c_str() );
488
489          for (int ri=0; ri<forceLoadNb; ri++)
490          {
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]); 
495          }
496          res = f->Load();
497
498          if ( !res )
499          {
500             std::cout << "Cannot process file [" << it->c_str() << "]" 
501                       << std::endl;
502             std::cout << "Either it doesn't exist, or it's read protected " 
503                       << std::endl;
504             std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
505                       << std::endl;
506             std::cout << "use 'PrintFile filein=... debug' "
507                       << "to try to guess the pb"
508                       << std::endl;
509             f->Delete();
510             continue;
511          }
512
513          gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
514          fh->SetPrintLevel( level );
515          fh->Print();
516
517 //------------------------------
518
519          // Lets's get and print some usefull fields about 'Orientation'
520          // ------------------------------------------------------------
521
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;
528     
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;
535          
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;
542
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;
549
550          std::string strImageOrientationRET = 
551                                        f->GetEntryString(0x0020,0x0035);
552          if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
553          && strImageOrientationRET != "" )
554          {
555             std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
556                      << strImageOrientationRET << "]" << std::endl;
557          }
558
559          // Let's compute 'user friendly' results about 'Orientation'
560          // ---------------------------------------------------------
561     
562          gdcm::Orientation *o = gdcm::Orientation::New(); 
563
564
565          if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
566             strImageOrientationRET     != gdcm::GDCM_UNFOUND )
567          {
568      
569             gdcm::OrientationType orient = o->GetOrientationType( f );
570     
571             std::cout << "TypeOrientation = " << orient << " (-> " 
572                      << o->GetOrientationTypeString(orient) << " )" << std::endl;
573          }
574
575          std::string ori = o->GetOrientation ( f );
576          if (ori != "\\" )
577             std::cout << "Orientation [" << ori << "]" << std::endl;
578    
579          o->gdcm::Orientation::Delete(); 
580
581 //------------------------------- 
582         
583          if (f->IsReadable())
584             std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
585          else
586             std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
587          std::cout << "\n\n" << std::endl;
588          f->Delete();
589          fh->Delete();
590       }
591       std::cout<<std::flush;
592    }
593    return 0;
594 }