Description
ImageRegionIterator does not work well with ImageAdaptor. For example, the following for loop appears to hang forever, because the iterator never reaches the end:
for (ImageRegionIterator iterator(adaptor, region); !iterator.IsAtEnd(); ++iterator)
{
iterator.Set(42);
}
Steps to Reproduce
#include "itkImage.h"
#include "itkImageAdaptor.h"
#include "itkImageRegionIterator.h"
#include <gtest/gtest.h>
TEST(ImageAdapter, SupportsRegionIterator)
{
class MyPixelAccessor
{
public:
using ExternalType = int;
using InternalType = int;
static void
Set(int & output, const int & input)
{
output = input;
}
static int
Get(const int & input)
{
return input;
}
};
using ImageType = itk::Image<int>;
const auto image = ImageType::New();
const auto adaptor = itk::ImageAdaptor<ImageType, MyPixelAccessor>::New();
adaptor->SetImage(image);
adaptor->SetRegions(itk::Size<>{ 4, 4 });
adaptor->Allocate();
const itk::ImageRegion region{ itk::Index<>{ 1, 1 }, itk::Size<>{ 2, 2 } };
// Loops "forever" on a small (2x2) region!
for (itk::ImageRegionIterator iterator(adaptor, region); !iterator.IsAtEnd(); ++iterator)
{
iterator.Set(42);
}
}
Expected behavior
The for loop should be executed quickly, for such a small region.
Actual behavior
The for loop takes forever (endless loop).
Reproducibility
100%
Versions
main branch, commit c29a8e1
Additional Information
It appears that ImageRegionConstIterator::Increment() gets the wrong value from m_Image->ComputeOffset(ind), here:
|
this->m_Offset = this->m_Image->ComputeOffset(ind); |
I think this should be solved by adding a ComputeOffset member function to itk::ImageAdaptor, which simply calls ComputeOffset on its internal image (not on its base class)!
Description
ImageRegionIteratordoes not work well withImageAdaptor. For example, the followingforloop appears to hang forever, because the iterator never reaches the end:Steps to Reproduce
Expected behavior
The
forloop should be executed quickly, for such a small region.Actual behavior
The
forloop takes forever (endless loop).Reproducibility
100%
Versions
main branch, commit c29a8e1
Additional Information
It appears that
ImageRegionConstIterator::Increment()gets the wrong value fromm_Image->ComputeOffset(ind), here:ITK/Modules/Core/Common/include/itkImageRegionConstIterator.hxx
Line 67 in fcee3e5
I think this should be solved by adding a
ComputeOffsetmember function toitk::ImageAdaptor, which simply callsComputeOffseton its internal image (not on its base class)!