1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteReadCompare.cxx,v $
6 Date: $Date: 2004/11/16 04:26:18 $
7 Version: $Revision: 1.7 $
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 =========================================================================*/
18 #include "gdcmHeader.h"
22 #include "gdcmDataImages.h"
24 int main(int argc, char* argv[])
28 std::cerr << "Test::TestReadWriteReadCompare: Usage: " << argv[0]
29 << " fileToCheck.dcm " << std::endl;
32 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
33 std::cout << " For all images in gdcmData (and not blacklisted in "
34 "Test/CMakeLists.txt)" << std::endl;
35 std::cout << " apply the following multistep test: " << std::endl;
36 std::cout << " step 1: parse the image (as gdcmHeader) and call"
37 << " IsReadable(). " << std::endl;
38 std::cout << " step 2: write the corresponding image in DICOM V3 "
39 << "with explicit" << std::endl
40 << " Value Representation in temporary file "
41 << "TestReadWriteReadCompare.dcm." << std::endl;
42 std::cout << " step 3: read the image written on step2 and call "
43 << " IsReadable(). " << std::endl;
44 std::cout << " step 4: compare (in memory with memcmp) that the two "
45 << "images " << std::endl
46 << " match (as expanded by gdcm)." << std::endl;
49 //while( gdcmDataImages[i] != 0 )
51 std::string filename = GDCM_DATA_ROOT;
53 //filename += gdcmDataImages[i++];
56 std::cout << " Testing: " << filename << std::endl;
58 //////////////// Step 1 (see above description):
60 gdcm::Header *header = new gdcm::Header( filename );
61 if( !header->IsReadable() )
63 std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
64 << filename << std::endl;
68 std::cout << " step 1 ...";
70 //////////////// Step 2:
72 gdcm::File* file = new gdcm::File( header );
73 int dataSize = file->GetImageDataSize();
74 uint8_t* imageData = file->GetImageData(); //EXTREMELY IMPORTANT
75 // Sure, it is : It's up to the user to decide if he wants to
76 // GetImageData or if he wants to GetImageDataRaw
77 // (even if we do it by setting a flag, he will have to decide)
79 /// \todo Following line commented out because gdcmFile::SetImageData() is
80 /// brain dead: it sets ImageDataSize to its argument and PixelRead to a.
81 /// Later on, when writing gdcmFile::WriteBase()
82 /// and because PixelRead == 1 we call
83 /// PixelElement->SetLength( ImageDataSizeRaw );
84 /// where we use ImageDataSizeRAW instead of ImageDataSize !
85 /// But when the original image made the transformation LUT -> RGB,
86 /// ImageDataSizeRaw is the third of ImageDataSize, and there is no
87 /// reason (since we called gdcmFile::SetImageData) to use the Raw image
88 /// size... This "bug" in gdcmFile made that we had to black list
89 /// images 8BitsUncompressedColor.dcm, OT-PAL-8-face.dcm and
90 /// US-PAL-8-10x-echo.dcm...
91 /// In conclusion fix gdcmFile, and then uncomment the following line.
93 // --> I did. ctest doesn't break. But ... is it enought to say it's OK ?
95 file->SetImageData(imageData, dataSize);
97 file->WriteDcmExplVR( "TestReadWriteReadCompare.dcm" );
100 //////////////// Step 3:
102 gdcm::File* reread = new gdcm::File( "TestReadWriteReadCompare.dcm" );
103 if( !reread->GetHeader()->IsReadable() )
105 std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
106 << "written:" << filename << std::endl;
113 // For the next step:
114 int dataSizeWritten = reread->GetImageDataSize();
115 uint8_t* imageDataWritten = reread->GetImageData();
117 //////////////// Step 4:
119 if (dataSize != dataSizeWritten)
121 std::cout << std::endl
122 << " Pixel areas lengths differ: "
123 << dataSize << " # " << dataSizeWritten << std::endl;
130 if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
133 std::cout << std::endl
134 << " Pixel differ (as expanded in memory)." << std::endl;
140 std::cout << "4...OK." << std::endl ;
142 //////////////// Clean up: