1 /*=========================================================================
4 Module: $RCSfile: VTKTestWrite.cxx,v $
6 Date: $Date: 2007/09/18 07:58:38 $
7 Version: $Revision: 1.12 $
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 "vtkGdcmWriter.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"
32 #include "gdcmDataImages.h"
34 #include "gdcmDebug.h"
36 #ifndef vtkFloatingPointType
37 #define vtkFloatingPointType float
40 int VTKWriteTest(vtkTesting *t,vtkImageViewer *viewer,
41 std::string const &filename,
42 std::string const &referenceFileName)
48 t->AddArgument( GDCM_DATA_ROOT );
50 t->AddArgument( referenceFileName.c_str() );
52 t->AddArgument( "." );
55 vtkGdcmReader *origin = vtkGdcmReader::New();
56 origin->SetFileName( filename.c_str() );
62 vtkGdcmWriter *writer = vtkGdcmWriter::New();
63 writer->SetFileName( "TestWrite.dcm" );
64 writer->SetInput(origin->GetOutput());
65 std::cout << "2' ...",
68 std::cout << "2'' ...";
72 // Read the written image
73 vtkGdcmReader *reader = vtkGdcmReader::New();
74 reader->SetFileName( "TestWrite.dcm" );
79 reader->GetOutput()->GetScalarRange(range);
81 reader->GetOutput()->GetDimensions( dim );
86 viewer->SetInput ( reader->GetOutput() );
88 viewer->SetColorWindow (range[1] - range[0]);
89 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
91 viewer->SetSize(dim[0], dim[1]);
94 //For multiframe dicom, take a snapshot of the center slice (+/- 1)
95 viewer->SetZSlice( dim[2] / 2 );
99 viewer->SetZSlice( 0 );
102 viewer->OffScreenRenderingOff();
104 viewer->SetInput(NULL);
107 std::cout << std::endl;
108 //----------------------------------------------------------------------
109 // Transform the image to be RGB unsigned char, due to the requests in
110 // vtkTesting processing
111 // The pipeline is broken after each process to keep maximum of memory
112 vtkImageData *image=reader->GetOutput();
114 image->Register(NULL);
117 // Get the middle slice in the image
120 int *ext=image->GetExtent();
121 vtkImageClip *clip=vtkImageClip::New();
122 clip->SetInput(image);
123 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
124 ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
126 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
127 translat->SetInput(clip->GetOutput());
128 translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
130 image->UnRegister(NULL);
131 image=translat->GetOutput();
133 image->Register(NULL);
134 translat->SetOutput(NULL);
139 // Set an unsigned char image
140 // Shift/Scale the image
142 vtkImageShiftScale *iss=vtkImageShiftScale::New();
143 iss->SetInput(image);
144 iss->SetOutputScalarTypeToUnsignedChar();
145 iss->SetShift(-range[0]);
146 iss->SetScale(255.0/(range[1]-range[0]));
147 iss->ClampOverflowOn();
149 image->UnRegister(NULL);
150 image=iss->GetOutput();
152 image->Register(NULL);
155 // Set 3 components to the image
156 if(image->GetNumberOfScalarComponents()==1)
158 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
163 image->UnRegister(NULL);
164 image=x3->GetOutput();
166 image->Register(NULL);
171 //----------------------------------------------------------------------
174 std::ostringstream str;
175 retVal = t->RegressionTest(image,0.0,str);
176 image->UnRegister(NULL);
178 if( retVal != vtkTesting::PASSED )
180 std::cerr << str.str();
183 if( retVal == vtkTesting::PASSED )
185 std::cerr << " ... OK" << std::endl;
190 std::cerr << " ... Failed" << std::endl;
195 int VTKTestWrite(int argc, char *argv[])
200 if( std::string(argv[1]) == "-V" )
206 // gdcm::Debug::DebugOn();
209 vtkTesting *t = vtkTesting::New();
210 vtkImageViewer *viewer;
212 viewer = vtkImageViewer::New();
218 std::cerr << "Usage: " << argv[0] << " [-V] image.dcm ref.png\n";
219 std::cerr << " -V : to view images to the screen... \n"
220 << " this mode can generate errors in the test\n\n";
224 ret = VTKWriteTest(t,viewer,argv[1+show],argv[2+show]);
232 // Test for all images
234 while( gdcmDataImages[i] != 0 )
236 std::string filename = GDCM_DATA_ROOT;
237 filename += "/"; //doh!
238 filename += gdcmDataImages[i];
239 std::cerr << "Filename: " << filename << std::endl;
241 //Extract name to find the png file matching:
242 std::string pngfile = gdcmDataImages[i++];
243 //pngfile.replace(pngfile.size()-3, 3, "png");
244 //More robust approach:
245 std::string::size_type dot_pos = pngfile.rfind( "." );
246 pngfile = pngfile.substr(0, dot_pos).append( ".png" );
247 pngfile.insert( 0, "Baseline/");
248 //std::cerr << "PNG file: " << pngfile << std::endl;
250 ret += VTKWriteTest(t,viewer,filename,pngfile);