1 /*=========================================================================
4 Module: $RCSfile: VTKTestWrite.cxx,v $
6 Date: $Date: 2005/01/25 15:45:49 $
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 "vtkGdcmWriter.h"
21 #include "vtkImageViewer.h"
22 #include "vtkImageData.h"
23 #include "vtkRegressionTestImage.h"
24 #include "vtkImageClip.h"
25 #include "vtkImageTranslateExtent.h"
26 #include "vtkImageAppendComponents.h"
27 #include "vtkImageShiftScale.h"
32 #include "gdcmDataImages.h"
34 #ifndef vtkFloatingPointType
35 #define vtkFloatingPointType float
38 int VTKWriteTest(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( "." );
53 vtkGdcmReader *origin = vtkGdcmReader::New();
54 origin->SetFileName( filename.c_str() );
58 vtkGdcmWriter *writer = vtkGdcmWriter::New();
59 writer->SetFileName( "TestWrite.dcm" );
60 writer->SetInput(origin->GetOutput());
66 // Read the written image
67 vtkGdcmReader *reader = vtkGdcmReader::New();
68 reader->SetFileName( "TestWrite.dcm" );
72 reader->GetOutput()->GetScalarRange(range);
74 reader->GetOutput()->GetDimensions( dim );
79 viewer->SetInput ( reader->GetOutput() );
81 viewer->SetColorWindow (range[1] - range[0]);
82 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
84 viewer->SetSize(dim[0], dim[1]);
87 //For multifame dicom, take a snapshot of the center slice (+/- 1)
88 viewer->SetZSlice( dim[2] / 2 );
92 viewer->SetZSlice( 0 );
95 viewer->OffScreenRenderingOff();
97 viewer->SetInput(NULL);
100 //----------------------------------------------------------------------
101 // Transform the image to be RGB unsigned char, due to the requests in
102 // vtkTesting processing
103 // The pipeline is broken after each process to keep maximum of memory
104 vtkImageData *image=reader->GetOutput();
106 image->Register(NULL);
109 // Get the middle slice in the image
112 int *ext=image->GetExtent();
113 vtkImageClip *clip=vtkImageClip::New();
114 clip->SetInput(image);
115 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
116 ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
118 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
119 translat->SetInput(clip->GetOutput());
120 translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
122 image->UnRegister(NULL);
123 image=translat->GetOutput();
125 image->Register(NULL);
126 translat->SetOutput(NULL);
131 // Set an unsigned char image
132 // Shift/Scale the image
134 vtkImageShiftScale *iss=vtkImageShiftScale::New();
135 iss->SetInput(image);
136 iss->SetOutputScalarTypeToUnsignedChar();
137 iss->SetShift(-range[0]);
138 iss->SetScale(255.0/(range[1]-range[0]));
139 iss->ClampOverflowOn();
141 image->UnRegister(NULL);
142 image=iss->GetOutput();
144 image->Register(NULL);
147 // Set 3 components to the image
148 if(image->GetNumberOfScalarComponents()==1)
150 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
155 image->UnRegister(NULL);
156 image=x3->GetOutput();
158 image->Register(NULL);
163 //----------------------------------------------------------------------
167 retVal = t->RegressionTest(image,0.0,str);
168 image->UnRegister(NULL);
170 if( retVal != vtkTesting::PASSED )
172 std::cerr << str.str();
174 str.rdbuf()->freeze(1);
176 if( retVal == vtkTesting::PASSED )
178 std::cerr << " ... OK" << std::endl;
183 std::cerr << " ... Failed" << std::endl;
188 int VTKTestWrite(int argc, char *argv[])
193 if( std::string(argv[1]) == "-V" )
200 vtkTesting* t = vtkTesting::New();
201 vtkImageViewer *viewer;
203 viewer = vtkImageViewer::New();
209 std::cerr << "Usage: " << argv[0] << " [-V] image.dcm ref.png\n";
210 std::cerr << " -V : to view images to the screen... \n"
211 << " this mode can generate errors in the test\n\n";
215 ret = VTKWriteTest(t,viewer,argv[1+show],argv[2+show]);
223 // Test for all images
225 while( gdcmDataImages[i] != 0 )
227 std::string filename = GDCM_DATA_ROOT;
228 filename += "/"; //doh!
229 filename += gdcmDataImages[i];
230 std::cerr << "Filename: " << filename << std::endl;
232 //Extract name to find the png file matching:
233 std::string pngfile = gdcmDataImages[i++];
234 //pngfile.replace(pngfile.size()-3, 3, "png");
235 //More robust approach:
236 std::string::size_type dot_pos = pngfile.rfind( "." );
237 pngfile = pngfile.substr(0, dot_pos).append( ".png" );
238 pngfile.insert( 0, "Baseline/");
239 //std::cerr << "PNG file: " << pngfile << std::endl;
241 ret += VTKWriteTest(t,viewer,filename,pngfile);