]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
ENH: Some unused vars
[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       gdcmDocEntry* d;
80
81       for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
82       {
83          d = tag->second;
84          if ( gdcmBinEntry* b = dynamic_cast<gdcmBinEntry*>(d) )
85          {              
86             copy->GetHeader()->ReplaceOrCreateByNumber( 
87                                  b->GetVoidArea(),
88                                  b->GetLength(),
89                                  b->GetGroup(), 
90                                  b->GetElement(),
91                                  b->GetVR() );
92             }
93             else  if ( gdcmValEntry* v = dynamic_cast<gdcmValEntry*>(d) )
94             {   
95                copy->GetHeader()->ReplaceOrCreateByNumber( 
96                                  v->GetValue(),
97                                  v->GetGroup(), 
98                                  v->GetElement(),
99                                  v->GetVR() ); 
100            }
101            else
102            {
103           // We skip pb of SQ recursive exploration
104           //std::cout << "Skipped Sequence " 
105           //          << "------------- " << d->GetVR() << " "<< std::hex
106           //          << d->GetGroup() << " " << d->GetElement()
107           //  << std::endl;    
108            }
109       }
110
111       size_t dataSize = original->GetImageDataSize();
112       void *imageData = original->GetImageData();
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