Skip to content

Instantly share code, notes, and snippets.

@dgobbi
Last active November 9, 2015 20:32
Show Gist options
  • Save dgobbi/f7facf51bc0c27266dc6 to your computer and use it in GitHub Desktop.
Save dgobbi/f7facf51bc0c27266dc6 to your computer and use it in GitHub Desktop.
An example of using a writer that automatically streams image data through VTK.
//
// A simple example of image streaming, using a reader that can stream
// from a file and a writer than can stream to a file.
//
// Currently, the only image writer in VTK that streams from its input
// is the vtkMINCImageWriter.
//
// Please send any comments to david.gobbi@gmail.com
//
/***************************************
# The CMakeLists.txt for ImageStreaming.cxx
cmake_minimum_required(VERSION 2.8)
find_package(VTK)
include(${VTK_USE_FILE})
add_executable(ImageStreaming ImageStreaming.cxx)
target_link_libraries(ImageStreaming ${VTK_LIBRARIES})
****************************************/
#include <vtkImageReader2.h>
#include <vtkMINCImageWriter.h>
#include <vtkSmartPointer.h>
#include <vtkImageCast.h>
#include <vtkCommand.h>
#include <vtkInformation.h>
#include <vtkStreamingDemandDrivenPipeline.h>
#include <vtkImageData.h>
#include <iostream>
// This silences error printing when CanReadFile is testing a file.
class Reporter : public vtkCommand
{
public:
static Reporter *New() { return new Reporter; }
vtkTypeMacro(Reporter,vtkCommand);
virtual void Execute(
vtkObject *caller, unsigned long eventId, void *callData);
protected:
Reporter() {};
Reporter(const Reporter& c) : vtkCommand(c) {}
void operator=(const Reporter&) {}
};
void Reporter::Execute(vtkObject *obj, unsigned long, void *)
{
// This method is called for EndEvent (after execution)
vtkAlgorithm *alg = vtkAlgorithm::SafeDownCast(obj);
vtkExecutive *executive = alg->GetExecutive();
// For this test, we check the input executive
vtkInformation *info = executive->GetInputInformation(0, 0);
if (info)
{
// Specifically check the extent of the data object
vtkImageData *data =
vtkImageData::SafeDownCast(
info->Get(vtkDataObject::DATA_OBJECT()));
if (data)
{
int extent[6];
data->GetExtent(extent);
// Print out the extent that was streamed from the input
std::cout << obj->GetClassName() << " streamed ["
<< extent[0] << "," << extent[1] << ","
<< extent[2] << "," << extent[3] << ","
<< extent[4] << "," << extent[5] << "]" << std::endl;
}
else
{
std::cout << obj->GetClassName() << ": no input data." << std::endl;
}
}
else
{
std::cout << obj->GetClassName() << ": no input executive." << std::endl;
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " /path/to/headsq/quarter\n";
std::cerr << "This will write a file \"test.mnc\""
" to the current directory.\n";
return 1;
}
vtkSmartPointer<vtkImageReader2> reader =
vtkSmartPointer<vtkImageReader2>::New();
reader->SetDataByteOrderToLittleEndian();
reader->SetDataExtent(0,63,0,63,1,93);
reader->SetDataSpacing(3.2, 3.2, 1.5);
reader->SetFilePrefix(argv[1]);
vtkSmartPointer<vtkImageCast> filter =
vtkSmartPointer<vtkImageCast>::New();
filter->SetOutputScalarTypeToShort();
filter->ClampOverflowOn();
filter->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkMINCImageWriter> writer =
vtkSmartPointer<vtkMINCImageWriter>::New();
writer->SetFileName("test.mnc");
writer->SetInputConnection(filter->GetOutputPort());
vtkSmartPointer<Reporter> observer =
vtkSmartPointer<Reporter>::New();
filter->AddObserver(vtkCommand::EndEvent, observer);
writer->AddObserver(vtkCommand::EndEvent, observer);
writer->Write();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment