1 /*=========================================================================
4 Module: $RCSfile: TestCopyDicom.cxx,v $
6 Date: $Date: 2005/10/21 08:35:49 $
7 Version: $Revision: 1.43 $
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.
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.
17 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmDataEntry.h"
23 #include "gdcmDataImages.h"
25 // return true if the file exists
26 bool FileExists(const char *filename);
27 bool RemoveFile(const char *source);
29 int CopyDicom(std::string const &filename,
30 std::string const &output )
32 std::cout << " Testing: " << filename << std::endl;
33 if( FileExists( output.c_str() ) )
35 if( !RemoveFile( output.c_str() ) )
37 std::cout << "Ouch, the file exist, but I cannot remove it" << std::endl;
42 //////////////// Step 1:
45 gdcm::File *originalH = new gdcm::File( );
46 gdcm::File *copyH = new gdcm::File( );
48 originalH->SetFileName( filename );
50 //First of all copy the file, field by field
52 //////////////// Step 2:
54 gdcm::DocEntry *d=originalH->GetFirstEntry();
57 if ( gdcm::DataEntry *de = dynamic_cast<gdcm::DataEntry *>(d) )
59 copyH->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
60 de->GetGroup(),de->GetElement(),
65 // We skip pb of SQ recursive exploration
68 d=originalH->GetNextEntry();
71 gdcm::FileHelper *original = new gdcm::FileHelper( originalH );
72 gdcm::FileHelper *copy = new gdcm::FileHelper( copyH );
74 size_t dataSize = original->GetImageDataSize();
75 uint8_t *imageData = original->GetImageData();
77 // Useless to set the image data, because it's already made when
78 // copying the corresponding DataEntry that contains the pixel data
80 // --> FIXME : Why do we let the following line?
81 // to avoid compile time warnings?
82 copy->SetImageData(imageData, dataSize);
84 //////////////// Step 3:
86 copy->SetWriteModeToRGB();
87 if( !copy->WriteDcmExplVR(output) )
89 std::cout << " Failed" << std::endl
90 << " " << output << " not written" << std::endl;
103 //////////////// Step 4:
105 copy = new gdcm::FileHelper( output );
107 //Is the file written still gdcm parsable ?
108 if ( !copy->GetFile()->IsReadable() )
110 std::cout << " Failed" << std::endl
111 << " " << output << " not readable" << std::endl;
119 //////////////// Step 5:
121 size_t dataSizeWritten = copy->GetImageDataSize();
122 uint8_t *imageDataWritten = copy->GetImageData();
124 if (dataSize != dataSizeWritten)
126 std::cout << " Failed" << std::endl
127 << " Pixel areas lengths differ: "
128 << dataSize << " # " << dataSizeWritten << std::endl;
137 if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
140 std::cout << " Failed" << std::endl
141 << " Pixel differ (as expanded in memory)." << std::endl;
149 std::cout << "OK." << std::endl ;
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
161 int TestCopyDicom(int argc, char *argv[])
165 // The test is specified a specific filename, use it instead of looping
167 const std::string input = argv[1];
168 const std::string reference = argv[2];
169 return CopyDicom( input, reference );
171 else if ( argc > 3 || argc == 2 )
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;
181 std::cout << " Description (Test::TestCopyDicom): "
183 std::cout << " For all images in gdcmData (and not blacklisted in "
184 "Test/CMakeLists.txt)"
186 std::cout << " apply the following to each filename.xxx: "
188 std::cout << " step 1: parse the image (as gdcmFile) and call"
189 << " IsReadable(). After that, call GetImageData() and "
190 << "GetImageDataSize() "
192 std::cout << " step 2: create a copy of the readed file and the new"
193 << " pixel data are set to the copy"
195 std::cout << " step 3: write the copy of the image"
197 std::cout << " step 4: read the copy and call IsReadable()"
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;
205 int retVal = 0; //by default this is an error
206 while( gdcmDataImages[i] != 0 )
208 std::string filename = GDCM_DATA_ROOT;
209 filename += "/"; //doh!
210 filename += gdcmDataImages[i];
212 std::string output = "output.dcm";
214 if( CopyDicom( filename, output ) != 0 )