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