vtkgdcmViewer2.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: vtkgdcmViewer2.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2007/06/19 13:09:45 $
00007   Version:   $Revision: 1.9 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
00016                                                                                 
00017 =========================================================================*/
00018 // This example illustrates how the vtkGdcmReader vtk class can be
00019 // used in order to:
00020 //  * produce a simple (vtk based) Dicom image STACK VIEWER.
00021 //  * dump the stack considered as a volume in a vtkStructuredPoints
00022 //    vtk file: the vtk gdcm wrappers can be seen as a simple way to convert
00023 //    a stack of Dicom images into a native vtk volume.
00024 //
00025 // Usage:
00026 //  * the filenames of the Dicom images constituting the stack should be
00027 //    given as command line arguments,
00028 //  * you can navigate through the stack by hitting any character key,
00029 //  * the produced vtk file is named "foo.vtk" (in the invocation directory).
00030 // 
00031 //----------------------------------------------------------------------------
00032 #include <vtkRenderWindowInteractor.h>
00033 #include <vtkImageViewer2.h>
00034 #include <vtkStructuredPoints.h>
00035 #include <vtkStructuredPointsWriter.h>
00036 #include <vtkCommand.h>
00037 #include <vtkRenderer.h>
00038 #include <vtkImageMapToColors.h>
00039 #include <vtkLookupTable.h>
00040 
00041 #include "vtkGdcmReader.h"
00042 #include "gdcmDocument.h"  // for NO_SHADOWSEQ
00043 
00044 #ifndef vtkFloatingPointType
00045 #define vtkFloatingPointType float
00046 #endif
00047 
00048 //----------------------------------------------------------------------------
00049 // Callback for the interaction
00050 class vtkgdcmObserver : public vtkCommand
00051 {
00052 public:
00053    virtual char const *GetClassName() const 
00054    { 
00055       return "vtkgdcmObserver";
00056    }
00057    static vtkgdcmObserver *New() 
00058    { 
00059       return new vtkgdcmObserver; 
00060    }
00061    vtkgdcmObserver()
00062    {
00063       this->ImageViewer = NULL;
00064    }
00065    virtual void Execute(vtkObject *, unsigned long event, void* )
00066    {
00067       if ( this->ImageViewer )
00068       {
00069          if ( event == vtkCommand::CharEvent )
00070          {
00071 #if (VTK_MAJOR_VERSION >= 5)
00072             int max = ImageViewer->GetSliceMax();
00073             int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
00074             ImageViewer->SetSlice( slice );
00075 #else
00076             int max = ImageViewer->GetWholeZMax();
00077             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
00078             ImageViewer->SetZSlice( slice );
00079 #endif
00080 #if !( (VTK_MAJOR_VERSION >= 5) || ( VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION >= 5 ) )
00081          // This used to be a bug in version VTK 4.4 and earlier
00082             ImageViewer->GetRenderer()->ResetCameraClippingRange();
00083 #endif
00084             ImageViewer->Render();
00085          }
00086       }
00087    }
00088    vtkImageViewer2 *ImageViewer;
00089 };
00090 
00091 
00092 int main(int argc, char *argv[])
00093 {
00094    if( argc < 2 )
00095       return 0;
00096   
00097    vtkGdcmReader *reader = vtkGdcmReader::New();
00098    reader->AllowLookupTableOff();
00099 
00100    if( argc == 2 )
00101       reader->SetFileName( argv[1] );
00102    else
00103       for(int i=1; i< argc; i++)
00104          reader->AddFileName( argv[i] );
00105 
00106 // TODO : allow user to choose Load Mode
00107    reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);  
00108    reader->Update();
00109 
00110    //print debug info:
00111    reader->GetOutput()->Print( cout );
00112 
00113    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
00114 
00115    vtkImageViewer2 *viewer = vtkImageViewer2::New();
00116 
00117    if( reader->GetLookupTable() )
00118    {
00119       //convert to color:
00120       vtkImageMapToColors *map = vtkImageMapToColors::New ();
00121       map->SetInput (reader->GetOutput());
00122       map->SetLookupTable (reader->GetLookupTable());
00123       map->SetOutputFormatToRGB();
00124       viewer->SetInput ( map->GetOutput() );
00125       map->Delete();
00126    }
00127    else
00128    {
00129    
00130    // For a single medical image, it would be more efficient to use
00131    // 0028|1050 [DS] [Window Center]
00132    // 0028|1051 [DS] [Window Width]
00133    // but vtkgdcmReader doesn't know about them :-(
00134 
00135       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
00136       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
00137       viewer->SetColorWindow (range[1] - range[0]);
00138 
00139       viewer->SetInput ( reader->GetOutput() );
00140    }
00141    viewer->SetupInteractor (iren);
00142   
00143    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
00144    //viewer->SetColorWindow (range[1] - range[0]);
00145    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
00146 
00147    // Here is where we setup the observer, 
00148    vtkgdcmObserver *obs = vtkgdcmObserver::New();
00149    obs->ImageViewer = viewer;
00150    iren->AddObserver(vtkCommand::CharEvent,obs);
00151    obs->Delete();
00152 
00153    //viewer->Render();
00154    iren->Initialize();
00155    iren->Start();
00156 
00157    //if you wish you can export dicom to a vtk file  
00158    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
00159    writer->SetInput( reader->GetOutput());
00160    writer->SetFileName( "foo.vtk" );
00161    writer->SetFileTypeToBinary();
00162    //writer->Write();
00163 
00164    reader->Delete();
00165    iren->Delete();
00166    viewer->Delete();
00167    writer->Delete();
00168 
00169    return 0;
00170 }

Generated on Fri Aug 24 12:53:21 2007 for gdcm by  doxygen 1.4.6