]> Creatis software - gdcm.git/blob - Testing/TestReadWriteReadCompare.cxx
ENH: Some fix, plus some cleanup
[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, false, true );
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
58       /// \todo Following line commented out because gdcmFile::SetImageData() is
59       /// brain dead: it sets ImageDataSize to its argument and PixelRead to a.
60       /// Later on, when writing gdcmFile::WriteBase() and because PixelRead == 1
61       /// we call
62       ///    PixelElement->SetLength( ImageDataSizeRaw );
63       /// where we use ImageDataSizeRAW instead of ImageDataSize !
64       /// But when the original image made the transformation LUT -> RGB, 
65       /// ImageDataSizeRaw is the third of ImageDataSize, and there is no
66       /// reason (since we called gdcmFile::SetImageData) to use the Raw image
67       /// size... This "bug" in gdcmFile made that we had to black list
68       /// images 8BitsUncompressedColor.dcm, OT-PAL-8-face.dcm and 
69       /// US-PAL-8-10x-echo.dcm...
70       /// In conclusion fix gdcmFile, and then uncomment the following line.
71       /// file->SetImageData(imageData, dataSize);
72       file->WriteDcmExplVR( "TestReadWriteReadCompare.dcm" );
73       std::cout << "2...";
74     
75       //////////////// Step 3:
76
77       gdcmFile* reread = new gdcmFile( "TestReadWriteReadCompare.dcm",
78                                       false, true );
79       if( !reread->GetHeader()->IsReadable() )
80       {
81         std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
82                   << "written:" << filename << std::endl;
83         delete header;
84         delete file;
85         delete reread;
86         return 1;
87       }
88       std::cout << "3...";
89       // For the next step:
90       int    dataSizeWritten = reread->GetImageDataSize();
91       void* imageDataWritten = reread->GetImageData();
92
93       //////////////// Step 4:
94  
95       if (dataSize != dataSizeWritten)
96       {
97          std::cout << std::endl
98             << "        Pixel areas lengths differ: "
99             << dataSize << " # " << dataSizeWritten << std::endl;
100          delete (char*)imageData;
101          delete (char*)imageDataWritten;
102          delete header;
103          delete file;
104          delete reread;
105          return 1;
106       }
107
108       if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
109       {
110          (void)res;
111          std::cout << std::endl
112             << "        Pixel differ (as expanded in memory)." << std::endl;
113          delete (char*)imageData;
114          delete (char*)imageDataWritten;
115          delete header;
116          delete file;
117          delete reread;
118          return 1;
119       }
120       std::cout << "4...OK." << std::endl ;
121
122       //////////////// Clean up:
123       delete (char*)imageData;
124       delete (char*)imageDataWritten;
125       delete header;
126       delete file;
127       delete reread;
128    }
129
130   return 0;
131 }