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 /// \todo Following line commented out because gdcmFile::SetImageData() is
58 /// brain dead: it sets ImageDataSize to its argument and PixelRead to a.
59 /// Later on, when writing gdcmFile::WriteBase() and because PixelRead == 1
61 /// PixelElement->SetLength( ImageDataSizeRaw );
62 /// where we use ImageDataSizeRAW instead of ImageDataSize !
63 /// But when the original image made the transformation LUT -> RGB,
64 /// ImageDataSizeRaw is the third of ImageDataSize, and there is no
65 /// reason (since we called gdcmFile::SetImageData) to use the Raw image
66 /// size... This "bug" in gdcmFile made that we had to black list
67 /// images 8BitsUncompressedColor.dcm, OT-PAL-8-face.dcm and
68 /// US-PAL-8-10x-echo.dcm...
69 /// In conclusion fix gdcmFile, and then uncomment the following line.
70 /// file->SetImageData(imageData, dataSize);
71 file->WriteDcmExplVR( "TestReadWriteReadCompare.dcm" );
74 //////////////// Step 3:
76 gdcmFile* reread = new gdcmFile( "TestReadWriteReadCompare.dcm",
78 if( !reread->GetHeader()->IsReadable() )
80 std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
81 << "written:" << filename << std::endl;
89 int dataSizeWritten = reread->GetImageDataSize();
90 void* imageDataWritten = reread->GetImageData();
92 //////////////// Step 4:
94 if (dataSize != dataSizeWritten)
96 std::cout << std::endl
97 << " Pixel areas lengths differ: "
98 << dataSize << " # " << dataSizeWritten << std::endl;
99 delete (char*)imageData;
100 delete (char*)imageDataWritten;
107 if (int res=memcmp(imageData, imageDataWritten, dataSize) !=0)
109 std::cout << std::endl
110 << " Pixel differ (as expanded in memory)." << std::endl;
111 delete (char*)imageData;
112 delete (char*)imageDataWritten;
118 std::cout << "4...OK." << std::endl ;
120 //////////////// Clean up:
121 delete (char*)imageData;
122 delete (char*)imageDataWritten;