1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmViewer2.cxx,v $
6 Date: $Date: 2011/03/29 07:36:02 $
7 Version: $Revision: 1.19 $
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 <vtkMetaImageWriter.h>
44 #include <vtkWindowToImageFilter.h>
45 #include <vtkRenderWindow.h>
46 #include <vtkCommand.h>
47 #include <vtkRenderer.h>
48 #include <vtkImageMapToColors.h>
49 #include <vtkLookupTable.h>
51 #include "vtkGdcmReader.h"
52 #include "gdcmDocument.h" // for NO_SHADOWSEQ
53 #ifndef vtkFloatingPointType
54 #define vtkFloatingPointType float
57 //----------------------------------------------------------------------------
58 // Callback for the interaction
59 class vtkgdcmObserver : public vtkCommand
62 virtual char const *GetClassName() const
64 return "vtkgdcmObserver";
66 static vtkgdcmObserver *New()
68 return new vtkgdcmObserver;
72 this->ImageViewer = NULL;
74 virtual void Execute(vtkObject *caller, unsigned long event, void* /*callData*/ )
76 if ( this->ImageViewer )
78 vtkRenderWindowInteractor * rwi = vtkRenderWindowInteractor::SafeDownCast( caller );
80 if( rwi ) keycode = rwi->GetKeyCode();
81 if ( event == vtkCommand::CharEvent && keycode != 's' )
83 #if VTK_MAJOR_VERSION >= 5
84 int max = ImageViewer->GetSliceMax();
85 int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
86 ImageViewer->SetSlice( slice );
88 int max = ImageViewer->GetWholeZMax();
89 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
90 ImageViewer->SetZSlice( slice );
92 #if !( (VTK_MAJOR_VERSION >= 5) || ( VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION >= 5 ) )
93 // This used to be a bug in version VTK 4.4 and earlier
94 ImageViewer->GetRenderer()->ResetCameraClippingRange();
96 ImageViewer->Render();
98 else if ( keycode == 's' )
100 vtkPNGWriter * writer = vtkPNGWriter::New();
101 vtkWindowToImageFilter * w2i = vtkWindowToImageFilter::New();
102 w2i->SetInput( rwi->GetRenderWindow() );
103 writer->SetInput( w2i->GetOutput() );
104 writer->SetFileName( "snapshot.png" );
106 //std::cerr << "Screenshort saved to snapshot.png" << std::endl;
110 #if VTK_MAJOR_VERSION >= 5
111 vtkImageColorViewer *ImageViewer;
113 vtkImageViewer2 *ImageViewer;
117 // --------------------------------------------------------------------------------
119 int main(int argc, char *argv[])
124 bool metaWrite = false;
126 vtkGdcmReader *reader = vtkGdcmReader::New();
127 reader->AllowLookupTableOff();
130 if (strcmp (argv[argc-1], "o") == 0)
132 argc--; // Is it allowed?!?
133 reader->SetKeepOverlays(true);
136 // not a very clever way to pass several params
137 // but it's just for checking
138 if (strcmp (argv[argc-1], "n") == 0)
140 argc--; // Is it allowed?!?
141 reader->SetFlipY(false);
144 if (strcmp (argv[argc-1], "m") == 0)
146 argc--; // Is it allowed?!?
151 reader->SetFileName( argv[1] );
153 for(int i=1; i< argc; i++)
154 reader->AddFileName( argv[i] );
156 // TODO : allow user to choose Load Mode
157 reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);
161 reader->GetOutput()->Print( cout );
164 //if you wish you can export dicom to a .mhd file
166 std::cout << "try to write .mhd" << std::endl;
167 vtkMetaImageWriter* w = vtkMetaImageWriter::New();
168 w->SetInput( reader->GetOutput());
169 w->SetFileName( "/home/jpr/Desktop/toto.mhd" );
170 w->SetFileDimensionality(3);
171 w->SetCompression(false);
175 std::cout << "end write .mhd" << std::endl;
178 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
179 #if VTK_MAJOR_VERSION >= 5
180 vtkImageColorViewer *viewer = vtkImageColorViewer::New();
182 vtkImageViewer2 *viewer = vtkImageViewer2::New();
185 if( reader->GetLookupTable() )
188 vtkImageMapToColors *map = vtkImageMapToColors::New ();
189 map->SetInput (reader->GetOutput());
190 map->SetLookupTable (reader->GetLookupTable());
191 map->SetOutputFormatToRGB();
192 viewer->SetInput ( map->GetOutput() );
198 // For a single medical image, it would be more efficient to use
199 // 0028|1050 [DS] [Window Center]
200 // 0028|1051 [DS] [Window Width]
201 // but vtkgdcmReader doesn't know about them :-(
203 if( reader->GetOutput()->GetNumberOfScalarComponents() == 1 )
205 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
206 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
207 viewer->SetColorWindow (range[1] - range[0]);
209 viewer->SetInput ( reader->GetOutput() );
211 viewer->SetupInteractor (iren);
213 // Here is where we setup the observer,
214 vtkgdcmObserver *obs = vtkgdcmObserver::New();
215 obs->ImageViewer = viewer;
216 iren->AddObserver(vtkCommand::CharEvent,obs);
219 #if VTK_MAJOR_VERSION >= 5
220 viewer->Render(); // Don't ask why...
225 //if you wish you can export dicom to a vtk file
226 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
227 writer->SetInput( reader->GetOutput());
228 writer->SetFileName( "foo.vtk" );
229 writer->SetFileTypeToBinary();
232 //if you wish you can export dicom to a .mhd file
235 vtkMetaImageWriter* w = vtkMetaImageWriter::New();
236 w->SetInput( reader->GetOutput());
237 w->SetFileName( "foo.mhd" );
238 w->SetCompression(false);