1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmSerieViewer.cxx,v $
6 Date: $Date: 2005/07/17 04:36:14 $
7 Version: $Revision: 1.1 $
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() ;
115 if ( l->size() > 0 ) // Why not ? Just an example, for testing
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 reader->SetCoherentFileList(l);
132 reader->SetFileName( argv[1] );
134 for(int i=1; i< argc; i++)
135 reader->AddFileName( argv[i] );
138 // TODO : allow user to choose Load Mode
140 // reader->SetLoadMode(NO_SHADOWSEQ);
144 reader->GetOutput()->Print( cout );
146 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
148 vtkImageViewer *viewer = vtkImageViewer::New();
150 if( reader->GetLookupTable() )
153 vtkImageMapToColors *map = vtkImageMapToColors::New ();
154 map->SetInput (reader->GetOutput());
155 map->SetLookupTable (reader->GetLookupTable());
156 map->SetOutputFormatToRGB();
157 viewer->SetInput ( map->GetOutput() );
162 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
163 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
164 viewer->SetColorWindow (range[1] - range[0]);
166 viewer->SetInput ( reader->GetOutput() );
168 viewer->SetupInteractor (iren);
170 //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
171 //viewer->SetColorWindow (range[1] - range[0]);
172 //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
174 // Here is where we setup the observer,
175 vtkgdcmObserver *obs = vtkgdcmObserver::New();
176 obs->ImageViewer = viewer;
177 iren->AddObserver(vtkCommand::CharEvent,obs);
184 //if you wish you can export dicom to a vtk file
185 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
186 writer->SetInput( reader->GetOutput());
187 writer->SetFileName( "foo.vtk" );
188 writer->SetFileTypeToBinary();