1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmViewer2.cxx,v $
6 Date: $Date: 2005/08/30 15:13:10 $
7 Version: $Revision: 1.5 $
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 int max = ImageViewer->GetWholeZMax();
72 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
73 ImageViewer->SetZSlice( slice );
74 ImageViewer->GetRenderer()->ResetCameraClippingRange();
75 ImageViewer->Render();
79 vtkImageViewer2 *ImageViewer;
83 int main(int argc, char *argv[])
88 vtkGdcmReader *reader = vtkGdcmReader::New();
89 reader->AllowLookupTableOff();
92 reader->SetFileName( argv[1] );
94 for(int i=1; i< argc; i++)
95 reader->AddFileName( argv[i] );
97 // TODO : allow user to choose Load Mode
98 reader->SetLoadMode(gdcm::LD_NOSHADOWSEQ);
102 reader->GetOutput()->Print( cout );
104 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
106 vtkImageViewer2 *viewer = vtkImageViewer2::New();
108 if( reader->GetLookupTable() )
111 vtkImageMapToColors *map = vtkImageMapToColors::New ();
112 map->SetInput (reader->GetOutput());
113 map->SetLookupTable (reader->GetLookupTable());
114 map->SetOutputFormatToRGB();
115 viewer->SetInput ( map->GetOutput() );
120 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
121 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
122 viewer->SetColorWindow (range[1] - range[0]);
124 viewer->SetInput ( reader->GetOutput() );
126 viewer->SetupInteractor (iren);
128 //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
129 //viewer->SetColorWindow (range[1] - range[0]);
130 //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
132 // Here is where we setup the observer,
133 vtkgdcmObserver *obs = vtkgdcmObserver::New();
134 obs->ImageViewer = viewer;
135 iren->AddObserver(vtkCommand::CharEvent,obs);
142 //if you wish you can export dicom to a vtk file
143 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
144 writer->SetInput( reader->GetOutput());
145 writer->SetFileName( "foo.vtk" );
146 writer->SetFileTypeToBinary();