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