]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
ENH: Add license to tests since they belong to gdcm
[gdcm.git] / Testing / TestCopyDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/16 04:28:20 $
7   Version:   $Revision: 1.16 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmHeader.h"
19 #include "gdcmFile.h"
20 #include "gdcmDocument.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23
24 //Generated file:
25 #include "gdcmDataImages.h"
26
27 #ifndef _WIN32
28 #include <unistd.h> //for access, unlink
29 #else
30 #include <io.h> //for _access on Win32
31 #endif
32
33 // return true if the file exists
34 bool FileExists(const char* filename)
35 {
36 #ifdef _MSC_VER
37 # define access _access
38 #endif
39 #ifndef R_OK
40 # define R_OK 04
41 #endif
42   if ( access(filename, R_OK) != 0 )
43     {
44     return false;
45     }
46   else
47     {
48     return true;
49     }
50 }
51
52 bool RemoveFile(const char* source)
53 {
54 #ifdef _MSC_VER
55 #define _unlink unlink
56 #endif
57   return unlink(source) != 0 ? false : true;
58 }
59
60 // Here we load a gdcmFile and then try to create from scratch a copy of it,
61 // copying field by field the dicom image
62
63 int TestCopyDicom(int , char* [])
64 {
65    int i =0;
66    int retVal = 0;  //by default this is an error
67    while( gdcmDataImages[i] != 0 )
68    {
69       std::string filename = GDCM_DATA_ROOT;
70       filename += "/";  //doh!
71       filename += gdcmDataImages[i];
72       std::cerr << "Filename: " << filename << std::endl;
73
74       std::string output = "../Testing/Temporary/output.dcm";
75
76       if( FileExists( output.c_str() ) )
77       {
78         // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
79          if( !RemoveFile( output.c_str() ) )
80          {
81             std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
82             return 1;
83          }
84       }
85
86       gdcm::File *original = new gdcm::File( filename );
87       gdcm::File *copy = new gdcm::File( output );
88
89       const gdcm::TagDocEntryHT & Ht = original->GetHeader()->GetTagHT();
90
91       size_t dataSize = original->GetImageDataSize();
92       uint8_t* imageData = original->GetImageData();
93
94       //First of all copy the header field by field
95   
96       // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
97       // It was commented out by Mathieu, that was a *good* idea
98       // (the user does NOT have to know the way we implemented the Header !)
99       // Waiting for a 'clean' solution, I keep the method ...JPRx
100
101       gdcm::DocEntry* d;
102
103       for (gdcm::TagDocEntryHT::const_iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
104       {
105          d = tag->second;
106          if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
107          {              
108             copy->GetHeader()->ReplaceOrCreateByNumber( 
109                                  b->GetBinArea(),
110                                  b->GetLength(),
111                                  b->GetGroup(), 
112                                  b->GetElement(),
113                                  b->GetVR() );
114          }
115          else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
116          {   
117              copy->GetHeader()->ReplaceOrCreateByNumber( 
118                                  v->GetValue(),
119                                  v->GetGroup(), 
120                                  v->GetElement(),
121                                  v->GetVR() ); 
122          }
123          else
124          {
125           // We skip pb of SQ recursive exploration
126           //std::cout << "Skipped Sequence " 
127           //          << "------------- " << d->GetVR() << " "<< std::hex
128           //          << d->GetGroup() << " " << d->GetElement()
129           //  << std::endl;    
130          }
131       }
132
133       copy->SetImageData(imageData, dataSize);
134       original->GetHeader()->SetImageDataSize(dataSize);
135
136       copy->WriteDcmExplVR( output );
137
138       delete original;
139       delete copy;
140
141       copy = new gdcm::File( output );
142
143       //Is the file written still gdcm parsable ?
144       if ( !copy->GetHeader()->IsReadable() )
145       { 
146          retVal +=1;
147          std::cout << output << " Failed" << std::endl;
148       }
149       i++;
150    }
151    return retVal;
152 }
153