]> Creatis software - gdcm.git/blob - vtk/vtkgdcmViewer.cxx
ENH: Some minor enhance:
[gdcm.git] / vtk / vtkgdcmViewer.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: vtkgdcmViewer.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/05/11 14:40:58 $
7   Version:   $Revision: 1.25 $
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 // 
31 //----------------------------------------------------------------------------
32 #include <vtkRenderWindowInteractor.h>
33 #include <vtkImageViewer.h>
34 #include <vtkStructuredPoints.h>
35 #include <vtkStructuredPointsWriter.h>
36 #include <vtkCommand.h>
37 #include <vtkRenderer.h>
38 #include <vtkImageMapToColors.h>
39 #include <vtkLookupTable.h>
40
41 #include "vtkGdcmReader.h"
42
43 #ifndef vtkFloatingPointType
44 #define vtkFloatingPointType float
45 #endif
46
47 //----------------------------------------------------------------------------
48 // Callback for the interaction
49 class vtkgdcmObserver : public vtkCommand
50 {
51 public:
52    virtual char const *GetClassName() const 
53    { 
54       return "vtkgdcmObserver";
55    }
56    static vtkgdcmObserver *New() 
57    { 
58       return new vtkgdcmObserver; 
59    }
60    vtkgdcmObserver()
61    {
62       this->ImageViewer = NULL;
63    }
64    virtual void Execute(vtkObject *, unsigned long event, void* )
65    {
66       if ( this->ImageViewer )
67       {
68          if ( event == vtkCommand::CharEvent )
69          {
70             int max = ImageViewer->GetWholeZMax();
71             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
72             ImageViewer->SetZSlice( slice );
73             ImageViewer->GetRenderer()->ResetCameraClippingRange();
74             ImageViewer->Render();
75          }
76       }
77    }
78    vtkImageViewer *ImageViewer;
79 };
80
81
82 int main(int argc, char *argv[])
83 {
84    if( argc < 2 )
85       return 0;
86   
87    vtkGdcmReader *reader = vtkGdcmReader::New();
88    reader->AllowLookupTableOff();
89
90    if( argc == 2 )
91       reader->SetFileName( argv[1] );
92    else
93       for(int i=1; i< argc; i++)
94          reader->AddFileName( argv[i] );
95
96    reader->Update();
97
98    //print debug info:
99    reader->GetOutput()->Print( cout );
100
101    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
102
103    vtkImageViewer *viewer = vtkImageViewer::New();
104
105    if( reader->GetLookupTable() )
106    {
107       //convert to color:
108       vtkImageMapToColors *map = vtkImageMapToColors::New ();
109       map->SetInput (reader->GetOutput());
110       map->SetLookupTable (reader->GetLookupTable());
111       map->SetOutputFormatToRGB();
112       viewer->SetInput ( map->GetOutput() );
113       map->Delete();
114    }
115    else
116    {
117       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
118       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
119       viewer->SetColorWindow (range[1] - range[0]);
120
121       viewer->SetInput ( reader->GetOutput() );
122    }
123    viewer->SetupInteractor (iren);
124   
125    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
126    //viewer->SetColorWindow (range[1] - range[0]);
127    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
128
129    // Here is where we setup the observer, 
130    vtkgdcmObserver *obs = vtkgdcmObserver::New();
131    obs->ImageViewer = viewer;
132    iren->AddObserver(vtkCommand::CharEvent,obs);
133    obs->Delete();
134
135    //viewer->Render();
136    iren->Initialize();
137    iren->Start();
138
139    //if you wish you can export dicom to a vtk file  
140    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
141    writer->SetInput( reader->GetOutput());
142    writer->SetFileName( "foo.vtk" );
143    writer->SetFileTypeToBinary();
144    //writer->Write();
145
146    reader->Delete();
147    iren->Delete();
148    viewer->Delete();
149    writer->Delete();
150
151    return 0;
152 }