1 /*=========================================================================
4 Module: $RCSfile: VTKTestRead.cxx,v $
6 Date: $Date: 2005/01/25 11:25:32 $
7 Version: $Revision: 1.7 $
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)
41 int retVal = 0; //by default this is an error
45 t->AddArgument( GDCM_DATA_ROOT );
47 t->AddArgument( referenceFileName.c_str() );
49 t->AddArgument( "." );
51 // Instead of directly reading the dicom let's write it down to another file
52 // do a scope to be sure everything gets cleanup
54 gdcm::FileHelper file( filename );
56 file.SetWriteModeToRaw();
57 file.WriteDcmExplVR( "TestWrite.dcm" );
60 // Ok for now still use the original image,
61 vtkGdcmReader *reader = vtkGdcmReader::New();
62 //reader->SetFileName( filename.c_str() );
63 reader->SetFileName( "TestWrite.dcm" );
66 double *range=reader->GetOutput()->GetScalarRange();
68 reader->GetOutput()->GetDimensions( dim );
73 viewer->SetInput ( reader->GetOutput() );
75 viewer->SetColorWindow (range[1] - range[0]);
76 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
78 viewer->SetSize(dim[0], dim[1]);
81 //For multifame dicom, take a snapshot of the center slice (+/- 1)
82 viewer->SetZSlice( dim[2] / 2 );
86 viewer->SetZSlice( 0 );
89 viewer->OffScreenRenderingOff();
91 viewer->SetInput(NULL);
94 //----------------------------------------------------------------------
95 // Transform the image to be RGB unsigned char, due to the requests in
96 // vtkTesting processing
97 // The pipeline is broken after each process to keep maximum of memory
98 vtkImageData *image=reader->GetOutput();
100 image->Register(NULL);
103 // Get the middle slice in the image
106 int *ext=image->GetExtent();
107 vtkImageClip *clip=vtkImageClip::New();
108 clip->SetInput(image);
109 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
110 ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
112 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
113 translat->SetInput(clip->GetOutput());
114 translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
116 image->UnRegister(NULL);
117 image=translat->GetOutput();
119 image->Register(NULL);
120 translat->SetOutput(NULL);
125 // Set an unsigned char image
126 // Shift/Scale the image
128 vtkImageShiftScale *iss=vtkImageShiftScale::New();
129 iss->SetInput(image);
130 iss->SetOutputScalarTypeToUnsignedChar();
131 iss->SetShift(-range[0]);
132 iss->SetScale(255.0/(range[1]-range[0]));
133 iss->ClampOverflowOn();
135 image->UnRegister(NULL);
136 image=iss->GetOutput();
138 image->Register(NULL);
141 // Set 3 components to the image
142 if(image->GetNumberOfScalarComponents()==1)
144 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
149 image->UnRegister(NULL);
150 image=x3->GetOutput();
152 image->Register(NULL);
157 //----------------------------------------------------------------------
161 retVal = t->RegressionTest(image,0.0,str);
162 image->UnRegister(NULL);
164 if( retVal != vtkTesting::PASSED )
166 std::cerr << str.str();
168 str.rdbuf()->freeze(1);
170 if( retVal == vtkTesting::PASSED )
172 std::cerr << " ... OK" << std::endl;
177 std::cerr << " ... Failed" << std::endl;
183 int VTKTestRead(int argc, char *argv[])
188 if( std::string(argv[1]) == "-V" )
195 vtkTesting* t = vtkTesting::New();
196 vtkImageViewer *viewer;
198 viewer = vtkImageViewer::New();
204 std::cerr << "Usage: " << argv[0] << " [-V] image.dcm ref.png\n";
205 std::cerr << " -V : to view images to the screen... \n"
206 << " this mode can generate errors in the test\n\n";
210 ret = VTKReadTest(t,viewer,argv[1+show],argv[2+show]);
218 // Test for all images
220 while( gdcmDataImages[i] != 0 )
222 std::string filename = GDCM_DATA_ROOT;
223 filename += "/"; //doh!
224 filename += gdcmDataImages[i];
225 std::cerr << "Filename: " << filename << std::endl;
227 //Extract name to find the png file matching:
228 std::string pngfile = gdcmDataImages[i++];
229 //pngfile.replace(pngfile.size()-3, 3, "png");
230 //More robust approach:
231 std::string::size_type dot_pos = pngfile.rfind( "." );
232 pngfile = pngfile.substr(0, dot_pos).append( ".png" );
233 pngfile.insert( 0, "Baseline/");
234 //std::cerr << "PNG file: " << pngfile << std::endl;
236 ret += VTKReadTest(t,viewer,filename,pngfile);