]> Creatis software - gdcm.git/blob - Testing/TestReadWriteReadCompare.cxx
* src/gdcmFile.cxx : now delete the PixelConvert instance.
[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    uint8_t* 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 header;
78       delete file;
79       delete reread;
80       return 1;
81    }
82
83    if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
84    {
85       (void)res;
86       std::cout << std::endl
87          << "        Pixel differ (as expanded in memory)." << std::endl;
88       delete header;
89       delete file;
90       delete reread;
91       return 1;
92    }
93    std::cout << "4...OK." << std::endl ;
94
95    //////////////// Clean up:
96    delete header;
97    delete file;
98    delete reread;
99
100    return 0;
101 }
102
103 int TestReadWriteReadCompare(int argc, char* argv[]) 
104 {
105    int result = 0;
106    if (argc == 3)
107    {
108       const std::string input = argv[1];
109       const std::string output = argv[2];
110       result += CompareInternal(input, output);
111    }
112    else if( argc > 3 || argc == 2 )
113    {
114       std::cerr << "Please read the manual" << std::endl;
115    }
116    else
117    {
118       std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
119       std::cout << "   For all images in gdcmData (and not blacklisted in "
120                    "Test/CMakeLists.txt)" << std::endl;
121       std::cout << "   apply the following multistep test: " << std::endl;
122       std::cout << "   step 1: parse the image (as gdcmHeader) and call"
123                 << " IsReadable(). " << std::endl;
124       std::cout << "   step 2: write the corresponding image in DICOM V3 "
125                 << "with explicit" << std::endl
126                 << "           Value Representation in temporary file "
127                 << "TestReadWriteReadCompare.dcm." << std::endl;
128       std::cout << "   step 3: read the image written on step2 and call "
129                 << " IsReadable(). " << std::endl;
130       std::cout << "   step 4: compare (in memory with memcmp) that the two "
131                 << "images " << std::endl
132                 << "           match (as expanded by gdcm)." << std::endl;
133    
134       int i = 0;
135       while( gdcmDataImages[i] != 0 )
136       {
137          std::string filename = GDCM_DATA_ROOT;
138          filename += "/";
139          filename += gdcmDataImages[i++];
140          result += CompareInternal(filename, "TestReadWriteReadCompare.dcm"); 
141       }
142    }
143    return result;
144 }