1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// First include the header file to be tested:
#include "itkImageRegionRange.h"
#include "itkImage.h"
#include "itkImageBufferRange.h"
#include "itkImageRegionIterator.h"
#include "itkMacro.h" // For itkNotUsed.
#include "itkRGBPixel.h"
#include "itkRangeGTestUtilities.h"
#include "itkVectorImage.h"
#include <gtest/gtest.h>
#include <algorithm> // For std::reverse_copy, std::equal, etc.
#include <numeric> // For std::inner_product
#include <type_traits> // For std::is_reference.
// Test template instantiations for various ImageDimension values, and const Image:
template class itk::ImageRegionRange<itk::Image<short, 1>>;
template class itk::ImageRegionRange<itk::Image<short, 2>>;
template class itk::ImageRegionRange<itk::Image<short, 3>>;
template class itk::ImageRegionRange<itk::Image<short, 4>>;
template class itk::ImageRegionRange<const itk::Image<short>>;
template class itk::ImageRegionRange<itk::VectorImage<short>>;
template class itk::ImageRegionRange<const itk::VectorImage<short>>;
template class itk::ImageRegionRange<itk::Image<itk::RGBPixel<uint8_t>>>;
template class itk::ImageRegionRange<itk::Image<itk::Vector<long, 11>>>;
using itk::ImageRegionRange;
namespace
{
// Tells whether or not ImageRegionRange<TImage>::iterator::operator*() returns a reference.
// (If it does not return a reference, it actually returns a proxy to the pixel.)
template <typename TImage>
constexpr bool
DoesImageRegionRangeIteratorDereferenceOperatorReturnReference()
{
using IteratorType = typename ImageRegionRange<TImage>::iterator;
return std::is_reference_v<decltype(*std::declval<IteratorType>())>;
}
static_assert(DoesImageRegionRangeIteratorDereferenceOperatorReturnReference<itk::Image<int>>(),
"ImageRegionRange::iterator::operator*() should return a reference for an itk::Image.");
static_assert(DoesImageRegionRangeIteratorDereferenceOperatorReturnReference<const itk::Image<int>>(),
"ImageRegionRange::iterator::operator*() should return a reference for a 'const' itk::Image.");
static_assert(!DoesImageRegionRangeIteratorDereferenceOperatorReturnReference<itk::VectorImage<int>>(),
"ImageRegionRange::iterator::operator*() should not return a reference for an itk::VectorImage.");
static_assert(!DoesImageRegionRangeIteratorDereferenceOperatorReturnReference<const itk::VectorImage<int>>(),
"ImageRegionRange::iterator::operator*() should not return a reference for a 'const' itk::VectorImage.");
// Tells whether or not ImageRegionRange<TImage>::iterator is the same type
// as ImageRegionRange<TImage>::const_iterator.
template <typename TImage>
constexpr bool
IsIteratorTypeTheSameAsConstIteratorType()
{
using RangeType = ImageRegionRange<TImage>;
return std::is_same_v<typename RangeType::iterator, typename RangeType::const_iterator>;
}
static_assert(!IsIteratorTypeTheSameAsConstIteratorType<itk::Image<int>>() &&
!IsIteratorTypeTheSameAsConstIteratorType<itk::VectorImage<int>>(),
"For a non-const image, non-const iterator and const_iterator should be different types!");
static_assert(IsIteratorTypeTheSameAsConstIteratorType<const itk::Image<int>>() &&
IsIteratorTypeTheSameAsConstIteratorType<const itk::VectorImage<int>>(),
"For a const image, non-const iterator and const_iterator should be the same type!");
template <typename TImage>
typename TImage::Pointer
CreateImage(const unsigned int sizeX, const unsigned int sizeY)
{
const auto image = TImage::New();
const typename TImage::SizeType imageSize = { { sizeX, sizeY } };
image->SetRegions(imageSize);
image->Allocate();
return image;
}
// Creates a test image, filled with a sequence of natural numbers, 1, 2, 3, ..., N.
template <typename TImage>
typename TImage::Pointer
CreateImageFilledWithSequenceOfNaturalNumbers(const unsigned int sizeX, const unsigned int sizeY)
{
using PixelType = typename TImage::PixelType;
const auto image = CreateImage<TImage>(sizeX, sizeY);
const itk::ImageBufferRange imageBufferRange{ *image };
std::iota(imageBufferRange.begin(), imageBufferRange.end(), PixelType{ 1 });
return image;
}
template <typename TPixel, unsigned int VImageDimension>
void
SetVectorLengthIfImageIsVectorImage(itk::VectorImage<TPixel, VImageDimension> & image, const unsigned int vectorLength)
{
image.SetVectorLength(vectorLength);
}
template <typename TPixel, unsigned int VImageDimension>
void
SetVectorLengthIfImageIsVectorImage(itk::Image<TPixel, VImageDimension> & itkNotUsed(image),
const unsigned int itkNotUsed(vectorLength))
{
// Do not set the VectorLength. The specified image is not a VectorImage.
}
template <typename TRange>
void
ExpectBeginIsEndWhenRangeIsDefaultConstructed()
{
TRange defaultConstructedRange;
EXPECT_EQ(defaultConstructedRange.begin(), defaultConstructedRange.end());
}
template <typename TRange>
void
ExpectRangeIsEmptyWhenDefaultConstructed()
{
TRange defaultConstructedRange;
EXPECT_TRUE(defaultConstructedRange.empty());
}
template <typename TImage>
void
ExpectRangeIsNotEmptyForNonEmptyImage()
{
// First create a non-empty image:
const auto image = TImage::New();
typename TImage::SizeType imageSize;
imageSize.Fill(1);
image->SetRegions(imageSize);
SetVectorLengthIfImageIsVectorImage(*image, 1);
image->Allocate();
EXPECT_FALSE((ImageRegionRange<TImage>{ *image }.empty()));
}
template <typename TImage>
void
Expect_ImageRegionRange_iterates_forward_over_same_pixels_as_ImageRegionIterator(
TImage & image,
const itk::ImageRegion<TImage::ImageDimension> & iterationRegion)
{
using PixelType = typename TImage::PixelType;
itk::ImageRegionIterator<TImage> imageRegionIterator{ &image, iterationRegion };
ImageRegionRange<TImage> range{ image, iterationRegion };
ASSERT_TRUE(imageRegionIterator.IsAtBegin());
for (const PixelType pixel : range)
{
ASSERT_FALSE(imageRegionIterator.IsAtEnd());
EXPECT_EQ(pixel, imageRegionIterator.Get());
++imageRegionIterator;
}
EXPECT_TRUE(imageRegionIterator.IsAtEnd());
}
template <typename TImage>
void
Expect_ImageRegionRange_iterates_backward_over_same_pixels_as_ImageRegionIterator(
TImage & image,
const itk::ImageRegion<TImage::ImageDimension> & iterationRegion)
{
itk::ImageRegionIterator<TImage> imageRegionIterator{ &image, iterationRegion };
ImageRegionRange<TImage> range{ image, iterationRegion };
auto rangeIterator = range.cend();
imageRegionIterator.GoToEnd();
while (!imageRegionIterator.IsAtBegin())
{
ASSERT_NE(rangeIterator, range.begin());
--imageRegionIterator;
--rangeIterator;
EXPECT_EQ(*rangeIterator, imageRegionIterator.Get());
}
EXPECT_EQ(rangeIterator, range.begin());
}
#ifdef NDEBUG
template <typename TImage>
void
Check_Range_constructor_throws_ExceptionObject_when_iteration_region_is_outside_of_buffered_region(
TImage & image,
const typename TImage::RegionType & region)
{
try
{
const ImageRegionRange<TImage> range{ image, region };
// The test fails when the construction of this range does not trigger an exception.
GTEST_FAIL();
}
catch (const itk::ExceptionObject & exceptionObject)
{
const char * const description = exceptionObject.GetDescription();
ASSERT_NE(description, nullptr);
EXPECT_TRUE(std::strstr(description, "outside of buffered region") != nullptr);
}
}
#endif
} // namespace
// Tests that a begin iterator compares equal to another begin iterator of the
// same range. Also does this test for end iterators.
TEST(ImageRegionRange, EquivalentBeginOrEndIteratorsCompareEqual)
{
using ImageType = itk::Image<int>;
using RangeType = ImageRegionRange<ImageType>;
const auto image = CreateImage<ImageType>(2, 3);
const RangeType range{ *image };
const RangeType::iterator begin = range.begin();
const RangeType::iterator end = range.end();
const RangeType::const_iterator cbegin = range.cbegin();
const RangeType::const_iterator cend = range.cend();
// An iterator object compares equal to itself:
EXPECT_EQ(begin, begin);
EXPECT_EQ(end, end);
EXPECT_EQ(cbegin, cbegin);
EXPECT_EQ(cend, cend);
// Multiple calls of the same function yield equivalent objects:
EXPECT_EQ(range.begin(), range.begin());
EXPECT_EQ(range.end(), range.end());
EXPECT_EQ(range.cbegin(), range.cbegin());
EXPECT_EQ(range.cend(), range.cend());
// Corresponding const_iterator and non-const iterator compare equal:
EXPECT_EQ(begin, cbegin);
EXPECT_EQ(end, cend);
EXPECT_EQ(cbegin, begin);
EXPECT_EQ(cend, end);
}
TEST(ImageRegionRange, BeginAndEndOfNonEmptyImageRegionAreNotEqual)
{
using ImageType = itk::Image<int>;
constexpr auto ImageDimension = ImageType::ImageDimension;
const auto image = CreateImage<ImageType>(2, 3);
const itk::ImageRegion<ImageDimension> region{ itk::Size<ImageDimension>::Filled(2) };
ImageRegionRange<ImageType> range{ *image, region };
EXPECT_FALSE(range.begin() == range.end());
EXPECT_NE(range.begin(), range.end());
}
// Tests that an iterator converts (implicitly) to a const_iterator.
TEST(ImageRegionRange, IteratorConvertsToConstIterator)
{
using ImageType = itk::Image<int>;
using RangeType = ImageRegionRange<ImageType>;
constexpr auto ImageDimension = ImageType::ImageDimension;
const auto image = CreateImage<ImageType>(2, 3);
const itk::ImageRegion<ImageDimension> region{ itk::Size<ImageDimension>::Filled(2) };
RangeType range{ *image, region };
const RangeType::iterator begin = range.begin();
const RangeType::const_iterator const_begin_from_begin = begin;
EXPECT_EQ(const_begin_from_begin, begin);
const RangeType::const_iterator const_begin_from_range_begin = range.begin();
EXPECT_EQ(const_begin_from_range_begin, range.begin());
}
// Tests that the iterators of an ImageRegionRange can be used as first and
// second argument of an std::vector constructor.
TEST(ImageRegionRange, IteratorsCanBePassedToStdVectorConstructor)
{
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType>;
constexpr auto ImageDimension = ImageType::ImageDimension;
enum
{
sizeX = 9,
sizeY = 11
};
const auto image = CreateImageFilledWithSequenceOfNaturalNumbers<ImageType>(sizeX, sizeY);
const itk::ImageRegion<ImageDimension> region{ itk::Size<ImageDimension>::Filled(2) };
ImageRegionRange<ImageType> range{ *image, region };
// Easily store all pixels of the ImageRegionRange in an std::vector:
const std::vector<PixelType> stdVector(range.begin(), range.end());
EXPECT_EQ(stdVector, std::vector<PixelType>(range.cbegin(), range.cend()));
EXPECT_TRUE(std::equal(stdVector.begin(), stdVector.end(), range.cbegin()));
}
// Tests that the iterators can be used as first and
// second argument of std::reverse (which requires bidirectional iterators).
TEST(ImageRegionRange, IteratorsCanBePassedToStdReverseCopy)
{
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType>;
constexpr auto ImageDimension = ImageType::ImageDimension;
enum
{
sizeX = 9,
sizeY = 11
};
const auto image = CreateImageFilledWithSequenceOfNaturalNumbers<ImageType>(sizeX, sizeY);
const itk::ImageRegion<ImageDimension> region{ itk::Size<ImageDimension>::Filled(2) };
ImageRegionRange<ImageType> range{ *image, region };
const unsigned int numberOfPixels = sizeX * sizeY;
const std::vector<PixelType> stdVector(range.begin(), range.end());
std::vector<PixelType> reversedStdVector1(numberOfPixels);
std::vector<PixelType> reversedStdVector2(numberOfPixels);
std::vector<PixelType> reversedStdVector3(numberOfPixels);
// Checks bidirectionality of the ImageRegionRange iterators!
std::reverse_copy(stdVector.cbegin(), stdVector.cend(), reversedStdVector1.begin());
std::reverse_copy(range.begin(), range.end(), reversedStdVector2.begin());
std::reverse_copy(range.cbegin(), range.cend(), reversedStdVector3.begin());
// Sanity check
EXPECT_NE(reversedStdVector1, stdVector);
EXPECT_NE(reversedStdVector2, stdVector);
EXPECT_NE(reversedStdVector3, stdVector);
// The real tests:
EXPECT_EQ(reversedStdVector1, reversedStdVector2);
EXPECT_EQ(reversedStdVector1, reversedStdVector3);
}
// Tests that the iterators can be used as first and
// second argument of std::inner_product.
TEST(ImageRegionRange, IteratorsCanBePassedToStdInnerProduct)
{
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType>;
constexpr auto ImageDimension = ImageType::ImageDimension;
enum
{
sizeX = 2,
sizeY = 2
};
const auto image = CreateImageFilledWithSequenceOfNaturalNumbers<ImageType>(sizeX, sizeY);
const itk::ImageRegion<ImageDimension> region{ itk::Size<ImageDimension>::Filled(2) };
ImageRegionRange<ImageType> range{ *image, region };
const double innerProduct = std::inner_product(range.begin(), range.end(), range.begin(), 0.0);
EXPECT_EQ(innerProduct, 30);
}
// Tests that the iterators can be used as first and
// second argument of std::for_each.
TEST(ImageRegionRange, IteratorsCanBePassedToStdForEach)
{
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType>;
constexpr auto ImageDimension = ImageType::ImageDimension;
enum
{
sizeX = 9,
sizeY = 11
};
const auto image = CreateImageFilledWithSequenceOfNaturalNumbers<ImageType>(sizeX, sizeY);
const itk::ImageRegion<ImageDimension> region{ itk::Size<ImageDimension>::Filled(2) };
ImageRegionRange<ImageType> range{ *image, region };
std::for_each(range.begin(), range.end(), [](const PixelType pixel) { EXPECT_TRUE(pixel > 0); });
}
// Tests that an ImageRegionRange can be used as the "range expression" of a
// C++11 range-based for loop.
TEST(ImageRegionRange, CanBeUsedAsExpressionOfRangeBasedForLoop)
{
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType>;
constexpr auto ImageDimension = ImageType::ImageDimension;
using RangeType = ImageRegionRange<ImageType>;
enum
{
sizeX = 2,
sizeY = 3
};
const auto image = CreateImageFilledWithSequenceOfNaturalNumbers<ImageType>(sizeX, sizeY);
const itk::ImageRegion<ImageDimension> region{ itk::Size<ImageDimension>::Filled(2) };
RangeType range{ *image, region };
for (const PixelType pixel : range)
{
EXPECT_NE(pixel, 42);
}
for (auto && pixel : range)
{
pixel = 42;
}
for (const PixelType pixel : range)
{
EXPECT_EQ(pixel, 42);
}
}
// Tests that ImageRegionRange<VectorImage<T>> is supported well.
TEST(ImageRegionRange, SupportsVectorImage)
{
using ImageType = itk::VectorImage<unsigned char>;
constexpr auto ImageDimension = ImageType::ImageDimension;
using PixelType = ImageType::PixelType;
enum
{
vectorLength = 2,
sizeX = 2,
sizeY = 2,
sizeZ = 2
};
const auto image = ImageType::New();
const typename ImageType::SizeType imageSize = { { sizeX, sizeY, sizeZ } };
image->SetRegions(imageSize);
image->SetVectorLength(vectorLength);
image->AllocateInitialized();
PixelType fillPixelValue(vectorLength);
fillPixelValue.Fill(42);
image->FillBuffer(fillPixelValue);
const itk::ImageRegion<ImageDimension> region{ itk::Size<ImageDimension>::Filled(2) };
using RangeType = ImageRegionRange<ImageType>;
RangeType range{ *image, region };
for (PixelType pixelValue : range)
{
EXPECT_EQ(pixelValue, fillPixelValue);
}
PixelType otherPixelValue(vectorLength);
otherPixelValue.Fill(1);
image->SetPixel({ {} }, otherPixelValue);
RangeType::const_iterator it = range.begin();
const PixelType firstPixelValueFromRange = *it;
EXPECT_EQ(firstPixelValueFromRange, otherPixelValue);
++it;
const PixelType secondPixelValueFromRange = *it;
EXPECT_EQ(secondPixelValueFromRange, fillPixelValue);
}
TEST(ImageRegionRange, IteratorIsDefaultConstructible)
{
itk::RangeGTestUtilities::ExpectIteratorIsDefaultConstructible<ImageRegionRange<itk::Image<int>>>();
}
TEST(ImageRegionRange, ProvidesReverseIterators)
{
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType>;
constexpr auto ImageDimension = ImageType::ImageDimension;
using RangeType = ImageRegionRange<ImageType>;
enum
{
sizeX = 9,
sizeY = 11
};
const auto image = CreateImageFilledWithSequenceOfNaturalNumbers<ImageType>(sizeX, sizeY);
const itk::ImageRegion<ImageDimension> region{ itk::Size<ImageDimension>::Filled(2) };
RangeType range{ *image, region };
const unsigned int numberOfPixels = sizeX * sizeY;
const std::vector<PixelType> stdVector(range.begin(), range.end());
std::vector<PixelType> reversedStdVector1(numberOfPixels);
std::vector<PixelType> reversedStdVector2(numberOfPixels);
std::vector<PixelType> reversedStdVector3(numberOfPixels);
std::reverse_copy(stdVector.cbegin(), stdVector.cend(), reversedStdVector1.begin());
const RangeType::const_reverse_iterator crbegin = range.crbegin();
const RangeType::const_reverse_iterator crend = range.crend();
const RangeType::reverse_iterator rbegin = range.rbegin();
const RangeType::reverse_iterator rend = range.rend();
EXPECT_EQ(crbegin, rbegin);
EXPECT_EQ(crend, rend);
std::copy(crbegin, crend, reversedStdVector2.begin());
std::copy(rbegin, rend, reversedStdVector3.begin());
// Sanity check
EXPECT_NE(reversedStdVector1, stdVector);
EXPECT_NE(reversedStdVector2, stdVector);
EXPECT_NE(reversedStdVector3, stdVector);
// The real tests:
EXPECT_EQ(reversedStdVector1, reversedStdVector2);
EXPECT_EQ(reversedStdVector1, reversedStdVector3);
}
// Tests that begin() == end() for a default-constructed range.
TEST(ImageRegionRange, BeginIsEndWhenDefaultConstructed)
{
ExpectBeginIsEndWhenRangeIsDefaultConstructed<ImageRegionRange<itk::Image<int>>>();
ExpectBeginIsEndWhenRangeIsDefaultConstructed<ImageRegionRange<itk::VectorImage<int>>>();
}
// Tests empty() for a default-constructed range.
TEST(ImageRegionRange, IsEmptyWhenDefaultConstructed)
{
ExpectRangeIsEmptyWhenDefaultConstructed<ImageRegionRange<itk::Image<int>>>();
ExpectRangeIsEmptyWhenDefaultConstructed<ImageRegionRange<itk::VectorImage<int>>>();
}
// Tests that range.empty() returns false for a non-empty image.
TEST(ImageRegionRange, IsNotEmptyWhenImageIsNonEmpty)
{
ExpectRangeIsNotEmptyForNonEmptyImage<itk::Image<int>>();
ExpectRangeIsNotEmptyForNonEmptyImage<itk::VectorImage<int>>();
}
TEST(ImageRegionRange, IteratesForwardOverSamePixelsAsImageRegionIterator)
{
using ImageType = itk::Image<unsigned char>;
using PixelType = ImageType::PixelType;
using IndexType = ImageType::IndexType;
using SizeType = ImageType::SizeType;
using RegionType = ImageType::RegionType;
const auto image = ImageType::New();
image->SetRegions(RegionType{ IndexType{ { -1, -2 } }, SizeType{ { 9, 11 } } });
image->Allocate();
const itk::ImageBufferRange imageBufferRange{ *image };
std::iota(imageBufferRange.begin(), imageBufferRange.end(), PixelType{ 1 });
Expect_ImageRegionRange_iterates_forward_over_same_pixels_as_ImageRegionIterator(
*image, RegionType{ IndexType{ { 1, 2 } }, SizeType{ { 3, 4 } } });
}
TEST(ImageRegionRange, IteratesBackwardOverSamePixelsAsImageRegionIterator)
{
using ImageType = itk::Image<unsigned char>;
using PixelType = ImageType::PixelType;
using IndexType = ImageType::IndexType;
using SizeType = ImageType::SizeType;
using RegionType = ImageType::RegionType;
const auto image = ImageType::New();
image->SetRegions(RegionType{ IndexType{ { -1, -2 } }, SizeType{ { 9, 11 } } });
image->Allocate();
const itk::ImageBufferRange imageBufferRange{ *image };
std::iota(imageBufferRange.begin(), imageBufferRange.end(), PixelType{ 1 });
Expect_ImageRegionRange_iterates_backward_over_same_pixels_as_ImageRegionIterator(
*image, RegionType{ IndexType{ { 1, 2 } }, SizeType{ { 3, 4 } } });
}
// Tests that a range constructor throws an itk::ExceptionObject when the
// iteration region is not entirely within the buffered region.
TEST(ImageRegionRange, ThrowsInReleaseWhenIterationRegionIsOutsideBufferedRegion)
{
#ifdef NDEBUG
// NDEBUG suggests that we are in Release mode.
using ImageType = itk::Image<unsigned char>;
using IndexType = ImageType::IndexType;
using SizeType = ImageType::SizeType;
using RegionType = ImageType::RegionType;
const auto image = ImageType::New();
const IndexType imageIndex{ { -1, -2 } };
const SizeType imageSize{ { 3, 4 } };
image->SetRegions(RegionType{ imageIndex, imageSize });
image->AllocateInitialized();
Check_Range_constructor_throws_ExceptionObject_when_iteration_region_is_outside_of_buffered_region(
*image, RegionType{ imageIndex, imageSize + SizeType::Filled(1) });
Check_Range_constructor_throws_ExceptionObject_when_iteration_region_is_outside_of_buffered_region(
*image, RegionType{ imageIndex - SizeType::Filled(1), imageSize });
#endif
}
|