]> Creatis software - gdcm.git/blob - Testing/TestReadWriteReadCompare.cxx
FIX: Could not compile with warnings flags on
[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     std::cerr << "Test::TestReadWriteReadCompare: Usage: " << argv[0]
11               << " (no arguments needed)." << std::endl;
12    }
13    
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;
29
30   int i = 0;
31   while( gdcmDataImages[i] != 0 )
32     {
33     std::string filename = GDCM_DATA_ROOT;
34     filename += "/";  //doh!
35     filename += gdcmDataImages[i++];
36    
37     std::cout << "   Testing: " << filename << std::endl;
38
39     //////////////// Step 1 (see above description):
40
41     gdcmHeader *header = new gdcmHeader( filename.c_str(), false, true );
42     if( !header->IsReadable() )
43       {
44       std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
45                 << filename << std::endl;
46       delete header;
47       return 0;
48       }
49     std::cout << "           step 1 ...";
50
51     //////////////// Step 2:
52
53     gdcmFile*  file = new gdcmFile( header );
54     int dataSize    = file->GetImageDataSize();
55     void* imageData = file->GetImageData(); //EXTREMELY IMPORTANT
56
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
60     /// we call
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" );
72     std::cout << "2...";
73     
74     //////////////// Step 3:
75
76     gdcmFile* reread = new gdcmFile( "TestReadWriteReadCompare.dcm",
77                                       false, true );
78     if( !reread->GetHeader()->IsReadable() )
79     {
80       std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
81                 << "written:" << filename << std::endl;
82       delete header;
83       delete file;
84       delete reread;
85       return 1;
86     }
87     std::cout << "3...";
88     // For the next step:
89     int    dataSizeWritten = reread->GetImageDataSize();
90     void* imageDataWritten = reread->GetImageData();
91
92     //////////////// Step 4:
93  
94     if (dataSize != dataSizeWritten)
95     {
96        std::cout << std::endl
97           << "        Pixel areas lengths differ: "
98           << dataSize << " # " << dataSizeWritten << std::endl;
99        delete (char*)imageData;
100        delete (char*)imageDataWritten;
101        delete header;
102        delete file;
103        delete reread;
104        return 1;
105     }
106
107     if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
108     {
109        (void)res;
110        std::cout << std::endl
111           << "        Pixel differ (as expanded in memory)." << std::endl;
112        delete (char*)imageData;
113        delete (char*)imageDataWritten;
114        delete header;
115        delete file;
116        delete reread;
117        return 1;
118     }
119     std::cout << "4...OK." << std::endl ;
120
121     //////////////// Clean up:
122     delete (char*)imageData;
123     delete (char*)imageDataWritten;
124     delete header;
125     delete file;
126     delete reread;
127   }
128
129   return 0;
130 }