1 /*=========================================================================
4 Module: $RCSfile: TestCopyDicom.cxx,v $
6 Date: $Date: 2004/12/07 18:16:39 $
7 Version: $Revision: 1.26 $
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 =========================================================================*/
18 #include "gdcmHeader.h"
20 #include "gdcmValEntry.h"
21 #include "gdcmBinEntry.h"
24 #include "gdcmDataImages.h"
27 #include <unistd.h> //for access, unlink
29 #include <io.h> //for _access on Win32
32 // return true if the file exists
33 bool FileExists(const char* filename)
36 # define access _access
41 if ( access(filename, R_OK) != 0 )
51 bool RemoveFile(const char* source)
54 #define _unlink unlink
56 return unlink(source) != 0 ? false : true;
59 int CopyDicom(std::string const & filename,
60 std::string const & output )
62 std::cout << " Testing: " << filename << std::endl;
63 if( FileExists( output.c_str() ) )
65 // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
66 if( !RemoveFile( output.c_str() ) )
68 std::cout << "Ouch, the file exist, but I cannot remove it" << std::endl;
73 //////////////// Step 1:
75 gdcm::Header *originalH = new gdcm::Header( filename );
76 gdcm::Header *copyH = new gdcm::Header( );
78 //First of all copy the header field by field
80 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
81 // It was commented out by Mathieu, that was a *good* idea
82 // (the user does NOT have to know the way we implemented the Header !)
83 // Waiting for a 'clean' solution, I keep the method ...JPRx
86 //////////////// Step 2:
88 originalH->Initialize();
89 gdcm::DocEntry* d=originalH->GetNextEntry();
93 if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
95 copyH->ReplaceOrCreateByNumber(
102 else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
104 copyH->ReplaceOrCreateByNumber(
112 // We skip pb of SQ recursive exploration
115 d=originalH->GetNextEntry();
118 gdcm::File *original = new gdcm::File( originalH );
119 gdcm::File *copy = new gdcm::File( copyH );
121 size_t dataSize = original->GetImageDataSize();
122 uint8_t* imageData = original->GetImageData();
124 // Useless to set the image datas, because it's already made when
125 // copying the corresponding BinEntry that contains the pixel datas
126 copy->SetImageData(imageData, dataSize);
128 //////////////// Step 3:
130 copy->SetWriteModeToRGB();
131 if( !copy->WriteDcmExplVR(output) )
133 std::cout << " Failed" << std::endl
134 << " " << output << " not written" << std::endl;
147 //////////////// Step 4:
149 copy = new gdcm::File( output );
151 //Is the file written still gdcm parsable ?
152 if ( !copy->GetHeader()->IsReadable() )
154 std::cout << " Failed" << std::endl
155 << " " << output << " not readable" << std::endl;
163 //////////////// Step 5:
165 size_t dataSizeWritten = copy->GetImageDataSize();
166 uint8_t* imageDataWritten = copy->GetImageData();
168 if (dataSize != dataSizeWritten)
170 std::cout << " Failed" << std::endl
171 << " Pixel areas lengths differ: "
172 << dataSize << " # " << dataSizeWritten << std::endl;
181 if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
184 std::cout << " Failed" << std::endl
185 << " Pixel differ (as expanded in memory)." << std::endl;
193 std::cout << "OK." << std::endl ;
202 // Here we load a gdcmFile and then try to create from scratch a copy of it,
203 // copying field by field the dicom image
205 int TestCopyDicom(int argc, char* argv[])
209 // The test is specified a specific filename, use it instead of looping
211 const std::string input = argv[1];
212 const std::string reference = argv[2];
213 return CopyDicom( input, reference );
215 else if ( argc > 3 || argc == 2 )
217 std::cout << " Usage: " << argv[0]
218 << " (no arguments needed)." << std::endl;
219 std::cout << "or Usage: " << argv[0]
220 << " filename.dcm reference.dcm" << std::endl;
225 std::cout << " Description (Test::TestCopyDicom): "
227 std::cout << " For all images in gdcmData (and not blacklisted in "
228 "Test/CMakeLists.txt)"
230 std::cout << " apply the following to each filename.xxx: "
232 std::cout << " step 1: parse the image (as gdcmHeader) and call"
233 << " IsReadable(). After that, call GetImageData() and "
234 << "GetImageDataSize() "
236 std::cout << " step 2: create a copy of the readed file and the new"
237 << " pixel datas are set to the copy"
239 std::cout << " step 3: write the copy of the image"
241 std::cout << " step 4: read the copy and call IsReadable()"
243 std::cout << " step 5: compare (in memory with memcmp) that the two "
244 << "images " << std::endl
245 << " match (as expanded by gdcm)." << std::endl;
246 std::cout << std::endl;
249 int retVal = 0; //by default this is an error
250 while( gdcmDataImages[i] != 0 )
252 std::string filename = GDCM_DATA_ROOT;
253 filename += "/"; //doh!
254 filename += gdcmDataImages[i];
256 std::string output = "output.dcm";
258 if( CopyDicom( filename, output ) != 0 )