Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

vtkgdcmViewer.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: vtkgdcmViewer.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2005/02/02 20:10:50 $
00007   Version:   $Revision: 1.24 $
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 <vtkImageViewer.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 
00043 #ifndef vtkFloatingPointType
00044 #define vtkFloatingPointType float
00045 #endif
00046 
00047 //----------------------------------------------------------------------------
00048 // Callback for the interaction
00049 class vtkgdcmObserver : public vtkCommand
00050 {
00051 public:
00052    virtual char const *GetClassName() const 
00053    { 
00054       return "vtkgdcmObserver";
00055    }
00056    static vtkgdcmObserver *New() 
00057    { 
00058       return new vtkgdcmObserver; 
00059    }
00060    vtkgdcmObserver()
00061    {
00062       this->ImageViewer = NULL;
00063    }
00064    virtual void Execute(vtkObject *, unsigned long event, void* )
00065    {
00066       if ( this->ImageViewer )
00067       {
00068          if ( event == vtkCommand::CharEvent )
00069          {
00070             int max = ImageViewer->GetWholeZMax();
00071             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
00072             ImageViewer->SetZSlice( slice );
00073             ImageViewer->GetRenderer()->ResetCameraClippingRange();
00074             ImageViewer->Render();
00075          }
00076       }
00077    }
00078    vtkImageViewer *ImageViewer;
00079 };
00080 
00081 
00082 int main(int argc, char *argv[])
00083 {
00084    if( argc < 2 )
00085       return 0;
00086   
00087    vtkGdcmReader *reader = vtkGdcmReader::New();
00088    reader->AllowLookupTableOff();
00089 
00090    if( argc == 2 )
00091       reader->SetFileName( argv[1] );
00092    else
00093       for(int i=1; i< argc; i++)
00094          reader->AddFileName( argv[i] );
00095 
00096    reader->Update();
00097 
00098    //print debug info:
00099    reader->GetOutput()->Print( cout );
00100 
00101    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
00102 
00103    vtkImageViewer *viewer = vtkImageViewer::New();
00104 
00105    if( reader->GetLookupTable() )
00106    {
00107       //convert to color:
00108       vtkImageMapToColors *map = vtkImageMapToColors::New ();
00109       map->SetInput (reader->GetOutput());
00110       map->SetLookupTable (reader->GetLookupTable());
00111       map->SetOutputFormatToRGB();
00112       viewer->SetInput ( map->GetOutput() );
00113       map->Delete();
00114    }
00115    else
00116    {
00117       double *range = reader->GetOutput()->GetScalarRange();
00118       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
00119       viewer->SetColorWindow (range[1] - range[0]);
00120 
00121       viewer->SetInput ( reader->GetOutput() );
00122    }
00123    viewer->SetupInteractor (iren);
00124   
00125    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
00126    //viewer->SetColorWindow (range[1] - range[0]);
00127    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
00128 
00129    // Here is where we setup the observer, 
00130    vtkgdcmObserver *obs = vtkgdcmObserver::New();
00131    obs->ImageViewer = viewer;
00132    iren->AddObserver(vtkCommand::CharEvent,obs);
00133    obs->Delete();
00134 
00135    //viewer->Render();
00136    iren->Initialize();
00137    iren->Start();
00138 
00139    //if you wish you can export dicom to a vtk file  
00140    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
00141    writer->SetInput( reader->GetOutput());
00142    writer->SetFileName( "foo.vtk" );
00143    writer->SetFileTypeToBinary();
00144    //writer->Write();
00145 
00146    reader->Delete();
00147    iren->Delete();
00148    viewer->Delete();
00149    writer->Delete();
00150 
00151    return 0;
00152 }

Generated on Thu Feb 10 22:18:01 2005 for gdcm by doxygen 1.3.6