]> Creatis software - gdcm.git/blob - Testing/TestReadWriteReadCompare.cxx
* Minor coding-style clean up
[gdcm.git] / Testing / TestReadWriteReadCompare.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:46 $
7   Version:   $Revision: 1.25 $
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::File *file = new gdcm::File( );
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       delete file;
40       return 1;
41    }
42    std::cout << "           step 1...";
43
44    //////////////// Step 2:
45    gdcm::FileHelper *filehelper = new gdcm::FileHelper( 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::File *fileout = new gdcm::File();
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      delete file;
70      delete filehelper;
71      return 1;
72    }
73
74    gdcm::FileHelper *reread = new gdcm::FileHelper( fileout );
75
76    std::cout << "3...";
77    // For the next step:
78    int    dataSizeWritten = reread->GetImageDataSize();
79    uint8_t *imageDataWritten = reread->GetImageData();
80
81    //////////////// Step 4:
82    // Test the image size
83    if (file->GetXSize() != reread->GetFile()->GetXSize() ||
84        file->GetYSize() != reread->GetFile()->GetYSize() ||
85        file->GetZSize() != reread->GetFile()->GetZSize())
86    {
87       std::cout << "Failed" << std::endl
88          << "        X Size differs: "
89          << "X: " << file->GetXSize() << " # " 
90                   << reread->GetFile()->GetXSize() << " | "
91          << "Y: " << file->GetYSize() << " # " 
92                   << reread->GetFile()->GetYSize() << " | "
93          << "Z: " << file->GetZSize() << " # " 
94                   << reread->GetFile()->GetZSize() << std::endl;
95       delete file;
96       delete filehelper;
97       delete reread;
98       return 1;
99    }
100
101    // Test the data size
102    if (dataSize != dataSizeWritten)
103    {
104       std::cout << "Failed" << std::endl
105          << "        Pixel areas lengths differ: "
106          << dataSize << " # " << dataSizeWritten << std::endl;
107       delete file;
108       delete filehelper;
109       delete reread;
110       return 1;
111    }
112
113    // Test the data's content
114    if (memcmp(imageData, imageDataWritten, dataSize) !=0)
115    {
116       std::cout << "Failed" << std::endl
117          << "        Pixel differ (as expanded in memory)." << std::endl;
118       delete file;
119       delete filehelper;
120       delete reread;
121       return 1;
122    }
123    std::cout << "4...OK." << std::endl ;
124
125    //////////////// Clean up:
126    delete file;
127    delete filehelper;
128    delete reread;
129
130    return 0;
131 }
132
133 int TestReadWriteReadCompare(int argc, char *argv[]) 
134 {
135    int result = 0;
136
137    if (argc == 4)
138       gdcm::Debug::DebugOn();
139
140    if (argc >= 3)
141    {
142       const std::string input  = argv[1];
143       const std::string output = argv[2];
144       result += CompareInternal(input, output);
145    }
146    else if( argc > 4 || argc == 2 )
147    {
148       std::cout << "Please read the manual" << std::endl;
149    }
150    else
151    {
152       std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
153       std::cout << "   For all images in gdcmData (and not blacklisted in "
154                    "Test/CMakeLists.txt)" << std::endl;
155       std::cout << "   apply the following multistep test: " << std::endl;
156       std::cout << "   step 1: parse the image (as gdcmFile) and call"
157                 << " IsReadable(). " << std::endl;
158       std::cout << "   step 2: write the corresponding image in DICOM V3 "
159                 << "with explicit" << std::endl
160                 << "           Value Representation in temporary file "
161                 << "TestReadWriteReadCompare.dcm." << std::endl;
162       std::cout << "   step 3: read the image written on step2 and call "
163                 << " IsReadable(). " << std::endl;
164       std::cout << "   step 4: compare (in memory with memcmp) that the two "
165                 << "images " << std::endl
166                 << "           match (as expanded by gdcm)." << std::endl;
167    
168       int i = 0;
169       while( gdcmDataImages[i] != 0 )
170       {
171          std::string filename = GDCM_DATA_ROOT;
172          filename += "/";
173          filename += gdcmDataImages[i++];
174          result += CompareInternal(filename, "TestReadWriteReadCompare.dcm"); 
175       }
176    }
177    return result;
178 }