]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
* VoidArea is now called BinArea (less confusing name),
[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       size_t dataSize = original->GetImageDataSize();
73       uint8_t* imageData = original->GetImageData();
74
75       //First of all copy the header field by field
76   
77       // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
78       // It was commented out by Mathieu, that was a *good* idea
79       // (the user does NOT have to know the way we implemented the Header !)
80       // Waiting for a 'clean' solution, I keep the method ...JPRx
81
82       gdcmDocEntry* d;
83
84       for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
85       {
86          d = tag->second;
87          if ( gdcmBinEntry* b = dynamic_cast<gdcmBinEntry*>(d) )
88          {              
89             copy->GetHeader()->ReplaceOrCreateByNumber( 
90                                  b->GetBinArea(),
91                                  b->GetLength(),
92                                  b->GetGroup(), 
93                                  b->GetElement(),
94                                  b->GetVR() );
95          }
96          else if ( gdcmValEntry* v = dynamic_cast<gdcmValEntry*>(d) )
97          {   
98              copy->GetHeader()->ReplaceOrCreateByNumber( 
99                                  v->GetValue(),
100                                  v->GetGroup(), 
101                                  v->GetElement(),
102                                  v->GetVR() ); 
103          }
104          else
105          {
106           // We skip pb of SQ recursive exploration
107           //std::cout << "Skipped Sequence " 
108           //          << "------------- " << d->GetVR() << " "<< std::hex
109           //          << d->GetGroup() << " " << d->GetElement()
110           //  << std::endl;    
111          }
112       }
113
114       copy->SetImageData(imageData, dataSize);
115       original->GetHeader()->SetImageDataSize(dataSize);
116
117       copy->WriteDcmExplVR( output );
118
119       delete original;
120       delete copy;
121
122       copy = new gdcmFile( output );
123
124       //Is the file written still gdcm parsable ?
125       if ( !copy->GetHeader()->IsReadable() )
126       { 
127          retVal +=1;
128          std::cout << output << " Failed" << std::endl;
129       }
130       i++;
131    }
132    return retVal;
133 }
134