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