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