1 /*=========================================================================
4 Module: $RCSfile: VTKTestRead.cxx,v $
6 Date: $Date: 2005/02/09 15:31:15 $
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"
27 #include "vtkPNGWriter.h"
32 #include "gdcmDataImages.h"
34 #ifndef vtkFloatingPointType
35 #define vtkFloatingPointType float
38 int VTKReadTest(vtkTesting *t,vtkImageViewer *viewer,
39 std::string const &filename,
40 std::string const &referenceFileName)
42 int retVal = 0; //by default this is an error
46 t->AddArgument( GDCM_DATA_ROOT );
48 t->AddArgument( referenceFileName.c_str() );
50 t->AddArgument( "." );
52 vtkGdcmReader *reader = vtkGdcmReader::New();
53 reader->SetFileName( filename.c_str() );
57 reader->GetOutput()->GetScalarRange(range);
59 reader->GetOutput()->GetDimensions( dim );
64 viewer->SetInput ( reader->GetOutput() );
66 viewer->SetColorWindow (range[1] - range[0]);
67 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
69 viewer->SetSize(dim[0], dim[1]);
72 //For multifame dicom, take a snapshot of the center slice (+/- 1)
73 viewer->SetZSlice( dim[2] / 2 );
77 viewer->SetZSlice( 0 );
80 viewer->OffScreenRenderingOff();
82 viewer->SetInput(NULL);
85 //----------------------------------------------------------------------
86 // Transform the image to be RGB unsigned char, due to the requests in
87 // vtkTesting processing
88 // The pipeline is broken after each process to keep maximum of memory
89 vtkImageData *image=reader->GetOutput();
91 image->Register(NULL);
94 // Get the middle slice in the image
97 int *ext=image->GetExtent();
98 vtkImageClip *clip=vtkImageClip::New();
99 clip->SetInput(image);
100 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
101 ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
103 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
104 translat->SetInput(clip->GetOutput());
105 translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
107 image->UnRegister(NULL);
108 image=translat->GetOutput();
110 image->Register(NULL);
111 translat->SetOutput(NULL);
116 // Set an unsigned char image
117 // Shift/Scale the image
118 vtkImageShiftScale *iss=vtkImageShiftScale::New();
119 iss->SetInput(image);
120 iss->SetOutputScalarTypeToUnsignedChar();
121 iss->SetShift(-range[0]);
122 iss->SetScale(255.0/(range[1]-range[0]));
123 iss->ClampOverflowOn();
125 image->UnRegister(NULL);
126 image=iss->GetOutput();
128 image->Register(NULL);
131 // Set 3 components to the image
132 if(image->GetNumberOfScalarComponents()==1)
134 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
139 image->UnRegister(NULL);
140 image=x3->GetOutput();
142 image->Register(NULL);
147 //----------------------------------------------------------------------
151 retVal = t->RegressionTest(image,0.0,str);
152 image->UnRegister(NULL);
154 if( retVal != vtkTesting::PASSED )
156 std::cerr << str.str();
158 str.rdbuf()->freeze(1);
160 if( retVal == vtkTesting::PASSED )
162 std::cerr << " ... OK" << std::endl;
167 std::cerr << " ... Failed" << std::endl;
173 int VTKTestRead(int argc, char *argv[])
178 if( std::string(argv[1]) == "-V" )
185 vtkTesting *t = vtkTesting::New();
186 vtkImageViewer *viewer;
188 viewer = vtkImageViewer::New();
194 std::cerr << "Usage: " << argv[0] << " [-V] image.dcm ref.png\n";
195 std::cerr << " -V : to view images to the screen... \n"
196 << " this mode can generate errors in the test\n\n";
200 ret = VTKReadTest(t,viewer,argv[1+show],argv[2+show]);
208 // Test for all images
210 while( gdcmDataImages[i] != 0 )
212 std::string filename = GDCM_DATA_ROOT;
213 filename += "/"; //doh!
214 filename += gdcmDataImages[i];
215 std::cerr << "Filename: " << filename << std::endl;
217 //Extract name to find the png file matching:
218 std::string pngfile = gdcmDataImages[i++];
219 //pngfile.replace(pngfile.size()-3, 3, "png");
220 //More robust approach:
221 std::string::size_type dot_pos = pngfile.rfind( "." );
222 pngfile = pngfile.substr(0, dot_pos).append( ".png" );
223 pngfile.insert( 0, "Baseline/");
224 //std::cerr << "PNG file: " << pngfile << std::endl;
226 ret += VTKReadTest(t,viewer,filename,pngfile);