]> Creatis software - gdcm.git/blob - Testing/TestReadWriteReadCompare.cxx
* Test/ : accelerate tests (it's not very significant accelerations).
[gdcm.git] / Testing / TestReadWriteReadCompare.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/09 15:06:48 $
7   Version:   $Revision: 1.22 $
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 "gdcmFile.h"
19 #include "gdcmFileHelper.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::File *file = new gdcm::File( filename );
31    if( !file->IsReadable() )
32    {
33       std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
34                 << filename << std::endl;
35       delete file;
36       return 1;
37    }
38    std::cout << "           step 1...";
39
40    //////////////// Step 2:
41    gdcm::FileHelper *filehelper = new gdcm::FileHelper( file );
42    int dataSize    = filehelper->GetImageDataSize();
43    uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
44           // Sure, it is : It's up to the user to decide if he wants to
45           // GetImageData or if he wants to GetImageDataRaw
46           // (even if we do it by setting a flag, he will have to decide) 
47
48    //filehelper->SetImageData(imageData, dataSize);
49    
50    filehelper->SetWriteModeToRGB();
51    filehelper->WriteDcmExplVR( output );
52    std::cout << "2...";
53  
54    //////////////// Step 3:
55    gdcm::FileHelper *reread = new gdcm::FileHelper( output );
56    if( !reread->GetFile()->IsReadable() )
57    {
58      std::cerr << "Failed" << std::endl
59                << "Test::TestReadWriteReadCompare: Could not reread image "
60                << "written:" << filename << std::endl;
61      delete file;
62      delete filehelper;
63      delete reread;
64      return 1;
65    }
66    std::cout << "3...";
67    // For the next step:
68    int    dataSizeWritten = reread->GetImageDataSize();
69    uint8_t *imageDataWritten = reread->GetImageData();
70
71    //////////////// Step 4:
72    // Test the image size
73    if (file->GetXSize() != reread->GetFile()->GetXSize() ||
74        file->GetYSize() != reread->GetFile()->GetYSize() ||
75        file->GetZSize() != reread->GetFile()->GetZSize())
76    {
77       std::cout << "Failed" << std::endl
78          << "        X Size differs: "
79          << "X: " << file->GetXSize() << " # " 
80                   << reread->GetFile()->GetXSize() << " | "
81          << "Y: " << file->GetYSize() << " # " 
82                   << reread->GetFile()->GetYSize() << " | "
83          << "Z: " << file->GetZSize() << " # " 
84                   << reread->GetFile()->GetZSize() << std::endl;
85       delete file;
86       delete filehelper;
87       delete reread;
88       return 1;
89    }
90
91    // Test the data size
92    if (dataSize != dataSizeWritten)
93    {
94       std::cout << "Failed" << std::endl
95          << "        Pixel areas lengths differ: "
96          << dataSize << " # " << dataSizeWritten << std::endl;
97       delete file;
98       delete filehelper;
99       delete reread;
100       return 1;
101    }
102
103    // Test the data's content
104    if (memcmp(imageData, imageDataWritten, dataSize) !=0)
105    {
106       std::cout << "Failed" << std::endl
107          << "        Pixel differ (as expanded in memory)." << std::endl;
108       delete file;
109       delete filehelper;
110       delete reread;
111       return 1;
112    }
113    std::cout << "4...OK." << std::endl ;
114
115    //////////////// Clean up:
116    delete file;
117    delete filehelper;
118    delete reread;
119
120    return 0;
121 }
122
123 int TestReadWriteReadCompare(int argc, char *argv[]) 
124 {
125    int result = 0;
126    if (argc == 3)
127    {
128       const std::string input = argv[1];
129       const std::string output = argv[2];
130       result += CompareInternal(input, output);
131    }
132    else if( argc > 3 || argc == 2 )
133    {
134       std::cerr << "Please read the manual" << std::endl;
135    }
136    else
137    {
138       std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
139       std::cout << "   For all images in gdcmData (and not blacklisted in "
140                    "Test/CMakeLists.txt)" << std::endl;
141       std::cout << "   apply the following multistep test: " << std::endl;
142       std::cout << "   step 1: parse the image (as gdcmFile) and call"
143                 << " IsReadable(). " << std::endl;
144       std::cout << "   step 2: write the corresponding image in DICOM V3 "
145                 << "with explicit" << std::endl
146                 << "           Value Representation in temporary file "
147                 << "TestReadWriteReadCompare.dcm." << std::endl;
148       std::cout << "   step 3: read the image written on step2 and call "
149                 << " IsReadable(). " << std::endl;
150       std::cout << "   step 4: compare (in memory with memcmp) that the two "
151                 << "images " << std::endl
152                 << "           match (as expanded by gdcm)." << std::endl;
153    
154       int i = 0;
155       while( gdcmDataImages[i] != 0 )
156       {
157          std::string filename = GDCM_DATA_ROOT;
158          filename += "/";
159          filename += gdcmDataImages[i++];
160          result += CompareInternal(filename, "TestReadWriteReadCompare.dcm"); 
161       }
162    }
163    return result;
164 }