]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
Now, TestCopyDicom deals with private Entries
[gdcm.git] / Testing / TestCopyDicom.cxx
1 #include "gdcmHeader.h"
2 #include "gdcmFile.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
5 #include "gdcmBinEntry.h"
6
7 //Generated file:
8 #include "gdcmDataImages.h"
9
10 #ifndef _WIN32
11 #include <unistd.h> //for access, unlink
12 #endif
13
14 // return true if the file exists
15 bool FileExists(const char* filename)
16 {
17 #ifdef _MSC_VER
18 # define access _access
19 #endif
20 #ifndef R_OK
21 # define R_OK 04
22 #endif
23   if ( access(filename, R_OK) != 0 )
24     {
25     return false;
26     }
27   else
28     {
29     return true;
30     }
31 }
32
33 bool RemoveFile(const char* source)
34 {
35 #ifdef _MSC_VER
36 #define _unlink unlink
37 #endif
38   return unlink(source) != 0 ? false : true;
39 }
40
41 // Here we load a gdcmFile and then try to create from scratch a copy of it,
42 // copying field by field the dicom image
43
44 int TestCopyDicom(int , char* [])
45 {
46    int i =0;
47    int retVal = 0;  //by default this is an error
48    while( gdcmDataImages[i] != 0 )
49    {
50       std::string filename = GDCM_DATA_ROOT;
51       filename += "/";  //doh!
52       filename += gdcmDataImages[i];
53       std::cerr << "Filename: " << filename << std::endl;
54
55       std::string output = "../Testing/Temporary/output.dcm";
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
67       gdcmFile *original = new gdcmFile( filename );
68       gdcmFile *copy = new gdcmFile( output );
69
70       TagDocEntryHT & Ht = original->GetHeader()->GetEntry(); 
71
72       //First of all copy the header field by field
73   
74       // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
75       // It was commented out by Mathieu, that was a *good* idea
76       // (the user does NOT have to know the way we implemented the Header !)
77       // Waiting for a 'clean' solution, I keep the method ...JPRx
78     
79       TagNameHT & nameHt = original->GetHeader()->GetPubDict()->GetEntriesByName();
80
81       gdcmValEntry* v;
82       gdcmBinEntry* b;
83       gdcmDocEntry* d;
84
85       for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
86       {
87          d = tag->second;
88  
89          if ( gdcmBinEntry* b = dynamic_cast<gdcmBinEntry*>(d) )
90          { 
91          // for private elements, the gdcmDictEntry is unknown
92          // and it's VR is unpredictable ...
93          // ( In *this* case ReplaceOrCreateByNumber 
94          //  should have a knowledge of the virtual dictionary 
95          //  gdcmDictSet::VirtualEntry ) 
96          // ReplaceOrCreateByNumber may now receive the VR of the source Entry
97          // as a extra parameter
98          //
99             //if ( d->GetGroup()%2 == 1) // Skip private Entries 
100             //   continue;
101
102            // TODO :write ReplaceOrCreateByNumber with VR, 
103            //       for BinEntries as well!
104              
105             copy->GetHeader()->ReplaceOrCreateByNumber( 
106                                  b->GetVoidArea(),
107                                  b->GetLength(),
108                                  b->GetGroup(), 
109                                  b->GetElement() );
110             }
111             else  if ( gdcmValEntry* v = dynamic_cast<gdcmValEntry*>(d) )
112             {
113                if ( d->GetGroup()%2 != 1)
114                {
115                   copy->GetHeader()->ReplaceOrCreateByNumber( 
116                                  v->GetValue(),
117                                  v->GetGroup(), 
118                                  v->GetElement() );
119                 }
120                 else
121                 {
122                   copy->GetHeader()->ReplaceOrCreateByNumber( 
123                                  v->GetValue(),
124                                  v->GetGroup(), 
125                                  v->GetElement(),
126                                  v->GetVR() ); 
127                 }
128          }
129          else
130          {
131           // We skip pb of SQ recursive exploration
132           //std::cout << "Skipped Sequence " 
133           //          << "------------- " << d->GetVR() << " "<< std::hex
134           //          << d->GetGroup() << " " << d->GetElement()
135           //  << std::endl;    
136          }
137       }
138
139       size_t dataSize = original->GetImageDataSize();
140       void *imageData = original->GetImageData();
141
142       copy->SetImageData(imageData, dataSize);
143       original->GetHeader()->SetImageDataSize(dataSize);
144
145       copy->WriteDcmExplVR( output );
146
147       delete original;
148       delete copy;
149
150       copy = new gdcmFile( output );
151
152       //Is the file written still gdcm parsable ?
153       if ( !copy->GetHeader()->IsReadable() )
154       { 
155          retVal +=1;
156          std::cout << output << " Failed" << std::endl;
157       }
158       i++;
159    }
160    return retVal;
161 }
162