]> Creatis software - gdcm.git/blobdiff - Example/TestCopyDicom.cxx
Remove useless Example/TestFromScratch.cxx (Example/TestCopyDicom does the same jpb)
[gdcm.git] / Example / TestCopyDicom.cxx
index f1b4978f92ce9128c458da4fb0f9e8dde0623dff..f44754df43536380cc070e53b7aee2f8f7f03820 100644 (file)
@@ -1,10 +1,34 @@
-#include "gdcmHeader.h"
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: TestCopyDicom.cxx,v $
+  Language:  C++
+  Date:      $Date: 2005/10/17 10:41:59 $
+  Version:   $Revision: 1.30 $
+                                                                                
+  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 "gdcmFile.h"
+#include "gdcmFileHelper.h"
 #include "gdcmDocument.h"
 #include "gdcmValEntry.h"
+#include "gdcmBinEntry.h"
+
+#ifndef _WIN32
+#include <unistd.h> //for access, unlink
+#else
+#include <io.h> //for _access
+#endif
 
 // return true if the file exists
-bool FileExists(const charfilename)
+bool FileExists(const char *filename)
 {
 #ifdef _MSC_VER
 # define access _access
@@ -22,7 +46,7 @@ bool FileExists(const char* filename)
     }
 }
 
-bool RemoveFile(const charsource)
+bool RemoveFile(const char *source)
 {
 #ifdef _MSC_VER
 #define _unlink unlink
@@ -33,7 +57,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 main(int argc, charargv[])
+int main(int argc, char *argv[])
 {
    if (argc < 3)
    {
@@ -42,59 +66,90 @@ int main(int argc, char* argv[])
       return 1;
    }
 
-   if( FileExists( argv[2] ) )
-   {
-      std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
-      if( !RemoveFile( argv[2] ) )
+// don't modify identation in order to let this source xdiffable with ../Test
+
+      std::string filename = argv[1];
+      std::string output = argv[2];
+
+      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] );
+      gdcm::File *fileOr = new gdcm::File();
+      fileOr->SetFileName( filename );
+      fileOr->Load();
+      gdcm::FileHelper *original = new gdcm::FileHelper( fileOr );
    
-   std::cout << "--- Original ----------------------" << std::endl;
-   original->GetHeader()->Print();
+      std::cout << "--- Original ----------------------" << std::endl;
+      //original->GetFile()->Print();
    
-   gdcmFile *copy = new gdcmFile( argv[2] );
+      gdcm::FileHelper *copy = new gdcm::FileHelper( );
+      copy->SetFileName( output );
+      copy->Load();
 
-   //First of all copy the header field by field
+      //size_t dataSize;
+      uint8_t *imageData;
+      //dataSize = original->GetImageDataSize();// just an accesor :useless here
+      
+      imageData = original->GetImageData(); // VERY important : 
+                                      // brings pixels into memory !
+      //(void)imageData; // not enough to avoid warning with icc compiler
+      //(void)dataSize; //  not enough to avoid warning on 'Golgot'
   
-   // 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 !)
-    
-   TagDocEntryHT & Ht = original->GetHeader()->GetEntry();   
-   
-   for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
-   {
-      //std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
-      tag->second->Print(); std::cout << std::endl;
-    
-      if (tag->second->GetVR() == "SQ") //to skip pb of SQ recursive exploration
-         continue;
-      // Well ... Should have dynamic cast here 
-      copy->GetHeader()->ReplaceOrCreateByNumber( 
-                                 ((gdcmValEntry*)(tag->second))->GetValue(),
-                                 tag->second->GetGroup(), 
-                                 tag->second->GetElement() );
-   
-     // todo : Setting Offset to 0 to avoid further missprint 
-   }
+      std::cout << imageData << std::endl; // to avoid warning ?
+
+      //First of all copy the header field by field
 
-   size_t dataSize = original->GetImageDataSize();
-   void *imageData = original->GetImageData();
+      gdcm::DocEntry *d = original->GetFile()->GetFirstEntry();
+      while(d)
+      {
+         if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
+         {              
+            copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
+                                             b->GetGroup(),b->GetElement(),
+                                             b->GetVR() );
+         }
+         else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
+         {   
+            copy->GetFile()->InsertValEntry( 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;    
+         }
+
+         d=original->GetFile()->GetNextEntry();
+      }
 
-   copy->SetImageData(imageData, dataSize);
+      //copy->GetImageData();
+      //copy->SetImageData(imageData, dataSize);
 
-   std::cout << "--- Copy ----------------------" << std::endl;
-   std::cout <<std::endl << "DO NOT care about Offset"  <<std::endl<<std::endl;; 
-   copy->GetHeader()->Print();
-   std::cout << "--- ---- ----------------------" << std::endl;
+      std::cout << "--- Copy ----------------------" << std::endl;
+      std::cout <<std::endl << "DO NOT care about Offset"  
+                <<std::endl << std::endl;; 
+      copy->GetFile()->Print();
+      std::cout << "--- ---- ----------------------" << std::endl;
    
-   copy->WriteDcmExplVR( argv[2] );
+      copy->WriteDcmExplVR( output );
+      
 
-   return 0;
+      delete fileOr;   // File
+      delete original; // FileHelper
+      delete copy;     // FileHelper
+      return 0;
 }