]> Creatis software - gdcm.git/blob - vtk/vtkgdcmViewer.cxx
ENH: Adding a new supp file for valgrind
[gdcm.git] / vtk / vtkgdcmViewer.cxx
1 // This example illustrates how the vtkGdcmReader vtk class can be
2 // used in order to:
3 //  * produce a simple (vtk based) Dicom image STACK VIEWER.
4 //  * dump the stack considered as a volume in a vtkStructuredPoints
5 //    vtk file: the vtk gdcm wrappers can be seen as a simple way to convert
6 //    a stack of Dicom images into a native vtk volume.
7 //
8 // Usage:
9 //  * the filenames of the Dicom images constituting the stack should be
10 //    given as command line arguments,
11 //  * you can navigate through the stack by hitting any character key,
12 //  * the produced vtk file is named "foo.vtk" (in the invocation directory).
13 // 
14 //----------------------------------------------------------------------------
15 #include <vtkRenderWindowInteractor.h>
16 #include <vtkImageViewer.h>
17 #include <vtkStructuredPoints.h>
18 #include <vtkStructuredPointsWriter.h>
19 #include <vtkPNGWriter.h>
20 #include <vtkCommand.h>
21 #include <vtkRenderer.h>
22 #include <vtkImageMapToColors.h>
23 #include <vtkLookupTable.h>
24
25 #include "vtkGdcmReader.h"
26 #include "gdcmDebug.h"
27
28 #include <iostream>
29
30 #ifndef vtkFloatingPointType
31 #define vtkFloatingPointType float
32 #endif
33
34 //----------------------------------------------------------------------------
35 // Callback for the interaction
36 class vtkgdcmObserver : public vtkCommand
37 {
38 public:
39    virtual char const *GetClassName() const 
40    { 
41       return "vtkgdcmObserver";
42    }
43    static vtkgdcmObserver *New() 
44    { 
45       return new vtkgdcmObserver; 
46    }
47    vtkgdcmObserver()
48    {
49       this->ImageViewer = NULL;
50    }
51    virtual void Execute(vtkObject *, unsigned long event, void* )
52    {
53       if ( this->ImageViewer )
54       {
55          if ( event == vtkCommand::CharEvent )
56          {
57             int max = ImageViewer->GetWholeZMax();
58             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
59             ImageViewer->SetZSlice( slice );
60             ImageViewer->GetRenderer()->ResetCameraClippingRange();
61             ImageViewer->Render();
62          }
63       }
64    }
65    vtkImageViewer *ImageViewer;
66 };
67
68
69 int main(int argc, char *argv[])
70 {
71    if( argc < 2 )
72       return 0;
73   
74    gdcm::Debug::SetDebugOff();
75    vtkGdcmReader *reader = vtkGdcmReader::New();
76    reader->AllowLookupTableOff();
77
78    if( argc == 2 )
79       reader->SetFileName( argv[1] );
80    else
81       for(int i=1; i< argc; i++)
82          reader->AddFileName( argv[i] );
83
84    reader->Update();
85
86    //print debug info:
87    reader->GetOutput()->Print( cout );
88
89    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
90
91    vtkImageViewer *viewer = vtkImageViewer::New();
92
93    if( reader->GetLookupTable() )
94    {
95       //convert to color:
96       vtkImageMapToColors *map = vtkImageMapToColors::New ();
97       map->SetInput (reader->GetOutput());
98       map->SetLookupTable (reader->GetLookupTable());
99       map->SetOutputFormatToRGB();
100       viewer->SetInput ( map->GetOutput() );
101       map->Delete();
102    }
103    else
104    {
105       double *range = reader->GetOutput()->GetScalarRange();
106       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
107       viewer->SetColorWindow (range[1] - range[0]);
108
109       viewer->SetInput ( reader->GetOutput() );
110    }
111    viewer->SetupInteractor (iren);
112   
113    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
114    //viewer->SetColorWindow (range[1] - range[0]);
115    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
116
117    // Here is where we setup the observer, 
118    vtkgdcmObserver *obs = vtkgdcmObserver::New();
119    obs->ImageViewer = viewer;
120    iren->AddObserver(vtkCommand::CharEvent,obs);
121    obs->Delete();
122
123    iren->Initialize();
124    iren->Start();
125
126    //if you wish you can export dicom to a vtk file  
127    //vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
128    vtkPNGWriter *writer = vtkPNGWriter::New();
129    writer->SetInput( reader->GetOutput());
130    writer->SetFileName( "foo.png" );
131    //writer->SetFileTypeToBinary();
132    writer->Write();
133
134
135    reader->Delete();
136    iren->Delete();
137    viewer->Delete();
138    writer->Delete();
139
140    return 0;
141 }