]> Creatis software - gdcm.git/blobdiff - Testing/TestCopyDicom.cxx
* src/gdcmDocument.cxx : now, when using the ReplaceOrCreateByNumber to
[gdcm.git] / Testing / TestCopyDicom.cxx
index e67a9926ce380aebb25487a81fa6963cd9a313e3..9706397c7a37bde09657f98ab7d7fe0b288ba8a1 100644 (file)
@@ -1,13 +1,33 @@
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: TestCopyDicom.cxx,v $
+  Language:  C++
+  Date:      $Date: 2004/11/17 10:20:06 $
+  Version:   $Revision: 1.17 $
+                                                                                
+  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+  l'Image). All rights reserved. See Doc/License.txt or
+  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
+                                                                                
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+                                                                                
+=========================================================================*/
 #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
@@ -55,7 +75,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;
@@ -63,53 +83,73 @@ int TestCopyDicom(int , char* [])
          }
       }
 
-      gdcmFile *original = new gdcmFile( filename );
-      gdcmFile *copy = new gdcmFile( output );
+      gdcm::File *original = new gdcm::File( filename );
+      gdcm::File *copy = new gdcm::File( output );
+
+      const gdcm::TagDocEntryHT & Ht = original->GetHeader()->GetTagHT();
+
+      size_t dataSize = original->GetImageDataSize();
+      uint8_t* imageData = original->GetImageData();
 
       //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
+
+      gdcm::DocEntry* d;
+
+      for (gdcm::TagDocEntryHT::const_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 ( 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;    
+         }
       }
 
-      size_t dataSize = original->GetImageDataSize();
-      void *imageData = original->GetImageData();
+      // Useless to set the image datas, because it's already made when
+      // copying the corresponding BinEntry that contains the pixel datas
+      //copy->SetImageData(imageData, dataSize);
+      original->GetHeader()->SetImageDataSize(dataSize);
+
+      copy->WriteDcmExplVR( output );
 
-      copy->SetImageData(imageData, dataSize);
-      //original->GetHeader()->SetImageDataSize(dataSize);
+      delete original;
+      delete copy;
 
-      //copy->GetHeader()->PrintEntry();
+      copy = new gdcm::File( 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;
 }
 
-
-