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