/*========================================================================= 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" /* * Inspired by dcm2pnm from DCMTK toolkit * for now it always output PNG file and does not handle very well window/level * since it needs to be stored in the DICOM file... * It also only takes the first window/level from the list */ int main(int argc, char *argv[]) { if( argc < 3 ) { std::cerr << argv[0] << " input.dcm output.pnm" << std::endl; return 1; } const char *filename = argv[1]; const char *outfilename = argv[2]; 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(); #if (VTK_MAJOR_VERSION >= 6) windowlevel->SetInputConnection( reader->GetOutputPort() ); #else windowlevel->SetInput( reader->GetOutput() ); #endif 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(); #if (VTK_MAJOR_VERSION >= 6) actor->SetInputData( windowlevel->GetOutput() ); #else actor->SetInput( windowlevel->GetOutput() ); #endif renderer->AddActor( actor ); renWin->Render(); vtkWindowToImageFilter *w2if = vtkWindowToImageFilter::New(); w2if->SetInput ( renWin ); vtkPNGWriter *wr = vtkPNGWriter::New(); #if (VTK_MAJOR_VERSION >= 6) wr->SetInputConnection( w2if->GetOutputPort() ); #else wr->SetInput( w2if->GetOutput() ); #endif wr->SetFileName ( outfilename ); wr->Write(); reader->Delete(); renWin->Delete(); renderer->Delete(); windowlevel->Delete(); actor->Delete(); w2if->Delete(); wr->Delete(); return 0; }