]> Creatis software - gdcm.git/blob - Example/TestReadWriteReadCompare.cxx
ENH: Adding 'gdcm' namespace. Be nice with me this was a ~13000 lines patch. Also...
[gdcm.git] / Example / TestReadWriteReadCompare.cxx
1 #include "gdcmHeader.h"
2 #include "gdcmFile.h"
3
4 //Generated file:
5 #include "gdcmDataImages.h"
6
7 int main(int argc, char* argv[]) 
8 {
9    if (argc<2)
10    {
11       std::cerr << "Test::TestReadWriteReadCompare: Usage: " << argv[0]
12                 << " fileToCheck.dcm " << 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      filename +=argv[1];
38    
39       std::cout << "   Testing: " << filename << std::endl;
40
41       //////////////// Step 1 (see above description): 
42
43       gdcm::Header *header = new gdcm::Header( filename );
44       if( !header->IsReadable() )
45       {
46          std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
47                    << filename << std::endl;
48          delete header;
49          return 1;
50       }
51       std::cout << "           step 1 ...";
52
53       //////////////// Step 2:
54
55       gdcm::File*  file = new gdcm::File( header );
56       int dataSize    = file->GetImageDataSize();
57       uint8_t* imageData = file->GetImageData(); //EXTREMELY IMPORTANT
58              // Sure, it is : It's up to the user to decide if he wants to
59              // GetImageData or if he wants to GetImageDataRaw
60              // (even if we do it by setting a flag, he will have to decide) 
61
62       /// \todo Following line commented out because gdcmFile::SetImageData() is
63       /// brain dead: it sets ImageDataSize to its argument and PixelRead to a.
64       /// Later on, when writing gdcmFile::WriteBase() 
65       /// and because PixelRead == 1 we call
66       ///    PixelElement->SetLength( ImageDataSizeRaw );
67       /// where we use ImageDataSizeRAW instead of ImageDataSize !
68       /// But when the original image made the transformation LUT -> RGB, 
69       /// ImageDataSizeRaw is the third of ImageDataSize, and there is no
70       /// reason (since we called gdcmFile::SetImageData) to use the Raw image
71       /// size... This "bug" in gdcmFile made that we had to black list
72       /// images 8BitsUncompressedColor.dcm, OT-PAL-8-face.dcm and 
73       /// US-PAL-8-10x-echo.dcm...
74       /// In conclusion fix gdcmFile, and then uncomment the following line.
75       
76       // --> I did. ctest doesn't break. But ... is it enought to say it's OK ?
77       
78       file->SetImageData(imageData, dataSize);
79       
80       file->WriteDcmExplVR( "TestReadWriteReadCompare.dcm" );
81       std::cout << "2...";
82     
83       //////////////// Step 3:
84
85       gdcm::File* reread = new gdcm::File( "TestReadWriteReadCompare.dcm" );
86       if( !reread->GetHeader()->IsReadable() )
87       {
88         std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
89                   << "written:" << filename << std::endl;
90         delete header;
91         delete file;
92         delete reread;
93         return 1;
94       }
95       std::cout << "3...";
96       // For the next step:
97       int    dataSizeWritten = reread->GetImageDataSize();
98       void* imageDataWritten = reread->GetImageData();
99
100       //////////////// Step 4:
101  
102       if (dataSize != dataSizeWritten)
103       {
104          std::cout << std::endl
105             << "        Pixel areas lengths differ: "
106             << dataSize << " # " << dataSizeWritten << std::endl;
107          delete (char*)imageData;
108          delete (char*)imageDataWritten;
109          delete header;
110          delete file;
111          delete reread;
112          return 1;
113       }
114
115       if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
116       {
117          (void)res;
118          std::cout << std::endl
119             << "        Pixel differ (as expanded in memory)." << std::endl;
120          delete (char*)imageData;
121          delete (char*)imageDataWritten;
122          delete header;
123          delete file;
124          delete reread;
125          return 1;
126       }
127       std::cout << "4...OK." << std::endl ;
128
129       //////////////// Clean up:
130       delete (char*)imageData;
131       delete (char*)imageDataWritten;
132       delete header;
133       delete file;
134       delete reread;
135    }
136
137   return 0;
138 }