1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmSerieViewer.cxx,v $
6 Date: $Date: 2005/07/19 15:28:54 $
7 Version: $Revision: 1.2 $
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 #include "gdcmSerieHelper.h"
44 #include "gdcmDebug.h"
45 #ifndef vtkFloatingPointType
46 #define vtkFloatingPointType float
49 //----------------------------------------------------------------------------
50 // Callback for the interaction
51 class vtkgdcmObserver : public vtkCommand
54 virtual char const *GetClassName() const
56 return "vtkgdcmObserver";
58 static vtkgdcmObserver *New()
60 return new vtkgdcmObserver;
64 this->ImageViewer = NULL;
66 virtual void Execute(vtkObject *, unsigned long event, void* )
68 if ( this->ImageViewer )
70 if ( event == vtkCommand::CharEvent )
72 int max = ImageViewer->GetWholeZMax();
73 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
74 ImageViewer->SetZSlice( slice );
75 ImageViewer->GetRenderer()->ResetCameraClippingRange();
76 ImageViewer->Render();
80 vtkImageViewer *ImageViewer;
84 int main(int argc, char *argv[])
90 gdcm::Debug::DebugOn();
93 vtkGdcmReader *reader = vtkGdcmReader::New();
94 reader->AllowLookupTableOff();
96 // ------------ to check Coherent File List as a parameter
98 gdcm::SerieHelper *sh = new gdcm::SerieHelper();
99 sh->SetLoadMode(NO_SHADOWSEQ);
100 sh->SetDirectory( argv[1], true);
105 // For all the Coherent Files lists of the gdcm::Serie
106 gdcm::FileList *l = sh->GetFirstCoherentFileList();
109 std::cout << "Oops! No CoherentFileList found ?!?" << std::endl;
114 nbFiles = l->size() ;
117 std::cout << "Sort list : " << nbFiles << " long" << std::endl;
118 sh->OrderFileList(l); // sort the list
119 break; // The first one is OK. user will have to check
123 std::cout << "Oops! Empty CoherentFileList found ?!?" << std::endl;
125 l = sh->GetNextCoherentFileList();
128 // Only the first FileList is dealt with (just an example)
129 // (The files will not be parsed twice by the reader)
131 //---------------------------------------------------------
132 reader->SetCoherentFileList(l);
133 //---------------------------------------------------------
136 // TODO : allow user to choose Load Mode
138 // reader->SetLoadMode(NO_SHADOWSEQ);
142 reader->GetOutput()->Print( cout );
144 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
146 vtkImageViewer *viewer = vtkImageViewer::New();
148 if( reader->GetLookupTable() )
151 vtkImageMapToColors *map = vtkImageMapToColors::New ();
152 map->SetInput (reader->GetOutput());
153 map->SetLookupTable (reader->GetLookupTable());
154 map->SetOutputFormatToRGB();
155 viewer->SetInput ( map->GetOutput() );
160 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
161 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
162 viewer->SetColorWindow (range[1] - range[0]);
164 viewer->SetInput ( reader->GetOutput() );
166 viewer->SetupInteractor (iren);
168 //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
169 //viewer->SetColorWindow (range[1] - range[0]);
170 //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
172 // Here is where we setup the observer,
173 vtkgdcmObserver *obs = vtkgdcmObserver::New();
174 obs->ImageViewer = viewer;
175 iren->AddObserver(vtkCommand::CharEvent,obs);
182 //if you wish you can export dicom to a vtk file
183 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
184 writer->SetInput( reader->GetOutput());
185 writer->SetFileName( "foo.vtk" );
186 writer->SetFileTypeToBinary();