1 #include "gdcmHeader.h"
5 #include "gdcmDataImages.h"
7 int CompareInternal(std::string const & filename, std::string const & output)
9 std::cout << " Testing: " << filename << std::endl;
11 //////////////// Step 1 (see above description):
13 gdcm::Header *header = new gdcm::Header( filename );
14 if( !header->IsReadable() )
16 std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
17 << filename << std::endl;
21 std::cout << " step 1 ...";
23 //////////////// Step 2:
25 gdcm::File* file = new gdcm::File( header );
26 int dataSize = file->GetImageDataSize();
27 uint8_t* imageData = file->GetImageData(); //EXTREMELY IMPORTANT
28 // Sure, it is : It's up to the user to decide if he wants to
29 // GetImageData or if he wants to GetImageDataRaw
30 // (even if we do it by setting a flag, he will have to decide)
32 /// \todo Following line commented out because gdcmFile::SetImageData() is
33 /// brain dead: it sets ImageDataSize to its argument and PixelRead to a.
34 /// Later on, when writing gdcmFile::WriteBase()
35 /// and because PixelRead == 1 we call
36 /// PixelElement->SetLength( ImageDataSizeRaw );
37 /// where we use ImageDataSizeRAW instead of ImageDataSize !
38 /// But when the original image made the transformation LUT -> RGB,
39 /// ImageDataSizeRaw is the third of ImageDataSize, and there is no
40 /// reason (since we called gdcmFile::SetImageData) to use the Raw image
41 /// size... This "bug" in gdcmFile made that we had to black list
42 /// images 8BitsUncompressedColor.dcm, OT-PAL-8-face.dcm and
43 /// US-PAL-8-10x-echo.dcm...
44 /// In conclusion fix gdcmFile, and then uncomment the following line.
46 // --> I did. ctest doesn't break. But ... is it enought to say it's OK ?
48 file->SetImageData(imageData, dataSize);
50 file->WriteDcmExplVR( output );
53 //////////////// Step 3:
55 gdcm::File* reread = new gdcm::File( output );
56 if( !reread->GetHeader()->IsReadable() )
58 std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
59 << "written:" << filename << std::endl;
67 int dataSizeWritten = reread->GetImageDataSize();
68 void* imageDataWritten = reread->GetImageData();
70 //////////////// Step 4:
72 if (dataSize != dataSizeWritten)
74 std::cout << std::endl
75 << " Pixel areas lengths differ: "
76 << dataSize << " # " << dataSizeWritten << std::endl;
77 delete[] (char*)imageData;
78 delete[] (char*)imageDataWritten;
85 if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
88 std::cout << std::endl
89 << " Pixel differ (as expanded in memory)." << std::endl;
90 delete[] (char*)imageData;
91 delete[] (char*)imageDataWritten;
97 std::cout << "4...OK." << std::endl ;
99 //////////////// Clean up:
100 delete[] (char*)imageData;
101 delete[] (char*)imageDataWritten;
109 int TestReadWriteReadCompare(int argc, char* argv[])
114 const std::string input = argv[1];
115 const std::string output = argv[2];
116 result += CompareInternal(input, output);
118 else if( argc > 3 || argc == 2 )
120 std::cerr << "Please read the manual" << std::endl;
124 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
125 std::cout << " For all images in gdcmData (and not blacklisted in "
126 "Test/CMakeLists.txt)" << std::endl;
127 std::cout << " apply the following multistep test: " << std::endl;
128 std::cout << " step 1: parse the image (as gdcmHeader) and call"
129 << " IsReadable(). " << std::endl;
130 std::cout << " step 2: write the corresponding image in DICOM V3 "
131 << "with explicit" << std::endl
132 << " Value Representation in temporary file "
133 << "TestReadWriteReadCompare.dcm." << std::endl;
134 std::cout << " step 3: read the image written on step2 and call "
135 << " IsReadable(). " << std::endl;
136 std::cout << " step 4: compare (in memory with memcmp) that the two "
137 << "images " << std::endl
138 << " match (as expanded by gdcm)." << std::endl;
141 while( gdcmDataImages[i] != 0 )
143 std::string filename = GDCM_DATA_ROOT;
145 filename += gdcmDataImages[i++];
146 result += CompareInternal(filename, "TestReadWriteReadCompare.dcm");