1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteReadCompare.cxx,v $
6 Date: $Date: 2005/07/21 04:51:26 $
7 Version: $Revision: 1.24 $
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 "gdcmDebug.h"
23 #include "gdcmDataImages.h"
25 int CompareInternal(std::string const &filename, std::string const &output)
27 std::cout << " Testing: " << filename << std::endl;
29 //////////////// Step 1 (see above description):
31 gdcm::File *file = new gdcm::File( );
32 file->SetFileName( filename );
34 if( !file->IsReadable() )
36 std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
37 << filename << std::endl;
41 std::cout << " step 1...";
43 //////////////// Step 2:
44 gdcm::FileHelper *filehelper = new gdcm::FileHelper( file );
45 int dataSize = filehelper->GetImageDataSize();
46 uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
47 // Sure, it is : It's up to the user to decide if he wants to
48 // GetImageData or if he wants to GetImageDataRaw
49 // (even if we do it by setting a flag, he will have to decide)
51 //filehelper->SetImageData(imageData, dataSize);
53 filehelper->SetWriteModeToRGB();
54 filehelper->WriteDcmExplVR( output );
57 //////////////// Step 3:
58 gdcm::File *fileout = new gdcm::File();
59 fileout->SetFileName( output );
61 // gdcm::FileHelper *reread = new gdcm::FileHelper( output ); // deprecated
63 if( !fileout->IsReadable() )
65 std::cerr << "Failed" << std::endl
66 << "Test::TestReadWriteReadCompare: Could not parse the newly "
67 << "written image:" << filename << std::endl;
73 gdcm::FileHelper *reread = new gdcm::FileHelper( fileout );
77 int dataSizeWritten = reread->GetImageDataSize();
78 uint8_t *imageDataWritten = reread->GetImageData();
80 //////////////// Step 4:
81 // Test the image size
82 if (file->GetXSize() != reread->GetFile()->GetXSize() ||
83 file->GetYSize() != reread->GetFile()->GetYSize() ||
84 file->GetZSize() != reread->GetFile()->GetZSize())
86 std::cout << "Failed" << std::endl
87 << " X Size differs: "
88 << "X: " << file->GetXSize() << " # "
89 << reread->GetFile()->GetXSize() << " | "
90 << "Y: " << file->GetYSize() << " # "
91 << reread->GetFile()->GetYSize() << " | "
92 << "Z: " << file->GetZSize() << " # "
93 << reread->GetFile()->GetZSize() << std::endl;
100 // Test the data size
101 if (dataSize != dataSizeWritten)
103 std::cout << "Failed" << std::endl
104 << " Pixel areas lengths differ: "
105 << dataSize << " # " << dataSizeWritten << std::endl;
112 // Test the data's content
113 if (memcmp(imageData, imageDataWritten, dataSize) !=0)
115 std::cout << "Failed" << std::endl
116 << " Pixel differ (as expanded in memory)." << std::endl;
122 std::cout << "4...OK." << std::endl ;
124 //////////////// Clean up:
132 int TestReadWriteReadCompare(int argc, char *argv[])
137 gdcm::Debug::DebugOn();
141 const std::string input = argv[1];
142 const std::string output = argv[2];
143 result += CompareInternal(input, output);
145 else if( argc > 4 || argc == 2 )
147 std::cerr << "Please read the manual" << std::endl;
151 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
152 std::cout << " For all images in gdcmData (and not blacklisted in "
153 "Test/CMakeLists.txt)" << std::endl;
154 std::cout << " apply the following multistep test: " << std::endl;
155 std::cout << " step 1: parse the image (as gdcmFile) and call"
156 << " IsReadable(). " << std::endl;
157 std::cout << " step 2: write the corresponding image in DICOM V3 "
158 << "with explicit" << std::endl
159 << " Value Representation in temporary file "
160 << "TestReadWriteReadCompare.dcm." << std::endl;
161 std::cout << " step 3: read the image written on step2 and call "
162 << " IsReadable(). " << std::endl;
163 std::cout << " step 4: compare (in memory with memcmp) that the two "
164 << "images " << std::endl
165 << " match (as expanded by gdcm)." << std::endl;
168 while( gdcmDataImages[i] != 0 )
170 std::string filename = GDCM_DATA_ROOT;
172 filename += gdcmDataImages[i++];
173 result += CompareInternal(filename, "TestReadWriteReadCompare.dcm");