1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteReadCompare.cxx,v $
6 Date: $Date: 2005/02/09 15:06:48 $
7 Version: $Revision: 1.22 $
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"
22 #include "gdcmDataImages.h"
24 int CompareInternal(std::string const & filename, std::string const & output)
26 std::cout << " Testing: " << filename << std::endl;
28 //////////////// Step 1 (see above description):
30 gdcm::File *file = new gdcm::File( filename );
31 if( !file->IsReadable() )
33 std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
34 << filename << std::endl;
38 std::cout << " step 1...";
40 //////////////// Step 2:
41 gdcm::FileHelper *filehelper = new gdcm::FileHelper( file );
42 int dataSize = filehelper->GetImageDataSize();
43 uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
44 // Sure, it is : It's up to the user to decide if he wants to
45 // GetImageData or if he wants to GetImageDataRaw
46 // (even if we do it by setting a flag, he will have to decide)
48 //filehelper->SetImageData(imageData, dataSize);
50 filehelper->SetWriteModeToRGB();
51 filehelper->WriteDcmExplVR( output );
54 //////////////// Step 3:
55 gdcm::FileHelper *reread = new gdcm::FileHelper( output );
56 if( !reread->GetFile()->IsReadable() )
58 std::cerr << "Failed" << std::endl
59 << "Test::TestReadWriteReadCompare: Could not reread image "
60 << "written:" << filename << std::endl;
68 int dataSizeWritten = reread->GetImageDataSize();
69 uint8_t *imageDataWritten = reread->GetImageData();
71 //////////////// Step 4:
72 // Test the image size
73 if (file->GetXSize() != reread->GetFile()->GetXSize() ||
74 file->GetYSize() != reread->GetFile()->GetYSize() ||
75 file->GetZSize() != reread->GetFile()->GetZSize())
77 std::cout << "Failed" << std::endl
78 << " X Size differs: "
79 << "X: " << file->GetXSize() << " # "
80 << reread->GetFile()->GetXSize() << " | "
81 << "Y: " << file->GetYSize() << " # "
82 << reread->GetFile()->GetYSize() << " | "
83 << "Z: " << file->GetZSize() << " # "
84 << reread->GetFile()->GetZSize() << std::endl;
92 if (dataSize != dataSizeWritten)
94 std::cout << "Failed" << std::endl
95 << " Pixel areas lengths differ: "
96 << dataSize << " # " << dataSizeWritten << std::endl;
103 // Test the data's content
104 if (memcmp(imageData, imageDataWritten, dataSize) !=0)
106 std::cout << "Failed" << std::endl
107 << " Pixel differ (as expanded in memory)." << std::endl;
113 std::cout << "4...OK." << std::endl ;
115 //////////////// Clean up:
123 int TestReadWriteReadCompare(int argc, char *argv[])
128 const std::string input = argv[1];
129 const std::string output = argv[2];
130 result += CompareInternal(input, output);
132 else if( argc > 3 || argc == 2 )
134 std::cerr << "Please read the manual" << std::endl;
138 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
139 std::cout << " For all images in gdcmData (and not blacklisted in "
140 "Test/CMakeLists.txt)" << std::endl;
141 std::cout << " apply the following multistep test: " << std::endl;
142 std::cout << " step 1: parse the image (as gdcmFile) and call"
143 << " IsReadable(). " << std::endl;
144 std::cout << " step 2: write the corresponding image in DICOM V3 "
145 << "with explicit" << std::endl
146 << " Value Representation in temporary file "
147 << "TestReadWriteReadCompare.dcm." << std::endl;
148 std::cout << " step 3: read the image written on step2 and call "
149 << " IsReadable(). " << std::endl;
150 std::cout << " step 4: compare (in memory with memcmp) that the two "
151 << "images " << std::endl
152 << " match (as expanded by gdcm)." << std::endl;
155 while( gdcmDataImages[i] != 0 )
157 std::string filename = GDCM_DATA_ROOT;
159 filename += gdcmDataImages[i++];
160 result += CompareInternal(filename, "TestReadWriteReadCompare.dcm");