1 /*=========================================================================
4 Module: $RCSfile: VTKTestWriteSeq.cxx,v $
6 Date: $Date: 2007/06/21 14:59:06 $
7 Version: $Revision: 1.6 $
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 "gdcmDataSeqImages.h"
32 #include "gdcmDebug.h"
34 #ifndef vtkFloatingPointType
35 #define vtkFloatingPointType float
38 int VTKWriteSeqTest(vtkTesting *t, vtkImageViewer *viewer,
39 std::string const &filename,
40 std::string const &referenceFileName)
45 vtkGdcmReader *reader = vtkGdcmReader::New();
47 char *newFileDcm = new char[filename.size()+1];
52 sprintf(newFileDcm,filename.c_str(),i);
54 // Test the existance of the file
55 ifstream opened(newFileDcm,std::ios::in | std::ios::binary);
58 reader->AddFileName(newFileDcm);
68 vtkGdcmWriter *writer = vtkGdcmWriter::New();
69 writer->SetFilePattern("%s%02d.dcm");
70 char filePrefix[] = "TestWrite";
71 writer->SetFilePrefix(filePrefix);
72 writer->SetFileDimensionality(2);
73 writer->SetInput(reader->GetOutput());
80 reader = vtkGdcmReader::New();
81 reader->SetFilePattern("%s%02d.dcm");
82 reader->SetFilePrefix("TestWrite");
83 reader->SetFileNameSliceOffset(0);
84 reader->SetFileNameSliceSpacing(1);
85 reader->SetDataExtent(0,0,0,0,0,fileCount-1);
89 reader->GetOutput()->GetScalarRange(range);
94 viewer->SetInput ( reader->GetOutput() );
95 viewer->OffScreenRenderingOff();
97 viewer->SetColorWindow (range[1] - range[0]);
98 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
101 reader->GetOutput()->GetDimensions( dim );
102 viewer->SetSize(dim[0], dim[1]);
103 viewer->SetZSlice( 0 );
105 viewer->SetInput(NULL);
111 char *newFilePng = new char[referenceFileName.size()+1];
112 for(int j=0;j<fileCount;j++)
114 sprintf(newFilePng,referenceFileName.c_str(),j);
117 t->AddArgument("-D");
118 t->AddArgument( GDCM_DATA_ROOT );
119 t->AddArgument("-V");
120 t->AddArgument( newFilePng );
121 t->AddArgument("-T");
122 t->AddArgument( "." );
124 //----------------------------------------------------------------------
125 // Transform the image to be RGB unsigned char, due to the requests in
126 // vtkTesting processing
127 // The pipeline is broken after each process to keep maximum of memory
128 vtkImageData *image=reader->GetOutput();
130 image->Register(NULL);
132 // Get the middle slice in the image
133 int *ext=image->GetExtent();
134 vtkImageClip *clip=vtkImageClip::New();
135 clip->SetInput(image);
136 clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],j,j);
138 vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
139 translat->SetInput(clip->GetOutput());
140 translat->SetTranslation(0,0,-j);
142 image->UnRegister(NULL);
143 image=translat->GetOutput();
145 image->Register(NULL);
146 translat->SetOutput(NULL);
150 // Set an unsigned char image
151 // Shift/Scale the image
153 vtkImageShiftScale *iss=vtkImageShiftScale::New();
154 iss->SetInput(image);
155 iss->SetOutputScalarTypeToUnsignedChar();
156 iss->SetShift(-range[0]);
157 iss->SetScale(255.0/(range[1]-range[0]));
158 iss->ClampOverflowOn();
160 image->UnRegister(NULL);
161 image=iss->GetOutput();
163 image->Register(NULL);
166 // Set 3 components to the image
167 if(image->GetNumberOfScalarComponents()==1)
169 vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
174 image->UnRegister(NULL);
175 image=x3->GetOutput();
177 image->Register(NULL);
182 //----------------------------------------------------------------------
185 retVal = t->RegressionTest(image,2.0,str);
186 image->UnRegister(NULL);
188 if( retVal != vtkTesting::PASSED )
190 std::cerr << str.str();
192 str.rdbuf()->freeze(1);
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);