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