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