1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmViewer2.cxx,v $
6 Date: $Date: 2007/09/04 16:21:50 $
7 Version: $Revision: 1.11 $
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).
30 // * if the last 'filename' == "o", overlays (group 60xx) are loaded.
33 //----------------------------------------------------------------------------
34 #include <vtkRenderWindowInteractor.h>
35 #include <vtkImageViewer2.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>
43 #include "vtkGdcmReader.h"
44 #include "gdcmDocument.h" // for NO_SHADOWSEQ
45 #ifndef vtkFloatingPointType
46 #define vtkFloatingPointType float
49 //----------------------------------------------------------------------------
50 // Callback for the interaction
51 class vtkgdcmObserver : public vtkCommand
54 virtual char const *GetClassName() const
56 return "vtkgdcmObserver";
58 static vtkgdcmObserver *New()
60 return new vtkgdcmObserver;
64 this->ImageViewer = NULL;
66 virtual void Execute(vtkObject *, unsigned long event, void* )
68 if ( this->ImageViewer )
70 if ( event == vtkCommand::CharEvent )
72 #if (VTK_MAJOR_VERSION >= 5)
73 int max = ImageViewer->GetSliceMax();
74 int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
75 ImageViewer->SetSlice( slice );
77 int max = ImageViewer->GetWholeZMax();
78 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
79 ImageViewer->SetZSlice( slice );
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();
85 ImageViewer->Render();
89 vtkImageViewer2 *ImageViewer;
93 int main(int argc, char *argv[])
98 vtkGdcmReader *reader = vtkGdcmReader::New();
99 reader->AllowLookupTableOff();
102 if (strcmp (argv[argc-1], "o") == 0)
104 argc--; // Is it allowed?!?
105 reader->SetKeepOverlays(true);
109 reader->SetFileName( argv[1] );
111 for(int i=1; i< argc; i++)
112 reader->AddFileName( argv[i] );
114 // TODO : allow user to choose Load Mode
115 reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);
119 reader->GetOutput()->Print( cout );
121 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
123 vtkImageViewer2 *viewer = vtkImageViewer2::New();
125 if( reader->GetLookupTable() )
128 vtkImageMapToColors *map = vtkImageMapToColors::New ();
129 map->SetInput (reader->GetOutput());
130 map->SetLookupTable (reader->GetLookupTable());
131 map->SetOutputFormatToRGB();
132 viewer->SetInput ( map->GetOutput() );
138 // For a single medical image, it would be more efficient to use
139 // 0028|1050 [DS] [Window Center]
140 // 0028|1051 [DS] [Window Width]
141 // but vtkgdcmReader doesn't know about them :-(
143 if( reader->GetOutput()->GetNumberOfScalarComponents() == 1 )
145 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
146 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
147 viewer->SetColorWindow (range[1] - range[0]);
149 viewer->SetInput ( reader->GetOutput() );
151 viewer->SetupInteractor (iren);
153 // Here is where we setup the observer,
154 vtkgdcmObserver *obs = vtkgdcmObserver::New();
155 obs->ImageViewer = viewer;
156 iren->AddObserver(vtkCommand::CharEvent,obs);
163 //if you wish you can export dicom to a vtk file
164 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
165 writer->SetInput( reader->GetOutput());
166 writer->SetFileName( "foo.vtk" );
167 writer->SetFileTypeToBinary();