]> Creatis software - gdcm.git/commitdiff
dded a modified version of Mathieu's TestCopyDicom.cxx
authorjpr <jpr>
Thu, 2 Sep 2004 12:58:59 +0000 (12:58 +0000)
committerjpr <jpr>
Thu, 2 Sep 2004 12:58:59 +0000 (12:58 +0000)
(put here not to polute gdcm/Test)

Example/CMakeLists.txt
Example/TestCopyDicom.cxx [new file with mode: 0644]

index 2107d45dfea9d82b5bd14e3f4fd3efca2bcf6bda..07ed443b691cfee37c9c8a841e42730484f28602 100644 (file)
@@ -46,3 +46,6 @@ TARGET_LINK_LIBRARIES(PrintDicomDir gdcm)
 
 ADD_EXECUTABLE(BuildUpDicomDir BuildUpDicomDir.cxx)
 TARGET_LINK_LIBRARIES(BuildUpDicomDir gdcm)
+
+ADD_EXECUTABLE(TestCopyDicom TestCopyDicom.cxx)
+TARGET_LINK_LIBRARIES(TestCopyDicom  gdcm)
diff --git a/Example/TestCopyDicom.cxx b/Example/TestCopyDicom.cxx
new file mode 100644 (file)
index 0000000..f1b4978
--- /dev/null
@@ -0,0 +1,101 @@
+#include "gdcmHeader.h"
+#include "gdcmFile.h"
+#include "gdcmDocument.h"
+#include "gdcmValEntry.h"
+
+// return true if the file exists
+bool FileExists(const char* filename)
+{
+#ifdef _MSC_VER
+# define access _access
+#endif
+#ifndef R_OK
+# define R_OK 04
+#endif
+  if ( access(filename, R_OK) != 0 )
+    {
+    return false;
+    }
+  else
+    {
+    return true;
+    }
+}
+
+bool RemoveFile(const char* source)
+{
+#ifdef _MSC_VER
+#define _unlink unlink
+#endif
+  return unlink(source) != 0 ? false : true;
+}
+
+// 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, char* argv[])
+{
+   if (argc < 3)
+   {
+      std::cerr << "Usage :" << std::endl << 
+      argv[0] << " input_dicom output_dicom" << std::endl;
+      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] ) )
+      {
+         std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
+         return 1;
+      }
+   }
+   gdcmFile *original = new gdcmFile( argv[1] );
+   
+   std::cout << "--- Original ----------------------" << std::endl;
+   original->GetHeader()->Print();
+   
+   gdcmFile *copy = new gdcmFile( argv[2] );
+
+   //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 !)
+    
+   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 
+   }
+
+   size_t dataSize = original->GetImageDataSize();
+   void *imageData = original->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;
+   
+   copy->WriteDcmExplVR( argv[2] );
+
+   return 0;
+}
+
+
+