]> Creatis software - gdcm.git/blob - vtk/vtkgdcmSerieViewer.cxx
Add vtkgdcmSerieViewer to check new SetCoherentFileList ability of vtkGdcmRedaer
[gdcm.git] / vtk / vtkgdcmSerieViewer.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: vtkgdcmSerieViewer.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/17 04:36:14 $
7   Version:   $Revision: 1.1 $
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() > 0 ) // Why not ? Just an example, for testing
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    reader->SetCoherentFileList(l);
129
130 /*
131    if( argc == 2 )
132       reader->SetFileName( argv[1] );
133    else
134       for(int i=1; i< argc; i++)
135          reader->AddFileName( argv[i] );
136 */
137
138 // TODO : allow user to choose Load Mode
139
140  //  reader->SetLoadMode(NO_SHADOWSEQ);  
141    reader->Update();
142
143    //print debug info:
144    reader->GetOutput()->Print( cout );
145
146    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
147
148    vtkImageViewer *viewer = vtkImageViewer::New();
149
150    if( reader->GetLookupTable() )
151    {
152       //convert to color:
153       vtkImageMapToColors *map = vtkImageMapToColors::New ();
154       map->SetInput (reader->GetOutput());
155       map->SetLookupTable (reader->GetLookupTable());
156       map->SetOutputFormatToRGB();
157       viewer->SetInput ( map->GetOutput() );
158       map->Delete();
159    }
160    else
161    {
162       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
163       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
164       viewer->SetColorWindow (range[1] - range[0]);
165
166       viewer->SetInput ( reader->GetOutput() );
167    }
168    viewer->SetupInteractor (iren);
169   
170    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
171    //viewer->SetColorWindow (range[1] - range[0]);
172    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
173
174    // Here is where we setup the observer, 
175    vtkgdcmObserver *obs = vtkgdcmObserver::New();
176    obs->ImageViewer = viewer;
177    iren->AddObserver(vtkCommand::CharEvent,obs);
178    obs->Delete();
179
180    //viewer->Render();
181    iren->Initialize();
182    iren->Start();
183
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();
189    //writer->Write();
190
191    reader->Delete();
192    iren->Delete();
193    viewer->Delete();
194    writer->Delete();
195
196    return 0;
197 }