1 /*=========================================================================
4 Module: $RCSfile: VTKTestRead.cxx,v $
6 Date: $Date: 2005/01/19 14:45:40 $
7 Version: $Revision: 1.4 $
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 "gdcmDataImages.h"
33 #ifndef vtkFloatingPointType
34 #define vtkFloatingPointType float
37 int VTKReadTest(vtkTesting *t,vtkImageViewer *viewer,
38 std::string const & filename,
39 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 // Instead of directly reading the dicom let's write it down to another file
53 // do a scope to be sure everything gets cleanup
55 gdcm::File file( filename );
57 file.SetWriteModeToRaw();
58 file.WriteDcmExplVR( "TestWrite.dcm" );
61 // Ok for now still use the original image,
62 vtkGdcmReader *reader = vtkGdcmReader::New();
63 //reader->SetFileName( filename.c_str() );
64 reader->SetFileName( "TestWrite.dcm" );
67 double *range=reader->GetOutput()->GetScalarRange();
69 reader->GetOutput()->GetDimensions( dim );
74 viewer->SetInput ( reader->GetOutput() );
76 viewer->SetColorWindow (range[1] - range[0]);
77 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
79 viewer->SetSize(dim[0], dim[1]);
82 //For multifame dicom, take a snapshot of the center slice (+/- 1)
83 viewer->SetZSlice( dim[2] / 2 );
87 viewer->SetZSlice( 0 );
90 viewer->OffScreenRenderingOff();
92 viewer->SetInput(NULL);
95 //----------------------------------------------------------------------
96 // Transform the image to be RGB unsigned char, due to the requests in
97 // vtkTesting processing
98 // The pipeline is broken after each process to keep maximum of memory
99 vtkImageData *image=reader->GetOutput();
101 image->Register(NULL);
104 // Get the middle slice in the image
107 int *ext=image->GetExtent();
108 vtkImageClip *clip=vtkImageClip::New();
109 clip->SetInput(image);
110 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
111 ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
113 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
114 translat->SetInput(clip->GetOutput());
115 translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
117 image->UnRegister(NULL);
118 image=translat->GetOutput();
120 image->Register(NULL);
121 translat->SetOutput(NULL);
126 // Set an unsigned char image
127 // Shift/Scale the image
129 vtkImageShiftScale *iss=vtkImageShiftScale::New();
130 iss->SetInput(image);
131 iss->SetOutputScalarTypeToUnsignedChar();
132 iss->SetShift(-range[0]);
133 iss->SetScale(255.0/(range[1]-range[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 //----------------------------------------------------------------------
162 retVal = t->RegressionTest(image,0.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 << " ... OK" << std::endl;
178 std::cerr << " ... Failed" << std::endl;
184 int VTKTestRead(int argc, char *argv[])
189 if( std::string(argv[1]) == "-V" )
196 vtkTesting* t = vtkTesting::New();
197 vtkImageViewer *viewer;
199 viewer = vtkImageViewer::New();
205 std::cerr << "Usage: " << argv[0] << " [-V] image.dcm ref.png\n";
206 std::cerr << " -V : to view images to the screen... \n"
207 << " this mode can generate errors in the test\n\n";
211 ret = VTKReadTest(t,viewer,argv[1+show],argv[2+show],show);
219 // Test for all images
221 while( gdcmDataImages[i] != 0 )
223 std::string filename = GDCM_DATA_ROOT;
224 filename += "/"; //doh!
225 filename += gdcmDataImages[i];
226 std::cerr << "Filename: " << filename << std::endl;
228 //Extract name to find the png file matching:
229 std::string pngfile = gdcmDataImages[i++];
230 //pngfile.replace(pngfile.size()-3, 3, "png");
231 //More robust approach:
232 std::string::size_type dot_pos = pngfile.rfind( "." );
233 pngfile = pngfile.substr(0, dot_pos).append( ".png" );
234 pngfile.insert( 0, "Baseline/");
235 //std::cerr << "PNG file: " << pngfile << std::endl;
237 ret += VTKReadTest(t,viewer,filename,pngfile,show);