1 /*=========================================================================
4 Module: $RCSfile: VTKTestReadSeq.cxx,v $
6 Date: $Date: 2005/01/19 10:49:47 $
7 Version: $Revision: 1.2 $
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.
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.
17 =========================================================================*/
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"
31 #include "gdcmDataSeqImages.h"
33 #ifndef vtkFloatingPointType
34 #define vtkFloatingPointType float
37 int VTKReadSeqTest(vtkTesting *t,vtkImageViewer *viewer,
38 std::string const & filename,
39 std::string const & referenceFileName,
42 int retVal = 0; //by default this is an error
45 vtkGdcmReader *reader = vtkGdcmReader::New();
47 char *newFileDcm = new char[filename.size()+1];
52 sprintf(newFileDcm,filename.c_str(),i);
54 // Test the existance of the file
55 ifstream opened(newFileDcm,std::ios::in | std::ios::binary);
58 reader->AddFileName(newFileDcm);
69 viewer->SetInput ( reader->GetOutput() );
70 viewer->OffScreenRenderingOff();
72 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
73 viewer->SetColorWindow (range[1] - range[0]);
74 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
77 reader->GetOutput()->GetDimensions( dim );
78 viewer->SetSize(dim[0], dim[1]);
79 viewer->SetZSlice( 0 );
81 viewer->SetInput(NULL);
87 char *newFilePng = new char[referenceFileName.size()+1];
88 for(int j=0;j<fileCount;j++)
90 sprintf(newFilePng,referenceFileName.c_str(),j);
94 t->AddArgument( GDCM_DATA_ROOT );
96 t->AddArgument( newFilePng );
98 t->AddArgument( "." );
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();
106 image->Register(NULL);
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);
117 image->UnRegister(NULL);
118 image=translat->GetOutput();
120 image->Register(NULL);
121 translat->SetOutput(NULL);
125 // Set an unsigned char image
126 // Shift/Scale the image
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();
136 image->UnRegister(NULL);
137 image=iss->GetOutput();
139 image->Register(NULL);
142 // Set 3 components to the image
143 if(image->GetNumberOfScalarComponents()==1)
145 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
150 image->UnRegister(NULL);
151 image=x3->GetOutput();
153 image->Register(NULL);
158 //----------------------------------------------------------------------
161 retVal = t->RegressionTest(image,2.0,str);
162 image->UnRegister(NULL);
164 if( retVal != vtkTesting::PASSED )
166 std::cerr << str.str();
168 str.rdbuf()->freeze(1);
170 if( retVal == vtkTesting::PASSED )
172 std::cerr << " ...Slice " << j << ": OK" << std::endl;
176 std::cerr << " ...Slice " << j << ": Failed" << std::endl;
187 int VTKTestReadSeq(int argc, char *argv[])
192 if( std::string(argv[1]) == "-V" )
199 vtkTesting* t = vtkTesting::New();
200 vtkImageViewer *viewer;
202 viewer = vtkImageViewer::New();
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";
216 ret = VTKReadSeqTest(t,viewer,argv[1+show],argv[2+show],show);
223 // Test for all images
225 while( gdcmDataSeqImages[i] != 0 )
227 std::string filename = GDCM_DATA_ROOT;
228 filename += "/"; //doh!
229 filename += gdcmDataSeqImages[i];
230 std::cerr << "Filename: " << filename << std::endl;
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/");
238 ret += VTKReadSeqTest(t,viewer,filename,pngfile,show);