1 /*=========================================================================
4 Module: $RCSfile: VTKTestRead.cxx,v $
6 Date: $Date: 2007/09/18 07:58:38 $
7 Version: $Revision: 1.15 $
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"
33 #include "gdcmDataImages.h"
35 #ifndef vtkFloatingPointType
36 #define vtkFloatingPointType float
39 int VTKReadTest(vtkTesting *t,vtkImageViewer *viewer,
40 std::string const &filename,
41 std::string const &referenceFileName)
43 int retVal; // = 0; (to avoid bcc5.5 warnings)
47 t->AddArgument( GDCM_DATA_ROOT );
49 t->AddArgument( referenceFileName.c_str() );
51 t->AddArgument( "." );
53 vtkGdcmReader *reader = vtkGdcmReader::New();
54 reader->SetFileName( filename.c_str() );
58 reader->GetOutput()->GetScalarRange(range);
60 reader->GetOutput()->GetDimensions( dim );
65 viewer->SetInput ( reader->GetOutput() );
67 viewer->SetColorWindow (range[1] - range[0]);
68 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
70 viewer->SetSize(dim[0], dim[1]);
73 //For multifame dicom, take a snapshot of the center slice (+/- 1)
74 viewer->SetZSlice( dim[2] / 2 );
78 viewer->SetZSlice( 0 );
81 viewer->OffScreenRenderingOff();
83 viewer->SetInput(NULL);
86 //----------------------------------------------------------------------
87 // Transform the image to be RGB unsigned char, due to the requests in
88 // vtkTesting processing
89 // The pipeline is broken after each process to keep maximum of memory
90 vtkImageData *image=reader->GetOutput();
92 image->Register(NULL);
95 // Get the middle slice in the image
98 int *ext=image->GetExtent();
99 vtkImageClip *clip=vtkImageClip::New();
100 clip->SetInput(image);
101 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
102 ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
104 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
105 translat->SetInput(clip->GetOutput());
106 translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
108 image->UnRegister(NULL);
109 image=translat->GetOutput();
111 image->Register(NULL);
112 translat->SetOutput(NULL);
117 // Set an unsigned char image
118 // Shift/Scale the image
119 vtkImageShiftScale *iss=vtkImageShiftScale::New();
120 iss->SetInput(image);
121 iss->SetOutputScalarTypeToUnsignedChar();
122 iss->SetShift(-range[0]);
123 iss->SetScale(255.0/(range[1]-range[0]));
124 iss->ClampOverflowOn();
126 image->UnRegister(NULL);
127 image=iss->GetOutput();
129 image->Register(NULL);
132 // Set 3 components to the image
133 if(image->GetNumberOfScalarComponents()==1)
135 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
140 image->UnRegister(NULL);
141 image=x3->GetOutput();
143 image->Register(NULL);
148 //----------------------------------------------------------------------
151 std::ostringstream str;
152 retVal = t->RegressionTest(image,0.0,str);
153 image->UnRegister(NULL);
155 if( retVal != vtkTesting::PASSED )
157 std::cerr << str.str();
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 //gdcm::Debug::DebugOn(); // Comment out when no bug is to be tracked
210 // Test for all images
212 while( gdcmDataImages[i] != 0 )
214 std::string filename = GDCM_DATA_ROOT;
215 filename += "/"; //doh!
216 filename += gdcmDataImages[i];
217 std::cerr << "Filename: " << filename << std::endl;
219 //Extract name to find the png file matching:
220 std::string pngfile = gdcmDataImages[i++];
221 //pngfile.replace(pngfile.size()-3, 3, "png");
222 //More robust approach:
223 std::string::size_type dot_pos = pngfile.rfind( "." );
224 pngfile = pngfile.substr(0, dot_pos).append( ".png" );
225 pngfile.insert( 0, "Baseline/");
226 //std::cerr << "PNG file: " << pngfile << std::endl;
228 ret += VTKReadTest(t,viewer,filename,pngfile);