]> Creatis software - gdcm.git/blob - Testing/VTKTestWriteSeq.cxx
ENH: for now do not support our own bugs
[gdcm.git] / Testing / VTKTestWriteSeq.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: VTKTestWriteSeq.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/09/18 07:58:39 $
7   Version:   $Revision: 1.7 $
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 #include <sstream>
30
31 //Generated file:
32 #include "gdcmDataSeqImages.h"
33 #include "gdcmDebug.h"
34
35 #ifndef vtkFloatingPointType
36 #define vtkFloatingPointType float
37 #endif
38
39 int VTKWriteSeqTest(vtkTesting *t, vtkImageViewer *viewer,
40                    std::string const &filename, 
41                    std::string const &referenceFileName)
42 {
43    int retVal;
44
45    // Set the reader   
46    vtkGdcmReader *reader = vtkGdcmReader::New();
47
48    char *newFileDcm = new char[filename.size()+1];
49    int fileCount = 0;
50    for(int i=0;i<10;i++)
51    {
52       fileCount = i;
53       sprintf(newFileDcm,filename.c_str(),i);
54
55       // Test the existance of the file
56       ifstream opened(newFileDcm,std::ios::in | std::ios::binary);
57       if(opened)
58       {
59          reader->AddFileName(newFileDcm);
60          opened.close();
61       }
62       else
63          break;
64    }
65    delete[] newFileDcm;
66    reader->Update();
67
68    // Write the image
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());
75    writer->Write();
76
77    reader->Delete();
78    writer->Delete();
79
80    // Reread the image
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);
87    reader->Update();
88
89    double range[2];
90    reader->GetOutput()->GetScalarRange(range);
91
92    // Show
93    if( viewer )
94    {
95       viewer->SetInput ( reader->GetOutput() );
96       viewer->OffScreenRenderingOff();
97
98       viewer->SetColorWindow (range[1] - range[0]);
99       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
100
101       int dim[3];
102       reader->GetOutput()->GetDimensions( dim );
103       viewer->SetSize(dim[0], dim[1]);
104       viewer->SetZSlice( 0 );
105       viewer->Render();
106       viewer->SetInput(NULL);
107    }
108
109    // make test
110    int ret = 0;
111    std::ostringstream str;
112    char *newFilePng = new char[referenceFileName.size()+1];
113    for(int j=0;j<fileCount;j++)
114    {
115       sprintf(newFilePng,referenceFileName.c_str(),j);
116
117       t->CleanArguments();
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( "." );
124
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();
130       image->Update();
131       image->Register(NULL);
132
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);
138       clip->ClipDataOn();
139       vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
140       translat->SetInput(clip->GetOutput());
141       translat->SetTranslation(0,0,-j);
142
143       image->UnRegister(NULL);
144       image=translat->GetOutput();
145       image->Update();
146       image->Register(NULL);
147       translat->SetOutput(NULL);
148       clip->Delete();
149       translat->Delete();
150
151       // Set an unsigned char image
152       // Shift/Scale the image 
153       image->Update();
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();
160
161       image->UnRegister(NULL);
162       image=iss->GetOutput();
163       image->Update();
164       image->Register(NULL);
165       iss->Delete();
166
167       // Set 3 components to the image
168       if(image->GetNumberOfScalarComponents()==1)
169       {
170          vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
171          x3->AddInput(image);
172          x3->AddInput(image);
173          x3->AddInput(image);
174
175          image->UnRegister(NULL);
176          image=x3->GetOutput();
177          image->Update();
178          image->Register(NULL);
179          x3->SetOutput(NULL);
180          x3->Delete();
181       }
182       // End of transform
183       //----------------------------------------------------------------------
184
185       // make test
186       retVal = t->RegressionTest(image,2.0,str);
187       image->UnRegister(NULL);
188
189       if( retVal != vtkTesting::PASSED )
190       {
191          std::cerr << str.str();
192       }
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 }