Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 127 additions & 123 deletions Modules/IO/HDF5/test/itkHDF5ImageIOStreamingReadWriteTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -91,145 +91,149 @@ template <typename TPixel>
int
HDF5ReadWriteTest2(const char * fileName)
{
// Define image type.
using ImageType = typename itk::Image<TPixel, 3>;

// Create a source object (in this case a constant image).
typename ImageType::SizeType size;
size[2] = 5;
size[1] = 5;
size[0] = 5;
const typename itk::DemoImageSource<ImageType>::Pointer imageSource = itk::DemoImageSource<ImageType>::New();
imageSource->SetValue(static_cast<TPixel>(23)); // Not used.
imageSource->SetSize(size);

// Write image with streaming.
using WriterType = typename itk::ImageFileWriter<ImageType>;
auto writer = WriterType::New();
using MonitorFilterType = typename itk::PipelineMonitorImageFilter<ImageType>;
auto writerMonitor = MonitorFilterType::New();
writerMonitor->SetInput(imageSource->GetOutput());
writer->SetFileName(fileName);
writer->SetInput(writerMonitor->GetOutput());
writer->SetNumberOfStreamDivisions(5);
try
{
writer->Write();
}
catch (const itk::ExceptionObject & err)
{
std::cout << "itkHDF5ImageIOTest" << std::endl << "Exception Object caught: " << std::endl << err << std::endl;
return EXIT_FAILURE;
}
{ // START an isolation block for the streaming readers and writers.
// For streaming to work, the itk::ImageFileReader / itk::ImageFileWriter
// pipeline and the itk::StreamingImageFilter will maintain handles to open HDF5 files
// until their destructors run.

// Check streaming regions.
if (!writerMonitor->VerifyInputFilterExecutedStreaming(5))
{
return EXIT_FAILURE;
}
typename ImageType::RegionType expectedRegion;
expectedRegion.SetIndex(0, 0);
expectedRegion.SetIndex(1, 0);
expectedRegion.SetSize(0, 5);
expectedRegion.SetSize(1, 5);
expectedRegion.SetSize(2, 1);
typename MonitorFilterType::RegionVectorType writerRegionVector = writerMonitor->GetUpdatedBufferedRegions();
for (typename ImageType::RegionType::IndexValueType iRegion = 0; iRegion < 5; ++iRegion)
{
expectedRegion.SetIndex(2, iRegion);
if (writerRegionVector[iRegion] != expectedRegion)
using ImageType = typename itk::Image<TPixel, 3>;

// Create a source object (in this case a constant image).
typename ImageType::SizeType size;
size[2] = 5;
size[1] = 5;
size[0] = 5;
const typename itk::DemoImageSource<ImageType>::Pointer imageSource = itk::DemoImageSource<ImageType>::New();
imageSource->SetValue(static_cast<TPixel>(23)); // Not used.
imageSource->SetSize(size);

// Write image with streaming.
using WriterType = typename itk::ImageFileWriter<ImageType>;
auto writer = WriterType::New();
using MonitorFilterType = typename itk::PipelineMonitorImageFilter<ImageType>;
auto writerMonitor = MonitorFilterType::New();
writerMonitor->SetInput(imageSource->GetOutput());
writer->SetFileName(fileName);
writer->SetInput(writerMonitor->GetOutput());
writer->SetNumberOfStreamDivisions(5);
try
{
std::cout << "Written image region number " << iRegion << " :" << writerRegionVector[iRegion]
<< " doesn't match expected one: " << expectedRegion << std::endl;
writer->Write();
}
catch (const itk::ExceptionObject & err)
{
std::cout << "itkHDF5ImageIOTest" << std::endl << "Exception Object caught: " << std::endl << err << std::endl;
return EXIT_FAILURE;
}
}

// Force writer close.
writer = typename WriterType::Pointer();

// Read image with streaming.
using ReaderType = typename itk::ImageFileReader<ImageType>;
auto reader = ReaderType::New();
reader->SetFileName(fileName);
reader->SetUseStreaming(true);
auto readerMonitor = MonitorFilterType::New();
readerMonitor->SetInput(reader->GetOutput());
using StreamingFilter = typename itk::StreamingImageFilter<ImageType, ImageType>;
auto streamer = StreamingFilter::New();
streamer->SetInput(readerMonitor->GetOutput());
streamer->SetNumberOfStreamDivisions(5);
typename ImageType::Pointer image;
try
{
streamer->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << "itkHDF5ImageIOTest" << std::endl << "Exception Object caught: " << std::endl << err << std::endl;
return EXIT_FAILURE;
}
image = streamer->GetOutput();

// Check the largest possible and buffered regions.
expectedRegion.SetIndex(0, 0);
expectedRegion.SetIndex(1, 0);
expectedRegion.SetIndex(2, 0);
expectedRegion.SetSize(0, 5);
expectedRegion.SetSize(1, 5);
expectedRegion.SetSize(2, 5);
if (image->GetLargestPossibleRegion() != expectedRegion)
{
std::cout << "Read image largest possible region: " << image->GetLargestPossibleRegion()
<< " doesn't match expected one: " << expectedRegion << std::endl;
}
if (image->GetBufferedRegion() != expectedRegion)
{
std::cout << "Read image buffered region: " << image->GetBufferedRegion()
<< " doesn't match expected one: " << expectedRegion << std::endl;
}

// Check image pixel values.
itk::ImageRegionIterator<ImageType> it(image, image->GetLargestPossibleRegion());
typename ImageType::IndexType idx;
for (it.GoToBegin(); !it.IsAtEnd(); ++it)
{
idx = it.GetIndex();
const TPixel origValue(idx[2] * 100 + idx[1] * 10 + idx[0]);
if (itk::Math::NotAlmostEquals(it.Get(), origValue))
// Check streaming regions.
if (!writerMonitor->VerifyInputFilterExecutedStreaming(5))
{
std::cout << "Original Pixel (" << origValue << ") doesn't match read-in Pixel (" << it.Get() << ')' << std::endl;
return EXIT_FAILURE;
}
}
typename ImageType::RegionType expectedRegion;
expectedRegion.SetIndex(0, 0);
expectedRegion.SetIndex(1, 0);
expectedRegion.SetSize(0, 5);
expectedRegion.SetSize(1, 5);
expectedRegion.SetSize(2, 1);
typename MonitorFilterType::RegionVectorType writerRegionVector = writerMonitor->GetUpdatedBufferedRegions();
for (typename ImageType::RegionType::IndexValueType iRegion = 0; iRegion < 5; ++iRegion)
{
expectedRegion.SetIndex(2, iRegion);
if (writerRegionVector[iRegion] != expectedRegion)
{
std::cout << "Written image region number " << iRegion << " :" << writerRegionVector[iRegion]
<< " doesn't match expected one: " << expectedRegion << std::endl;
return EXIT_FAILURE;
}
}

// Check number of streaming regions.
if (!readerMonitor->VerifyInputFilterExecutedStreaming(5))
{
return EXIT_FAILURE;
}
// Force writer close.
writer = typename WriterType::Pointer();

// Check streaming regions.
typename MonitorFilterType::RegionVectorType readerRegionVector = readerMonitor->GetUpdatedBufferedRegions();
expectedRegion.SetIndex(0, 0);
expectedRegion.SetIndex(1, 0);
expectedRegion.SetSize(0, 5);
expectedRegion.SetSize(1, 5);
expectedRegion.SetSize(2, 1);
for (typename ImageType::RegionType::IndexValueType iRegion = 0; iRegion < 5; ++iRegion)
{
expectedRegion.SetIndex(2, iRegion);
if (readerRegionVector[iRegion] != expectedRegion)
// Read image with streaming.
using ReaderType = typename itk::ImageFileReader<ImageType>;
auto reader = ReaderType::New();
reader->SetFileName(fileName);
reader->SetUseStreaming(true);
auto readerMonitor = MonitorFilterType::New();
readerMonitor->SetInput(reader->GetOutput());
using StreamingFilter = typename itk::StreamingImageFilter<ImageType, ImageType>;
auto streamer = StreamingFilter::New();
streamer->SetInput(readerMonitor->GetOutput());
streamer->SetNumberOfStreamDivisions(5);
typename ImageType::Pointer image;
try
{
streamer->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << "itkHDF5ImageIOTest" << std::endl << "Exception Object caught: " << std::endl << err << std::endl;
return EXIT_FAILURE;
}
image = streamer->GetOutput();

// Check the largest possible and buffered regions.
expectedRegion.SetIndex(0, 0);
expectedRegion.SetIndex(1, 0);
expectedRegion.SetIndex(2, 0);
expectedRegion.SetSize(0, 5);
expectedRegion.SetSize(1, 5);
expectedRegion.SetSize(2, 5);
if (image->GetLargestPossibleRegion() != expectedRegion)
{
std::cout << "Read image largest possible region: " << image->GetLargestPossibleRegion()
<< " doesn't match expected one: " << expectedRegion << std::endl;
}
if (image->GetBufferedRegion() != expectedRegion)
{
std::cout << "Read image region number " << iRegion << " :" << readerRegionVector[iRegion]
std::cout << "Read image buffered region: " << image->GetBufferedRegion()
<< " doesn't match expected one: " << expectedRegion << std::endl;
}

// Check image pixel values.
itk::ImageRegionIterator<ImageType> it(image, image->GetLargestPossibleRegion());
typename ImageType::IndexType idx;
for (it.GoToBegin(); !it.IsAtEnd(); ++it)
{
idx = it.GetIndex();
const TPixel origValue(idx[2] * 100 + idx[1] * 10 + idx[0]);
if (itk::Math::NotAlmostEquals(it.Get(), origValue))
{
std::cout << "Original Pixel (" << origValue << ") doesn't match read-in Pixel (" << it.Get() << ')'
<< std::endl;
return EXIT_FAILURE;
}
}

// Check number of streaming regions.
if (!readerMonitor->VerifyInputFilterExecutedStreaming(5))
{
return EXIT_FAILURE;
}
}

// Check streaming regions.
typename MonitorFilterType::RegionVectorType readerRegionVector = readerMonitor->GetUpdatedBufferedRegions();
expectedRegion.SetIndex(0, 0);
expectedRegion.SetIndex(1, 0);
expectedRegion.SetSize(0, 5);
expectedRegion.SetSize(1, 5);
expectedRegion.SetSize(2, 1);
for (typename ImageType::RegionType::IndexValueType iRegion = 0; iRegion < 5; ++iRegion)
{
expectedRegion.SetIndex(2, iRegion);
if (readerRegionVector[iRegion] != expectedRegion)
{
std::cout << "Read image region number " << iRegion << " :" << readerRegionVector[iRegion]
<< " doesn't match expected one: " << expectedRegion << std::endl;
return EXIT_FAILURE;
}
}
} // END the streaming isolation block to force destructors and close files
// Clean working directory.
itk::IOTestHelper::Remove(fileName);

itk::IOTestHelper::Remove(fileName); // Must ensure that the file is closed before removing the file.
return EXIT_SUCCESS;
}

Expand Down
Loading