]> Creatis software - gdcm.git/blob - Testing/VTKTestReadSeq.cxx
* Test/VTKTest*.cxx : one more bug stilled
[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 14:45:40 $
7   Version:   $Revision: 1.4 $
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    double *range = reader->GetOutput()->GetScalarRange();
68
69    // Show
70    if( show )
71    {
72       viewer->SetInput ( reader->GetOutput() );
73       viewer->OffScreenRenderingOff();
74
75       viewer->SetColorWindow (range[1] - range[0]);
76       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
77
78       int dim[3];
79       reader->GetOutput()->GetDimensions( dim );
80       viewer->SetSize(dim[0], dim[1]);
81       viewer->SetZSlice( 0 );
82       viewer->Render();
83       viewer->SetInput(NULL);
84    }
85
86    // make test
87    int ret = 0;
88    ostrstream str;
89    char *newFilePng = new char[referenceFileName.size()+1];
90    for(int j=0;j<fileCount;j++)
91    {
92       sprintf(newFilePng,referenceFileName.c_str(),j);
93
94       t->CleanArguments();
95       t->AddArgument("-D");
96       t->AddArgument( GDCM_DATA_ROOT );
97       t->AddArgument("-V");
98       t->AddArgument( newFilePng );
99       t->AddArgument("-T");
100       t->AddArgument( "." );
101
102       //----------------------------------------------------------------------
103       // Transform the image to be RGB unsigned char, due to the requests in
104       // vtkTesting processing
105       // The pipeline is broken after each process to keep maximum of memory
106       vtkImageData *image=reader->GetOutput();
107       image->Update();
108       image->Register(NULL);
109
110       // Get the middle slice in the image
111       int *ext=image->GetExtent();
112       vtkImageClip *clip=vtkImageClip::New();
113       clip->SetInput(image);
114       clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],j,j);
115       clip->ClipDataOn();
116       vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
117       translat->SetInput(clip->GetOutput());
118       translat->SetTranslation(0,0,-j);
119
120       image->UnRegister(NULL);
121       image=translat->GetOutput();
122       image->Update();
123       image->Register(NULL);
124       translat->SetOutput(NULL);
125       clip->Delete();
126       translat->Delete();
127
128       // Set an unsigned char image
129       // Shift/Scale the image 
130       image->Update();
131       vtkImageShiftScale *iss=vtkImageShiftScale::New();
132       iss->SetInput(image);
133       iss->SetOutputScalarTypeToUnsignedChar();
134       iss->SetShift(-range[0]);
135       iss->SetScale(255.0/(range[1]-range[0]));
136       iss->ClampOverflowOn();
137
138       image->UnRegister(NULL);
139       image=iss->GetOutput();
140       image->Update();
141       image->Register(NULL);
142       iss->Delete();
143
144       // Set 3 components to the image
145       if(image->GetNumberOfScalarComponents()==1)
146       {
147          vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
148          x3->AddInput(image);
149          x3->AddInput(image);
150          x3->AddInput(image);
151
152          image->UnRegister(NULL);
153          image=x3->GetOutput();
154          image->Update();
155          image->Register(NULL);
156          x3->SetOutput(NULL);
157          x3->Delete();
158       }
159       // End of transform
160       //----------------------------------------------------------------------
161
162       // make test
163       retVal = t->RegressionTest(image,2.0,str);
164       image->UnRegister(NULL);
165
166       if( retVal != vtkTesting::PASSED )
167       {
168          std::cerr << str.str();
169       }
170       str.rdbuf()->freeze(1);
171
172       if( retVal == vtkTesting::PASSED )
173       {
174          std::cerr << "       ...Slice " << j << ": OK" << std::endl;
175       }
176       else
177       {
178          std::cerr << "       ...Slice " << j << ": Failed" << std::endl;
179          ret++;
180       }
181    }
182
183    delete[] newFilePng;
184    reader->Delete();
185
186    return ret;
187 }
188
189 int VTKTestReadSeq(int argc, char *argv[])
190 {
191    bool show = false;
192    if( argc >= 2 )
193    {
194       if( std::string(argv[1]) == "-V" )
195       {
196          show = true;
197       }
198    }
199
200    int ret = 0;
201    vtkTesting* t = vtkTesting::New();
202    vtkImageViewer *viewer;
203    if( show )
204       viewer = vtkImageViewer::New();
205    else
206       viewer = NULL;
207
208    if( argc < 3+show )
209    {
210       std::cerr << "Usage: " << argv[0] << " [-V] image%d.dcm ref%d.png\n";
211       std::cerr << "   -V : to view images to the screen... \n"
212                 << "        this mode can generate errors in the test\n";
213       std::cerr << "   %d : this will be replaced by a number at execution.\n"
214                 << "        It will be from 0 to 9 only with a step of 1\n\n";
215    }
216    else
217    {
218       ret = VTKReadSeqTest(t,viewer,argv[1+show],argv[2+show],show);
219       t->Delete();
220       if( viewer )
221          viewer->Delete();
222
223       return ret;
224    }
225
226    // Test for all images
227    int i = 0;
228    while( gdcmDataSeqImages[i] != 0 )
229    {
230       std::string filename = GDCM_DATA_ROOT;
231       filename += "/";  //doh!
232       filename += gdcmDataSeqImages[i];
233       std::cerr << "Filename: " << filename << std::endl;
234
235       //Extract name to find the png file matching:
236       std::string pngfile = gdcmDataSeqImages[i++];
237       std::string::size_type dot_pos = pngfile.rfind( "." );
238       pngfile = pngfile.substr(0, dot_pos).append( ".png" );
239       pngfile.insert( 0, "Baseline/");
240       
241       ret += VTKReadSeqTest(t,viewer,filename,pngfile,show);
242    }
243    t->Delete();
244    if( viewer )
245       viewer->Delete();
246
247    return ret;
248 }