]> Creatis software - gdcm.git/blob - Testing/VTKTestWriteSeq.cxx
Add some verbosity (usefull at debugging time)
[gdcm.git] / Testing / VTKTestWriteSeq.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: VTKTestWriteSeq.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/21 14:59:06 $
7   Version:   $Revision: 1.6 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
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"
27
28 #include <iostream>
29
30 //Generated file:
31 #include "gdcmDataSeqImages.h"
32 #include "gdcmDebug.h"
33
34 #ifndef vtkFloatingPointType
35 #define vtkFloatingPointType float
36 #endif
37
38 int VTKWriteSeqTest(vtkTesting *t, vtkImageViewer *viewer,
39                    std::string const &filename, 
40                    std::string const &referenceFileName)
41 {
42    int retVal;
43
44    // Set the reader   
45    vtkGdcmReader *reader = vtkGdcmReader::New();
46
47    char *newFileDcm = new char[filename.size()+1];
48    int fileCount = 0;
49    for(int i=0;i<10;i++)
50    {
51       fileCount = i;
52       sprintf(newFileDcm,filename.c_str(),i);
53
54       // Test the existance of the file
55       ifstream opened(newFileDcm,std::ios::in | std::ios::binary);
56       if(opened)
57       {
58          reader->AddFileName(newFileDcm);
59          opened.close();
60       }
61       else
62          break;
63    }
64    delete[] newFileDcm;
65    reader->Update();
66
67    // Write the image
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());
74    writer->Write();
75
76    reader->Delete();
77    writer->Delete();
78
79    // Reread the image
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);
86    reader->Update();
87
88    double range[2];
89    reader->GetOutput()->GetScalarRange(range);
90
91    // Show
92    if( viewer )
93    {
94       viewer->SetInput ( reader->GetOutput() );
95       viewer->OffScreenRenderingOff();
96
97       viewer->SetColorWindow (range[1] - range[0]);
98       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
99
100       int dim[3];
101       reader->GetOutput()->GetDimensions( dim );
102       viewer->SetSize(dim[0], dim[1]);
103       viewer->SetZSlice( 0 );
104       viewer->Render();
105       viewer->SetInput(NULL);
106    }
107
108    // make test
109    int ret = 0;
110    ostrstream str;
111    char *newFilePng = new char[referenceFileName.size()+1];
112    for(int j=0;j<fileCount;j++)
113    {
114       sprintf(newFilePng,referenceFileName.c_str(),j);
115
116       t->CleanArguments();
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( "." );
123
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();
129       image->Update();
130       image->Register(NULL);
131
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);
137       clip->ClipDataOn();
138       vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
139       translat->SetInput(clip->GetOutput());
140       translat->SetTranslation(0,0,-j);
141
142       image->UnRegister(NULL);
143       image=translat->GetOutput();
144       image->Update();
145       image->Register(NULL);
146       translat->SetOutput(NULL);
147       clip->Delete();
148       translat->Delete();
149
150       // Set an unsigned char image
151       // Shift/Scale the image 
152       image->Update();
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();
159
160       image->UnRegister(NULL);
161       image=iss->GetOutput();
162       image->Update();
163       image->Register(NULL);
164       iss->Delete();
165
166       // Set 3 components to the image
167       if(image->GetNumberOfScalarComponents()==1)
168       {
169          vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
170          x3->AddInput(image);
171          x3->AddInput(image);
172          x3->AddInput(image);
173
174          image->UnRegister(NULL);
175          image=x3->GetOutput();
176          image->Update();
177          image->Register(NULL);
178          x3->SetOutput(NULL);
179          x3->Delete();
180       }
181       // End of transform
182       //----------------------------------------------------------------------
183
184       // make test
185       retVal = t->RegressionTest(image,2.0,str);
186       image->UnRegister(NULL);
187
188       if( retVal != vtkTesting::PASSED )
189       {
190          std::cerr << str.str();
191       }
192       str.rdbuf()->freeze(1);
193
194       if( retVal == vtkTesting::PASSED )
195       {
196          std::cerr << "       ...Slice " << j << ": OK" << std::endl;
197       }
198       else
199       {
200          std::cerr << "       ...Slice " << j << ": Failed" << std::endl;
201          ret++;
202       }
203    }
204
205    delete[] newFilePng;
206    reader->Delete();
207
208    return ret;
209 }
210
211 int VTKTestWriteSeq(int argc, char *argv[])
212 {
213    bool show = false;
214    if( argc >= 2 )
215    {
216       if( std::string(argv[1]) == "-V" )
217       {
218          show = true;
219       }
220    }
221
222    GDCM_NAME_SPACE::Debug::DebugOn();
223
224    int ret = 0;
225    vtkTesting *t = vtkTesting::New();
226    vtkImageViewer *viewer;
227    if( show )
228       viewer = vtkImageViewer::New();
229    else
230       viewer = NULL;
231
232    if( argc < 3+show )
233    {
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";
239    }
240    else
241    {
242       ret = VTKWriteSeqTest(t,viewer,argv[1+show],argv[2+show]);
243       t->Delete();
244       if( viewer )
245          viewer->Delete();
246
247       return ret;
248    }
249
250    // Test for all images
251    int i = 0;
252    while( gdcmDataSeqImages[i] != 0 )
253    {
254       std::string filename = GDCM_DATA_ROOT;
255       filename += "/";  //doh!
256       filename += gdcmDataSeqImages[i];
257       std::cerr << "Filename: " << filename << std::endl;
258
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/");
264       
265       ret += VTKWriteSeqTest(t,viewer,filename,pngfile);
266    }
267    t->Delete();
268    if( viewer )
269       viewer->Delete();
270
271    return ret;
272 }