]> Creatis software - gdcm.git/blob - Testing/TestReadWriteReadCompare.cxx
remove commented out line after fix of gdcmFile::SetImageData()
[gdcm.git] / Testing / TestReadWriteReadCompare.cxx
1 #include "gdcmHeader.h"
2 #include "gdcmFile.h"
3
4 //Generated file:
5 #include "gdcmDataImages.h"
6
7 int TestReadWriteReadCompare(int argc, char* argv[]) 
8 {
9    if (argc)
10    {
11       std::cerr << "Test::TestReadWriteReadCompare: Usage: " << argv[0]
12                 << " (no arguments needed)." << std::endl;
13    }
14    
15    std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
16    std::cout << "   For all images in gdcmData (and not blacklisted in "
17                 "Test/CMakeLists.txt)" << std::endl;
18    std::cout << "   apply the following multistep test: " << std::endl;
19    std::cout << "   step 1: parse the image (as gdcmHeader) and call"
20              << " IsReadable(). " << std::endl;
21    std::cout << "   step 2: write the corresponding image in DICOM V3 "
22              << "with explicit" << std::endl
23              << "           Value Representation in temporary file "
24              << "TestReadWriteReadCompare.dcm." << std::endl;
25    std::cout << "   step 3: read the image written on step2 and call "
26              << " IsReadable(). " << std::endl;
27    std::cout << "   step 4: compare (in memory with memcmp) that the two "
28              << "images " << std::endl
29              << "           match (as expanded by gdcm)." << std::endl;
30
31    int i = 0;
32    while( gdcmDataImages[i] != 0 )
33    {
34       std::string filename = GDCM_DATA_ROOT;
35       filename += "/";
36       filename += gdcmDataImages[i++];
37    
38       std::cout << "   Testing: " << filename << std::endl;
39
40       //////////////// Step 1 (see above description):
41
42       gdcmHeader *header = new gdcmHeader( filename );
43       if( !header->IsReadable() )
44       {
45          std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
46                    << filename << std::endl;
47          delete header;
48          return 1;
49       }
50       std::cout << "           step 1 ...";
51
52       //////////////// Step 2:
53
54       gdcmFile*  file = new gdcmFile( header );
55       int dataSize    = file->GetImageDataSize();
56       void* imageData = file->GetImageData(); //EXTREMELY IMPORTANT
57              // Sure, it is : It's up to the user to decide if he wants to
58              // GetImageData or if he wants to GetImageDataRaw
59              // (even if we do it by setting a flag, he will have to decide) 
60
61       /// \todo Following line commented out because gdcmFile::SetImageData() is
62       /// brain dead: it sets ImageDataSize to its argument and PixelRead to a.
63       /// Later on, when writing gdcmFile::WriteBase() 
64       /// and because PixelRead == 1 we call
65       ///    PixelElement->SetLength( ImageDataSizeRaw );
66       /// where we use ImageDataSizeRAW instead of ImageDataSize !
67       /// But when the original image made the transformation LUT -> RGB, 
68       /// ImageDataSizeRaw is the third of ImageDataSize, and there is no
69       /// reason (since we called gdcmFile::SetImageData) to use the Raw image
70       /// size... This "bug" in gdcmFile made that we had to black list
71       /// images 8BitsUncompressedColor.dcm, OT-PAL-8-face.dcm and 
72       /// US-PAL-8-10x-echo.dcm...
73       /// In conclusion fix gdcmFile, and then uncomment the following line.
74       
75       // --> I did. ctest doesn't break. But ... is it enought to say it's OK ?
76       
77       file->SetImageData(imageData, dataSize);
78       
79       file->WriteDcmExplVR( "TestReadWriteReadCompare.dcm" );
80       std::cout << "2...";
81     
82       //////////////// Step 3:
83
84       gdcmFile* reread = new gdcmFile( "TestReadWriteReadCompare.dcm" );
85       if( !reread->GetHeader()->IsReadable() )
86       {
87         std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
88                   << "written:" << filename << std::endl;
89         delete header;
90         delete file;
91         delete reread;
92         return 1;
93       }
94       std::cout << "3...";
95       // For the next step:
96       int    dataSizeWritten = reread->GetImageDataSize();
97       void* imageDataWritten = reread->GetImageData();
98
99       //////////////// Step 4:
100  
101       if (dataSize != dataSizeWritten)
102       {
103          std::cout << std::endl
104             << "        Pixel areas lengths differ: "
105             << dataSize << " # " << dataSizeWritten << std::endl;
106          delete (char*)imageData;
107          delete (char*)imageDataWritten;
108          delete header;
109          delete file;
110          delete reread;
111          return 1;
112       }
113
114       if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
115       {
116          (void)res;
117          std::cout << std::endl
118             << "        Pixel differ (as expanded in memory)." << std::endl;
119          delete (char*)imageData;
120          delete (char*)imageDataWritten;
121          delete header;
122          delete file;
123          delete reread;
124          return 1;
125       }
126       std::cout << "4...OK." << std::endl ;
127
128       //////////////// Clean up:
129       delete (char*)imageData;
130       delete (char*)imageDataWritten;
131       delete header;
132       delete file;
133       delete reread;
134    }
135
136   return 0;
137 }