1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmViewer2.cxx,v $
6 Date: $Date: 2007/09/13 11:32:53 $
7 Version: $Revision: 1.14 $
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 #if VTK_MAJOR_VERSION >= 5
36 #include "vtkImageColorViewer.h"
38 #include <vtkImageViewer2.h>
40 #include <vtkStructuredPoints.h>
41 #include <vtkStructuredPointsWriter.h>
42 #include <vtkCommand.h>
43 #include <vtkRenderer.h>
44 #include <vtkImageMapToColors.h>
45 #include <vtkLookupTable.h>
47 #include "vtkGdcmReader.h"
48 #include "gdcmDocument.h" // for NO_SHADOWSEQ
49 #ifndef vtkFloatingPointType
50 #define vtkFloatingPointType float
53 //----------------------------------------------------------------------------
54 // Callback for the interaction
55 class vtkgdcmObserver : public vtkCommand
58 virtual char const *GetClassName() const
60 return "vtkgdcmObserver";
62 static vtkgdcmObserver *New()
64 return new vtkgdcmObserver;
68 this->ImageViewer = NULL;
70 virtual void Execute(vtkObject *, unsigned long event, void* )
72 if ( this->ImageViewer )
74 if ( event == vtkCommand::CharEvent )
76 #if VTK_MAJOR_VERSION >= 5
77 int max = ImageViewer->GetSliceMax();
78 int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
79 ImageViewer->SetSlice( slice );
81 int max = ImageViewer->GetWholeZMax();
82 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
83 ImageViewer->SetZSlice( slice );
85 #if !( (VTK_MAJOR_VERSION >= 5) || ( VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION >= 5 ) )
86 // This used to be a bug in version VTK 4.4 and earlier
87 ImageViewer->GetRenderer()->ResetCameraClippingRange();
89 ImageViewer->Render();
93 #if VTK_MAJOR_VERSION >= 5
94 vtkImageColorViewer *ImageViewer;
96 vtkImageViewer2 *ImageViewer;
101 int main(int argc, char *argv[])
106 vtkGdcmReader *reader = vtkGdcmReader::New();
107 reader->AllowLookupTableOff();
110 if (strcmp (argv[argc-1], "o") == 0)
112 argc--; // Is it allowed?!?
113 reader->SetKeepOverlays(true);
117 reader->SetFileName( argv[1] );
119 for(int i=1; i< argc; i++)
120 reader->AddFileName( argv[i] );
122 // TODO : allow user to choose Load Mode
123 reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);
127 reader->GetOutput()->Print( cout );
129 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
130 #if VTK_MAJOR_VERSION >= 5
131 vtkImageColorViewer *viewer = vtkImageColorViewer::New();
133 vtkImageViewer2 *viewer = vtkImageViewer2::New();
136 if( reader->GetLookupTable() )
139 vtkImageMapToColors *map = vtkImageMapToColors::New ();
140 map->SetInput (reader->GetOutput());
141 map->SetLookupTable (reader->GetLookupTable());
142 map->SetOutputFormatToRGB();
143 viewer->SetInput ( map->GetOutput() );
149 // For a single medical image, it would be more efficient to use
150 // 0028|1050 [DS] [Window Center]
151 // 0028|1051 [DS] [Window Width]
152 // but vtkgdcmReader doesn't know about them :-(
154 if( reader->GetOutput()->GetNumberOfScalarComponents() == 1 )
156 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
157 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
158 viewer->SetColorWindow (range[1] - range[0]);
160 viewer->SetInput ( reader->GetOutput() );
162 viewer->SetupInteractor (iren);
164 // Here is where we setup the observer,
165 vtkgdcmObserver *obs = vtkgdcmObserver::New();
166 obs->ImageViewer = viewer;
167 iren->AddObserver(vtkCommand::CharEvent,obs);
170 #if VTK_MAJOR_VERSION >= 5
171 viewer->Render(); // Don't ask why...
176 //if you wish you can export dicom to a vtk file
177 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
178 writer->SetInput( reader->GetOutput());
179 writer->SetFileName( "foo.vtk" );
180 writer->SetFileTypeToBinary();