1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteReadCompare.cxx,v $
6 Date: $Date: 2005/07/06 09:53:43 $
7 Version: $Revision: 1.23 $
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( filename );
32 if( !file->IsReadable() )
34 std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
35 << filename << std::endl;
39 std::cout << " step 1...";
41 //////////////// Step 2:
42 gdcm::FileHelper *filehelper = new gdcm::FileHelper( file );
43 int dataSize = filehelper->GetImageDataSize();
44 uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
45 // Sure, it is : It's up to the user to decide if he wants to
46 // GetImageData or if he wants to GetImageDataRaw
47 // (even if we do it by setting a flag, he will have to decide)
49 //filehelper->SetImageData(imageData, dataSize);
51 filehelper->SetWriteModeToRGB();
52 filehelper->WriteDcmExplVR( output );
55 //////////////// Step 3:
56 gdcm::FileHelper *reread = new gdcm::FileHelper( output );
57 if( !reread->GetFile()->IsReadable() )
59 std::cerr << "Failed" << std::endl
60 << "Test::TestReadWriteReadCompare: Could not reread image "
61 << "written:" << filename << std::endl;
69 int dataSizeWritten = reread->GetImageDataSize();
70 uint8_t *imageDataWritten = reread->GetImageData();
72 //////////////// Step 4:
73 // Test the image size
74 if (file->GetXSize() != reread->GetFile()->GetXSize() ||
75 file->GetYSize() != reread->GetFile()->GetYSize() ||
76 file->GetZSize() != reread->GetFile()->GetZSize())
78 std::cout << "Failed" << std::endl
79 << " X Size differs: "
80 << "X: " << file->GetXSize() << " # "
81 << reread->GetFile()->GetXSize() << " | "
82 << "Y: " << file->GetYSize() << " # "
83 << reread->GetFile()->GetYSize() << " | "
84 << "Z: " << file->GetZSize() << " # "
85 << reread->GetFile()->GetZSize() << std::endl;
93 if (dataSize != dataSizeWritten)
95 std::cout << "Failed" << std::endl
96 << " Pixel areas lengths differ: "
97 << dataSize << " # " << dataSizeWritten << std::endl;
104 // Test the data's content
105 if (memcmp(imageData, imageDataWritten, dataSize) !=0)
107 std::cout << "Failed" << std::endl
108 << " Pixel differ (as expanded in memory)." << std::endl;
114 std::cout << "4...OK." << std::endl ;
116 //////////////// Clean up:
124 int TestReadWriteReadCompare(int argc, char *argv[])
129 gdcm::Debug::DebugOn();
133 const std::string input = argv[1];
134 const std::string output = argv[2];
135 result += CompareInternal(input, output);
137 else if( argc > 4 || argc == 2 )
139 std::cerr << "Please read the manual" << std::endl;
143 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
144 std::cout << " For all images in gdcmData (and not blacklisted in "
145 "Test/CMakeLists.txt)" << std::endl;
146 std::cout << " apply the following multistep test: " << std::endl;
147 std::cout << " step 1: parse the image (as gdcmFile) and call"
148 << " IsReadable(). " << std::endl;
149 std::cout << " step 2: write the corresponding image in DICOM V3 "
150 << "with explicit" << std::endl
151 << " Value Representation in temporary file "
152 << "TestReadWriteReadCompare.dcm." << std::endl;
153 std::cout << " step 3: read the image written on step2 and call "
154 << " IsReadable(). " << std::endl;
155 std::cout << " step 4: compare (in memory with memcmp) that the two "
156 << "images " << std::endl
157 << " match (as expanded by gdcm)." << std::endl;
160 while( gdcmDataImages[i] != 0 )
162 std::string filename = GDCM_DATA_ROOT;
164 filename += gdcmDataImages[i++];
165 result += CompareInternal(filename, "TestReadWriteReadCompare.dcm");