]> Creatis software - gdcm.git/blob - vtk/vtkgdcmSerieViewer.cxx
Fix comments
[gdcm.git] / vtk / vtkgdcmSerieViewer.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: vtkgdcmSerieViewer.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/19 15:28:54 $
7   Version:   $Revision: 1.2 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18 // This example illustrates how the vtkGdcmReader vtk class can be
19 // used in order to:
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.
24 //
25 // Usage:
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 // 
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>
40
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
47 #endif
48
49 //----------------------------------------------------------------------------
50 // Callback for the interaction
51 class vtkgdcmObserver : public vtkCommand
52 {
53 public:
54    virtual char const *GetClassName() const 
55    { 
56       return "vtkgdcmObserver";
57    }
58    static vtkgdcmObserver *New() 
59    { 
60       return new vtkgdcmObserver; 
61    }
62    vtkgdcmObserver()
63    {
64       this->ImageViewer = NULL;
65    }
66    virtual void Execute(vtkObject *, unsigned long event, void* )
67    {
68       if ( this->ImageViewer )
69       {
70          if ( event == vtkCommand::CharEvent )
71          {
72             int max = ImageViewer->GetWholeZMax();
73             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
74             ImageViewer->SetZSlice( slice );
75             ImageViewer->GetRenderer()->ResetCameraClippingRange();
76             ImageViewer->Render();
77          }
78       }
79    }
80    vtkImageViewer *ImageViewer;
81 };
82
83
84 int main(int argc, char *argv[])
85 {
86    if( argc < 2 )
87       return 0;
88
89   if( argc > 2 )
90     gdcm::Debug::DebugOn();
91
92   
93    vtkGdcmReader *reader = vtkGdcmReader::New();
94    reader->AllowLookupTableOff();
95
96    // ------------ to check Coherent File List as a parameter
97
98    gdcm::SerieHelper *sh = new gdcm::SerieHelper();
99    sh->SetLoadMode(NO_SHADOWSEQ);
100    sh->SetDirectory( argv[1], true);
101     
102    // Just to see
103
104    int nbFiles;
105    // For all the Coherent Files lists of the gdcm::Serie
106    gdcm::FileList *l = sh->GetFirstCoherentFileList();
107    if (l == 0 )
108    {
109       std::cout << "Oops! No CoherentFileList found ?!?" << std::endl;
110       return 0;
111    }
112    while (l)
113    { 
114       nbFiles = l->size() ;
115       if ( l->size() > 1 )
116       {
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
120       }
121       else
122       {
123          std::cout << "Oops! Empty CoherentFileList found ?!?" << std::endl;
124       }
125       l = sh->GetNextCoherentFileList();
126    }
127
128    // Only the first FileList is dealt with (just an example)
129    // (The files will not be parsed twice by the reader)
130
131    //---------------------------------------------------------
132    reader->SetCoherentFileList(l);
133    //---------------------------------------------------------
134
135
136 // TODO : allow user to choose Load Mode
137
138  //  reader->SetLoadMode(NO_SHADOWSEQ);  
139    reader->Update();
140
141    //print debug info:
142    reader->GetOutput()->Print( cout );
143
144    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
145
146    vtkImageViewer *viewer = vtkImageViewer::New();
147
148    if( reader->GetLookupTable() )
149    {
150       //convert to color:
151       vtkImageMapToColors *map = vtkImageMapToColors::New ();
152       map->SetInput (reader->GetOutput());
153       map->SetLookupTable (reader->GetLookupTable());
154       map->SetOutputFormatToRGB();
155       viewer->SetInput ( map->GetOutput() );
156       map->Delete();
157    }
158    else
159    {
160       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
161       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
162       viewer->SetColorWindow (range[1] - range[0]);
163
164       viewer->SetInput ( reader->GetOutput() );
165    }
166    viewer->SetupInteractor (iren);
167   
168    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
169    //viewer->SetColorWindow (range[1] - range[0]);
170    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
171
172    // Here is where we setup the observer, 
173    vtkgdcmObserver *obs = vtkgdcmObserver::New();
174    obs->ImageViewer = viewer;
175    iren->AddObserver(vtkCommand::CharEvent,obs);
176    obs->Delete();
177
178    //viewer->Render();
179    iren->Initialize();
180    iren->Start();
181
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();
187    //writer->Write();
188
189    reader->Delete();
190    iren->Delete();
191    viewer->Delete();
192    writer->Delete();
193
194    return 0;
195 }