1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteReadCompare.cxx,v $
6 Date: $Date: 2007/09/04 13:02:45 $
7 Version: $Revision: 1.33 $
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...";
43 std::cout << "--- End of Reading \n";
45 //////////////// Step 2:
46 GDCM_NAME_SPACE::FileHelper *filehelper = GDCM_NAME_SPACE::FileHelper::New( file );
47 int dataSize = filehelper->GetImageDataSize();
48 uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
49 // Sure, it is : It's up to the user to decide if he wants to
50 // GetImageData or if he wants to GetImageDataRaw
51 // (even if we do it by setting a flag, *he* will have to decide)
53 //filehelper->SetImageData(imageData, dataSize);
55 filehelper->SetWriteModeToRGB();
56 filehelper->WriteDcmExplVR( output );
58 std::cout << "--- End of Writting \n";
60 //////////////// Step 3:
61 GDCM_NAME_SPACE::File *fileout = GDCM_NAME_SPACE::File::New();
62 fileout->SetFileName( output );
64 // gdcm::FileHelper *reread = new gdcm::FileHelper( output ); // deprecated
66 if( !fileout->IsReadable() )
68 std::cout << "Failed" << std::endl
69 << "Test::TestReadWriteReadCompare: Could not parse the newly "
70 << "written image:" << filename << std::endl;
76 std::cout << "--- End of Re-Reading \n";
77 GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
81 int dataSizeWritten = reread->GetImageDataSize();
82 uint8_t *imageDataWritten = reread->GetImageData();
83 std::cout << "--- End of GetImageData \n";
85 //////////////// Step 4:
86 // Test the image size
87 if (file->GetXSize() != reread->GetFile()->GetXSize() ||
88 file->GetYSize() != reread->GetFile()->GetYSize() ||
89 file->GetZSize() != reread->GetFile()->GetZSize())
91 std::cout << "Failed" << std::endl
92 << " X Size differs: "
93 << "X: " << file->GetXSize() << " # "
94 << reread->GetFile()->GetXSize() << " | "
95 << "Y: " << file->GetYSize() << " # "
96 << reread->GetFile()->GetYSize() << " | "
97 << "Z: " << file->GetZSize() << " # "
98 << reread->GetFile()->GetZSize() << std::endl;
100 filehelper->Delete();
106 // Test the data size
107 // beware of odd length Pixel Element!
108 int dataSizeFixed = dataSize + dataSize%2;
109 int dataSizeWrittenFixed = dataSizeWritten + dataSizeWritten%2;
111 if (dataSizeFixed != dataSizeWrittenFixed)
113 std::cout << "Failed" << std::endl
114 << " Pixel areas lengths differ: "
115 << dataSize << " # " << dataSizeWritten << std::endl;
117 filehelper->Delete();
123 // Test the data's content
124 if (memcmp(imageData, imageDataWritten, dataSize) !=0)
126 std::cout << "Failed" << std::endl
127 << " Pixel differ (as expanded in memory)." << std::endl;
129 filehelper->Delete();
134 std::cout << "4...OK." << std::endl ;
136 //////////////// Clean up:
138 filehelper->Delete();
145 int TestReadWriteReadCompare(int argc, char *argv[])
150 GDCM_NAME_SPACE::Debug::DebugOn();
154 const std::string input = argv[1];
155 const std::string output = argv[2];
156 result += CompareInternal(input, output);
158 else if( argc > 4 || argc == 2 )
160 std::cout << "Please read the manual" << std::endl;
164 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
165 std::cout << " For all images in gdcmData (and not blacklisted in "
166 "Test/CMakeLists.txt)" << std::endl;
167 std::cout << " apply the following multistep test: " << std::endl;
168 std::cout << " step 1: parse the image (as gdcmFile) and call"
169 << " IsReadable(). " << std::endl;
170 std::cout << " step 2: write the corresponding image in DICOM V3 "
171 << "with explicit" << std::endl
172 << " Value Representation in temporary file "
173 << "TestReadWriteReadCompare.dcm." << std::endl;
174 std::cout << " step 3: read the image written on step2 and call "
175 << " IsReadable(). " << std::endl;
176 std::cout << " step 4: compare (in memory with memcmp) that the two "
177 << "images " << std::endl
178 << " match (as expanded by gdcm)." << std::endl;
181 while( gdcmDataImages[i] != 0 )
183 std::string filename = GDCM_DATA_ROOT;
185 filename += gdcmDataImages[i++];
186 result += CompareInternal(filename, "TestReadWriteReadCompare.dcm");