]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
ENH: Adding a test I know that segfault
[gdcm.git] / Testing / TestCopyDicom.cxx
1 #include "gdcmHeader.h"
2 #include "gdcmFile.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
5
6 //Generated file:
7 #include "gdcmDataImages.h"
8
9 // return true if the file exists
10 bool FileExists(const char* filename)
11 {
12 #ifdef _MSC_VER
13 # define access _access
14 #endif
15 #ifndef R_OK
16 # define R_OK 04
17 #endif
18   if ( access(filename, R_OK) != 0 )
19     {
20     return false;
21     }
22   else
23     {
24     return true;
25     }
26 }
27
28 bool RemoveFile(const char* source)
29 {
30 #ifdef _MSC_VER
31 #define _unlink unlink
32 #endif
33   return unlink(source) != 0 ? false : true;
34 }
35
36 // Here we load a gdcmFile and then try to create from scratch a copy of it,
37 // copying field by field the dicom image
38
39 int TestCopyDicom(int , char* [])
40 {
41    int i =0;
42    int retVal = 0;  //by default this is an error
43    while( gdcmDataImages[i] != 0 )
44    {
45       std::string filename = GDCM_DATA_ROOT;
46       filename += "/";  //doh!
47       filename += gdcmDataImages[i];
48       std::cerr << "Filename: " << filename << std::endl;
49
50       std::string output = "../Testing/Temporary/output.dcm";
51
52       if( FileExists( output.c_str() ) )
53       {
54          std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
55          if( !RemoveFile( output.c_str() ) )
56          {
57             std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
58             return 1;
59          }
60       }
61
62       gdcmFile *original = new gdcmFile( filename );
63       gdcmFile *copy = new gdcmFile( output );
64
65       //First of all copy the header field by field
66       TagNameHT & nameHt = original->GetHeader()->GetPubDict()->GetEntriesByName();
67       for (TagNameHT::iterator tag = nameHt.begin(); tag != nameHt.end(); ++tag)
68       {
69          if (tag->second->GetVR() == "SQ") //to skip pb of SQ recursive exploration
70             continue;
71
72          //According to JPR I should also skip those:
73 //         if (tag->second->GetVR() == "unkn") //to skip pb of SQ recursive exploration
74 //            continue;
75
76          //no clue what to do with this one
77          //std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
78          std::string value = ((gdcmValEntry*)(tag->second))->GetValue();
79 //         if( value.find( "gdcm::NotLoaded" ) == 0 )
80 //            continue;
81
82          //no clue what to do with this one
83          if( value.find( "gdcm::Loaded" ) == 0 )
84             continue;
85
86          copy->GetHeader()->ReplaceOrCreateByNumber( 
87                value, 
88                tag->second->GetGroup(), 
89                tag->second->GetElement() );
90       }
91
92       size_t dataSize = original->GetImageDataSize();
93       void *imageData = original->GetImageData();
94
95       copy->SetImageData(imageData, dataSize);
96       //original->GetHeader()->SetImageDataSize(dataSize);
97
98       //copy->GetHeader()->PrintEntry();
99
100       //copy->WriteDcmExplVR( output );
101       
102       //Is the file written still gdcm parsable ?
103       gdcmFile check( output );
104       retVal += !check.GetHeader()->IsReadable();
105    }
106
107    return retVal;
108 }
109
110
111