1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmViewer2.cxx,v $
6 Date: $Date: 2007/06/19 13:09:45 $
7 Version: $Revision: 1.9 $
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.
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.
17 =========================================================================*/
18 // This example illustrates how the vtkGdcmReader vtk class can be
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.
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).
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>
41 #include "vtkGdcmReader.h"
42 #include "gdcmDocument.h" // for NO_SHADOWSEQ
44 #ifndef vtkFloatingPointType
45 #define vtkFloatingPointType float
48 //----------------------------------------------------------------------------
49 // Callback for the interaction
50 class vtkgdcmObserver : public vtkCommand
53 virtual char const *GetClassName() const
55 return "vtkgdcmObserver";
57 static vtkgdcmObserver *New()
59 return new vtkgdcmObserver;
63 this->ImageViewer = NULL;
65 virtual void Execute(vtkObject *, unsigned long event, void* )
67 if ( this->ImageViewer )
69 if ( event == vtkCommand::CharEvent )
71 #if (VTK_MAJOR_VERSION >= 5)
72 int max = ImageViewer->GetSliceMax();
73 int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
74 ImageViewer->SetSlice( slice );
76 int max = ImageViewer->GetWholeZMax();
77 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
78 ImageViewer->SetZSlice( slice );
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();
84 ImageViewer->Render();
88 vtkImageViewer2 *ImageViewer;
92 int main(int argc, char *argv[])
97 vtkGdcmReader *reader = vtkGdcmReader::New();
98 reader->AllowLookupTableOff();
101 reader->SetFileName( argv[1] );
103 for(int i=1; i< argc; i++)
104 reader->AddFileName( argv[i] );
106 // TODO : allow user to choose Load Mode
107 reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);
111 reader->GetOutput()->Print( cout );
113 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
115 vtkImageViewer2 *viewer = vtkImageViewer2::New();
117 if( reader->GetLookupTable() )
120 vtkImageMapToColors *map = vtkImageMapToColors::New ();
121 map->SetInput (reader->GetOutput());
122 map->SetLookupTable (reader->GetLookupTable());
123 map->SetOutputFormatToRGB();
124 viewer->SetInput ( map->GetOutput() );
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 :-(
135 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
136 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
137 viewer->SetColorWindow (range[1] - range[0]);
139 viewer->SetInput ( reader->GetOutput() );
141 viewer->SetupInteractor (iren);
143 //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
144 //viewer->SetColorWindow (range[1] - range[0]);
145 //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
147 // Here is where we setup the observer,
148 vtkgdcmObserver *obs = vtkgdcmObserver::New();
149 obs->ImageViewer = viewer;
150 iren->AddObserver(vtkCommand::CharEvent,obs);
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();