]> Creatis software - gdcm.git/blob - Testing/VTKTestRead.cxx
* Test/ : rename VTK tests to have a best name for the tests
[gdcm.git] / Testing / VTKTestRead.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: VTKTestRead.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/19 10:43:01 $
7   Version:   $Revision: 1.1 $
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 "gdcmFile.h"
19 #include "vtkGdcmReader.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 "gdcmDataImages.h"
32
33 #ifndef vtkFloatingPointType
34 #define vtkFloatingPointType float
35 #endif
36
37 int VTKReadTest(vtkTesting *t,vtkImageViewer *viewer,
38                 std::string const & filename, 
39                 std::string const & referenceFileName,
40                 bool show )
41 {
42    int retVal = 0;  //by default this is an error
43
44    t->CleanArguments();
45    t->AddArgument("-D");
46    t->AddArgument( GDCM_DATA_ROOT );
47    t->AddArgument("-V");
48    t->AddArgument( referenceFileName.c_str() );
49    t->AddArgument("-T");
50    t->AddArgument( "." );
51    
52    // Instead of directly reading the dicom let's write it down to another file
53    // do a scope to be sure everything gets cleanup
54    {
55       gdcm::File file( filename );
56       file.GetImageData();
57       file.SetWriteModeToRaw();
58       file.WriteDcmExplVR( "TestWrite.dcm" );
59    }
60  
61    // Ok for now still use the original image, 
62    vtkGdcmReader *reader = vtkGdcmReader::New();
63    //reader->SetFileName( filename.c_str() );
64    reader->SetFileName( "TestWrite.dcm" );
65    reader->Update();
66
67    int dim[3];
68    reader->GetOutput()->GetDimensions( dim );
69
70    // Show
71    if(viewer)
72    {
73       viewer->SetInput ( reader->GetOutput() );
74
75       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
76       viewer->SetColorWindow (range[1] - range[0]);
77       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
78
79       viewer->SetSize(dim[0], dim[1]);
80       if(dim[2] != 1)
81       {
82          //For multifame dicom, take a snapshot of the center slice (+/- 1)
83          viewer->SetZSlice( dim[2] / 2 );
84       }
85       else
86       {
87          viewer->SetZSlice( 0 );
88       }
89
90       viewer->OffScreenRenderingOff();
91       viewer->Render();
92       viewer->SetInput(NULL);
93    }
94
95    //----------------------------------------------------------------------
96    // Transform the image to be RGB unsigned char, due to the requests in
97    // vtkTesting processing
98    // The pipeline is broken after each process to keep maximum of memory
99    vtkImageData *image=reader->GetOutput();
100    image->Update();
101    image->Register(NULL);
102    reader->Delete();
103
104    // Get the middle slice in the image
105    if(dim[2] != 1)
106    {
107       int *ext=image->GetExtent();
108       vtkImageClip *clip=vtkImageClip::New();
109       clip->SetInput(image);
110       clip->SetOutputWholeExtent(ext[0],ext[1],ext[2],ext[3],
111                                  ext[4]+dim[2] / 2,ext[4]+dim[2] / 2);
112       vtkImageTranslateExtent *translat=vtkImageTranslateExtent::New();
113       translat->SetInput(clip->GetOutput());
114       translat->SetTranslation(0,0,-ext[4]-dim[2] / 2);
115
116       image->UnRegister(NULL);
117       image=translat->GetOutput();
118       image->Update();
119       image->Register(NULL);
120       translat->SetOutput(NULL);
121       clip->Delete();
122       translat->Delete();
123    }
124
125    // Set an unsigned char image
126    // Shift/Scale the image 
127    image->Update();
128    double *rng=image->GetScalarRange();
129    vtkImageShiftScale *iss=vtkImageShiftScale::New();
130    iss->SetInput(image);
131    iss->SetOutputScalarTypeToUnsignedChar();
132    iss->SetShift(-rng[0]);
133    iss->SetScale(255.0/(rng[1]-rng[0]));
134    iss->ClampOverflowOn();
135
136    image->UnRegister(NULL);
137    image=iss->GetOutput();
138    image->Update();
139    image->Register(NULL);
140    iss->Delete();
141
142    // Set 3 components to the image
143    if(image->GetNumberOfScalarComponents()==1)
144    {
145       vtkImageAppendComponents *x3=vtkImageAppendComponents::New();
146       x3->AddInput(image);
147       x3->AddInput(image);
148       x3->AddInput(image);
149
150       image->UnRegister(NULL);
151       image=x3->GetOutput();
152       image->Update();
153       image->Register(NULL);
154       x3->SetOutput(NULL);
155       x3->Delete();
156    }
157    // End of transform
158    //----------------------------------------------------------------------
159
160    // make test
161    ostrstream str;
162    retVal = t->RegressionTest(image,0.0,str);
163    image->UnRegister(NULL);
164
165    if( retVal != vtkTesting::PASSED )
166    {
167       std::cerr << str.str();
168    }
169    str.rdbuf()->freeze(1);
170
171    if( retVal == vtkTesting::PASSED )
172    {
173       std::cerr << "       ... OK" << std::endl;
174       return 0;
175    }
176    else
177    {
178       std::cerr << "       ... Failed" << std::endl;
179       return 1;
180    }
181
182 }
183
184 int VTKTestRead(int argc, char *argv[])
185 {
186    bool show = false;
187    if( argc >= 2 )
188    {
189       if( std::string(argv[1]) == "-V" )
190       {
191          show = true;
192       }
193    }
194
195    int ret = 0;
196    vtkTesting* t = vtkTesting::New();
197    vtkImageViewer *viewer;
198    if( show )
199       viewer = vtkImageViewer::New();
200    else
201       viewer = NULL;
202
203    if( argc < 3+show )
204    {
205       std::cerr << "Usage: " << argv[0] << " [-V] image.dcm ref.png\n";
206       std::cerr << "   -V : to view images to the screen... \n"
207                 << "        this mode can generate errors in the test\n\n";
208    }
209    else
210    {
211       ret = VTKReadTest(t,viewer,argv[1+show],argv[2+show],show);
212       t->Delete();
213       viewer->Delete();
214
215       return ret;
216    }
217
218    // Test for all images
219    int i = 0;
220    while( gdcmDataImages[i] != 0 )
221    {
222       std::string filename = GDCM_DATA_ROOT;
223       filename += "/";  //doh!
224       filename += gdcmDataImages[i];
225       std::cerr << "Filename: " << filename << std::endl;
226
227       //Extract name to find the png file matching:
228       std::string pngfile = gdcmDataImages[i++];
229       //pngfile.replace(pngfile.size()-3, 3, "png");
230       //More robust approach:
231       std::string::size_type dot_pos = pngfile.rfind( "." );
232       pngfile = pngfile.substr(0, dot_pos).append( ".png" );
233       pngfile.insert( 0, "Baseline/");
234       //std::cerr << "PNG file: " << pngfile << std::endl;
235
236       ret += VTKReadTest(t,viewer,filename,pngfile,show);
237    }
238    t->Delete();
239    viewer->Delete();
240
241    return ret;
242 }