GDCM 2.2.0

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Directories
- Examples

offscreenimage.cxx

    /*=========================================================================

      Program: GDCM (Grassroots DICOM). A DICOM library

      Copyright (c) 2006-2011 Mathieu Malaterre
      All rights reserved.
      See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

         This software is distributed WITHOUT ANY WARRANTY; without even
         the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
         PURPOSE.  See the above copyright notice for more information.

    =========================================================================*/
    #include "vtkGDCMImageReader.h"
    #include "vtkRenderWindow.h"
    #include "vtkRenderer.h"
    #include "vtkImageMapToWindowLevelColors.h"
    #include "vtkImageActor.h"
    #include "vtkPNGWriter.h"
    #include "vtkWindowToImageFilter.h"
    #include "vtkMedicalImageProperties.h"

    int main(int argc, char *argv[])
    {
      if( argc < 2 )
        {
        return 1;
        }
      const char *filename = argv[1];

      vtkGDCMImageReader *reader = vtkGDCMImageReader::New();
      reader->SetFileName( filename );
      reader->Update(); // important to read the window/level info

      vtkMedicalImageProperties *prop = reader->GetMedicalImageProperties();

      vtkRenderWindow *renWin = vtkRenderWindow::New();
      renWin->OffScreenRenderingOn();

      vtkRenderer *renderer = vtkRenderer::New();
      renWin->AddRenderer(renderer);

      vtkImageMapToWindowLevelColors *windowlevel = vtkImageMapToWindowLevelColors::New();
      windowlevel->SetInput( reader->GetOutput() );
      unsigned int n = prop->GetNumberOfWindowLevelPresets();
      if( n )
        {
        // Take the first one by default:
        const double *wl = prop->GetNthWindowLevelPreset(0);
        windowlevel->SetWindow( wl[0] );
        windowlevel->SetLevel( wl[1] );
        }

      vtkImageActor *actor = vtkImageActor::New();
      actor->SetInput( windowlevel->GetOutput() );

      renderer->AddActor( actor );

      renWin->Render();

      vtkWindowToImageFilter *w2if = vtkWindowToImageFilter::New();
      w2if->SetInput ( renWin );

      vtkPNGWriter *wr = vtkPNGWriter::New();
      wr->SetInput( w2if->GetOutput() );
      wr->SetFileName ( "offscreenimage.png" );
      wr->Write();

      reader->Delete();
      renWin->Delete();
      renderer->Delete();
      windowlevel->Delete();
      actor->Delete();
      w2if->Delete();
      wr->Delete();

      return 0;
    }