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