]> Creatis software - gdcm.git/blob - Testing/TestReadWriteReadCompare.cxx
First stage of name normalisation : gdcm::File replace by gdcm::FileHelper
[gdcm.git] / Testing / TestReadWriteReadCompare.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/20 16:16:59 $
7   Version:   $Revision: 1.18 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmHeader.h"
19 #include "gdcmFile.h"
20
21 //Generated file:
22 #include "gdcmDataImages.h"
23
24 int CompareInternal(std::string const & filename, std::string const & output)
25 {
26    std::cout << "   Testing: " << filename << std::endl;
27
28    //////////////// Step 1 (see above description):
29
30    gdcm::Header *header = new gdcm::Header( filename );
31    if( !header->IsReadable() )
32    {
33       std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
34                 << filename << std::endl;
35       delete header;
36       return 1;
37    }
38    std::cout << "           step 1...";
39
40    //////////////// Step 2:
41
42    gdcm::FileHelper*  file = new gdcm::FileHelper( header );
43    int dataSize    = file->GetImageDataSize();
44    uint8_t* imageData = file->GetImageData(); //EXTREMELY IMPORTANT
45           // Sure, it is : It's up to the user to decide if he wants to
46           // GetImageData or if he wants to GetImageDataRaw
47           // (even if we do it by setting a flag, he will have to decide) 
48
49    /// \todo Following line commented out because gdcmFile::SetImageData() is
50    /// brain dead: it sets ImageDataSize to its argument and PixelRead to a.
51    /// Later on, when writing gdcmFile::WriteBase() 
52    /// and because PixelRead == 1 we call
53    ///    PixelElement->SetLength( ImageDataSizeRaw );
54    /// where we use ImageDataSizeRAW instead of ImageDataSize !
55    /// But when the original image made the transformation LUT -> RGB, 
56    /// ImageDataSizeRaw is the third of ImageDataSize, and there is no
57    /// reason (since we called gdcmFile::SetImageData) to use the Raw image
58    /// size... This "bug" in gdcmFile made that we had to black list
59    /// images 8BitsUncompressedColor.dcm, OT-PAL-8-face.dcm and 
60    /// US-PAL-8-10x-echo.dcm...
61    /// In conclusion fix gdcmFile, and then uncomment the following line.
62    
63    // --> I did. ctest doesn't break. But ... is it enought to say it's OK ?
64    
65    file->SetImageData(imageData, dataSize);
66    
67    file->SetWriteModeToRGB();
68    file->WriteDcmExplVR( output );
69    std::cout << "2...";
70  
71    //////////////// Step 3:
72
73    gdcm::FileHelper* reread = new gdcm::FileHelper( output );
74    if( !reread->GetHeader()->IsReadable() )
75    {
76      std::cerr << "Failed" << std::endl
77                << "Test::TestReadWriteReadCompare: Could not reread image "
78                << "written:" << filename << std::endl;
79      delete header;
80      delete file;
81      delete reread;
82      return 1;
83    }
84    std::cout << "3...";
85    // For the next step:
86    int    dataSizeWritten = reread->GetImageDataSize();
87    uint8_t* imageDataWritten = reread->GetImageData();
88
89    //////////////// Step 4:
90    // Test the image size
91    if (header->GetXSize() != reread->GetHeader()->GetXSize() ||
92        header->GetYSize() != reread->GetHeader()->GetYSize() ||
93        header->GetZSize() != reread->GetHeader()->GetZSize())
94    {
95       std::cout << "Failed" << std::endl
96          << "        X Size differs: "
97          << "X: " << header->GetXSize() << " # " 
98                   << reread->GetHeader()->GetXSize() << " | "
99          << "Y: " << header->GetYSize() << " # " 
100                   << reread->GetHeader()->GetYSize() << " | "
101          << "Z: " << header->GetZSize() << " # " 
102                   << reread->GetHeader()->GetZSize() << std::endl;
103       delete header;
104       delete file;
105       delete reread;
106       return 1;
107    }
108
109    // Test the data size
110    if (dataSize != dataSizeWritten)
111    {
112       std::cout << "Failed" << std::endl
113          << "        Pixel areas lengths differ: "
114          << dataSize << " # " << dataSizeWritten << std::endl;
115       delete header;
116       delete file;
117       delete reread;
118       return 1;
119    }
120
121    // Test the data's content
122    if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
123    {
124       (void)res;
125       std::cout << "Failed" << std::endl
126          << "        Pixel differ (as expanded in memory)." << std::endl;
127       delete header;
128       delete file;
129       delete reread;
130       return 1;
131    }
132    std::cout << "4...OK." << std::endl ;
133
134    //////////////// Clean up:
135    delete header;
136    delete file;
137    delete reread;
138
139    return 0;
140 }
141
142 int TestReadWriteReadCompare(int argc, char* argv[]) 
143 {
144    int result = 0;
145    if (argc == 3)
146    {
147       const std::string input = argv[1];
148       const std::string output = argv[2];
149       result += CompareInternal(input, output);
150    }
151    else if( argc > 3 || argc == 2 )
152    {
153       std::cerr << "Please read the manual" << std::endl;
154    }
155    else
156    {
157       std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
158       std::cout << "   For all images in gdcmData (and not blacklisted in "
159                    "Test/CMakeLists.txt)" << std::endl;
160       std::cout << "   apply the following multistep test: " << std::endl;
161       std::cout << "   step 1: parse the image (as gdcmHeader) and call"
162                 << " IsReadable(). " << std::endl;
163       std::cout << "   step 2: write the corresponding image in DICOM V3 "
164                 << "with explicit" << std::endl
165                 << "           Value Representation in temporary file "
166                 << "TestReadWriteReadCompare.dcm." << std::endl;
167       std::cout << "   step 3: read the image written on step2 and call "
168                 << " IsReadable(). " << std::endl;
169       std::cout << "   step 4: compare (in memory with memcmp) that the two "
170                 << "images " << std::endl
171                 << "           match (as expanded by gdcm)." << std::endl;
172    
173       int i = 0;
174       while( gdcmDataImages[i] != 0 )
175       {
176          std::string filename = GDCM_DATA_ROOT;
177          filename += "/";
178          filename += gdcmDataImages[i++];
179          result += CompareInternal(filename, "TestReadWriteReadCompare.dcm"); 
180       }
181    }
182    return result;
183 }