1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmViewer2.cxx,v $
6 Date: $Date: 2009/04/18 14:42:51 $
7 Version: $Revision: 1.16 $
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 <vtkPNGWriter.h>
43 #include <vtkWindowToImageFilter.h>
44 #include <vtkRenderWindow.h>
45 #include <vtkCommand.h>
46 #include <vtkRenderer.h>
47 #include <vtkImageMapToColors.h>
48 #include <vtkLookupTable.h>
50 #include "vtkGdcmReader.h"
51 #include "gdcmDocument.h" // for NO_SHADOWSEQ
52 #ifndef vtkFloatingPointType
53 #define vtkFloatingPointType float
56 //----------------------------------------------------------------------------
57 // Callback for the interaction
58 class vtkgdcmObserver : public vtkCommand
61 virtual char const *GetClassName() const
63 return "vtkgdcmObserver";
65 static vtkgdcmObserver *New()
67 return new vtkgdcmObserver;
71 this->ImageViewer = NULL;
73 virtual void Execute(vtkObject *caller, unsigned long event, void* /*callData*/ )
75 if ( this->ImageViewer )
77 vtkRenderWindowInteractor * rwi = vtkRenderWindowInteractor::SafeDownCast( caller );
79 if( rwi ) keycode = rwi->GetKeyCode();
80 if ( event == vtkCommand::CharEvent && keycode != 's' )
82 #if VTK_MAJOR_VERSION >= 5
83 int max = ImageViewer->GetSliceMax();
84 int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
85 ImageViewer->SetSlice( slice );
87 int max = ImageViewer->GetWholeZMax();
88 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
89 ImageViewer->SetZSlice( slice );
91 #if !( (VTK_MAJOR_VERSION >= 5) || ( VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION >= 5 ) )
92 // This used to be a bug in version VTK 4.4 and earlier
93 ImageViewer->GetRenderer()->ResetCameraClippingRange();
95 ImageViewer->Render();
97 else if ( keycode == 's' )
99 vtkPNGWriter * writer = vtkPNGWriter::New();
100 vtkWindowToImageFilter * w2i = vtkWindowToImageFilter::New();
101 w2i->SetInput( rwi->GetRenderWindow() );
102 writer->SetInput( w2i->GetOutput() );
103 writer->SetFileName( "snapshot.png" );
105 //std::cerr << "Screenshort saved to snapshot.png" << std::endl;
109 #if VTK_MAJOR_VERSION >= 5
110 vtkImageColorViewer *ImageViewer;
112 vtkImageViewer2 *ImageViewer;
117 int main(int argc, char *argv[])
122 vtkGdcmReader *reader = vtkGdcmReader::New();
123 reader->AllowLookupTableOff();
126 if (strcmp (argv[argc-1], "o") == 0)
128 argc--; // Is it allowed?!?
129 reader->SetKeepOverlays(true);
132 // not a very clever way to pass several params
133 // but it's just for checking
134 if (strcmp (argv[argc-1], "n") == 0)
136 argc--; // Is it allowed?!?
137 reader->SetFlipY(false);
141 reader->SetFileName( argv[1] );
143 for(int i=1; i< argc; i++)
144 reader->AddFileName( argv[i] );
146 // TODO : allow user to choose Load Mode
147 reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);
151 reader->GetOutput()->Print( cout );
153 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
154 #if VTK_MAJOR_VERSION >= 5
155 vtkImageColorViewer *viewer = vtkImageColorViewer::New();
157 vtkImageViewer2 *viewer = vtkImageViewer2::New();
160 if( reader->GetLookupTable() )
163 vtkImageMapToColors *map = vtkImageMapToColors::New ();
164 map->SetInput (reader->GetOutput());
165 map->SetLookupTable (reader->GetLookupTable());
166 map->SetOutputFormatToRGB();
167 viewer->SetInput ( map->GetOutput() );
173 // For a single medical image, it would be more efficient to use
174 // 0028|1050 [DS] [Window Center]
175 // 0028|1051 [DS] [Window Width]
176 // but vtkgdcmReader doesn't know about them :-(
178 if( reader->GetOutput()->GetNumberOfScalarComponents() == 1 )
180 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
181 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
182 viewer->SetColorWindow (range[1] - range[0]);
184 viewer->SetInput ( reader->GetOutput() );
186 viewer->SetupInteractor (iren);
188 // Here is where we setup the observer,
189 vtkgdcmObserver *obs = vtkgdcmObserver::New();
190 obs->ImageViewer = viewer;
191 iren->AddObserver(vtkCommand::CharEvent,obs);
194 #if VTK_MAJOR_VERSION >= 5
195 viewer->Render(); // Don't ask why...
200 //if you wish you can export dicom to a vtk file
201 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
202 writer->SetInput( reader->GetOutput());
203 writer->SetFileName( "foo.vtk" );
204 writer->SetFileTypeToBinary();