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