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: 2007/06/19 13:09:45 $
00007   Version:   $Revision: 1.29 $
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 #include "gdcmDocument.h"  // for NO_SHADOWSEQ
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 // TODO : allow user to choose Load Mode
00097    reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);  
00098    reader->Update();
00099 
00100    //print debug info:
00101    reader->GetOutput()->Print( cout );
00102 
00103    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
00104 
00105    vtkImageViewer *viewer = vtkImageViewer::New();
00106 
00107    if( reader->GetLookupTable() )
00108    {
00109       //convert to color:
00110       vtkImageMapToColors *map = vtkImageMapToColors::New ();
00111       map->SetInput (reader->GetOutput());
00112       map->SetLookupTable (reader->GetLookupTable());
00113       map->SetOutputFormatToRGB();
00114       viewer->SetInput ( map->GetOutput() );
00115       map->Delete();
00116    }
00117    else
00118    {
00119       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
00120       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
00121       viewer->SetColorWindow (range[1] - range[0]);
00122 
00123       viewer->SetInput ( reader->GetOutput() );
00124    }
00125    viewer->SetupInteractor (iren);
00126   
00127    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
00128    //viewer->SetColorWindow (range[1] - range[0]);
00129    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
00130 
00131    // Here is where we setup the observer, 
00132    vtkgdcmObserver *obs = vtkgdcmObserver::New();
00133    obs->ImageViewer = viewer;
00134    iren->AddObserver(vtkCommand::CharEvent,obs);
00135    obs->Delete();
00136 
00137    //viewer->Render();
00138    iren->Initialize();
00139    iren->Start();
00140 
00141    //if you wish you can export dicom to a vtk file  
00142    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
00143    writer->SetInput( reader->GetOutput());
00144    writer->SetFileName( "foo.vtk" );
00145    writer->SetFileTypeToBinary();
00146    //writer->Write();
00147 
00148    reader->Delete();
00149    iren->Delete();
00150    viewer->Delete();
00151    writer->Delete();
00152 
00153    return 0;
00154 }

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