1 /*=========================================================================
4 Module: $RCSfile: VTKTestWriteSeq.cxx,v $
6 Date: $Date: 2007/09/18 07:58:39 $
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 "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 "gdcmDataSeqImages.h"
33 #include "gdcmDebug.h"
35 #ifndef vtkFloatingPointType
36 #define vtkFloatingPointType float
39 int VTKWriteSeqTest(vtkTesting *t, vtkImageViewer *viewer,
40 std::string const &filename,
41 std::string const &referenceFileName)
46 vtkGdcmReader *reader = vtkGdcmReader::New();
48 char *newFileDcm = new char[filename.size()+1];
53 sprintf(newFileDcm,filename.c_str(),i);
55 // Test the existance of the file
56 ifstream opened(newFileDcm,std::ios::in | std::ios::binary);
59 reader->AddFileName(newFileDcm);
69 vtkGdcmWriter *writer = vtkGdcmWriter::New();
70 writer->SetFilePattern("%s%02d.dcm");
71 char filePrefix[] = "TestWrite";
72 writer->SetFilePrefix(filePrefix);
73 writer->SetFileDimensionality(2);
74 writer->SetInput(reader->GetOutput());
81 reader = vtkGdcmReader::New();
82 reader->SetFilePattern("%s%02d.dcm");
83 reader->SetFilePrefix("TestWrite");
84 reader->SetFileNameSliceOffset(0);
85 reader->SetFileNameSliceSpacing(1);
86 reader->SetDataExtent(0,0,0,0,0,fileCount-1);
90 reader->GetOutput()->GetScalarRange(range);
95 viewer->SetInput ( reader->GetOutput() );
96 viewer->OffScreenRenderingOff();
98 viewer->SetColorWindow (range[1] - range[0]);
99 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
102 reader->GetOutput()->GetDimensions( dim );
103 viewer->SetSize(dim[0], dim[1]);
104 viewer->SetZSlice( 0 );
106 viewer->SetInput(NULL);
111 std::ostringstream str;
112 char *newFilePng = new char[referenceFileName.size()+1];
113 for(int j=0;j<fileCount;j++)
115 sprintf(newFilePng,referenceFileName.c_str(),j);
118 t->AddArgument("-D");
119 t->AddArgument( GDCM_DATA_ROOT );
120 t->AddArgument("-V");
121 t->AddArgument( newFilePng );
122 t->AddArgument("-T");
123 t->AddArgument( "." );
125 //----------------------------------------------------------------------
126 // Transform the image to be RGB unsigned char, due to the requests in
127 // vtkTesting processing
128 // The pipeline is broken after each process to keep maximum of memory
129 vtkImageData *image=reader->GetOutput();
131 image->Register(NULL);
133 // Get the middle slice in the image
134 int *ext=image->GetExtent();
135 vtkImageClip *clip=vtkImageClip::New();
136 clip->SetInput(image);
137 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],j,j);
139 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
140 translat->SetInput(clip->GetOutput());
141 translat->SetTranslation(0,0,-j);
143 image->UnRegister(NULL);
144 image=translat->GetOutput();
146 image->Register(NULL);
147 translat->SetOutput(NULL);
151 // Set an unsigned char image
152 // Shift/Scale the image
154 vtkImageShiftScale *iss=vtkImageShiftScale::New();
155 iss->SetInput(image);
156 iss->SetOutputScalarTypeToUnsignedChar();
157 iss->SetShift(-range[0]);
158 iss->SetScale(255.0/(range[1]-range[0]));
159 iss->ClampOverflowOn();
161 image->UnRegister(NULL);
162 image=iss->GetOutput();
164 image->Register(NULL);
167 // Set 3 components to the image
168 if(image->GetNumberOfScalarComponents()==1)
170 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
175 image->UnRegister(NULL);
176 image=x3->GetOutput();
178 image->Register(NULL);
183 //----------------------------------------------------------------------
186 retVal = t->RegressionTest(image,2.0,str);
187 image->UnRegister(NULL);
189 if( retVal != vtkTesting::PASSED )
191 std::cerr << str.str();
194 if( retVal == vtkTesting::PASSED )
196 std::cerr << " ...Slice " << j << ": OK" << std::endl;
200 std::cerr << " ...Slice " << j << ": Failed" << std::endl;
211 int VTKTestWriteSeq(int argc, char *argv[])
216 if( std::string(argv[1]) == "-V" )
222 GDCM_NAME_SPACE::Debug::DebugOn();
225 vtkTesting *t = vtkTesting::New();
226 vtkImageViewer *viewer;
228 viewer = vtkImageViewer::New();
234 std::cerr << "Usage: " << argv[0] << " [-V] image%d.dcm ref%d.png\n";
235 std::cerr << " -V : to view images to the screen... \n"
236 << " this mode can generate errors in the test\n";
237 std::cerr << " %d : this will be replaced by a number at execution.\n"
238 << " It will be from 0 to 9 only with a step of 1\n\n";
242 ret = VTKWriteSeqTest(t,viewer,argv[1+show],argv[2+show]);
250 // Test for all images
252 while( gdcmDataSeqImages[i] != 0 )
254 std::string filename = GDCM_DATA_ROOT;
255 filename += "/"; //doh!
256 filename += gdcmDataSeqImages[i];
257 std::cerr << "Filename: " << filename << std::endl;
259 //Extract name to find the png file matching:
260 std::string pngfile = gdcmDataSeqImages[i++];
261 std::string::size_type dot_pos = pngfile.rfind( "." );
262 pngfile = pngfile.substr(0, dot_pos).append( ".png" );
263 pngfile.insert( 0, "Baseline/");
265 ret += VTKWriteSeqTest(t,viewer,filename,pngfile);