Skip to content

Commit

Permalink
BUG: TestingExtractSliceImageFilter lacked test.
Browse files Browse the repository at this point in the history
In this patch we add a unit test for the itkTestingExtractSliceImageFilter
class.

NOTE: The unit test uncovered a bug in SetExtractionRegion. The
dimension of the output size and index were being exceeded, causing a
crash on Windows systems.

This class is critical, given that it is used as part of the testing
infrastructure for the rest of the toolkit.

Change-Id: I0b5a80f88f22ccd754c1a6f433a2b0a57475f69a
  • Loading branch information
Luis Ibanez authored and lorensen committed Apr 13, 2014
1 parent 50d18a6 commit cba75da
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define __itkTestingExtractSliceImageFilter_h

#include "itkSmartPointer.h"
#include "itkImageSource.h"
#include "itkExtractImageFilterRegionCopier.h"

namespace itk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ void
ExtractSliceImageFilter< TInputImage, TOutputImage >
::SetExtractionRegion(InputImageRegionType extractRegion)
{
m_ExtractionRegion = extractRegion;

unsigned int nonzeroSizeCount = 0;
InputImageSizeType inputSize = extractRegion.GetSize();
Expand All @@ -88,17 +87,20 @@ ExtractSliceImageFilter< TInputImage, TOutputImage >
{
if ( inputSize[i] )
{
outputSize[nonzeroSizeCount] = inputSize[i];
outputIndex[nonzeroSizeCount] = extractRegion.GetIndex()[i];
if (nonzeroSizeCount < OutputImageDimension)
{
outputSize[nonzeroSizeCount] = inputSize[i];
outputIndex[nonzeroSizeCount] = extractRegion.GetIndex()[i];
}
nonzeroSizeCount++;
}
}

if ( nonzeroSizeCount != OutputImageDimension )
{
itkExceptionMacro("Extraction Region not consistent with output image");
}

m_ExtractionRegion = extractRegion;
m_OutputImageRegion.SetSize(outputSize);
m_OutputImageRegion.SetIndex(outputIndex);
this->Modified();
Expand Down
11 changes: 11 additions & 0 deletions Modules/Core/TestKernel/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
itk_module_test()
set(ITKTestKernelTests
itkTestingExtractSliceImageFilterTest.cxx
)

CreateTestDriver(ITKTestKernel "${ITKCommon_LIBRARIES}" "${ITKTestKernelTests}")

set(BASELINE "${ITK_DATA_ROOT}/Baseline/Common")
set(TEMP ${ITK_TEST_OUTPUT_DIR})

itk_add_test(NAME itkTestingExtractSliceImageFilterTest COMMAND ITKTestKernelTestDriver itkTestingExtractSliceImageFilterTest)
142 changes: 142 additions & 0 deletions Modules/Core/TestKernel/test/itkTestingExtractSliceImageFilterTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* 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
*
* http://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.
*
*=========================================================================*/

#include <iostream>
#include "itkImage.h"
#include "itkTestingExtractSliceImageFilter.h"
#include "itkTestingMacros.h"

int itkTestingExtractSliceImageFilterTest(int, char* [] )
{

typedef unsigned char PixelType;
const unsigned int InputDimension = 3;
const unsigned int OutputDimension = 2;

typedef itk::Image< PixelType, InputDimension > InputImageType;
typedef itk::Image< PixelType, OutputDimension > OutputImageType;

typedef itk::Testing::ExtractSliceImageFilter< InputImageType, OutputImageType > FilterType;

FilterType::Pointer filter = FilterType::New();

InputImageType::Pointer inputImage = InputImageType::New();

InputImageType::SizeType size;
size[0] = 20;
size[1] = 20;
size[2] = 20;

InputImageType::RegionType region;
region.SetSize(size);

inputImage->SetRegions(region);
inputImage->Allocate();

inputImage->FillBuffer(0);

InputImageType::DirectionType direction;
direction[0][0] = 1.0;
direction[0][1] = 0.5;
direction[0][2] = 0.0;
direction[1][0] = 0.5;
direction[1][1] = 1.0;
direction[1][2] = 0.5;
direction[2][0] = 0.5;
direction[2][1] = 0.5;
direction[2][2] = 1.0;

inputImage->SetDirection( direction );

InputImageType::SpacingType spacing;
spacing[0] = 2.5;
spacing[1] = 1.5;
spacing[2] = 3.5;

inputImage->SetSpacing( spacing );

filter->SetInput( inputImage );

FilterType::InputImageRegionType extractRegion = inputImage->GetBufferedRegion();

InputImageType::SizeType regionSize = extractRegion.GetSize();

// expect exception, because for output dimension = 2, one of the size
// components must be zero.
TRY_EXPECT_EXCEPTION( filter->SetExtractionRegion( extractRegion ) );

// Set properly, one of the size components to zero.
regionSize[2] = 0;
extractRegion.SetSize(regionSize);

// Now it should be good, with the zero inserted.
TRY_EXPECT_NO_EXCEPTION( filter->SetExtractionRegion( extractRegion ) );
TEST_SET_GET_VALUE( extractRegion, filter->GetExtractionRegion() );

FilterType::DIRECTIONCOLLAPSESTRATEGY strategy = FilterType::DIRECTIONCOLLAPSETOUNKOWN;

TEST_SET_GET_VALUE( strategy, filter->GetDirectionCollapseToStrategy() );
TRY_EXPECT_EXCEPTION( filter->Update() );

TRY_EXPECT_EXCEPTION( filter->SetDirectionCollapseToStrategy( FilterType::DIRECTIONCOLLAPSETOUNKOWN ) );

filter->SetDirectionCollapseToIdentity();
strategy = FilterType::DIRECTIONCOLLAPSETOIDENTITY;
TEST_SET_GET_VALUE( strategy, filter->GetDirectionCollapseToStrategy() );
TRY_EXPECT_NO_EXCEPTION( filter->Update() );

filter->SetDirectionCollapseToGuess();
strategy = FilterType::DIRECTIONCOLLAPSETOGUESS;
TEST_SET_GET_VALUE( strategy, filter->GetDirectionCollapseToStrategy() );
TRY_EXPECT_NO_EXCEPTION( filter->Update() );

filter->SetDirectionCollapseToSubmatrix();
strategy = FilterType::DIRECTIONCOLLAPSETOSUBMATRIX;
TEST_SET_GET_VALUE( strategy, filter->GetDirectionCollapseToStrategy() );
TRY_EXPECT_NO_EXCEPTION( filter->Update() );

strategy = FilterType::DIRECTIONCOLLAPSETOIDENTITY;
filter->SetDirectionCollapseToStrategy(strategy);
TEST_SET_GET_VALUE( strategy, filter->GetDirectionCollapseToStrategy() );
TRY_EXPECT_NO_EXCEPTION( filter->Update() );

strategy = FilterType::DIRECTIONCOLLAPSETOGUESS;
filter->SetDirectionCollapseToStrategy(strategy);
TEST_SET_GET_VALUE( strategy, filter->GetDirectionCollapseToStrategy() );
TRY_EXPECT_NO_EXCEPTION( filter->Update() );

strategy = FilterType::DIRECTIONCOLLAPSETOSUBMATRIX;
filter->SetDirectionCollapseToStrategy(strategy);
TEST_SET_GET_VALUE( strategy, filter->GetDirectionCollapseToStrategy() );
TRY_EXPECT_NO_EXCEPTION( filter->Update() );

try
{
filter->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}

// Exercise PrintSelf()
filter->Print( std::cout );

return EXIT_SUCCESS;
}

0 comments on commit cba75da

Please # to comment.