1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmViewer.cxx,v $
6 Date: $Date: 2011/03/29 07:36:02 $
7 Version: $Revision: 1.33 $
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 <vtkImageViewer.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
43 #ifndef vtkFloatingPointType
44 #define vtkFloatingPointType float
47 //----------------------------------------------------------------------------
48 // Callback for the interaction
49 class vtkgdcmObserver : public vtkCommand
52 virtual char const *GetClassName() const
54 return "vtkgdcmObserver";
56 static vtkgdcmObserver *New()
58 return new vtkgdcmObserver;
62 this->ImageViewer = NULL;
64 virtual void Execute(vtkObject *, unsigned long event, void* )
66 if ( this->ImageViewer )
68 if ( event == vtkCommand::CharEvent )
70 int max = ImageViewer->GetWholeZMax();
71 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
72 ImageViewer->SetZSlice( slice );
73 ImageViewer->Render();
77 vtkImageViewer *ImageViewer;
81 int main(int argc, char *argv[])
86 vtkGdcmReader *reader = vtkGdcmReader::New();
87 reader->AllowLookupTableOff();
90 reader->SetFileName( argv[1] );
92 for(int i=1; i< argc; i++)
93 reader->AddFileName( argv[i] );
95 // TODO : allow user to choose Load Mode
96 reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);
101 std::cout << "[0][0]==========" <<
102 reader->GetOutput()->GetScalarComponentAsFloat(0,0,0,0) <<
103 "===================="
105 std::cout << "[127][127]==========" <<
106 reader->GetOutput()->GetScalarComponentAsFloat(0,127,0,0) <<
107 "===================="
112 reader->GetOutput()->Print( cout );
114 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
116 vtkImageViewer *viewer = vtkImageViewer::New();
118 if( reader->GetLookupTable() )
121 vtkImageMapToColors *map = vtkImageMapToColors::New ();
122 map->SetInput (reader->GetOutput());
123 map->SetLookupTable (reader->GetLookupTable());
124 map->SetOutputFormatToRGB();
125 viewer->SetInput ( map->GetOutput() );
131 // For a single medical image, it would be more efficient to use
132 // 0028|1050 [DS] [Window Center]
133 // 0028|1051 [DS] [Window Width]
134 // but vtkgdcmReader doesn't know about them :-(
136 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
137 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
138 viewer->SetColorWindow (range[1] - range[0]);
140 viewer->SetInput ( reader->GetOutput() );
142 viewer->SetupInteractor (iren);
144 //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
145 //viewer->SetColorWindow (range[1] - range[0]);
146 //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
148 // Here is where we setup the observer,
149 vtkgdcmObserver *obs = vtkgdcmObserver::New();
150 obs->ImageViewer = viewer;
151 iren->AddObserver(vtkCommand::CharEvent,obs);
158 //if you wish you can export dicom to a vtk file
159 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
160 writer->SetInput( reader->GetOutput());
161 writer->SetFileName( "foo.vtk" );
162 writer->SetFileTypeToBinary();
165 std::cout << "==========" << std::endl;
167 std::cout << "==========" <<
168 reader->GetOutput()->GetScalarComponentAsFloat(0,0,0,0) <<
169 "===================="