]> Creatis software - gdcm.git/blob - Example/TestCopyDicom.cxx
* VoidArea is now called BinArea (less confusing name),
[gdcm.git] / Example / TestCopyDicom.cxx
1 #include "gdcmHeader.h"
2 #include "gdcmFile.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
5 #include "gdcmBinEntry.h"
6
7 #ifndef _WIN32
8 #include <unistd.h> //for access, unlink
9 #endif
10
11 // return true if the file exists
12 bool FileExists(const char* filename)
13 {
14 #ifdef _MSC_VER
15 # define access _access
16 #endif
17 #ifndef R_OK
18 # define R_OK 04
19 #endif
20   if ( access(filename, R_OK) != 0 )
21     {
22     return false;
23     }
24   else
25     {
26     return true;
27     }
28 }
29
30 bool RemoveFile(const char* source)
31 {
32 #ifdef _MSC_VER
33 #define _unlink unlink
34 #endif
35   return unlink(source) != 0 ? false : true;
36 }
37
38 // Here we load a gdcmFile and then try to create from scratch a copy of it,
39 // copying field by field the dicom image
40
41 int main(int argc, char* argv[])
42 {
43    if (argc < 3)
44    {
45       std::cerr << "Usage :" << std::endl << 
46       argv[0] << " input_dicom output_dicom" << std::endl;
47       return 1;
48    }
49
50 // don't modify identation in order to let this source xdiffable with ../Test
51
52       std::string filename = argv[1];
53       std::string output = argv[2];
54
55       if( FileExists( output.c_str() ) )
56       {
57          std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
58          if( !RemoveFile( output.c_str() ) )
59          {
60             std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
61             return 1;
62          }
63       }
64       gdcmFile *original = new gdcmFile( filename );
65    
66       std::cout << "--- Original ----------------------" << std::endl;
67       //original->GetHeader()->Print();
68    
69       gdcmFile *copy = new gdcmFile( output );
70
71       TagDocEntryHT & Ht = original->GetHeader()->GetEntry();
72
73       size_t dataSize = original->GetImageDataSize();
74       uint8_t* imageData = original->GetImageData();
75   
76       //First of all copy the header field by field
77   
78       // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
79       // It was commented out by Mathieu, that was a *good* idea
80       // (the user does NOT have to know the way we implemented the Header !)
81       // Waiting for a 'clean' solution, I keep the method ...JPRx
82
83       gdcmDocEntry* d;
84
85       for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
86       {
87          d = tag->second;
88          d->Print(); std::cout << std::endl;
89          if ( gdcmBinEntry* b = dynamic_cast<gdcmBinEntry*>(d) )
90          {              
91             copy->GetHeader()->ReplaceOrCreateByNumber( 
92                                  b->GetBinArea(),
93                                  b->GetLength(),
94                                  b->GetGroup(), 
95                                  b->GetElement(),
96                                  b->GetVR() );
97          }
98          else if ( gdcmValEntry* v = dynamic_cast<gdcmValEntry*>(d) )
99          {   
100             copy->GetHeader()->ReplaceOrCreateByNumber( 
101                                  v->GetValue(),
102                                  v->GetGroup(), 
103                                  v->GetElement(),
104                                  v->GetVR() ); 
105          }
106          else
107          {
108           // We skip pb of SQ recursive exploration
109           //std::cout << "Skipped Sequence " 
110           //          << "------------- " << d->GetVR() << " "<< std::hex
111           //          << d->GetGroup() << " " << d->GetElement()
112           //  << std::endl;    
113          }
114       }
115
116
117
118
119
120       //copy->GetImageData();
121       copy->SetImageData(imageData, dataSize);
122
123       std::cout << "--- Copy ----------------------" << std::endl;
124       std::cout <<std::endl << "DO NOT care about Offset"  <<std::endl<<std::endl;; 
125       copy->GetHeader()->Print();
126       std::cout << "--- ---- ----------------------" << std::endl;
127    
128       copy->WriteDcmExplVR( output );
129
130       return 0;
131 }
132
133
134