]> Creatis software - gdcm.git/blob - vtk/vtkgdcmViewer.cxx
Unify with vtkgdcmViewer2.cxx
[gdcm.git] / vtk / vtkgdcmViewer.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: vtkgdcmViewer.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/08/28 09:41:39 $
7   Version:   $Revision: 1.30 $
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 #include "gdcmDocument.h"  // for NO_SHADOWSEQ
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 #if (VTK_MAJOR_VERSION >= 5)
71             int max = ImageViewer->GetSliceMax();
72             int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
73             ImageViewer->SetSlice( slice );
74 #else
75             int max = ImageViewer->GetWholeZMax();
76             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
77             ImageViewer->SetZSlice( slice );
78 #endif
79 #if !( (VTK_MAJOR_VERSION >= 5) || ( VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION >= 5 ) )
80          // This used to be a bug in version VTK 4.4 and earlier
81             ImageViewer->GetRenderer()->ResetCameraClippingRange();
82 #endif
83             ImageViewer->Render();
84          }
85       }
86    }
87    vtkImageViewer *ImageViewer;
88 };
89
90
91 int main(int argc, char *argv[])
92 {
93    if( argc < 2 )
94       return 0;
95   
96    vtkGdcmReader *reader = vtkGdcmReader::New();
97    reader->AllowLookupTableOff();
98
99    if( argc == 2 )
100       reader->SetFileName( argv[1] );
101    else
102       for(int i=1; i< argc; i++)
103          reader->AddFileName( argv[i] );
104
105 // TODO : allow user to choose Load Mode
106    reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);  
107    reader->Update();
108
109    //print debug info:
110    reader->GetOutput()->Print( cout );
111
112    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
113
114    vtkImageViewer *viewer = vtkImageViewer::New();
115
116    if( reader->GetLookupTable() )
117    {
118       //convert to color:
119       vtkImageMapToColors *map = vtkImageMapToColors::New ();
120       map->SetInput (reader->GetOutput());
121       map->SetLookupTable (reader->GetLookupTable());
122       map->SetOutputFormatToRGB();
123       viewer->SetInput ( map->GetOutput() );
124       map->Delete();
125    }
126    else
127    {
128    
129    // For a single medical image, it would be more efficient to use
130    // 0028|1050 [DS] [Window Center]
131    // 0028|1051 [DS] [Window Width]
132    // but vtkgdcmReader doesn't know about them :-(
133
134       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
135       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
136       viewer->SetColorWindow (range[1] - range[0]);
137
138       viewer->SetInput ( reader->GetOutput() );
139    }
140    viewer->SetupInteractor (iren);
141   
142    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
143    //viewer->SetColorWindow (range[1] - range[0]);
144    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
145
146    // Here is where we setup the observer, 
147    vtkgdcmObserver *obs = vtkgdcmObserver::New();
148    obs->ImageViewer = viewer;
149    iren->AddObserver(vtkCommand::CharEvent,obs);
150    obs->Delete();
151
152    //viewer->Render();
153    iren->Initialize();
154    iren->Start();
155
156    //if you wish you can export dicom to a vtk file  
157    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
158    writer->SetInput( reader->GetOutput());
159    writer->SetFileName( "foo.vtk" );
160    writer->SetFileTypeToBinary();
161    //writer->Write();
162
163    reader->Delete();
164    iren->Delete();
165    viewer->Delete();
166    writer->Delete();
167
168    return 0;
169 }