1 /*=========================================================================
4 Module: $RCSfile: VTKTestWrite.cxx,v $
6 Date: $Date: 2005/02/09 15:31:15 $
7 Version: $Revision: 1.9 $
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 #ifndef vtkFloatingPointType
34 #define vtkFloatingPointType float
37 int VTKWriteTest(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( "." );
52 vtkGdcmReader *origin = vtkGdcmReader::New();
53 origin->SetFileName( filename.c_str() );
57 vtkGdcmWriter *writer = vtkGdcmWriter::New();
58 writer->SetFileName( "TestWrite.dcm" );
59 writer->SetInput(origin->GetOutput());
65 // Read the written image
66 vtkGdcmReader *reader = vtkGdcmReader::New();
67 reader->SetFileName( "TestWrite.dcm" );
71 reader->GetOutput()->GetScalarRange(range);
73 reader->GetOutput()->GetDimensions( dim );
78 viewer->SetInput ( reader->GetOutput() );
80 viewer->SetColorWindow (range[1] - range[0]);
81 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
83 viewer->SetSize(dim[0], dim[1]);
86 //For multifame dicom, take a snapshot of the center slice (+/- 1)
87 viewer->SetZSlice( dim[2] / 2 );
91 viewer->SetZSlice( 0 );
94 viewer->OffScreenRenderingOff();
96 viewer->SetInput(NULL);
99 //----------------------------------------------------------------------
100 // Transform the image to be RGB unsigned char, due to the requests in
101 // vtkTesting processing
102 // The pipeline is broken after each process to keep maximum of memory
103 vtkImageData *image=reader->GetOutput();
105 image->Register(NULL);
108 // Get the middle slice in the image
111 int *ext=image->GetExtent();
112 vtkImageClip *clip=vtkImageClip::New();
113 clip->SetInput(image);
114 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
115 ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
117 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
118 translat->SetInput(clip->GetOutput());
119 translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
121 image->UnRegister(NULL);
122 image=translat->GetOutput();
124 image->Register(NULL);
125 translat->SetOutput(NULL);
130 // Set an unsigned char image
131 // Shift/Scale the image
133 vtkImageShiftScale *iss=vtkImageShiftScale::New();
134 iss->SetInput(image);
135 iss->SetOutputScalarTypeToUnsignedChar();
136 iss->SetShift(-range[0]);
137 iss->SetScale(255.0/(range[1]-range[0]));
138 iss->ClampOverflowOn();
140 image->UnRegister(NULL);
141 image=iss->GetOutput();
143 image->Register(NULL);
146 // Set 3 components to the image
147 if(image->GetNumberOfScalarComponents()==1)
149 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
154 image->UnRegister(NULL);
155 image=x3->GetOutput();
157 image->Register(NULL);
162 //----------------------------------------------------------------------
166 retVal = t->RegressionTest(image,0.0,str);
167 image->UnRegister(NULL);
169 if( retVal != vtkTesting::PASSED )
171 std::cerr << str.str();
173 str.rdbuf()->freeze(1);
175 if( retVal == vtkTesting::PASSED )
177 std::cerr << " ... OK" << std::endl;
182 std::cerr << " ... Failed" << std::endl;
187 int VTKTestWrite(int argc, char *argv[])
192 if( std::string(argv[1]) == "-V" )
199 vtkTesting *t = vtkTesting::New();
200 vtkImageViewer *viewer;
202 viewer = vtkImageViewer::New();
208 std::cerr << "Usage: " << argv[0] << " [-V] image.dcm ref.png\n";
209 std::cerr << " -V : to view images to the screen... \n"
210 << " this mode can generate errors in the test\n\n";
214 ret = VTKWriteTest(t,viewer,argv[1+show],argv[2+show]);
222 // Test for all images
224 while( gdcmDataImages[i] != 0 )
226 std::string filename = GDCM_DATA_ROOT;
227 filename += "/"; //doh!
228 filename += gdcmDataImages[i];
229 std::cerr << "Filename: " << filename << std::endl;
231 //Extract name to find the png file matching:
232 std::string pngfile = gdcmDataImages[i++];
233 //pngfile.replace(pngfile.size()-3, 3, "png");
234 //More robust approach:
235 std::string::size_type dot_pos = pngfile.rfind( "." );
236 pngfile = pngfile.substr(0, dot_pos).append( ".png" );
237 pngfile.insert( 0, "Baseline/");
238 //std::cerr << "PNG file: " << pngfile << std::endl;
240 ret += VTKWriteTest(t,viewer,filename,pngfile);