]> Creatis software - gdcm.git/blob - Example/TestCopyDicom.cxx
ENH: Adding a test I know that segfault
[gdcm.git] / Example / TestCopyDicom.cxx
1 #include "gdcmHeader.h"
2 #include "gdcmFile.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
5
6 #ifndef _WIN32
7 #include <unistd.h>
8 #endif
9
10 // return true if the file exists
11 bool FileExists(const char* filename)
12 {
13 #ifdef _MSC_VER
14 # define access _access
15 #endif
16 #ifndef R_OK
17 # define R_OK 04
18 #endif
19   if ( access(filename, R_OK) != 0 )
20     {
21     return false;
22     }
23   else
24     {
25     return true;
26     }
27 }
28
29 bool RemoveFile(const char* source)
30 {
31 #ifdef _MSC_VER
32 #define _unlink unlink
33 #endif
34   return unlink(source) != 0 ? false : true;
35 }
36
37 // Here we load a gdcmFile and then try to create from scratch a copy of it,
38 // copying field by field the dicom image
39
40 int main(int argc, char* argv[])
41 {
42    if (argc < 3)
43    {
44       std::cerr << "Usage :" << std::endl << 
45       argv[0] << " input_dicom output_dicom" << std::endl;
46       return 1;
47    }
48
49    if( FileExists( argv[2] ) )
50    {
51       std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
52       if( !RemoveFile( argv[2] ) )
53       {
54          std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
55          return 1;
56       }
57    }
58    gdcmFile *original = new gdcmFile( argv[1] );
59    
60    std::cout << "--- Original ----------------------" << std::endl;
61    //original->GetHeader()->Print();
62    
63    gdcmFile *copy = new gdcmFile( argv[2] );
64
65    //First of all copy the header field by field
66   
67    // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
68    //It was commented out by Mathieu, that was a *good* idea
69    // (the user does NOT have to know the way we implemented the Header !)
70     
71    TagDocEntryHT & Ht = original->GetHeader()->GetEntry();   
72    
73    for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
74    {
75       if (tag->second->GetVR() == "SQ") //to skip pb of SQ recursive exploration
76          continue;
77
78       std::string value = ((gdcmValEntry*)(tag->second))->GetValue();
79       //According to JPR I should also skip those:
80 //      if (tag->second->GetVR() == "unkn") //to skip pb of SQ recursive exploration
81 //         continue;
82
83 //      if( value.find( "gdcm::NotLoaded" ) == 0 )
84 //         continue;
85
86       if( value.find( "gdcm::Loaded" ) == 0 )
87          continue;
88          
89       //std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
90       //tag->second->Print(); std::cout << std::endl;
91     
92       std::cerr << "Reading: " << value  << std::endl;
93
94       // Well ... Should have dynamic cast here 
95       copy->GetHeader()->ReplaceOrCreateByNumber( 
96                                  value,
97                                  tag->second->GetGroup(), 
98                                  tag->second->GetElement() );
99    
100      // todo : Setting Offset to 0 to avoid further missprint 
101    }
102
103    size_t dataSize = original->GetImageDataSize();
104    void *imageData = original->GetImageData();
105
106    //copy->GetImageData();
107    copy->SetImageData(imageData, dataSize);
108
109    std::cout << "--- Copy ----------------------" << std::endl;
110    std::cout <<std::endl << "DO NOT care about Offset"  <<std::endl<<std::endl;; 
111    //copy->GetHeader()->Print();
112    std::cout << "--- ---- ----------------------" << std::endl;
113    
114    copy->WriteDcmExplVR( argv[2] );
115
116    return 0;
117 }
118
119
120