]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
New test for testing the copying for creating dicom file from scratch is ok
[gdcm.git] / Testing / TestCopyDicom.cxx
1 #include "gdcmHeader.h"
2 #include "gdcmFile.h"
3
4 // return true if the file exists
5 bool FileExists(const char* filename)
6 {
7 #ifdef _MSC_VER
8 # define access _access
9 #endif
10 #ifndef R_OK
11 # define R_OK 04
12 #endif
13   if ( access(filename, R_OK) != 0 )
14     {
15     return false;
16     }
17   else
18     {
19     return true;
20     }
21 }
22
23 bool RemoveFile(const char* source)
24 {
25 #ifdef _MSC_VER
26 #define _unlink unlink
27 #endif
28   return unlink(source) != 0 ? false : true;
29 }
30
31 // Here we load a gdcmFile and then try to create from scratch a copy of it,
32 // copying field by field the dicom image
33
34 int TestCopyDicom(int argc, char* argv[])
35 {
36    if (argc < 3)
37    {
38       std::cerr << "Usage :" << std::endl << 
39       argv[0] << " input_dicom output_dicom" << std::endl;
40       return 1;
41    }
42
43    if( FileExists( argv[2] ) )
44    {
45       std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
46       if( !RemoveFile( argv[2] ) )
47       {
48          std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
49          return 1;
50       }
51    }
52    gdcmFile *original = new gdcmFile( argv[1] );
53    gdcmFile *copy = new gdcmFile( argv[2] );
54
55    //First of all copy the header field by field
56    TagNameHT & nameHt = original->GetHeader()->GetPubDict()->GetEntriesByName();
57    for (TagNameHT::iterator tag = nameHt.begin(); tag != nameHt.end(); ++tag)
58    {
59       std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
60
61       copy->GetHeader()->ReplaceOrCreateByNumber( tag->second->GetVR(), 
62             tag->second->GetGroup(), tag->second->GetElement() );
63    }
64
65    size_t dataSize = original->GetImageDataSize();
66    void *imageData = original->GetImageData();
67
68    copy->SetImageData(imageData, dataSize);
69    //original->GetHeader()->SetImageDataSize(dataSize);
70
71    //copy->GetHeader()->PrintEntry();
72
73    copy->WriteDcmExplVR( argv[2] );
74
75    return 0;
76 }
77
78
79