1 /*=========================================================================
4 Module: $RCSfile: VTKTestWrite.cxx,v $
6 Date: $Date: 2005/09/08 14:40:15 $
7 Version: $Revision: 1.11 $
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"
31 #include "gdcmDataImages.h"
33 #include "gdcmDebug.h"
35 #ifndef vtkFloatingPointType
36 #define vtkFloatingPointType float
39 int VTKWriteTest(vtkTesting *t,vtkImageViewer *viewer,
40 std::string const &filename,
41 std::string const &referenceFileName)
47 t->AddArgument( GDCM_DATA_ROOT );
49 t->AddArgument( referenceFileName.c_str() );
51 t->AddArgument( "." );
54 vtkGdcmReader *origin = vtkGdcmReader::New();
55 origin->SetFileName( filename.c_str() );
61 vtkGdcmWriter *writer = vtkGdcmWriter::New();
62 writer->SetFileName( "TestWrite.dcm" );
63 writer->SetInput(origin->GetOutput());
64 std::cout << "2' ...",
67 std::cout << "2'' ...";
71 // Read the written image
72 vtkGdcmReader *reader = vtkGdcmReader::New();
73 reader->SetFileName( "TestWrite.dcm" );
78 reader->GetOutput()->GetScalarRange(range);
80 reader->GetOutput()->GetDimensions( dim );
85 viewer->SetInput ( reader->GetOutput() );
87 viewer->SetColorWindow (range[1] - range[0]);
88 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
90 viewer->SetSize(dim[0], dim[1]);
93 //For multiframe dicom, take a snapshot of the center slice (+/- 1)
94 viewer->SetZSlice( dim[2] / 2 );
98 viewer->SetZSlice( 0 );
101 viewer->OffScreenRenderingOff();
103 viewer->SetInput(NULL);
106 std::cout << std::endl;
107 //----------------------------------------------------------------------
108 // Transform the image to be RGB unsigned char, due to the requests in
109 // vtkTesting processing
110 // The pipeline is broken after each process to keep maximum of memory
111 vtkImageData *image=reader->GetOutput();
113 image->Register(NULL);
116 // Get the middle slice in the image
119 int *ext=image->GetExtent();
120 vtkImageClip *clip=vtkImageClip::New();
121 clip->SetInput(image);
122 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
123 ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
125 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
126 translat->SetInput(clip->GetOutput());
127 translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
129 image->UnRegister(NULL);
130 image=translat->GetOutput();
132 image->Register(NULL);
133 translat->SetOutput(NULL);
138 // Set an unsigned char image
139 // Shift/Scale the image
141 vtkImageShiftScale *iss=vtkImageShiftScale::New();
142 iss->SetInput(image);
143 iss->SetOutputScalarTypeToUnsignedChar();
144 iss->SetShift(-range[0]);
145 iss->SetScale(255.0/(range[1]-range[0]));
146 iss->ClampOverflowOn();
148 image->UnRegister(NULL);
149 image=iss->GetOutput();
151 image->Register(NULL);
154 // Set 3 components to the image
155 if(image->GetNumberOfScalarComponents()==1)
157 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
162 image->UnRegister(NULL);
163 image=x3->GetOutput();
165 image->Register(NULL);
170 //----------------------------------------------------------------------
174 retVal = t->RegressionTest(image,0.0,str);
175 image->UnRegister(NULL);
177 if( retVal != vtkTesting::PASSED )
179 std::cerr << str.str();
181 str.rdbuf()->freeze(1);
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);