]> Creatis software - gdcm.git/blob - vtk/vtkgdcmViewer2.cxx
minor re-indent
[gdcm.git] / vtk / vtkgdcmViewer2.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: vtkgdcmViewer2.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/09/01 13:33:36 $
7   Version:   $Revision: 1.18 $
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 // This example illustrates how the vtkGdcmReader vtk class can be
19 // used in order to:
20 //  * produce a simple (vtk based) Dicom image STACK VIEWER.
21 //  * dump the stack considered as a volume in a vtkStructuredPoints
22 //    vtk file: the vtk gdcm wrappers can be seen as a simple way to convert
23 //    a stack of Dicom images into a native vtk volume.
24 //
25 // Usage:
26 //  * the filenames of the Dicom images constituting the stack should be
27 //    given as command line arguments,
28 //  * you can navigate through the stack by hitting any character key,
29 //  * the produced vtk file is named "foo.vtk" (in the invocation directory).
30 //  * if the last 'filename' == "o", overlays (group 60xx)  are loaded.
31 //  * just a test!
32 // 
33 //----------------------------------------------------------------------------
34 #include <vtkRenderWindowInteractor.h>
35 #if VTK_MAJOR_VERSION >= 5
36 #include "vtkImageColorViewer.h"
37 #else
38 #include <vtkImageViewer2.h>
39 #endif
40 #include <vtkStructuredPoints.h>
41 #include <vtkStructuredPointsWriter.h>
42 #include <vtkPNGWriter.h>
43 #include <vtkMetaImageWriter.h>
44 #include <vtkWindowToImageFilter.h>
45 #include <vtkRenderWindow.h>
46 #include <vtkCommand.h>
47 #include <vtkRenderer.h>
48 #include <vtkImageMapToColors.h>
49 #include <vtkLookupTable.h>
50
51 #include "vtkGdcmReader.h"
52 #include "gdcmDocument.h"  // for NO_SHADOWSEQ
53 #ifndef vtkFloatingPointType
54 #define vtkFloatingPointType float
55 #endif
56
57 //----------------------------------------------------------------------------
58 // Callback for the interaction
59 class vtkgdcmObserver : public vtkCommand
60 {
61 public:
62    virtual char const *GetClassName() const 
63    { 
64       return "vtkgdcmObserver";
65    }
66    static vtkgdcmObserver *New() 
67    { 
68       return new vtkgdcmObserver; 
69    }
70    vtkgdcmObserver()
71    {
72       this->ImageViewer = NULL;
73    }
74    virtual void Execute(vtkObject *caller, unsigned long event, void* /*callData*/ )
75    {
76       if ( this->ImageViewer )
77       {
78          vtkRenderWindowInteractor * rwi = vtkRenderWindowInteractor::SafeDownCast( caller );
79          char keycode = 0;
80          if( rwi ) keycode = rwi->GetKeyCode();
81          if ( event == vtkCommand::CharEvent && keycode != 's' )
82          {
83 #if VTK_MAJOR_VERSION >= 5
84             int max = ImageViewer->GetSliceMax();
85             int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
86             ImageViewer->SetSlice( slice );
87 #else
88             int max = ImageViewer->GetWholeZMax();
89             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
90             ImageViewer->SetZSlice( slice );
91 #endif
92 #if !( (VTK_MAJOR_VERSION >= 5) || ( VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION >= 5 ) )
93          // This used to be a bug in version VTK 4.4 and earlier
94             ImageViewer->GetRenderer()->ResetCameraClippingRange();
95 #endif
96             ImageViewer->Render();
97          }
98          else if ( keycode == 's' )
99          {
100            vtkPNGWriter * writer = vtkPNGWriter::New();
101            vtkWindowToImageFilter * w2i = vtkWindowToImageFilter::New();
102            w2i->SetInput( rwi->GetRenderWindow() );
103            writer->SetInput( w2i->GetOutput() );
104            writer->SetFileName( "snapshot.png" );
105            writer->Write();
106            //std::cerr << "Screenshort saved to snapshot.png" << std::endl;
107          }
108       }
109    }
110 #if VTK_MAJOR_VERSION >= 5
111    vtkImageColorViewer *ImageViewer;
112 #else
113    vtkImageViewer2 *ImageViewer;
114 #endif
115 };
116
117 // --------------------------------------------------------------------------------
118
119 int main(int argc, char *argv[])
120 {
121    if( argc < 2 )
122       return 0;
123       
124    bool metaWrite = false;
125   
126    vtkGdcmReader *reader = vtkGdcmReader::New();
127    reader->AllowLookupTableOff();
128
129
130    if (strcmp (argv[argc-1], "o") == 0)
131    {
132       argc--; // Is it allowed?!?
133       reader->SetKeepOverlays(true);
134    }
135  
136 // not a very clever way to pass several params
137 // but it's just for checking
138    if (strcmp (argv[argc-1], "n") == 0)
139    {
140       argc--; // Is it allowed?!?
141       reader->SetFlipY(false);
142    }
143
144    if (strcmp (argv[argc-1], "m") == 0)
145    {
146       argc--; // Is it allowed?!?
147       metaWrite = true;
148    }
149    
150    if( argc == 2 )
151       reader->SetFileName( argv[1] );
152    else
153       for(int i=1; i< argc; i++)
154          reader->AddFileName( argv[i] );
155
156 // TODO : allow user to choose Load Mode
157    reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);  
158    reader->Update();
159
160    //print debug info:
161    reader->GetOutput()->Print( cout );
162
163
164    //if you wish you can export dicom to a .mhd file
165    //if (metaWrite) {
166    std::cout << "try to write .mhd" << std::endl;
167       vtkMetaImageWriter* w = vtkMetaImageWriter::New();
168       w->SetInput( reader->GetOutput());
169       w->SetFileName( "/home/jpr/Desktop/toto.mhd" );
170       w->SetFileDimensionality(3);
171       w->SetCompression(false);
172       w->Update();
173       w->Write();
174       //w->Delete();
175       std::cout << "end write .mhd" << std::endl;
176     //}
177     
178     
179    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
180 #if VTK_MAJOR_VERSION >= 5
181    vtkImageColorViewer *viewer = vtkImageColorViewer::New();
182 #else
183    vtkImageViewer2 *viewer = vtkImageViewer2::New();
184 #endif
185
186    if( reader->GetLookupTable() )
187    {
188       //convert to color:
189       vtkImageMapToColors *map = vtkImageMapToColors::New ();
190       map->SetInput (reader->GetOutput());
191       map->SetLookupTable (reader->GetLookupTable());
192       map->SetOutputFormatToRGB();
193       viewer->SetInput ( map->GetOutput() );
194       map->Delete();
195    }
196    else
197    {
198    
199    // For a single medical image, it would be more efficient to use
200    // 0028|1050 [DS] [Window Center]
201    // 0028|1051 [DS] [Window Width]
202    // but vtkgdcmReader doesn't know about them :-(
203
204    if( reader->GetOutput()->GetNumberOfScalarComponents() == 1 )
205      {
206      vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
207      viewer->SetColorLevel (0.5 * (range[1] + range[0]));
208      viewer->SetColorWindow (range[1] - range[0]);
209      }
210       viewer->SetInput ( reader->GetOutput() );
211    }
212    viewer->SetupInteractor (iren);
213
214    // Here is where we setup the observer, 
215    vtkgdcmObserver *obs = vtkgdcmObserver::New();
216    obs->ImageViewer = viewer;
217    iren->AddObserver(vtkCommand::CharEvent,obs);
218    obs->Delete();
219
220 #if VTK_MAJOR_VERSION >= 5
221    viewer->Render(); // Don't ask why...
222 #endif
223    iren->Initialize();
224    iren->Start();
225
226    //if you wish you can export dicom to a vtk file  
227    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
228    writer->SetInput( reader->GetOutput());
229    writer->SetFileName( "foo.vtk" );
230    writer->SetFileTypeToBinary();
231    //writer->Write();
232    
233    //if you wish you can export dicom to a .mhd file
234    /*
235    if (metaWrite) {
236       vtkMetaImageWriter* w = vtkMetaImageWriter::New();
237       w->SetInput( reader->GetOutput());
238       w->SetFileName( "foo.mhd" );
239       w->SetCompression(false);
240       w->Write();
241       w->Delete();
242     }
243 */
244    reader->Delete();
245    iren->Delete();
246    viewer->Delete();
247    writer->Delete();
248
249    return 0;
250 }