]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
STYLE: ivars should start with a capital letter. Accessors should be const to avoid...
[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 #else
13 #include <io.h> //for _access on Win32
14 #endif
15
16 // return true if the file exists
17 bool FileExists(const char* filename)
18 {
19 #ifdef _MSC_VER
20 # define access _access
21 #endif
22 #ifndef R_OK
23 # define R_OK 04
24 #endif
25   if ( access(filename, R_OK) != 0 )
26     {
27     return false;
28     }
29   else
30     {
31     return true;
32     }
33 }
34
35 bool RemoveFile(const char* source)
36 {
37 #ifdef _MSC_VER
38 #define _unlink unlink
39 #endif
40   return unlink(source) != 0 ? false : true;
41 }
42
43 // Here we load a gdcmFile and then try to create from scratch a copy of it,
44 // copying field by field the dicom image
45
46 int TestCopyDicom(int , char* [])
47 {
48    int i =0;
49    int retVal = 0;  //by default this is an error
50    while( gdcmDataImages[i] != 0 )
51    {
52       std::string filename = GDCM_DATA_ROOT;
53       filename += "/";  //doh!
54       filename += gdcmDataImages[i];
55       std::cerr << "Filename: " << filename << std::endl;
56
57       std::string output = "../Testing/Temporary/output.dcm";
58
59       if( FileExists( output.c_str() ) )
60       {
61         // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
62          if( !RemoveFile( output.c_str() ) )
63          {
64             std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
65             return 1;
66          }
67       }
68
69       gdcm::File *original = new gdcm::File( filename );
70       gdcm::File *copy = new gdcm::File( output );
71
72       const gdcm::TagDocEntryHT & Ht = original->GetHeader()->GetTagHT();
73
74       size_t dataSize = original->GetImageDataSize();
75       uint8_t* imageData = original->GetImageData();
76
77       //First of all copy the header field by field
78   
79       // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
80       // It was commented out by Mathieu, that was a *good* idea
81       // (the user does NOT have to know the way we implemented the Header !)
82       // Waiting for a 'clean' solution, I keep the method ...JPRx
83
84       gdcm::DocEntry* d;
85
86       for (gdcm::TagDocEntryHT::const_iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
87       {
88          d = tag->second;
89          if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(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 ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(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       copy->SetImageData(imageData, dataSize);
117       original->GetHeader()->SetImageDataSize(dataSize);
118
119       copy->WriteDcmExplVR( output );
120
121       delete original;
122       delete copy;
123
124       copy = new gdcm::File( output );
125
126       //Is the file written still gdcm parsable ?
127       if ( !copy->GetHeader()->IsReadable() )
128       { 
129          retVal +=1;
130          std::cout << output << " Failed" << std::endl;
131       }
132       i++;
133    }
134    return retVal;
135 }
136