]> Creatis software - gdcm.git/blob - Testing/VTKTestReadSeq.cxx
* Test/VTKTest*.cxx : bug fix. Now the viewer is used only when the
[gdcm.git] / Testing / VTKTestReadSeq.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: VTKTestReadSeq.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/19 10:49:47 $
7   Version:   $Revision: 1.2 $
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 "vtkGdcmReader.h"
20 #include "vtkImageViewer.h"
21 #include "vtkImageData.h"
22 #include "vtkRegressionTestImage.h"
23 #include "vtkImageClip.h"
24 #include "vtkImageTranslateExtent.h"
25 #include "vtkImageAppendComponents.h"
26 #include "vtkImageShiftScale.h"
27
28 #include <iostream>
29
30 //Generated file:
31 #include "gdcmDataSeqImages.h"
32
33 #ifndef vtkFloatingPointType
34 #define vtkFloatingPointType float
35 #endif
36
37 int VTKReadSeqTest(vtkTesting *t,vtkImageViewer *viewer,
38                    std::string const & filename, 
39                    std::string const & referenceFileName,
40                    bool show )
41 {
42    int retVal = 0;  //by default this is an error
43
44    // Set the reader   
45    vtkGdcmReader *reader = vtkGdcmReader::New();
46
47    char *newFileDcm = new char[filename.size()+1];
48    int fileCount = 0;
49    for(int i=0;i<9;i++)
50    {
51       fileCount = i;
52       sprintf(newFileDcm,filename.c_str(),i);
53
54       // Test the existance of the file
55       ifstream opened(newFileDcm,std::ios::in | std::ios::binary);
56       if(opened)
57       {
58          reader->AddFileName(newFileDcm);
59          opened.close();
60       }
61       else
62          break;
63    }
64    delete[] newFileDcm;
65    reader->Update();
66
67    if( show )
68    {
69       viewer->SetInput ( reader->GetOutput() );
70       viewer->OffScreenRenderingOff();
71
72       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
73       viewer->SetColorWindow (range[1] - range[0]);
74       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
75
76       int dim[3];
77       reader->GetOutput()->GetDimensions( dim );
78       viewer->SetSize(dim[0], dim[1]);
79       viewer->SetZSlice( 0 );
80       viewer->Render();
81       viewer->SetInput(NULL);
82    }
83
84    // make test
85    int ret = 0;
86    ostrstream str;
87    char *newFilePng = new char[referenceFileName.size()+1];
88    for(int j=0;j<fileCount;j++)
89    {
90       sprintf(newFilePng,referenceFileName.c_str(),j);
91
92       t->CleanArguments();
93       t->AddArgument("-D");
94       t->AddArgument( GDCM_DATA_ROOT );
95       t->AddArgument("-V");
96       t->AddArgument( newFilePng );
97       t->AddArgument("-T");
98       t->AddArgument( "." );
99
100       //----------------------------------------------------------------------
101       // Transform the image to be RGB unsigned char, due to the requests in
102       // vtkTesting processing
103       // The pipeline is broken after each process to keep maximum of memory
104       vtkImageData *image=reader->GetOutput();
105       image->Update();
106       image->Register(NULL);
107
108       // Get the middle slice in the image
109       int *ext=image->GetExtent();
110       vtkImageClip *clip=vtkImageClip::New();
111       clip->SetInput(image);
112       clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],j,j);
113       vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
114       translat->SetInput(clip->GetOutput());
115       translat->SetTranslation(0,0,-j);
116
117       image->UnRegister(NULL);
118       image=translat->GetOutput();
119       image->Update();
120       image->Register(NULL);
121       translat->SetOutput(NULL);
122       clip->Delete();
123       translat->Delete();
124
125       // Set an unsigned char image
126       // Shift/Scale the image 
127       image->Update();
128       double *rng=image->GetScalarRange();
129       vtkImageShiftScale *iss=vtkImageShiftScale::New();
130       iss->SetInput(image);
131       iss->SetOutputScalarTypeToUnsignedChar();
132       iss->SetShift(-rng[0]);
133       iss->SetScale(255.0/(rng[1]-rng[0]));
134       iss->ClampOverflowOn();
135
136       image->UnRegister(NULL);
137       image=iss->GetOutput();
138       image->Update();
139       image->Register(NULL);
140       iss->Delete();
141
142       // Set 3 components to the image
143       if(image->GetNumberOfScalarComponents()==1)
144       {
145          vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
146          x3->AddInput(image);
147          x3->AddInput(image);
148          x3->AddInput(image);
149
150          image->UnRegister(NULL);
151          image=x3->GetOutput();
152          image->Update();
153          image->Register(NULL);
154          x3->SetOutput(NULL);
155          x3->Delete();
156       }
157       // End of transform
158       //----------------------------------------------------------------------
159
160       // make test
161       retVal = t->RegressionTest(image,2.0,str);
162       image->UnRegister(NULL);
163
164       if( retVal != vtkTesting::PASSED )
165       {
166          std::cerr << str.str();
167       }
168       str.rdbuf()->freeze(1);
169
170       if( retVal == vtkTesting::PASSED )
171       {
172          std::cerr << "       ...Slice " << j << ": OK" << std::endl;
173       }
174       else
175       {
176          std::cerr << "       ...Slice " << j << ": Failed" << std::endl;
177          ret++;
178       }
179    }
180
181    delete[] newFilePng;
182    reader->Delete();
183
184    return ret;
185 }
186
187 int VTKTestReadSeq(int argc, char *argv[])
188 {
189    bool show = false;
190    if( argc >= 2 )
191    {
192       if( std::string(argv[1]) == "-V" )
193       {
194          show = true;
195       }
196    }
197
198    int ret = 0;
199    vtkTesting* t = vtkTesting::New();
200    vtkImageViewer *viewer;
201    if( show )
202       viewer = vtkImageViewer::New();
203    else
204       viewer = NULL;
205
206    if( argc < 3+show )
207    {
208       std::cerr << "Usage: " << argv[0] << " [-V] image%d.dcm ref%d.png\n";
209       std::cerr << "   -V : to view images to the screen... \n"
210                 << "        this mode can generate errors in the test\n";
211       std::cerr << "   %d : this will be replaced by a number at execution.\n"
212                 << "        It will be from 0 to 9 only with a step of 1\n\n";
213    }
214    else
215    {
216       ret = VTKReadSeqTest(t,viewer,argv[1+show],argv[2+show],show);
217       t->Delete();
218       viewer->Delete();
219
220       return ret;
221    }
222
223    // Test for all images
224    int i = 0;
225    while( gdcmDataSeqImages[i] != 0 )
226    {
227       std::string filename = GDCM_DATA_ROOT;
228       filename += "/";  //doh!
229       filename += gdcmDataSeqImages[i];
230       std::cerr << "Filename: " << filename << std::endl;
231
232       //Extract name to find the png file matching:
233       std::string pngfile = gdcmDataSeqImages[i++];
234       std::string::size_type dot_pos = pngfile.rfind( "." );
235       pngfile = pngfile.substr(0, dot_pos).append( ".png" );
236       pngfile.insert( 0, "Baseline/");
237       
238       ret += VTKReadSeqTest(t,viewer,filename,pngfile,show);
239    }
240    t->Delete();
241    if(viewer)
242       viewer->Delete();
243
244    return ret;
245 }