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