1 /*=========================================================================
4 Module: $RCSfile: VTKTestRead.cxx,v $
6 Date: $Date: 2005/01/22 22:51:40 $
7 Version: $Revision: 1.6 $
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 "gdcmFileHelper.h"
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
47 t->AddArgument( GDCM_DATA_ROOT );
49 t->AddArgument( referenceFileName.c_str() );
51 t->AddArgument( "." );
53 // Instead of directly reading the dicom let's write it down to another file
54 // do a scope to be sure everything gets cleanup
56 gdcm::FileHelper file( filename );
58 file.SetWriteModeToRaw();
59 file.WriteDcmExplVR( "TestWrite.dcm" );
62 // Ok for now still use the original image,
63 vtkGdcmReader *reader = vtkGdcmReader::New();
64 //reader->SetFileName( filename.c_str() );
65 reader->SetFileName( "TestWrite.dcm" );
68 double *range=reader->GetOutput()->GetScalarRange();
70 reader->GetOutput()->GetDimensions( dim );
75 viewer->SetInput ( reader->GetOutput() );
77 viewer->SetColorWindow (range[1] - range[0]);
78 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
80 viewer->SetSize(dim[0], dim[1]);
83 //For multifame dicom, take a snapshot of the center slice (+/- 1)
84 viewer->SetZSlice( dim[2] / 2 );
88 viewer->SetZSlice( 0 );
91 viewer->OffScreenRenderingOff();
93 viewer->SetInput(NULL);
96 //----------------------------------------------------------------------
97 // Transform the image to be RGB unsigned char, due to the requests in
98 // vtkTesting processing
99 // The pipeline is broken after each process to keep maximum of memory
100 vtkImageData *image=reader->GetOutput();
102 image->Register(NULL);
105 // Get the middle slice in the image
108 int *ext=image->GetExtent();
109 vtkImageClip *clip=vtkImageClip::New();
110 clip->SetInput(image);
111 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
112 ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
114 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
115 translat->SetInput(clip->GetOutput());
116 translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
118 image->UnRegister(NULL);
119 image=translat->GetOutput();
121 image->Register(NULL);
122 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 //----------------------------------------------------------------------
163 retVal = t->RegressionTest(image,0.0,str);
164 image->UnRegister(NULL);
166 if( retVal != vtkTesting::PASSED )
168 std::cerr << str.str();
170 str.rdbuf()->freeze(1);
172 if( retVal == vtkTesting::PASSED )
174 std::cerr << " ... OK" << std::endl;
179 std::cerr << " ... Failed" << std::endl;
185 int VTKTestRead(int argc, char *argv[])
190 if( std::string(argv[1]) == "-V" )
197 vtkTesting* t = vtkTesting::New();
198 vtkImageViewer *viewer;
200 viewer = vtkImageViewer::New();
206 std::cerr << "Usage: " << argv[0] << " [-V] image.dcm ref.png\n";
207 std::cerr << " -V : to view images to the screen... \n"
208 << " this mode can generate errors in the test\n\n";
212 ret = VTKReadTest(t,viewer,argv[1+show],argv[2+show],show);
220 // Test for all images
222 while( gdcmDataImages[i] != 0 )
224 std::string filename = GDCM_DATA_ROOT;
225 filename += "/"; //doh!
226 filename += gdcmDataImages[i];
227 std::cerr << "Filename: " << filename << std::endl;
229 //Extract name to find the png file matching:
230 std::string pngfile = gdcmDataImages[i++];
231 //pngfile.replace(pngfile.size()-3, 3, "png");
232 //More robust approach:
233 std::string::size_type dot_pos = pngfile.rfind( "." );
234 pngfile = pngfile.substr(0, dot_pos).append( ".png" );
235 pngfile.insert( 0, "Baseline/");
236 //std::cerr << "PNG file: " << pngfile << std::endl;
238 ret += VTKReadTest(t,viewer,filename,pngfile,show);