1 /*=========================================================================
4 Module: $RCSfile: VTKTestReadSeq.cxx,v $
6 Date: $Date: 2007/09/18 07:58:38 $
7 Version: $Revision: 1.12 $
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"
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)
41 int retVal; // = 0; (to avoid bcc 5.5 warnings)
44 vtkGdcmReader *reader = vtkGdcmReader::New();
46 char *newFileDcm = new char[filename.size()+1];
51 sprintf(newFileDcm,filename.c_str(),i);
53 // Test the existance of the file
54 ifstream opened(newFileDcm,std::ios::in | std::ios::binary);
57 reader->AddFileName(newFileDcm);
67 reader->GetOutput()->GetScalarRange(range);
72 viewer->SetInput ( reader->GetOutput() );
73 viewer->OffScreenRenderingOff();
75 viewer->SetColorWindow (range[1] - range[0]);
76 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
79 reader->GetOutput()->GetDimensions( dim );
80 viewer->SetSize(dim[0], dim[1]);
81 viewer->SetZSlice( 0 );
83 viewer->SetInput(NULL);
88 std::ostringstream str;
89 char *newFilePng = new char[referenceFileName.size()+1];
90 for(int j=0;j<fileCount;j++)
92 sprintf(newFilePng,referenceFileName.c_str(),j);
96 t->AddArgument( GDCM_DATA_ROOT );
98 t->AddArgument( newFilePng );
100 t->AddArgument( "." );
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();
108 image->Register(NULL);
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);
116 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
117 translat->SetInput(clip->GetOutput());
118 translat->SetTranslation(0,0,-j);
120 image->UnRegister(NULL);
121 image=translat->GetOutput();
123 image->Register(NULL);
124 translat->SetOutput(NULL);
128 // Set an unsigned char image
129 // Shift/Scale the image
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();
138 image->UnRegister(NULL);
139 image=iss->GetOutput();
141 image->Register(NULL);
144 // Set 3 components to the image
145 if(image->GetNumberOfScalarComponents()==1)
147 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
152 image->UnRegister(NULL);
153 image=x3->GetOutput();
155 image->Register(NULL);
160 //----------------------------------------------------------------------
163 retVal = t->RegressionTest(image,2.0,str);
164 image->UnRegister(NULL);
166 if( retVal != vtkTesting::PASSED )
168 std::cerr << str.str();
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);