]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
Fix mistypings
[gdcm.git] / Testing / TestCopyDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/10/30 09:13:45 $
7   Version:   $Revision: 1.44 $
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       GDCM_NAME_SPACE::File *originalH = GDCM_NAME_SPACE::File::New();
45       GDCM_NAME_SPACE::File *copyH     = GDCM_NAME_SPACE::File::New();
46
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_NAME_SPACE::DocEntry *d=originalH->GetFirstEntry();
55       while(d)
56       {
57          if ( GDCM_NAME_SPACE::DataEntry *de = dynamic_cast<GDCM_NAME_SPACE::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       GDCM_NAME_SPACE::FileHelper *original = GDCM_NAME_SPACE::FileHelper::New(originalH);
71       GDCM_NAME_SPACE::FileHelper *copy     = GDCM_NAME_SPACE::FileHelper::New(copyH);
72
73       size_t dataSize = original->GetImageDataSize();
74       uint8_t *imageData = original->GetImageData();
75
76       // Useless to set the image data, because it's already made when
77       // copying the corresponding DataEntry that contains the pixel data
78       
79       // --> FIXME : Why do we let the following line?
80       //             to avoid compile time warnings?
81       copy->SetImageData(imageData, dataSize);
82
83       //////////////// Step 3:
84       std::cout << "3...";
85       copy->SetWriteModeToRGB();
86       if( !copy->WriteDcmExplVR(output) )
87       {
88          std::cout << " Failed" << std::endl
89                    << "       " << output << " not written" << std::endl;
90
91          original->Delete();
92          copy->Delete();
93          originalH->Delete();
94          copyH->Delete();
95
96          return 1;
97       }
98
99       copy->Delete();
100       copyH->Delete();
101
102       //////////////// Step 4:
103       std::cout << "4...";
104 //      copy = new GDCM_NAME_SPACE::FileHelper( output );
105       copy = GDCM_NAME_SPACE::FileHelper::New(output);  // ???
106       //Is the file written still gdcm parsable ?
107       if ( !copy->GetFile()->IsReadable() )
108       { 
109          std::cout << " Failed" << std::endl
110                    << "        " << output << " not readable" << std::endl;
111
112          original->Delete();
113          originalH->Delete();
114
115          return 1;
116       }
117
118       //////////////// Step 5:
119       std::cout << "5...";
120       size_t    dataSizeWritten = copy->GetImageDataSize();
121       uint8_t *imageDataWritten = copy->GetImageData();
122
123       if (dataSize != dataSizeWritten)
124       {
125          std::cout << " Failed" << std::endl
126                    << "        Pixel areas lengths differ: "
127                    << dataSize << " # " << dataSizeWritten << std::endl;
128
129          original->Delete();
130          copy->Delete();
131          originalH->Delete();
132
133          return 1;
134       }
135
136       if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
137       {
138          (void)res;
139          std::cout << " Failed" << std::endl
140                    << "        Pixel differ (as expanded in memory)." << std::endl;
141
142          original->Delete();
143          copy->Delete();
144          originalH->Delete();
145
146          return 1;
147       }
148       std::cout << "OK." << std::endl ;
149
150       original->Delete();
151       copy->Delete();
152       originalH->Delete();
153
154       return 0;
155 }
156
157 // Here we load a gdcmFile and then try to create from scratch a copy of it,
158 // copying field by field the dicom image
159
160 int TestCopyDicom(int argc, char *argv[])
161 {
162    if ( argc == 3 )
163    {
164       // The test is specified a specific filename, use it instead of looping
165       // over all images
166       const std::string input = argv[1];
167       const std::string reference = argv[2];
168       return CopyDicom( input, reference );
169    }
170    else if ( argc > 3 || argc == 2 )
171    {
172       std::cout << "   Usage: " << argv[0]
173                 << " (no arguments needed)." << std::endl;
174       std::cout << "or   Usage: " << argv[0]
175                 << " filename.dcm reference.dcm" << std::endl;
176       return 1;
177    }
178    // else other cases:
179
180    std::cout << "   Description (Test::TestCopyDicom): "
181              << std::endl;
182    std::cout << "   For all images in gdcmData (and not blacklisted in "
183                 "Test/CMakeLists.txt)"
184              << std::endl;
185    std::cout << "   apply the following to each filename.xxx: "
186              << std::endl;
187    std::cout << "   step 1: parse the image (as gdcmFile) and call"
188              << " IsReadable(). After that, call GetImageData() and "
189              << "GetImageDataSize() "
190              << std::endl;
191    std::cout << "   step 2: create a copy of the readed file and the new"
192              << " pixel data are set to the copy"
193              << std::endl;
194    std::cout << "   step 3: write the copy of the image"
195              << std::endl;
196    std::cout << "   step 4: read the copy and call IsReadable()"
197              << std::endl;
198    std::cout << "   step 5: compare (in memory with memcmp) that the two "
199              << "images " << std::endl
200              << "           match (as expanded by gdcm)." << std::endl;
201    std::cout << std::endl;
202
203    int i =0;
204    int retVal = 0;  //by default this is an error
205    while( gdcmDataImages[i] != 0 )
206    {
207       std::string filename = GDCM_DATA_ROOT;
208       filename += "/";  //doh!
209       filename += gdcmDataImages[i];
210
211       std::string output = "output.dcm";
212
213       if( CopyDicom( filename, output ) != 0 )
214       {
215          retVal++;
216       }
217
218       i++;
219    }
220    return retVal;
221 }
222