1 /*=========================================================================
4 Module: $RCSfile: TestCopyDicom.cxx,v $
6 Date: $Date: 2007/10/30 09:13:45 $
7 Version: $Revision: 1.44 $
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:
44 GDCM_NAME_SPACE::File *originalH = GDCM_NAME_SPACE::File::New();
45 GDCM_NAME_SPACE::File *copyH = GDCM_NAME_SPACE::File::New();
48 originalH->SetFileName( filename );
50 //First of all copy the file, field by field
52 //////////////// Step 2:
54 GDCM_NAME_SPACE::DocEntry *d=originalH->GetFirstEntry();
57 if ( GDCM_NAME_SPACE::DataEntry *de = dynamic_cast<GDCM_NAME_SPACE::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();
70 GDCM_NAME_SPACE::FileHelper *original = GDCM_NAME_SPACE::FileHelper::New(originalH);
71 GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New(copyH);
73 size_t dataSize = original->GetImageDataSize();
74 uint8_t *imageData = original->GetImageData();
76 // Useless to set the image data, because it's already made when
77 // copying the corresponding DataEntry that contains the pixel data
79 // --> FIXME : Why do we let the following line?
80 // to avoid compile time warnings?
81 copy->SetImageData(imageData, dataSize);
83 //////////////// Step 3:
85 copy->SetWriteModeToRGB();
86 if( !copy->WriteDcmExplVR(output) )
88 std::cout << " Failed" << std::endl
89 << " " << output << " not written" << std::endl;
102 //////////////// Step 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() )
109 std::cout << " Failed" << std::endl
110 << " " << output << " not readable" << std::endl;
118 //////////////// Step 5:
120 size_t dataSizeWritten = copy->GetImageDataSize();
121 uint8_t *imageDataWritten = copy->GetImageData();
123 if (dataSize != dataSizeWritten)
125 std::cout << " Failed" << std::endl
126 << " Pixel areas lengths differ: "
127 << dataSize << " # " << dataSizeWritten << std::endl;
136 if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
139 std::cout << " Failed" << std::endl
140 << " Pixel differ (as expanded in memory)." << std::endl;
148 std::cout << "OK." << std::endl ;
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
160 int TestCopyDicom(int argc, char *argv[])
164 // The test is specified a specific filename, use it instead of looping
166 const std::string input = argv[1];
167 const std::string reference = argv[2];
168 return CopyDicom( input, reference );
170 else if ( argc > 3 || argc == 2 )
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;
180 std::cout << " Description (Test::TestCopyDicom): "
182 std::cout << " For all images in gdcmData (and not blacklisted in "
183 "Test/CMakeLists.txt)"
185 std::cout << " apply the following to each filename.xxx: "
187 std::cout << " step 1: parse the image (as gdcmFile) and call"
188 << " IsReadable(). After that, call GetImageData() and "
189 << "GetImageDataSize() "
191 std::cout << " step 2: create a copy of the readed file and the new"
192 << " pixel data are set to the copy"
194 std::cout << " step 3: write the copy of the image"
196 std::cout << " step 4: read the copy and call IsReadable()"
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;
204 int retVal = 0; //by default this is an error
205 while( gdcmDataImages[i] != 0 )
207 std::string filename = GDCM_DATA_ROOT;
208 filename += "/"; //doh!
209 filename += gdcmDataImages[i];
211 std::string output = "output.dcm";
213 if( CopyDicom( filename, output ) != 0 )