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