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