]> Creatis software - gdcm.git/blob - Testing/TestReadWriteReadCompare.cxx
GDCM_NAME_SPACE
[gdcm.git] / Testing / TestReadWriteReadCompare.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/21 14:59:06 $
7   Version:   $Revision: 1.29 $
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 #include "gdcmDebug.h"
21
22 //Generated file:
23 #include "gdcmDataImages.h"
24
25 int CompareInternal(std::string const &filename, std::string const &output)
26 {
27    std::cout << "   Testing: " << filename << std::endl;
28
29    //////////////// Step 1 (see above description):
30
31    GDCM_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New( );
32    file->SetFileName( filename );
33    file->Load ();
34    if( !file->IsReadable() )
35    {
36       std::cout << "Failed" << std::endl
37                 << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
38                 << filename << std::endl;
39       file->Delete();
40       return 1;
41    }
42    std::cout << "           step 1...";
43
44    //////////////// Step 2:
45    GDCM_NAME_SPACE::FileHelper *filehelper = GDCM_NAME_SPACE::FileHelper::New( file );
46    int dataSize    = filehelper->GetImageDataSize();
47    uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
48           // Sure, it is : It's up to the user to decide if he wants to
49           // GetImageData or if he wants to GetImageDataRaw
50           // (even if we do it by setting a flag, he will have to decide) 
51
52    //filehelper->SetImageData(imageData, dataSize);
53    
54    filehelper->SetWriteModeToRGB();
55    filehelper->WriteDcmExplVR( output );
56    std::cout << "2...";
57  
58    //////////////// Step 3:
59    GDCM_NAME_SPACE::File *fileout = GDCM_NAME_SPACE::File::New();
60    fileout->SetFileName( output );
61    fileout->Load();
62   // gdcm::FileHelper *reread = new gdcm::FileHelper( output ); // deprecated
63
64    if( !fileout->IsReadable() )
65    {
66       std::cout << "Failed" << std::endl
67                 << "Test::TestReadWriteReadCompare: Could not parse the newly "
68                 << "written image:" << filename << std::endl;
69       file->Delete();
70       filehelper->Delete();
71       fileout->Delete();
72       return 1;
73    }
74
75    GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
76
77    std::cout << "3...";
78    // For the next step:
79    int    dataSizeWritten = reread->GetImageDataSize();
80    uint8_t *imageDataWritten = reread->GetImageData();
81
82    //////////////// Step 4:
83    // Test the image size
84    if (file->GetXSize() != reread->GetFile()->GetXSize() ||
85        file->GetYSize() != reread->GetFile()->GetYSize() ||
86        file->GetZSize() != reread->GetFile()->GetZSize())
87    {
88       std::cout << "Failed" << std::endl
89          << "        X Size differs: "
90          << "X: " << file->GetXSize() << " # " 
91                   << reread->GetFile()->GetXSize() << " | "
92          << "Y: " << file->GetYSize() << " # " 
93                   << reread->GetFile()->GetYSize() << " | "
94          << "Z: " << file->GetZSize() << " # " 
95                   << reread->GetFile()->GetZSize() << std::endl;
96       file->Delete();
97       filehelper->Delete();
98       fileout->Delete();
99       reread->Delete();
100       return 1;
101    }
102
103    // Test the data size
104    if (dataSize != dataSizeWritten)
105    {
106       std::cout << "Failed" << std::endl
107          << "        Pixel areas lengths differ: "
108          << dataSize << " # " << dataSizeWritten << std::endl;
109       file->Delete();
110       filehelper->Delete();
111       fileout->Delete();
112       reread->Delete();
113       return 1;
114    }
115
116    // Test the data's content
117    if (memcmp(imageData, imageDataWritten, dataSize) !=0)
118    {
119       std::cout << "Failed" << std::endl
120          << "        Pixel differ (as expanded in memory)." << std::endl;
121       file->Delete();
122       filehelper->Delete();
123       fileout->Delete();
124       reread->Delete();
125       return 1;
126    }
127    std::cout << "4...OK." << std::endl ;
128
129    //////////////// Clean up:
130    file->Delete();
131    filehelper->Delete();
132    fileout->Delete();
133    reread->Delete();
134
135    return 0;
136 }
137
138 int TestReadWriteReadCompare(int argc, char *argv[]) 
139 {
140    int result = 0;
141
142    if (argc == 4)
143       GDCM_NAME_SPACE::Debug::DebugOn();
144
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 > 4 || argc == 2 )
152    {
153       std::cout << "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 gdcmFile) 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 }