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

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: 2005/08/30 15:13:10 $
00007   Version:   $Revision: 1.5 $
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             int max = ImageViewer->GetWholeZMax();
00072             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
00073             ImageViewer->SetZSlice( slice );
00074             ImageViewer->GetRenderer()->ResetCameraClippingRange();
00075             ImageViewer->Render();
00076          }
00077       }
00078    }
00079    vtkImageViewer2 *ImageViewer;
00080 };
00081 
00082 
00083 int main(int argc, char *argv[])
00084 {
00085    if( argc < 2 )
00086       return 0;
00087   
00088    vtkGdcmReader *reader = vtkGdcmReader::New();
00089    reader->AllowLookupTableOff();
00090 
00091    if( argc == 2 )
00092       reader->SetFileName( argv[1] );
00093    else
00094       for(int i=1; i< argc; i++)
00095          reader->AddFileName( argv[i] );
00096 
00097 // TODO : allow user to choose Load Mode
00098    reader->SetLoadMode(gdcm::LD_NOSHADOWSEQ);  
00099    reader->Update();
00100 
00101    //print debug info:
00102    reader->GetOutput()->Print( cout );
00103 
00104    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
00105 
00106    vtkImageViewer2 *viewer = vtkImageViewer2::New();
00107 
00108    if( reader->GetLookupTable() )
00109    {
00110       //convert to color:
00111       vtkImageMapToColors *map = vtkImageMapToColors::New ();
00112       map->SetInput (reader->GetOutput());
00113       map->SetLookupTable (reader->GetLookupTable());
00114       map->SetOutputFormatToRGB();
00115       viewer->SetInput ( map->GetOutput() );
00116       map->Delete();
00117    }
00118    else
00119    {
00120       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
00121       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
00122       viewer->SetColorWindow (range[1] - range[0]);
00123 
00124       viewer->SetInput ( reader->GetOutput() );
00125    }
00126    viewer->SetupInteractor (iren);
00127   
00128    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
00129    //viewer->SetColorWindow (range[1] - range[0]);
00130    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
00131 
00132    // Here is where we setup the observer, 
00133    vtkgdcmObserver *obs = vtkgdcmObserver::New();
00134    obs->ImageViewer = viewer;
00135    iren->AddObserver(vtkCommand::CharEvent,obs);
00136    obs->Delete();
00137 
00138    //viewer->Render();
00139    iren->Initialize();
00140    iren->Start();
00141 
00142    //if you wish you can export dicom to a vtk file  
00143    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
00144    writer->SetInput( reader->GetOutput());
00145    writer->SetFileName( "foo.vtk" );
00146    writer->SetFileTypeToBinary();
00147    //writer->Write();
00148 
00149    reader->Delete();
00150    iren->Delete();
00151    viewer->Delete();
00152    writer->Delete();
00153 
00154    return 0;
00155 }

Generated on Fri Jan 20 10:14:26 2006 for gdcm by  doxygen 1.4.4