]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
66a4fbfd62ba500add92a2ff63ed5028626e3c24
[gdcm.git] / Testing / TestCopyDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/21 08:35:49 $
7   Version:   $Revision: 1.43 $
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 "gdcmDataEntry.h"
21
22 //Generated file:
23 #include "gdcmDataImages.h"
24
25 // return true if the file exists
26 bool FileExists(const char *filename);
27 bool RemoveFile(const char *source);
28
29 int CopyDicom(std::string const &filename, 
30               std::string const &output )
31 {
32       std::cout << "   Testing: " << filename << std::endl;
33       if( FileExists( output.c_str() ) )
34       {
35          if( !RemoveFile( output.c_str() ) )
36          {
37             std::cout << "Ouch, the file exist, but I cannot remove it" << std::endl;
38             return 1;
39          }
40       }
41
42       //////////////// Step 1:
43       std::cout << "      1...";
44       
45       gdcm::File *originalH = new gdcm::File( );
46       gdcm::File *copyH     = new gdcm::File( );
47
48       originalH->SetFileName( filename );
49       originalH->Load( );
50       //First of all copy the file, field by field
51
52       //////////////// Step 2:
53       std::cout << "2...";
54       gdcm::DocEntry *d=originalH->GetFirstEntry();
55       while(d)
56       {
57          if ( gdcm::DataEntry *de = dynamic_cast<gdcm::DataEntry *>(d) )
58          {              
59             copyH->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
60                                        de->GetGroup(),de->GetElement(),
61                                        de->GetVR() );
62          }
63          else
64          {
65           // We skip pb of SQ recursive exploration
66          }
67
68          d=originalH->GetNextEntry();
69       }
70
71       gdcm::FileHelper *original = new gdcm::FileHelper( originalH );
72       gdcm::FileHelper *copy     = new gdcm::FileHelper( copyH );
73
74       size_t dataSize = original->GetImageDataSize();
75       uint8_t *imageData = original->GetImageData();
76
77       // Useless to set the image data, because it's already made when
78       // copying the corresponding DataEntry that contains the pixel data
79       
80       // --> FIXME : Why do we let the following line?
81       //             to avoid compile time warnings?
82       copy->SetImageData(imageData, dataSize);
83
84       //////////////// Step 3:
85       std::cout << "3...";
86       copy->SetWriteModeToRGB();
87       if( !copy->WriteDcmExplVR(output) )
88       {
89          std::cout << " Failed" << std::endl
90                    << "       " << output << " not written" << std::endl;
91
92          delete original;
93          delete copy;
94          delete originalH;
95          delete copyH;
96
97          return 1;
98       }
99
100       delete copy;
101       delete copyH;
102
103       //////////////// Step 4:
104       std::cout << "4...";
105       copy = new gdcm::FileHelper( output );
106
107       //Is the file written still gdcm parsable ?
108       if ( !copy->GetFile()->IsReadable() )
109       { 
110          std::cout << " Failed" << std::endl
111                    << "        " << output << " not readable" << std::endl;
112
113          delete original;
114          delete originalH;
115
116          return 1;
117       }
118
119       //////////////// Step 5:
120       std::cout << "5...";
121       size_t    dataSizeWritten = copy->GetImageDataSize();
122       uint8_t *imageDataWritten = copy->GetImageData();
123
124       if (dataSize != dataSizeWritten)
125       {
126          std::cout << " Failed" << std::endl
127                    << "        Pixel areas lengths differ: "
128                    << dataSize << " # " << dataSizeWritten << std::endl;
129
130          delete original;
131          delete copy;
132          delete originalH;
133
134          return 1;
135       }
136
137       if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
138       {
139          (void)res;
140          std::cout << " Failed" << std::endl
141                    << "        Pixel differ (as expanded in memory)." << std::endl;
142
143          delete original;
144          delete copy;
145          delete originalH;
146
147          return 1;
148       }
149       std::cout << "OK." << std::endl ;
150
151       delete original;
152       delete copy;
153       delete originalH;
154
155       return 0;
156 }
157
158 // Here we load a gdcmFile and then try to create from scratch a copy of it,
159 // copying field by field the dicom image
160
161 int TestCopyDicom(int argc, char *argv[])
162 {
163    if ( argc == 3 )
164    {
165       // The test is specified a specific filename, use it instead of looping
166       // over all images
167       const std::string input = argv[1];
168       const std::string reference = argv[2];
169       return CopyDicom( input, reference );
170    }
171    else if ( argc > 3 || argc == 2 )
172    {
173       std::cout << "   Usage: " << argv[0]
174                 << " (no arguments needed)." << std::endl;
175       std::cout << "or   Usage: " << argv[0]
176                 << " filename.dcm reference.dcm" << std::endl;
177       return 1;
178    }
179    // else other cases:
180
181    std::cout << "   Description (Test::TestCopyDicom): "
182              << std::endl;
183    std::cout << "   For all images in gdcmData (and not blacklisted in "
184                 "Test/CMakeLists.txt)"
185              << std::endl;
186    std::cout << "   apply the following to each filename.xxx: "
187              << std::endl;
188    std::cout << "   step 1: parse the image (as gdcmFile) and call"
189              << " IsReadable(). After that, call GetImageData() and "
190              << "GetImageDataSize() "
191              << std::endl;
192    std::cout << "   step 2: create a copy of the readed file and the new"
193              << " pixel data are set to the copy"
194              << std::endl;
195    std::cout << "   step 3: write the copy of the image"
196              << std::endl;
197    std::cout << "   step 4: read the copy and call IsReadable()"
198              << std::endl;
199    std::cout << "   step 5: compare (in memory with memcmp) that the two "
200              << "images " << std::endl
201              << "           match (as expanded by gdcm)." << std::endl;
202    std::cout << std::endl;
203
204    int i =0;
205    int retVal = 0;  //by default this is an error
206    while( gdcmDataImages[i] != 0 )
207    {
208       std::string filename = GDCM_DATA_ROOT;
209       filename += "/";  //doh!
210       filename += gdcmDataImages[i];
211
212       std::string output = "output.dcm";
213
214       if( CopyDicom( filename, output ) != 0 )
215       {
216          retVal++;
217       }
218
219       i++;
220    }
221    return retVal;
222 }
223