1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteReadCompare.cxx,v $
6 Date: $Date: 2007/08/29 15:56:41 $
7 Version: $Revision: 1.32 $
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_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New( );
32 file->SetFileName( filename );
34 if( !file->IsReadable() )
36 std::cout << "Failed" << std::endl
37 << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
38 << filename << std::endl;
42 std::cout << " step 1...";
44 //////////////// Step 2:
45 GDCM_NAME_SPACE::FileHelper *filehelper = GDCM_NAME_SPACE::FileHelper::New( file );
46 int dataSize = filehelper->GetImageDataSize();
47 uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
48 // Sure, it is : It's up to the user to decide if he wants to
49 // GetImageData or if he wants to GetImageDataRaw
50 // (even if we do it by setting a flag, *he* will have to decide)
52 //filehelper->SetImageData(imageData, dataSize);
54 filehelper->SetWriteModeToRGB();
55 filehelper->WriteDcmExplVR( output );
58 //////////////// Step 3:
59 GDCM_NAME_SPACE::File *fileout = GDCM_NAME_SPACE::File::New();
60 fileout->SetFileName( output );
62 // gdcm::FileHelper *reread = new gdcm::FileHelper( output ); // deprecated
64 if( !fileout->IsReadable() )
66 std::cout << "Failed" << std::endl
67 << "Test::TestReadWriteReadCompare: Could not parse the newly "
68 << "written image:" << filename << std::endl;
75 GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
79 int dataSizeWritten = reread->GetImageDataSize();
80 uint8_t *imageDataWritten = reread->GetImageData();
82 //////////////// Step 4:
83 // Test the image size
84 if (file->GetXSize() != reread->GetFile()->GetXSize() ||
85 file->GetYSize() != reread->GetFile()->GetYSize() ||
86 file->GetZSize() != reread->GetFile()->GetZSize())
88 std::cout << "Failed" << std::endl
89 << " X Size differs: "
90 << "X: " << file->GetXSize() << " # "
91 << reread->GetFile()->GetXSize() << " | "
92 << "Y: " << file->GetYSize() << " # "
93 << reread->GetFile()->GetYSize() << " | "
94 << "Z: " << file->GetZSize() << " # "
95 << reread->GetFile()->GetZSize() << std::endl;
103 // Test the data size
104 // beware of odd length Pixel Element!
105 int dataSizeFixed = dataSize + dataSize%2;
106 int dataSizeWrittenFixed = dataSizeWritten + dataSizeWritten%2;
108 if (dataSizeFixed != dataSizeWrittenFixed)
110 std::cout << "Failed" << std::endl
111 << " Pixel areas lengths differ: "
112 << dataSize << " # " << dataSizeWritten << std::endl;
114 filehelper->Delete();
120 // Test the data's content
121 if (memcmp(imageData, imageDataWritten, dataSize) !=0)
123 std::cout << "Failed" << std::endl
124 << " Pixel differ (as expanded in memory)." << std::endl;
126 filehelper->Delete();
131 std::cout << "4...OK." << std::endl ;
133 //////////////// Clean up:
135 filehelper->Delete();
142 int TestReadWriteReadCompare(int argc, char *argv[])
147 GDCM_NAME_SPACE::Debug::DebugOn();
151 const std::string input = argv[1];
152 const std::string output = argv[2];
153 result += CompareInternal(input, output);
155 else if( argc > 4 || argc == 2 )
157 std::cout << "Please read the manual" << std::endl;
161 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
162 std::cout << " For all images in gdcmData (and not blacklisted in "
163 "Test/CMakeLists.txt)" << std::endl;
164 std::cout << " apply the following multistep test: " << std::endl;
165 std::cout << " step 1: parse the image (as gdcmFile) and call"
166 << " IsReadable(). " << std::endl;
167 std::cout << " step 2: write the corresponding image in DICOM V3 "
168 << "with explicit" << std::endl
169 << " Value Representation in temporary file "
170 << "TestReadWriteReadCompare.dcm." << std::endl;
171 std::cout << " step 3: read the image written on step2 and call "
172 << " IsReadable(). " << std::endl;
173 std::cout << " step 4: compare (in memory with memcmp) that the two "
174 << "images " << std::endl
175 << " match (as expanded by gdcm)." << std::endl;
178 while( gdcmDataImages[i] != 0 )
180 std::string filename = GDCM_DATA_ROOT;
182 filename += gdcmDataImages[i++];
183 result += CompareInternal(filename, "TestReadWriteReadCompare.dcm");