]> Creatis software - gdcm.git/blobdiff - Example/PrintFile.cxx
minor improvements
[gdcm.git] / Example / PrintFile.cxx
index 1da00db2ac3fd11e0d6fc881b9945ea28d06fc0f..23f648359c4b9ceff43a4c82de5324deaf281cb3 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: PrintFile.cxx,v $
   Language:  C++
-  Date:      $Date: 2007/09/26 08:07:42 $
-  Version:   $Revision: 1.86 $
+  Date:      $Date: 2009/05/28 15:44:34 $
+  Version:   $Revision: 1.93 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -30,7 +30,7 @@
 #include "gdcmOrientation.h"
 #include <iostream>
 
-// TODO : code factorization, for 'single file' an 'whole directory' processing
+/// \todo : code factorization, for 'single file' an 'whole directory' processing
 
 void ShowLutData(GDCM_NAME_SPACE::File *f);
 
@@ -127,11 +127,12 @@ int main(int argc, char *argv[])
    " usage: PrintFile {filein=inputFileName|dirin=inputDirectoryName}[level=n]",
    "                       [forceload=listOfElementsToForceLoad] [rec] [noex] ",
    "                       [4DLoc= ][dict= privateDirectory]                  ",
-   "                       [ { [noshadowseq] | [noshadow][noseq] } ]          ",
+   "                       [ { [noshadowseq] | [noshadow][noseq] } ] [load]   ",
    "                       [debug] [warning]                                  ",
    "      level = 0,1,2 : depending on the amount of details user wants to see",
    "      rec : user wants to parse recursively the directory                 ",
-   "      noex : user doen't want extra 'user friendly' info                  ",   
+   "      load : user wants to load the pixels, as well (to see warning info) ",
+   "      noex : user doesn't want extra 'user friendly' info                 ",   
    "      4DLoc: group-elem(in hexa, no space) of the DataEntry holdind 4thDim",
    "      listOfElementsToForceLoad : group-elem,g2-e2,... (in hexa, no space)",
    "                                of Elements to load whatever their length ",
@@ -193,7 +194,7 @@ int main(int argc, char *argv[])
    int forceLoadNb;
    uint16_t *elemsToForceLoad 
                            = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
-   
+
    int nbP =0;
    uint16_t *FourthDimLoc;
    if ( am->ArgMgrDefined("4DLoc") )
@@ -208,6 +209,8 @@ int main(int argc, char *argv[])
       }
    }
 
+   bool load = ( 0 != am->ArgMgrDefined("load") );
+   
    bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
 
    bool ddict = am->ArgMgrDefined("dict") ? true : false;
@@ -230,47 +233,56 @@ int main(int argc, char *argv[])
 
    // ----------- End Arguments Manager ---------
 
+
    if (ddict)
    {
-      GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);   
+      GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);
    }
 
    if ( fileName != 0 ) // ====== Deal with a single file ======
-   { 
+   {
       GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
       f->SetLoadMode(loadMode);
       f->SetFileName( fileName );
+   f->SetMaxSizeLoadEntry(0xffff);
 
       for (int ri=0; ri<forceLoadNb; ri++)
       {
          f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri], 
-                                (uint32_t)elemsToForceLoad[2*ri+1] ); 
+                                (uint32_t)elemsToForceLoad[2*ri+1] );
       }
 // TODO : find why such a polution
-// To avoid polluting the output with messages 
+// To avoid polluting the output with messages
 // 'Last system error was : No such file or directory'
 
 errno = 0; 
 
 
-      bool res = f->Load();
+      bool res = false;
+      try
+        {
+        res = f->Load();
+        }
+      catch(std::exception &ex)
+        {
+        std::cerr << "sorry an exception was thrown: " << ex.what() << std::endl;
+        }
       // GDCM_NAME_SPACE::File::IsReadable() is no usable here, because we deal with
-      // any kind of gdcm-Parsable *document* 
+      // any kind of gdcm-Parsable *document*
       // not only GDCM_NAME_SPACE::File (as opposed to GDCM_NAME_SPACE::DicomDir)
       if ( !res )
       {
          std::cout << "Cannot process file [" << fileName << "]" << std::endl;
-         std::cout << "Either it doesn't exist, or it's read protected " 
+         std::cout << "Either it doesn't exist, or it's read protected "
                    << std::endl;
-         std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
+         std::cout << "or it's not a Dicom File, or its 'header' is bugged"
                    << std::endl;
          std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
                    << std::endl;
          f->Delete();
          return 0;
       }
-      
+
       if (nbP == 1)
          f->SetFourthDimensionLocation(FourthDimLoc[0],FourthDimLoc[1]);
 
@@ -280,7 +292,7 @@ errno = 0;
 
       fh->Print();
 
-      std::cout << "\n\n" << std::endl; 
+      std::cout << "\n\n" << std::endl;
 
       std::cout <<std::endl;
       std::cout <<" dataSize    " << fh->GetImageDataSize()    << std::endl;
@@ -335,13 +347,18 @@ if (!noex)
       std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
       std::cout << " ------" << std::endl;
 
+
       std::cout << "\n\n" << std::endl; 
       std::cout << "X spacing " << f->GetXSpacing() << std::endl;
       std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
       std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
-    
+   
 //------------------------------
 
+
+
+
+
       // Let's get and print some usefull fields about 'Orientation'
       // ------------------------------------------------------------
 
