]> Creatis software - gdcm.git/blobdiff - Testing/TestCopyDicom.cxx
Last modif before gdcmPixelData class introduction :
[gdcm.git] / Testing / TestCopyDicom.cxx
index a5b6dfa3ce8aed993a34c1d2b6bd04a30a2179f7..5b35c48bd57c22bf2cc8dc365f98fa28d81a4b01 100644 (file)
@@ -2,10 +2,15 @@
 #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
+#endif
+
 // return true if the file exists
 bool FileExists(const char* filename)
 {
@@ -36,7 +41,7 @@ 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 , char* [])
+int TestCopyDicom(int argc, char* argv[])
 {
    int i =0;
    int retVal = 0;  //by default this is an error
@@ -51,7 +56,7 @@ int TestCopyDicom(int , char* [])
 
       if( FileExists( output.c_str() ) )
       {
-         std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
+        // 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;
@@ -62,50 +67,68 @@ int TestCopyDicom(int , char* [])
       gdcmFile *original = new gdcmFile( filename );
       gdcmFile *copy = new gdcmFile( output );
 
+      TagDocEntryHT & Ht = original->GetHeader()->GetEntry(); 
+
       //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)
+  
+      // 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
+
+      gdcmDocEntry* d;
+
+      for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
       {
-         if (tag->second->GetVR() == "SQ") //to skip pb of SQ recursive exploration
-            continue;
-
-         //According to JPR I should also skip those:
-//         if (tag->second->GetVR() == "unkn") //to skip pb of SQ recursive exploration
-//            continue;
-
-         //no clue what to do with this one
-         //std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
-         std::string value = ((gdcmValEntry*)(tag->second))->GetValue();
-//         if( value.find( "gdcm::NotLoaded" ) == 0 )
-//            continue;
-
-         //no clue what to do with this one
-         if( value.find( "gdcm::Loaded" ) == 0 )
-            continue;
-
-         copy->GetHeader()->ReplaceOrCreateByNumber( 
-               value, 
-               tag->second->GetGroup(), 
-               tag->second->GetElement() );
+         d = tag->second;
+         if ( gdcmBinEntry* b = dynamic_cast<gdcmBinEntry*>(d) )
+         {              
+            copy->GetHeader()->ReplaceOrCreateByNumber( 
+                                 b->GetVoidArea(),
+                                 b->GetLength(),
+                                 b->GetGroup(), 
+                                 b->GetElement(),
+                                 b->GetVR() );
+            }
+            else  if ( gdcmValEntry* v = dynamic_cast<gdcmValEntry*>(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;    
+           }
       }
 
       size_t dataSize = original->GetImageDataSize();
       void *imageData = original->GetImageData();
 
       copy->SetImageData(imageData, dataSize);
-      //original->GetHeader()->SetImageDataSize(dataSize);
+      original->GetHeader()->SetImageDataSize(dataSize);
+
+      copy->WriteDcmExplVR( output );
 
-      //copy->GetHeader()->PrintEntry();
+      delete original;
+      delete copy;
+
+      copy = new gdcmFile( output );
 
-      //copy->WriteDcmExplVR( output );
-      
       //Is the file written still gdcm parsable ?
-      gdcmFile check( output );
-      retVal += !check.GetHeader()->IsReadable();
+      if ( !copy->GetHeader()->IsReadable() )
+      { 
+         retVal +=1;
+         std::cout << output << " Failed" << std::endl;
+      }
+      i++;
    }
-
    return retVal;
 }
 
-
-