]> Creatis software - gdcm.git/blob - vtk/vtkgdcmViewer2.cxx
Propagate Mathieu's modif from 1.2 to CVS version.
[gdcm.git] / vtk / vtkgdcmViewer2.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: vtkgdcmViewer2.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/05/02 10:09:43 $
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 // 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 <vtkImageViewer2.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 #include "gdcmDocument.h"  // for NO_SHADOWSEQ
43
44 #ifndef vtkFloatingPointType
45 #define vtkFloatingPointType float
46 #endif
47
48 //----------------------------------------------------------------------------
49 // Callback for the interaction
50 class vtkgdcmObserver : public vtkCommand
51 {
52 public:
53    virtual char const *GetClassName() const 
54    { 
55       return "vtkgdcmObserver";
56    }
57    static vtkgdcmObserver *New() 
58    { 
59       return new vtkgdcmObserver; 
60    }
61    vtkgdcmObserver()
62    {
63       this->ImageViewer = NULL;
64    }
65    virtual void Execute(vtkObject *, unsigned long event, void* )
66    {
67       if ( this->ImageViewer )
68       {
69          if ( event == vtkCommand::CharEvent )
70          {
71  #if (VTK_MAJOR_VERSION >= 5)
72             int max = ImageViewer->GetSliceMax();
73             int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
74             ImageViewer->SetSlice( slice );
75 #else
76             int max = ImageViewer->GetWholeZMax();
77             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
78             ImageViewer->SetZSlice( slice );
79 #endif
80 #if !( (VTK_MAJOR_VERSION >= 5) || ( VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION >= 5 ) )
81           // This used to be a bug in version VTK 4.4 and earlier    
82             ImageViewer->GetRenderer()->ResetCameraClippingRange();
83 #endif
84             ImageViewer->Render();
85          }
86       }
87    }
88    vtkImageViewer2 *ImageViewer;
89 };
90
91
92 int main(int argc, char *argv[])
93 {
94    if( argc < 2 )
95       return 0;
96   
97    vtkGdcmReader *reader = vtkGdcmReader::New();
98    reader->AllowLookupTableOff();
99
100    if( argc == 2 )
101       reader->SetFileName( argv[1] );
102    else
103       for(int i=1; i< argc; i++)
104          reader->AddFileName( argv[i] );
105
106 // TODO : allow user to choose Load Mode
107    reader->SetLoadMode(gdcm::LD_NOSHADOWSEQ);  
108    reader->Update();
109
110    //print debug info:
111    reader->GetOutput()->Print( cout );
112
113    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
114
115    vtkImageViewer2 *viewer = vtkImageViewer2::New();
116
117    if( reader->GetLookupTable() )
118    {
119       //convert to color:
120       vtkImageMapToColors *map = vtkImageMapToColors::New ();
121       map->SetInput (reader->GetOutput());
122       map->SetLookupTable (reader->GetLookupTable());
123       map->SetOutputFormatToRGB();
124       viewer->SetInput ( map->GetOutput() );
125       map->Delete();
126    }
127    else
128    {
129    
130    // For a single medical image, it would be more efficient to use
131    // 0028|1050 [DS] [Window Center]
132    // 0028|1051 [DS] [Window Width]
133    // but vtkgdcmReader doesn't know about them :-(
134
135       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
136       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
137       viewer->SetColorWindow (range[1] - range[0]);
138
139       viewer->SetInput ( reader->GetOutput() );
140    }
141    viewer->SetupInteractor (iren);
142   
143    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
144    //viewer->SetColorWindow (range[1] - range[0]);
145    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
146
147    // Here is where we setup the observer, 
148    vtkgdcmObserver *obs = vtkgdcmObserver::New();
149    obs->ImageViewer = viewer;
150    iren->AddObserver(vtkCommand::CharEvent,obs);
151    obs->Delete();
152
153    //viewer->Render();
154    iren->Initialize();
155    iren->Start();
156
157    //if you wish you can export dicom to a vtk file  
158    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
159    writer->SetInput( reader->GetOutput());
160    writer->SetFileName( "foo.vtk" );
161    writer->SetFileTypeToBinary();
162    //writer->Write();
163
164    reader->Delete();
165    iren->Delete();
166    viewer->Delete();
167    writer->Delete();
168
169    return 0;
170 }