]> Creatis software - gdcm.git/blob - Example/PrintFile.cxx
minor improvements
[gdcm.git] / Example / PrintFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PrintFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2009/05/28 15:44:34 $
7   Version:   $Revision: 1.93 $
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_NAME_SPACE::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_NAME_SPACE::File *f)
42 {  
43    GDCM_NAME_SPACE::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
44    if ( modLutSeq !=0 )
45    {
46       GDCM_NAME_SPACE::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_NAME_SPACE::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] [rec] [noex] ",
129    "                       [4DLoc= ][dict= privateDirectory]                  ",
130    "                       [ { [noshadowseq] | [noshadow][noseq] } ] [load]   ",
131    "                       [debug] [warning]                                  ",
132    "      level = 0,1,2 : depending on the amount of details user wants to see",
133    "      rec : user wants to parse recursively the directory                 ",
134    "      load : user wants to load the pixels, as well (to see warning info) ",
135    "      noex : user doesn't want extra 'user friendly' info                 ",   
136    "      4DLoc: group-elem(in hexa, no space) of the DataEntry holdind 4thDim",
137    "      listOfElementsToForceLoad : group-elem,g2-e2,... (in hexa, no space)",
138    "                                of Elements to load whatever their length ",
139    "      privateDirectory : source file full path name of Shadow Group elems ",
140    "      noshadowseq: user doesn't want to load Private Sequences            ",
141    "      noshadow   : user doesn't want to load Private groups (odd number)  ",
142    "      noseq      : user doesn't want to load Sequences                    ",
143    "      debug      : user wants to run the program in 'debug mode'          ",
144    "      warning    : user wants to be warned about any oddity in the File   ",
145    "      showlut :user wants to display the Palette Color (as an int array)  ",
146    FINISH_USAGE
147
148    // Initialize Arguments Manager   
149    GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
150   
151    if (argc == 1 || am->ArgMgrDefined("usage") )
152    {
153       am->ArgMgrUsage(usage); // Display 'usage'
154       delete am;
155       return 1;
156    }
157
158    const char *fileName = am->ArgMgrGetString("filein");
159    const char *dirName  = am->ArgMgrGetString("dirin");
160
161    if ( (fileName == 0 && dirName == 0) ||
162         (fileName != 0 && dirName != 0) )
163    {
164       std::cerr << std::endl
165         << "Either 'filein=' or 'dirin=' must be present;" 
166         << std::endl << "Not both" << std::endl;
167       am->ArgMgrUsage(usage); // Display 'usage'  
168       delete am;
169       return 1;
170    }   
171       
172    bool noex = ( 0 != am->ArgMgrDefined("noex") );
173    bool rec  = ( 0 != am->ArgMgrDefined("rec") );   
174         
175    if (am->ArgMgrDefined("debug"))
176       GDCM_NAME_SPACE::Debug::DebugOn();
177
178    if (am->ArgMgrDefined("warning"))
179       GDCM_NAME_SPACE::Debug::WarningOn();
180        
181    int loadMode = GDCM_NAME_SPACE::LD_ALL;
182    if ( am->ArgMgrDefined("noshadowseq") )
183       loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
184    else 
185    {
186       if ( am->ArgMgrDefined("noshadow") )
187          loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
188       if ( am->ArgMgrDefined("noseq") )
189          loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
190    }
191
192    int level = am->ArgMgrGetInt("level", 1);
193
194    int forceLoadNb;
195    uint16_t *elemsToForceLoad 
196                            = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
197
198    int nbP =0;
199    uint16_t *FourthDimLoc;
200    if ( am->ArgMgrDefined("4DLoc") )
201    {
202       FourthDimLoc = am->ArgMgrGetXInt16Enum("4DLoc", &nbP);
203    
204       if (nbP != 1) 
205       {   
206          std::cout << "4DLoc must have 2 and only 2 components!" << std::endl;
207          delete am;
208          return 1;      
209       }
210    }
211
212    bool load = ( 0 != am->ArgMgrDefined("load") );
213    
214    bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
215
216    bool ddict = am->ArgMgrDefined("dict") ? true : false;
217    const char *dict = 0;
218
219    if (ddict)
220    {
221      dict = am->ArgMgrGetString("dict",0);
222    }
223
224    /* if unused Param we give up */
225    if ( am->ArgMgrPrintUnusedLabels() )
226    {
227       am->ArgMgrUsage(usage);
228       delete am;
229       return 1;
230    } 
231
232    delete am;  // we don't need Argument Manager any longer
233
234    // ----------- End Arguments Manager ---------
235
236
237    if (ddict)
238    {
239       GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);
240    }
241
242    if ( fileName != 0 ) // ====== Deal with a single file ======
243    {
244       GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
245       f->SetLoadMode(loadMode);
246       f->SetFileName( fileName );
247    f->SetMaxSizeLoadEntry(0xffff);
248
249       for (int ri=0; ri<forceLoadNb; ri++)
250       {
251          f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri], 
252                                 (uint32_t)elemsToForceLoad[2*ri+1] );
253       }
254 // TODO : find why such a polution
255 // To avoid polluting the output with messages
256 // 'Last system error was : No such file or directory'
257
258 errno = 0; 
259
260
261       bool res = false;
262       try
263         {
264         res = f->Load();
265         }
266       catch(std::exception &ex)
267         {
268         std::cerr << "sorry an exception was thrown: " << ex.what() << std::endl;
269         }
270       // GDCM_NAME_SPACE::File::IsReadable() is no usable here, because we deal with
271       // any kind of gdcm-Parsable *document*
272       // not only GDCM_NAME_SPACE::File (as opposed to GDCM_NAME_SPACE::DicomDir)
273       if ( !res )
274       {
275          std::cout << "Cannot process file [" << fileName << "]" << std::endl;
276          std::cout << "Either it doesn't exist, or it's read protected "
277                    << std::endl;
278          std::cout << "or it's not a Dicom File, or its 'header' is bugged"
279                    << std::endl;
280          std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
281                    << std::endl;
282          f->Delete();
283          return 0;
284       }
285
286       if (nbP == 1)
287          f->SetFourthDimensionLocation(FourthDimLoc[0],FourthDimLoc[1]);
288
289
290       GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
291       fh->SetPrintLevel( level );
292
293       fh->Print();
294
295       std::cout << "\n\n" << std::endl;
296
297       std::cout <<std::endl;
298       std::cout <<" dataSize    " << fh->GetImageDataSize()    << std::endl;
299       std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
300 if (!noex)
301 {
302       int nX,nY,nZ,nT,sPP,planarConfig;
303       std::string pixelType;
304       nX=f->GetXSize();
305       nY=f->GetYSize();
306       nZ=f->GetZSize();
307       nT=f->GetTSize();
308       std::cout << " DIMX=" << nX << " DIMY=" << nY 
309                 << " DIMZ=" << nZ << " DIMT=" << nT
310                 << std::endl;
311
312       pixelType    = f->GetPixelType();
313       sPP          = f->GetSamplesPerPixel();
314       std::cout << " pixelType= ["            << pixelType 
315                 << "] SamplesPerPixel= ["     << sPP
316                 << "] ";
317
318       if (sPP == 3)
319       {
320          planarConfig = f->GetPlanarConfiguration();
321          std::cout << " PlanarConfiguration= [" << planarConfig 
322                 << "] "<< std::endl;
323       } 
324       std::cout << " PhotometricInterpretation= [" 
325                 << f->GetEntryString(0x0028,0x0004)
326                 << "] "<< std::endl;
327
328       int numberOfScalarComponents=f->GetNumberOfScalarComponents();
329       std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents 
330                 <<std::endl
331                 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
332                 << std::endl;
333
334       if ( f->GetDataEntry(0x0002,0x0010) )
335          if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() ) 
336          {
337             std::cout << "Transfer Syntax not loaded. " << std::endl
338                      << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
339                   << std::endl;
340             f->Delete();
341             return 0;
342          }
343   
344       std::string transferSyntaxName = f->GetTransferSyntaxName();
345       std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" 
346                 << std::endl;
347       std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
348       std::cout << " ------" << std::endl;
349
350
351       std::cout << "\n\n" << std::endl; 
352       std::cout << "X spacing " << f->GetXSpacing() << std::endl;
353       std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
354       std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
355    
356 //------------------------------
357
358
359
360
361
362       // Let's get and print some usefull fields about 'Orientation'
363       // ------------------------------------------------------------
364
365       std::string strPatientPosition = 
366                                       f->GetEntryString(0x0018,0x5100);
367       if ( strPatientPosition != GDCM_NAME_SPACE::GDCM_UNFOUND 
368         && strPatientPosition != "" )  
369             std::cout << "PatientPosition (0x0010,0x5100)= [" 
370                       << strPatientPosition << "]" << std::endl;
371  
372       std::string strViewPosition = 
373                                       f->GetEntryString(0x0018,0x5101);
374       if ( strViewPosition != GDCM_NAME_SPACE::GDCM_UNFOUND 
375         && strViewPosition != "" )  
376             std::cout << "View Position (0x0018,0x5101)= [" 
377                       << strViewPosition << "]" << std::endl;
378       
379      std::string strPatientOrientation = 
380                                       f->GetEntryString(0x0020,0x0020);
381       if ( strPatientOrientation != GDCM_NAME_SPACE::GDCM_UNFOUND
382         && strPatientOrientation != "")  
383          std::cout << "PatientOrientation (0x0020,0x0020)= [" 
384                    << strPatientOrientation << "]" << std::endl;
385
386       std::string strImageOrientationPatient = 
387                                       f->GetEntryString(0x0020,0x0037);  
388       if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND
389         && strImageOrientationPatient != "" )  
390          std::cout << "ImageOrientationPatient (0x0020,0x0037)= [" 
391                    << strImageOrientationPatient << "]" << std::endl;
392
393       std::string strImageOrientationRET = 
394                                       f->GetEntryString(0x0020,0x0035);
395       if ( strImageOrientationRET != GDCM_NAME_SPACE::GDCM_UNFOUND
396         && strImageOrientationRET != "" )  
397          std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
398                    << strImageOrientationRET << "]" << std::endl;
399
400       std::string strImagePositionPatient = 
401                                       f->GetEntryString(0x0020,0x0032);  
402       if ( strImagePositionPatient != GDCM_NAME_SPACE::GDCM_UNFOUND
403         && strImagePositionPatient != "" )  
404          std::cout << "ImagePositionPatient (0x0020,0x0032)= [" 
405                    << strImagePositionPatient << "]" << std::endl;
406
407       std::string strImagePositionPatientRET = 
408                                       f->GetEntryString(0x0020,0x0030);
409       if ( strImagePositionPatientRET != GDCM_NAME_SPACE::GDCM_UNFOUND
410         && strImagePositionPatientRET != "" )  
411          std::cout << "ImagePositionPatientRET (0x0020,0x0030)= [" 
412                    << strImagePositionPatientRET << "]" << std::endl;
413   
414      float iop[6];
415      //bool riop = 
416         f->GetImageOrientationPatient(iop);  
417      float ipp[3];
418      //bool ripp = 
419         f->GetImagePositionPatient(ipp);
420
421      std::cout << "Image Position (0x0020,0x0032|0x0030) : "
422                << ipp[0] << " , " << ipp[1] << " , "<< ipp[2]
423                << std::endl;
424      std::cout << "Image Orientation (0x0020,0x0037|0x0035) : "
425                << iop[0] << " , " << iop[1] << " , "<< iop[2] << " , "
426                << iop[3] << " , " << iop[4] << " , "<< iop[5]
427                << std::endl; 
428
429
430       // Let's compute 'user friendly' results about 'Orientation'
431       // ---------------------------------------------------------
432  
433       GDCM_NAME_SPACE::Orientation *o = GDCM_NAME_SPACE::Orientation::New();
434
435       if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND ||
436            strImageOrientationRET     != GDCM_NAME_SPACE::GDCM_UNFOUND )
437       {
438   
439          GDCM_NAME_SPACE::OrientationType orient = o->GetOrientationType( f );
440  
441          std::cout << "TypeOrientation = " << orient << " (-> " 
442                    << o->GetOrientationTypeString(orient) << " )" << std::endl;
443       }
444
445       std::string ori = o->GetOrientation ( f );
446       if (ori != "\\" )
447          std::cout << "Orientation [" << ori << "]" << std::endl;
448       o->Delete();
449       
450       
451 /*      
452 std::vector <double> valueVector; 
453 GDCM_NAME_SPACE::DataEntry *e_0018_5212 = f->GetDataEntry(0x0018, 0x5212);
454 bool resJP = e_0018_5212->GetDSValue(valueVector);
455 if (resJP) {
456    double test;
457    for ( int i=0; i < 3; i++ ) {
458        test = valueVector[i];
459        std::cout << " test " << test << std::endl;
460     }
461 }
462 //e_0018_5212->Delete();
463 */       
464
465
466 /* -----------------------------
467
468 // Try :
469 std::cout << std::endl << std::endl << "===========Try Get Numerical ======="
470           << std::endl;
471 GDCM_NAME_SPACE::DataEntry *e;
472 bool res;
473 std::vector<double> vd;
474
475 // Transfert Syntax
476 e=f->GetDataEntry(0x0002,0x0010);
477 if (e){
478   res=e->GetNumerical(vd);
479   if (!res){
480     std::cout << "0x0002,0x0010 not numerical, size =" << vd.size() << std::endl;  
481   }
482 }
483 // Columns
484 e=f->GetDataEntry(0x0028,0x0011);
485 if (e){
486   res=e->GetNumerical(vd);
487   if (!res){
488     std::cout << "0x0028,0x0011 not numerical, size =" << vd.size() << std::endl;  
489   } else {
490     std::cout << "0x0028,0x0011 numerical, size =" << vd.size() << std::endl;
491     std::cout << vd[0]<< std::endl;
492   }
493 }
494 // Im Orient (Pat)
495 e=f->GetDataEntry(0x0020,0x0032);
496 if (e){
497   res=e->GetNumerical(vd);
498   if (!res){
499     std::cout << "0x0020,0x0032 not numerical, size =" << vd.size() << std::endl;  
500   } else {
501     std::cout << "0x0020,0x0032 numerical, size =" << vd.size() << std::endl;
502     for(int l=0; l<vd.size(); l++)
503       std::cout << "vd[" << l << "]=" << vd[l]<< std::endl;
504   }
505 }
506
507 // Pixel Spacing
508
509 e=f->GetDataEntry(0x0028,0x0030);
510 if (e){
511   res=e->GetNumerical(vd);
512   if (!res){
513     std::cout << "0x0028,0x0030 not numerical, size =" << vd.size() << std::endl;  
514   } else {
515     std::cout << "0x0028,0x0030 numerical, size =" << vd.size() << std::endl;
516     for(int l=0; l<vd.size(); l++)
517       std::cout << "vd[" << l << "]=" << vd[l]<< std::endl;
518   }
519 }
520
521 ----------------------------------------------*/  
522     
523 }  
524 //------------------------------
525
526
527       // Display the LUT as an int array (for debugging purpose)
528       if ( f->HasLUT() && showlut )
529       {
530          uint8_t* lutrgba = fh->GetLutRGBA();
531          if ( lutrgba == 0 )
532          {
533             std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
534  
535            // Nothing is written yet to get LUT Data user friendly
536            // The following is to be moved into a PixelRedaConvert method
537   
538             GDCM_NAME_SPACE::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
539             if ( modLutSeq !=0 )
540             {
541                GDCM_NAME_SPACE::SQItem *sqi= modLutSeq->GetFirstSQItem();
542                if ( !sqi )
543                {
544                   std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
545                   int length;   // LUT length in Bytes
546                   int deb;      // Subscript of the first Lut Value
547                   int nbits;    // Lut item size (in Bits)
548                   int nbRead;   // nb of items in LUT descriptor (must be = 3)
549
550                   nbRead = sscanf( lutDescriptor.c_str(),
551                                     "%d\\%d\\%d",
552                                      &length, &deb, &nbits );
553                   if ( nbRead != 3 )
554                   {
555                       std::cout << "Wrong LUT descriptor" << std::endl;
556                   }                                                  
557                   GDCM_NAME_SPACE::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
558                   if ( b != 0 )
559                   {
560                      if ( b->GetLength() != 0 )
561                      {
562                         std::cout << "---------------------------------------"
563                                << " We should never reach this point      "
564                                << std::endl;
565                         //LoadEntryBinArea(b);    //LUT Data (CTX dependent)
566                      }   
567                  }
568               }      
569             }
570             else
571                std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
572         }
573         /*
574          else
575          {
576             if ( fh->GetLutItemSize() == 8 )
577             {
578                for (int i=0;i<fh->GetLutItemNumber();i++)
579                   std::cout << i << " : \t"
580                          << (int)(lutrgba[i*4])   << " "
581                          << (int)(lutrgba[i*4+1]) << " "
582                          << (int)(lutrgba[i*4+2]) << std::endl;
583             }
584             else // LutItemSize assumed to be = 16
585             {
586                uint16_t* lutrgba16 = (uint16_t*)lutrgba;
587                for (int i=0;i<fh->GetLutItemNumber();i++)
588                   std::cout << i << " : \t"
589                          << (int)(lutrgba16[i*4])   << " "
590                          << (int)(lutrgba16[i*4+1]) << " "
591                          << (int)(lutrgba16[i*4+2]) << std::endl;
592             }
593          }
594          */
595       }
596       else if (showlut)
597       {
598          std::cout << "Try LUT Data "<< std::endl;
599          ShowLutData(f);
600       }
601
602       // Parsability of the GDCM_NAME_SPACE::Document already checked, after Load() !
603       
604       if ( f->IsReadable() )
605       {
606          std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
607       }
608       else if ( f->GetSeqEntry(0x0041,0x1010) )
609       {
610          std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
611                    <<std::endl;
612       }
613       else if ( f->GetSeqEntry(0x0004,0x1220) )
614       {
615          std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
616                    <<std::endl;
617       }
618       else 
619       {
620          std::cout <<std::endl<<fileName<<" doesn't look like an image file "
621              <<std::endl; 
622       }
623  
624       std::cout<<std::flush;
625       f->Delete();
626       fh->Delete();
627    }
628          // ===========================================================================
629    else  // =============================== Deal with a Directory =====================
630    {     // ===========================================================================
631       std::cout << "dirName [" << dirName << "]" << std::endl;
632       
633       GDCM_NAME_SPACE::DirList dirList(dirName,rec); // gets recursively (or not) the file list
634       GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
635       GDCM_NAME_SPACE::File *f;
636       bool res;
637
638       if (fileList.size() == 0)
639       {
640          std::cout << "No file found in : [" << dirName << "]" << std::endl;
641       }
642       
643       for( GDCM_NAME_SPACE::DirListType::iterator it  = fileList.begin();
644                                  it != fileList.end();
645                                  ++it )
646       {
647          std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
648                    << std::endl;
649          f = GDCM_NAME_SPACE::File::New();
650          f->SetLoadMode(loadMode);
651          f->SetFileName( it->c_str() );
652
653          for (int ri=0; ri<forceLoadNb; ri++)
654          {
655             printf("%04x,%04x\n",elemsToForceLoad[2*ri], 
656                                  elemsToForceLoad[2*ri+1]);
657             f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri], 
658                                    (uint32_t)elemsToForceLoad[2*ri+1]); 
659          }
660          res = f->Load();
661
662          if ( !res )
663          {
664             std::cout << "Cannot process file [" << it->c_str() << "]" 
665                       << std::endl;
666             std::cout << "Either it doesn't exist, or it's read protected " 
667                       << std::endl;
668             std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
669                       << std::endl;
670             std::cout << "use 'PrintFile filein=... debug' "
671                       << "to try to guess the pb"
672                       << std::endl;
673             f->Delete();
674             continue;
675          }
676
677          GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
678          fh->SetPrintLevel( level );
679          fh->Print();
680
681 //------------------------------
682 if (!noex)
683 {
684          // Lets's get and print some usefull fields about 'Orientation'
685          // ------------------------------------------------------------
686
687          std::string strPatientPosition = 
688                                        f->GetEntryString(0x0018,0x5100);
689          if ( strPatientPosition != GDCM_NAME_SPACE::GDCM_UNFOUND 
690          && strPatientPosition != "" )  
691                std::cout << "PatientPosition (0x0010,0x5100)= [" 
692                         << strPatientPosition << "]" << std::endl;
693     
694          std::string strViewPosition = 
695                                        f->GetEntryString(0x0018,0x5101);
696          if ( strViewPosition != GDCM_NAME_SPACE::GDCM_UNFOUND 
697          && strViewPosition != "" )  
698                std::cout << "strViewPosition (0x0010,0x5101)= [" 
699                         << strViewPosition << "]" << std::endl;
700          
701          std::string strPatientOrientation = 
702                                        f->GetEntryString(0x0020,0x0020);
703          if ( strPatientOrientation != GDCM_NAME_SPACE::GDCM_UNFOUND
704          && strPatientOrientation != "")  
705             std::cout << "PatientOrientation (0x0020,0x0020)= [" 
706                       << strPatientOrientation << "]" << std::endl;
707
708          std::string strImageOrientationPatient = 
709                                        f->GetEntryString(0x0020,0x0037);  
710          if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND
711          && strImageOrientationPatient != "" )  
712             std::cout << "ImageOrientationPatient (0x0020,0x0037)= [" 
713                      << strImageOrientationPatient << "]" << std::endl;
714
715          std::string strImageOrientationRET = 
716                                        f->GetEntryString(0x0020,0x0035);
717          if ( strImageOrientationRET != GDCM_NAME_SPACE::GDCM_UNFOUND
718          && strImageOrientationRET != "" )
719          {
720             std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
721                      << strImageOrientationRET << "]" << std::endl;
722          }
723
724          // Let's compute 'user friendly' results about 'Orientation'
725          // ---------------------------------------------------------
726     
727          GDCM_NAME_SPACE::Orientation *o = GDCM_NAME_SPACE::Orientation::New(); 
728
729
730          if ( strImageOrientationPatient != GDCM_NAME_SPACE::GDCM_UNFOUND ||
731             strImageOrientationRET     != GDCM_NAME_SPACE::GDCM_UNFOUND )
732          {
733      
734             GDCM_NAME_SPACE::OrientationType orient = o->GetOrientationType( f );
735     
736             std::cout << "TypeOrientation = " << orient << " (-> " 
737                      << o->GetOrientationTypeString(orient) << " )" << std::endl;
738          }
739
740          std::string ori = o->GetOrientation ( f );
741          if (ori != "\\" )
742             std::cout << "Orientation [" << ori << "]" << std::endl;
743          o->Delete();
744 }
745 //------------------------------- 
746         
747          if (f->IsReadable())
748          {
749             if (load)  // just to see warning messages at load time !
750             {
751                uint8_t *pixels = fh->GetImageData(); (void)pixels;
752                uint32_t lgth   = fh->GetImageDataSize(); (void)lgth;
753             }         
754
755             std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
756          }
757          else
758             std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
759          std::cout << "\n\n" << std::endl;
760          
761
762
763          f->Delete();
764          fh->Delete();
765       }
766       std::cout<<std::flush;
767    }
768    return 0;
769 }