]> Creatis software - gdcm.git/blob - vtk/vtkgdcmViewer2.cxx
Temporary modif for vtk reading of illegal 'DICOM FD' (64 bits 'double' pixels)
[gdcm.git] / vtk / vtkgdcmViewer2.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: vtkgdcmViewer2.cxx,v $
5   Language:  C++
6   Date:      $Date: 2009/11/03 14:05:23 $
7   Version:   $Revision: 1.17 $
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 //  * if the last 'filename' == "o", overlays (group 60xx)  are loaded.
31 //  * just a test!
32 // 
33 //----------------------------------------------------------------------------
34 #include <vtkRenderWindowInteractor.h>
35 #if VTK_MAJOR_VERSION >= 5
36 #include "vtkImageColorViewer.h"
37 #else
38 #include <vtkImageViewer2.h>
39 #endif
40 #include <vtkStructuredPoints.h>
41 #include <vtkStructuredPointsWriter.h>
42 #include <vtkPNGWriter.h>
43 #include <vtkWindowToImageFilter.h>
44 #include <vtkRenderWindow.h>
45 #include <vtkCommand.h>
46 #include <vtkRenderer.h>
47 #include <vtkImageMapToColors.h>
48 #include <vtkLookupTable.h>
49
50 #include "vtkGdcmReader.h"
51 #include "gdcmDocument.h"  // for NO_SHADOWSEQ
52 #ifndef vtkFloatingPointType
53 #define vtkFloatingPointType float
54 #endif
55
56 //----------------------------------------------------------------------------
57 // Callback for the interaction
58 class vtkgdcmObserver : public vtkCommand
59 {
60 public:
61    virtual char const *GetClassName() const 
62    { 
63       return "vtkgdcmObserver";
64    }
65    static vtkgdcmObserver *New() 
66    { 
67       return new vtkgdcmObserver; 
68    }
69    vtkgdcmObserver()
70    {
71       this->ImageViewer = NULL;
72    }
73    virtual void Execute(vtkObject *caller, unsigned long event, void* /*callData*/ )
74    {
75       if ( this->ImageViewer )
76       {
77          vtkRenderWindowInteractor * rwi = vtkRenderWindowInteractor::SafeDownCast( caller );
78          char keycode = 0;
79          if( rwi ) keycode = rwi->GetKeyCode();
80          if ( event == vtkCommand::CharEvent && keycode != 's' )
81          {
82 #if VTK_MAJOR_VERSION >= 5
83             int max = ImageViewer->GetSliceMax();
84             int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
85             ImageViewer->SetSlice( slice );
86 #else
87             int max = ImageViewer->GetWholeZMax();
88             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
89             ImageViewer->SetZSlice( slice );
90 #endif
91 #if !( (VTK_MAJOR_VERSION >= 5) || ( VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION >= 5 ) )
92          // This used to be a bug in version VTK 4.4 and earlier
93             ImageViewer->GetRenderer()->ResetCameraClippingRange();
94 #endif
95             ImageViewer->Render();
96          }
97          else if ( keycode == 's' )
98          {
99            vtkPNGWriter * writer = vtkPNGWriter::New();
100            vtkWindowToImageFilter * w2i = vtkWindowToImageFilter::New();
101            w2i->SetInput( rwi->GetRenderWindow() );
102            writer->SetInput( w2i->GetOutput() );
103            writer->SetFileName( "snapshot.png" );
104            writer->Write();
105            //std::cerr << "Screenshort saved to snapshot.png" << std::endl;
106          }
107       }
108    }
109 #if VTK_MAJOR_VERSION >= 5
110    vtkImageColorViewer *ImageViewer;
111 #else
112    vtkImageViewer2 *ImageViewer;
113 #endif
114 };
115
116 // --------------------------------------------------------------------------------
117
118 int main(int argc, char *argv[])
119 {
120    if( argc < 2 )
121       return 0;
122   
123    vtkGdcmReader *reader = vtkGdcmReader::New();
124    reader->AllowLookupTableOff();
125
126
127    if (strcmp (argv[argc-1], "o") == 0)
128    {
129       argc--; // Is it allowed?!?
130       reader->SetKeepOverlays(true);
131    }
132
133 // not a very clever way to pass several params
134 // but it's just for checking
135    if (strcmp (argv[argc-1], "n") == 0)
136    {
137       argc--; // Is it allowed?!?
138       reader->SetFlipY(false);
139    }
140
141    if( argc == 2 )
142       reader->SetFileName( argv[1] );
143    else
144       for(int i=1; i< argc; i++)
145          reader->AddFileName( argv[i] );
146
147 // TODO : allow user to choose Load Mode
148    reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);  
149    reader->Update();
150
151    //print debug info:
152    reader->GetOutput()->Print( cout );
153
154    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
155 #if VTK_MAJOR_VERSION >= 5
156    vtkImageColorViewer *viewer = vtkImageColorViewer::New();
157 #else
158    vtkImageViewer2 *viewer = vtkImageViewer2::New();
159 #endif
160
161    if( reader->GetLookupTable() )
162    {
163       //convert to color:
164       vtkImageMapToColors *map = vtkImageMapToColors::New ();
165       map->SetInput (reader->GetOutput());
166       map->SetLookupTable (reader->GetLookupTable());
167       map->SetOutputFormatToRGB();
168       viewer->SetInput ( map->GetOutput() );
169       map->Delete();
170    }
171    else
172    {
173    
174    // For a single medical image, it would be more efficient to use
175    // 0028|1050 [DS] [Window Center]
176    // 0028|1051 [DS] [Window Width]
177    // but vtkgdcmReader doesn't know about them :-(
178
179    if( reader->GetOutput()->GetNumberOfScalarComponents() == 1 )
180      {
181      vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
182      viewer->SetColorLevel (0.5 * (range[1] + range[0]));
183      viewer->SetColorWindow (range[1] - range[0]);
184      }
185       viewer->SetInput ( reader->GetOutput() );
186    }
187    viewer->SetupInteractor (iren);
188
189    // Here is where we setup the observer, 
190    vtkgdcmObserver *obs = vtkgdcmObserver::New();
191    obs->ImageViewer = viewer;
192    iren->AddObserver(vtkCommand::CharEvent,obs);
193    obs->Delete();
194
195 #if VTK_MAJOR_VERSION >= 5
196    viewer->Render(); // Don't ask why...
197 #endif
198    iren->Initialize();
199    iren->Start();
200
201    //if you wish you can export dicom to a vtk file  
202    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
203    writer->SetInput( reader->GetOutput());
204    writer->SetFileName( "foo.vtk" );
205    writer->SetFileTypeToBinary();
206    //writer->Write();
207
208    reader->Delete();
209    iren->Delete();
210    viewer->Delete();
211    writer->Delete();
212
213    return 0;
214 }