1 #include "gdcmHeader.h"
5 #include "gdcmDataImages.h"
7 int TestReadWriteReadCompare(int argc, char* argv[])
10 std::cerr << "Test::TestReadWriteReadCompare: Usage: " << argv[0]
11 << " (no arguments needed)." << std::endl;
14 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
15 std::cout << " For all images in gdcmData (and not blacklisted in "
16 "Test/CMakeLists.txt)" << std::endl;
17 std::cout << " apply the following multistep test: " << std::endl;
18 std::cout << " step 1: parse the image (as gdcmHeader) and call"
19 << " IsReadable(). " << std::endl;
20 std::cout << " step 2: write the corresponding image in DICOM V3 "
21 << "with explicit" << std::endl
22 << " Value Representation in temporary file "
23 << "TestReadWriteReadCompare.dcm." << std::endl;
24 std::cout << " step 3: read the image written on step2 and call "
25 << " IsReadable(). " << std::endl;
26 std::cout << " step 4: compare (in memory with memcmp) that the two "
27 << "images " << std::endl
28 << " match (as expanded by gdcm)." << std::endl;
31 while( gdcmDataImages[i] != 0 )
33 std::string filename = GDCM_DATA_ROOT;
34 filename += "/"; //doh!
35 filename += gdcmDataImages[i++];
37 std::cout << " Testing: " << filename << std::endl;
39 //////////////// Step 1 (see above description):
41 gdcmHeader *header = new gdcmHeader( filename.c_str(), false, true );
42 if( !header->IsReadable() )
44 std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
45 << filename << std::endl;
49 std::cout << " step 1 ...";
51 //////////////// Step 2:
53 gdcmFile* file = new gdcmFile( header );
54 int dataSize = file->GetImageDataSize();
55 void* imageData = file->GetImageData(); //EXTREMELY IMPORTANT
57 file->SetImageData(imageData, dataSize);
58 file->WriteDcmExplVR( "TestReadWriteReadCompare.dcm" );
61 //////////////// Step 3:
63 gdcmFile* reread = new gdcmFile( "TestReadWriteReadCompare.dcm",
65 if( !reread->GetHeader()->IsReadable() )
67 std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
68 << "written:" << filename << std::endl;
76 int dataSizeWritten = reread->GetImageDataSize();
77 void* imageDataWritten = reread->GetImageData();
79 //////////////// Step 4:
81 if (dataSize != dataSizeWritten)
83 std::cout << std::endl
84 << " Pixel areas lengths differ: "
85 << dataSize << " # " << dataSizeWritten << std::endl;
86 delete (char*)imageData;
87 delete (char*)imageDataWritten;
94 if (int res=memcmp(imageData, imageDataWritten, dataSize) !=0)
96 std::cout << std::endl
97 << " Pixel differ (as expanded in memory)." << std::endl;
98 delete (char*)imageData;
99 delete (char*)imageDataWritten;
105 std::cout << " 4...OK." << std::endl ;
107 //////////////// Clean up:
108 delete (char*)imageData;
109 delete (char*)imageDataWritten;