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