]> Creatis software - gdcm.git/blob - Testing/TestReadWriteReadCompare.cxx
* Test/TestWriteRead.cxx and TestReadWrite.cxx merged (because of
[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     file->SetImageData(imageData, dataSize);
58     file->WriteDcmExplVR( "TestReadWriteReadCompare.dcm" );
59     std::cout << " 2...";
60     
61     //////////////// Step 3:
62
63     gdcmFile* reread = new gdcmFile( "TestReadWriteReadCompare.dcm",
64                                       false, true );
65     if( !reread->GetHeader()->IsReadable() )
66     {
67       std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
68                 << "written:" << filename << std::endl;
69       delete header;
70       delete file;
71       delete reread;
72       return 1;
73     }
74     std::cout << " 3...";
75     // For the next step:
76     int    dataSizeWritten = reread->GetImageDataSize();
77     void* imageDataWritten = reread->GetImageData();
78
79     //////////////// Step 4:
80  
81     if (dataSize != dataSizeWritten)
82     {
83        std::cout << std::endl
84           << "        Pixel areas lengths differ: "
85           << dataSize << " # " << dataSizeWritten << std::endl;
86        delete (char*)imageData;
87        delete (char*)imageDataWritten;
88        delete header;
89        delete file;
90        delete reread;
91        return 1;
92     }
93
94     if (int res=memcmp(imageData, imageDataWritten, dataSize) !=0)
95     {
96        std::cout << std::endl
97           << "        Pixel differ (as expanded in memory)." << std::endl;
98        delete (char*)imageData;
99        delete (char*)imageDataWritten;
100        delete header;
101        delete file;
102        delete reread;
103        return 1;
104     }
105     std::cout << " 4...OK." << std::endl ;
106
107     //////////////// Clean up:
108     delete (char*)imageData;
109     delete (char*)imageDataWritten;
110     delete header;
111     delete file;
112     delete reread;
113   }
114
115   return 0;
116 }