Skip to content

ImageAdaptor should have its own ComputeOffset, ImageRegionIterator support broken #5870

@N-Dekker

Description

@N-Dekker

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)!

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:BugInconsistencies or issues which will cause an incorrect result under some or all circumstances

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions