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