1 /*=========================================================================
4 Module: $RCSfile: VTKTestReadSeq.cxx,v $
6 Date: $Date: 2005/09/16 17:19:25 $
7 Version: $Revision: 1.11 $
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 =========================================================================*/
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"
30 #include "gdcmDataSeqImages.h"
32 #ifndef vtkFloatingPointType
33 #define vtkFloatingPointType float
36 int VTKReadSeqTest(vtkTesting *t, vtkImageViewer *viewer,
37 std::string const &filename,
38 std::string const &referenceFileName)
40 int retVal; // = 0; (to avoid bcc 5.5 warnings)
43 vtkGdcmReader *reader = vtkGdcmReader::New();
45 char *newFileDcm = new char[filename.size()+1];
50 sprintf(newFileDcm,filename.c_str(),i);
52 // Test the existance of the file
53 ifstream opened(newFileDcm,std::ios::in | std::ios::binary);
56 reader->AddFileName(newFileDcm);
66 reader->GetOutput()->GetScalarRange(range);
71 viewer->SetInput ( reader->GetOutput() );
72 viewer->OffScreenRenderingOff();
74 viewer->SetColorWindow (range[1] - range[0]);
75 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
78 reader->GetOutput()->GetDimensions( dim );
79 viewer->SetSize(dim[0], dim[1]);
80 viewer->SetZSlice( 0 );
82 viewer->SetInput(NULL);
88 char *newFilePng = new char[referenceFileName.size()+1];
89 for(int j=0;j<fileCount;j++)
91 sprintf(newFilePng,referenceFileName.c_str(),j);
95 t->AddArgument( GDCM_DATA_ROOT );
97 t->AddArgument( newFilePng );
99 t->AddArgument( "." );
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();
107 image->Register(NULL);
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);
115 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
116 translat->SetInput(clip->GetOutput());
117 translat->SetTranslation(0,0,-j);
119 image->UnRegister(NULL);
120 image=translat->GetOutput();
122 image->Register(NULL);
123 translat->SetOutput(NULL);
127 // Set an unsigned char image
128 // Shift/Scale the image
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();
137 image->UnRegister(NULL);
138 image=iss->GetOutput();
140 image->Register(NULL);
143 // Set 3 components to the image
144 if(image->GetNumberOfScalarComponents()==1)
146 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
151 image->UnRegister(NULL);
152 image=x3->GetOutput();
154 image->Register(NULL);
159 //----------------------------------------------------------------------
162 retVal = t->RegressionTest(image,2.0,str);
163 image->UnRegister(NULL);
165 if( retVal != vtkTesting::PASSED )
167 std::cerr << str.str();
169 str.rdbuf()->freeze(1);
171 if( retVal == vtkTesting::PASSED )
173 std::cerr << " ...Slice " << j << ": OK" << std::endl;
177 std::cerr << " ...Slice " << j << ": Failed" << std::endl;
188 int VTKTestReadSeq(int argc, char *argv[])
193 if( std::string(argv[1]) == "-V" )
200 vtkTesting *t = vtkTesting::New();
201 vtkImageViewer *viewer;
203 viewer = vtkImageViewer::New();
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";
217 ret = VTKReadSeqTest(t,viewer,argv[1+show],argv[2+show]);
225 // Test for all images
227 while( gdcmDataSeqImages[i] != 0 )
229 std::string filename = GDCM_DATA_ROOT;
230 filename += "/"; //doh!
231 filename += gdcmDataSeqImages[i];
232 std::cerr << "Filename: " << filename << std::endl;
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/");
240 ret += VTKReadSeqTest(t,viewer,filename,pngfile);