]> Creatis software - gdcm.git/blobdiff - Testing/TestCopyDicom.cxx
ENH: Apply first patch toward better string comparison when dealing with broken DICOM...
[gdcm.git] / Testing / TestCopyDicom.cxx
index 0225453919dc50341f12ec1d33418a71f5663b89..3689353aa348185eae3cc2f8269ff993caec710e 100644 (file)
@@ -1,5 +1,17 @@
 #include "gdcmHeader.h"
 #include "gdcmFile.h"
+#include "gdcmDocument.h"
+#include "gdcmValEntry.h"
+#include "gdcmBinEntry.h"
+
+//Generated file:
+#include "gdcmDataImages.h"
+
+#ifndef _WIN32
+#include <unistd.h> //for access, unlink
+#else
+#include <io.h> //for _access on Win32
+#endif
 
 // return true if the file exists
 bool FileExists(const char* filename)
@@ -31,49 +43,94 @@ bool RemoveFile(const char* source)
 // Here we load a gdcmFile and then try to create from scratch a copy of it,
 // copying field by field the dicom image
 
-int TestCopyDicom(int argc, char* argv[])
+int TestCopyDicom(int , char* [])
 {
-   if (argc < 3)
+   int i =0;
+   int retVal = 0;  //by default this is an error
+   while( gdcmDataImages[i] != 0 )
    {
-      std::cerr << "Usage :" << std::endl << 
-      argv[0] << " input_dicom output_dicom" << std::endl;
-      return 1;
-   }
+      std::string filename = GDCM_DATA_ROOT;
+      filename += "/";  //doh!
+      filename += gdcmDataImages[i];
+      std::cerr << "Filename: " << filename << std::endl;
 
-   if( FileExists( argv[2] ) )
-   {
-      std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
-      if( !RemoveFile( argv[2] ) )
+      std::string output = "../Testing/Temporary/output.dcm";
+
+      if( FileExists( output.c_str() ) )
       {
-         std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
-         return 1;
+        // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
+         if( !RemoveFile( output.c_str() ) )
+         {
+            std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
+            return 1;
+         }
       }
-   }
-   gdcmFile *original = new gdcmFile( argv[1] );
-   gdcmFile *copy = new gdcmFile( argv[2] );
 
-   //First of all copy the header field by field
-   TagNameHT & nameHt = original->GetHeader()->GetPubDict()->GetEntriesByName();
-   for (TagNameHT::iterator tag = nameHt.begin(); tag != nameHt.end(); ++tag)
-   {
-      std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
+      gdcm::File *original = new gdcm::File( filename );
+      gdcm::File *copy = new gdcm::File( output );
 
-      copy->GetHeader()->ReplaceOrCreateByNumber( tag->second->GetVR(), 
-            tag->second->GetGroup(), tag->second->GetElement() );
-   }
+      const gdcm::TagDocEntryHT & Ht = original->GetHeader()->GetTagHT();
 
-   size_t dataSize = original->GetImageDataSize();
-   void *imageData = original->GetImageData();
+      size_t dataSize = original->GetImageDataSize();
+      uint8_t* imageData = original->GetImageData();
 
-   copy->SetImageData(imageData, dataSize);
-   //original->GetHeader()->SetImageDataSize(dataSize);
+      //First of all copy the header field by field
+  
+      // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
+      // It was commented out by Mathieu, that was a *good* idea
+      // (the user does NOT have to know the way we implemented the Header !)
+      // Waiting for a 'clean' solution, I keep the method ...JPRx
 
-   //copy->GetHeader()->PrintEntry();
+      gdcm::DocEntry* d;
 
-   copy->WriteDcmExplVR( argv[2] );
+      for (gdcm::TagDocEntryHT::const_iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
+      {
+         d = tag->second;
+         if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
+         {              
+            copy->GetHeader()->ReplaceOrCreateByNumber( 
+                                 b->GetBinArea(),
+                                 b->GetLength(),
+                                 b->GetGroup(), 
+                                 b->GetElement(),
+                                 b->GetVR() );
+         }
+         else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
+         {   
+             copy->GetHeader()->ReplaceOrCreateByNumber( 
+                                 v->GetValue(),
+                                 v->GetGroup(), 
+                                 v->GetElement(),
+                                 v->GetVR() ); 
+         }
+         else
+         {
+          // We skip pb of SQ recursive exploration
+          //std::cout << "Skipped Sequence " 
+          //          << "------------- " << d->GetVR() << " "<< std::hex
+          //          << d->GetGroup() << " " << d->GetElement()
+          //  << std::endl;    
+         }
+      }
 
-   return 0;
-}
+      copy->SetImageData(imageData, dataSize);
+      original->GetHeader()->SetImageDataSize(dataSize);
+
+      copy->WriteDcmExplVR( output );
 
+      delete original;
+      delete copy;
 
+      copy = new gdcm::File( output );
+
+      //Is the file written still gdcm parsable ?
+      if ( !copy->GetHeader()->IsReadable() )
+      { 
+         retVal +=1;
+         std::cout << output << " Failed" << std::endl;
+      }
+      i++;
+   }
+   return retVal;
+}