@@ -395,9 +412,11 @@ if (!noex)
                    << strImagePositionPatientRET << "]" << std::endl;
   
      float iop[6];
-     /*bool riop = */f->GetImageOrientationPatient(iop);  
+     //bool riop = 
+        f->GetImageOrientationPatient(iop);  
      float ipp[3];
-     /*bool ripp = */f->GetImagePositionPatient(ipp);
+     //bool ripp = 
+        f->GetImagePositionPatient(ipp);
 
      std::cout << "Image Position (0x0020,0x0032|0x0030) : "
                << ipp[0] << " , " << ipp[1] << " , "<< ipp[2]
@@ -427,6 +446,80 @@ if (!noex)
       if (ori != "\\" )
          std::cout << "Orientation [" << ori << "]" << std::endl;
       o->Delete();
+      
+      
+/*      
+std::vector <double> valueVector; 
+GDCM_NAME_SPACE::DataEntry *e_0018_5212 = f->GetDataEntry(0x0018, 0x5212);
+bool resJP = e_0018_5212->GetDSValue(valueVector);
+if (resJP) {
+   double test;
+   for ( int i=0; i < 3; i++ ) {
+       test = valueVector[i];
+       std::cout << " test " << test << std::endl;
+    }
+}
+//e_0018_5212->Delete();
+*/       
+
+
+/* -----------------------------
+
+// Try :
+std::cout << std::endl << std::endl << "===========Try Get Numerical ======="
+          << std::endl;
+GDCM_NAME_SPACE::DataEntry *e;
+bool res;
+std::vector<double> vd;
+
+// Transfert Syntax
+e=f->GetDataEntry(0x0002,0x0010);
+if (e){
+  res=e->GetNumerical(vd);
+  if (!res){
+    std::cout << "0x0002,0x0010 not numerical, size =" << vd.size() << std::endl;  
+  }
+}
+// Columns
+e=f->GetDataEntry(0x0028,0x0011);
+if (e){
+  res=e->GetNumerical(vd);
+  if (!res){
+    std::cout << "0x0028,0x0011 not numerical, size =" << vd.size() << std::endl;  
+  } else {
+    std::cout << "0x0028,0x0011 numerical, size =" << vd.size() << std::endl;
+    std::cout << vd[0]<< std::endl;
+  }
+}
+// Im Orient (Pat)
+e=f->GetDataEntry(0x0020,0x0032);
+if (e){
+  res=e->GetNumerical(vd);
+  if (!res){
+    std::cout << "0x0020,0x0032 not numerical, size =" << vd.size() << std::endl;  
+  } else {
+    std::cout << "0x0020,0x0032 numerical, size =" << vd.size() << std::endl;
+    for(int l=0; l<vd.size(); l++)
+      std::cout << "vd[" << l << "]=" << vd[l]<< std::endl;
+  }
+}
+
+// Pixel Spacing
+
+e=f->GetDataEntry(0x0028,0x0030);
+if (e){
+  res=e->GetNumerical(vd);
+  if (!res){
+    std::cout << "0x0028,0x0030 not numerical, size =" << vd.size() << std::endl;  
+  } else {
+    std::cout << "0x0028,0x0030 numerical, size =" << vd.size() << std::endl;
+    for(int l=0; l<vd.size(); l++)
+      std::cout << "vd[" << l << "]=" << vd[l]<< std::endl;
+  }
+}
+
+----------------------------------------------*/  
+    
 }  
 //------------------------------
 
@@ -477,6 +570,7 @@ if (!noex)
             else
                std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
         }
+        /*
          else
          {
             if ( fh->GetLutItemSize() == 8 )
@@ -497,6 +591,7 @@ if (!noex)
                          << (int)(lutrgba16[i*4+2]) << std::endl;
             }
          }
+         */
       }
       else if (showlut)
       {
@@ -530,8 +625,9 @@ if (!noex)
       f->Delete();
       fh->Delete();
    }
-   else  // ====== Deal with a Directory ======
-   {
+         // ===========================================================================
+   else  // =============================== Deal with a Directory =====================
+   {     // ===========================================================================
       std::cout << "dirName [" << dirName << "]" << std::endl;
       
       GDCM_NAME_SPACE::DirList dirList(dirName,rec); // gets recursively (or not) the file list
@@ -539,6 +635,11 @@ if (!noex)
       GDCM_NAME_SPACE::File *f;
       bool res;
 
+      if (fileList.size() == 0)
+      {
+         std::cout << "No file found in : [" << dirName << "]" << std::endl;
+      }
+      
       for( GDCM_NAME_SPACE::DirListType::iterator it  = fileList.begin();
                                  it != fileList.end();
                                  ++it )
@@ -639,15 +740,26 @@ if (!noex)
          std::string ori = o->GetOrientation ( f );
          if (ori != "\\" )
             std::cout << "Orientation [" << ori << "]" << std::endl;
-         o->Delete(); 
+         o->Delete();
 }
 //------------------------------- 
         
          if (f->IsReadable())
+         {
+            if (load)  // just to see warning messages at load time !
+            {
+               uint8_t *pixels = fh->GetImageData(); (void)pixels;
+               uint32_t lgth   = fh->GetImageDataSize(); (void)lgth;
+            }         
+
             std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
+         }
          else
             std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
          std::cout << "\n\n" << std::endl;
+         
+
+
          f->Delete();
          fh->Delete();
       }