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