]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
* src/gdcmDocEntryArchive.[h|cxx] : bug fix and add a method to temporary
[gdcm.git] / Testing / TestCopyDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/24 10:23:46 $
7   Version:   $Revision: 1.19 $
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 int CopyDicom(std::string const & filename, 
61               std::string const & output )
62 {
63       std::cout << "   Testing: " << filename << std::endl;
64       if( FileExists( output.c_str() ) )
65       {
66         // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
67          if( !RemoveFile( output.c_str() ) )
68          {
69             std::cout << "Ouch, the file exist, but I cannot remove it" << std::endl;
70             return 1;
71          }
72       }
73
74       //////////////// Step 1:
75       std::cout << "      1...";
76       gdcm::File *original = new gdcm::File( filename );
77       gdcm::File *copy = new gdcm::File( output );
78
79       size_t dataSize = original->GetImageDataSize();
80       uint8_t* imageData = original->GetImageData();
81       (void)dataSize;  // To use the variable and not have warnings at compil.
82       (void)imageData; // To use the variable and not have warnings at compil.
83
84       //First of all copy the header field by field
85   
86       // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
87       // It was commented out by Mathieu, that was a *good* idea
88       // (the user does NOT have to know the way we implemented the Header !)
89       // Waiting for a 'clean' solution, I keep the method ...JPRx
90
91
92       //////////////// Step 2:
93       std::cout << "2...";
94       original->GetHeader()->Initialize();
95       gdcm::DocEntry* d=original->GetHeader()->GetNextEntry();
96
97       while(d)
98       {
99          if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
100          {
101             copy->GetHeader()->ReplaceOrCreateByNumber( 
102                                  b->GetBinArea(),
103                                  b->GetLength(),
104                                  b->GetGroup(), 
105                                  b->GetElement(),
106                                  b->GetVR() );
107          }
108          else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
109          {   
110              copy->GetHeader()->ReplaceOrCreateByNumber( 
111                                  v->GetValue(),
112                                  v->GetGroup(), 
113                                  v->GetElement(),
114                                  v->GetVR() ); 
115          }
116          else
117          {
118           // We skip pb of SQ recursive exploration
119           //std::cout << "Skipped Sequence " 
120           //          << "------------- " << d->GetVR() << " "<< std::hex
121           //          << d->GetGroup() << " " << d->GetElement()
122           //  << std::endl;    
123          }
124
125          d=original->GetHeader()->GetNextEntry();
126       }
127
128
129       // Useless to set the image datas, because it's already made when
130       // copying the corresponding BinEntry that contains the pixel datas
131       copy->SetImageData(imageData, dataSize);
132 //      copy->GetImageData();
133 //      original->GetHeader()->SetImageDataSize(dataSize);
134
135       //////////////// Step 3:
136       std::cout << "3...";
137       copy->WriteDcmExplVR( output );
138
139       delete copy;
140
141       //////////////// Step 4:
142       std::cout << "4...";
143       copy = new gdcm::File( output );
144
145       //Is the file written still gdcm parsable ?
146       if ( !copy->GetHeader()->IsReadable() )
147       { 
148          std::cout << "=> " << output << " Failed" << std::endl;
149          delete original;
150          return(1);
151       }
152
153       //////////////// Step 5:
154       std::cout << "5...";
155       int    dataSizeWritten = copy->GetImageDataSize();
156       uint8_t* imageDataWritten = copy->GetImageData();
157
158       if (dataSize != dataSizeWritten)
159       {
160          std::cout << " Failed" << std::endl
161             << "        Pixel areas lengths differ: "
162             << dataSize << " # " << dataSizeWritten << std::endl;
163
164          delete original;
165          delete copy;
166
167          return 1;
168       }
169
170       if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
171       {
172          (void)res;
173          std::cout << " Failed" << std::endl
174             << "        Pixel differ (as expanded in memory)." << std::endl;
175
176          delete original;
177          delete copy;
178
179          return 1;
180       }
181       std::cout << "OK." << std::endl ;
182
183       delete original;
184       delete copy;
185
186       return 0;
187 }
188
189 // Here we load a gdcmFile and then try to create from scratch a copy of it,
190 // copying field by field the dicom image
191
192 int TestCopyDicom(int argc, char* argv[])
193 {
194    if ( argc == 3 )
195    {
196       // The test is specified a specific filename, use it instead of looping
197       // over all images
198       const std::string input = argv[1];
199       const std::string reference = argv[2];
200       return CopyDicom( input, reference );
201    }
202    else if ( argc > 3 || argc == 2 )
203    {
204       std::cout << "   Usage: " << argv[0]
205                 << " (no arguments needed)." << std::endl;
206       std::cout << "or   Usage: " << argv[0]
207                 << " filename.dcm reference.dcm" << std::endl;
208       return 1;
209    }
210    // else other cases:
211
212    std::cout << "   Description (Test::TestCopyDicom): "
213              << std::endl;
214    std::cout << "   For all images in gdcmData (and not blacklisted in "
215                 "Test/CMakeLists.txt)"
216              << std::endl;
217    std::cout << "   apply the following to each filename.xxx: "
218              << std::endl;
219    std::cout << "   step 1: parse the image (as gdcmHeader) and call"
220              << " IsReadable(). After that, call GetImageData() and "
221              << "GetImageDataSize() "
222              << std::endl;
223    std::cout << "   step 2: create a copy of the readed file and the new"
224              << " pixel datas are set to the copy"
225              << std::endl;
226    std::cout << "   step 3: write the copy of the image"
227              << std::endl;
228    std::cout << "   step 4: read the copy and call IsReadable()"
229              << std::endl;
230    std::cout << "   step 5: compare (in memory with memcmp) that the two "
231              << "images " << std::endl
232              << "           match (as expanded by gdcm)." << std::endl;
233    std::cout << std::endl;
234
235    int i =0;
236    int retVal = 0;  //by default this is an error
237    while( gdcmDataImages[i] != 0 )
238    {
239       std::string filename = GDCM_DATA_ROOT;
240       filename += "/";  //doh!
241       filename += gdcmDataImages[i];
242
243 //      std::string output = "../Testing/Temporary/output.dcm";
244       std::string output = "output.dcm";
245
246       if( CopyDicom( filename, output ) != 0 )
247       {
248          retVal++;
249       }
250
251       i++;
252    }
253    return retVal;
254 }
255