Skip to content

Commit 0ef6d0d

Browse files
committed
STYLE: Deref sampler input and output at the start of GenerateData()
`Deref` checks that the input and output pointers are not null. It appears more secure to just do that right at the begin of the `GenerateData()` member function of these ImageSamplers.
1 parent 9359784 commit 0ef6d0d

3 files changed

+39
-40
lines changed

Common/ImageSamplers/itkImageRandomCoordinateSampler.hxx

+15-15
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ void
3434
ImageRandomCoordinateSampler<TInputImage>::GenerateData()
3535
{
3636
/** Get handles to the input image, output sample container, and interpolator. */
37-
InputImageConstPointer inputImage = this->GetInput();
38-
typename ImageSampleContainerType::Pointer sampleContainer = this->GetOutput();
39-
typename InterpolatorType::Pointer interpolator = this->GetModifiableInterpolator();
37+
const InputImageType & inputImage = elastix::Deref(this->GetInput());
38+
ImageSampleContainerType & sampleContainer = elastix::Deref(this->GetOutput());
39+
typename InterpolatorType::Pointer interpolator = this->GetModifiableInterpolator();
4040

4141
/** Set up the interpolator. */
42-
interpolator->SetInputImage(inputImage); // only once?
42+
interpolator->SetInputImage(&inputImage); // only once?
4343

4444
const auto croppedInputImageRegion = this->GetCroppedInputImageRegion();
4545

@@ -57,7 +57,7 @@ ImageRandomCoordinateSampler<TInputImage>::GenerateData()
5757
typename MaskType::ConstPointer mask = this->GetMask();
5858
if (mask.IsNull() && Superclass::m_UseMultiThread)
5959
{
60-
auto & samples = elastix::Deref(sampleContainer).CastToSTLContainer();
60+
auto & samples = sampleContainer.CastToSTLContainer();
6161
samples.resize(this->Superclass::m_NumberOfSamples);
6262

6363
/** Clear the random number list. */
@@ -73,7 +73,7 @@ ImageRandomCoordinateSampler<TInputImage>::GenerateData()
7373
m_RandomCoordinates.push_back(randomCIndex);
7474
}
7575

76-
m_OptionalUserData.emplace(m_RandomCoordinates, *inputImage, *interpolator, samples);
76+
m_OptionalUserData.emplace(m_RandomCoordinates, inputImage, *interpolator, samples);
7777

7878
MultiThreaderBase & multiThreader = elastix::Deref(this->ProcessObject::GetMultiThreader());
7979
multiThreader.SetSingleMethod(&Self::ThreaderCallback, &*m_OptionalUserData);
@@ -82,18 +82,18 @@ ImageRandomCoordinateSampler<TInputImage>::GenerateData()
8282
}
8383

8484
/** Reserve memory for the output. */
85-
sampleContainer->Reserve(this->GetNumberOfSamples());
85+
sampleContainer.Reserve(this->GetNumberOfSamples());
8686

8787
/** Setup an iterator over the output, which is of ImageSampleContainerType. */
8888
typename ImageSampleContainerType::Iterator iter;
89-
typename ImageSampleContainerType::ConstIterator end = sampleContainer->End();
89+
typename ImageSampleContainerType::ConstIterator end = sampleContainer.End();
9090

9191
InputImageContinuousIndexType sampleContIndex;
9292
/** Fill the sample container. */
9393
if (mask.IsNull())
9494
{
9595
/** Start looping over the sample container. */
96-
for (iter = sampleContainer->Begin(); iter != end; ++iter)
96+
for (iter = sampleContainer.Begin(); iter != end; ++iter)
9797
{
9898
/** Make a reference to the current sample in the container. */
9999
InputImagePointType & samplePoint = iter->Value().m_ImageCoordinates;
@@ -103,7 +103,7 @@ ImageRandomCoordinateSampler<TInputImage>::GenerateData()
103103
this->GenerateRandomCoordinate(smallestContIndex, largestContIndex, sampleContIndex);
104104

105105
/** Convert to point */
106-
inputImage->TransformContinuousIndexToPhysicalPoint(sampleContIndex, samplePoint);
106+
inputImage.TransformContinuousIndexToPhysicalPoint(sampleContIndex, samplePoint);
107107

108108
/** Compute the value at the continuous index. */
109109
sampleValue = static_cast<ImageSampleValueType>(this->m_Interpolator->EvaluateAtContinuousIndex(sampleContIndex));
@@ -121,7 +121,7 @@ ImageRandomCoordinateSampler<TInputImage>::GenerateData()
121121
unsigned long maximumNumberOfSamplesToTry = 10 * this->GetNumberOfSamples();
122122

123123
/** Start looping over the sample container */
124-
for (iter = sampleContainer->Begin(); iter != end; ++iter)
124+
for (iter = sampleContainer.Begin(); iter != end; ++iter)
125125
{
126126
/** Make a reference to the current sample in the container. */
127127
InputImagePointType & samplePoint = iter->Value().m_ImageCoordinates;
@@ -135,17 +135,17 @@ ImageRandomCoordinateSampler<TInputImage>::GenerateData()
135135
if (numberOfSamplesTried > maximumNumberOfSamplesToTry)
136136
{
137137
/** Squeeze the sample container to the size that is still valid. */
138-
typename ImageSampleContainerType::iterator stlnow = sampleContainer->begin();
139-
typename ImageSampleContainerType::iterator stlend = sampleContainer->end();
138+
typename ImageSampleContainerType::iterator stlnow = sampleContainer.begin();
139+
typename ImageSampleContainerType::iterator stlend = sampleContainer.end();
140140
stlnow += iter.Index();
141-
sampleContainer->erase(stlnow, stlend);
141+
sampleContainer.erase(stlnow, stlend);
142142
itkExceptionMacro(
143143
<< "Could not find enough image samples within reasonable time. Probably the mask is too small");
144144
}
145145

146146
/** Generate a point in the input image region. */
147147
this->GenerateRandomCoordinate(smallestContIndex, largestContIndex, sampleContIndex);
148-
inputImage->TransformContinuousIndexToPhysicalPoint(sampleContIndex, samplePoint);
148+
inputImage.TransformContinuousIndexToPhysicalPoint(sampleContIndex, samplePoint);
149149

150150
} while (!interpolator->IsInsideBuffer(sampleContIndex) || !mask->IsInsideInWorldSpace(samplePoint));
151151

Common/ImageSamplers/itkImageRandomSampler.hxx

+14-15
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,19 @@ void
3737
ImageRandomSampler<TInputImage>::GenerateData()
3838
{
3939
/** Get handles to the input image, output sample container. */
40-
InputImageConstPointer inputImage = this->GetInput();
41-
typename ImageSampleContainerType::Pointer sampleContainer = this->GetOutput();
40+
const InputImageType & inputImage = elastix::Deref(this->GetInput());
41+
ImageSampleContainerType & sampleContainer = elastix::Deref(this->GetOutput());
4242

4343
/** Get a handle to the mask. If there was no mask supplied we exercise a multi-threaded version. */
4444
typename MaskType::ConstPointer mask = this->GetMask();
4545
if (mask.IsNull() && Superclass::m_UseMultiThread)
4646
{
4747
Superclass::GenerateRandomNumberList();
4848
const auto & randomNumberList = Superclass::m_RandomNumberList;
49-
auto & samples = elastix::Deref(sampleContainer).CastToSTLContainer();
49+
auto & samples = sampleContainer.CastToSTLContainer();
5050
samples.resize(randomNumberList.size());
5151

52-
m_OptionalUserData.emplace(
53-
randomNumberList, elastix::Deref(inputImage), this->GetCroppedInputImageRegion(), samples);
52+
m_OptionalUserData.emplace(randomNumberList, inputImage, this->GetCroppedInputImageRegion(), samples);
5453

5554
MultiThreaderBase & multiThreader = elastix::Deref(this->ProcessObject::GetMultiThreader());
5655
multiThreader.SetSingleMethod(&Self::ThreaderCallback, &*m_OptionalUserData);
@@ -59,11 +58,11 @@ ImageRandomSampler<TInputImage>::GenerateData()
5958
}
6059

6160
/** Reserve memory for the output. */
62-
sampleContainer->Reserve(this->GetNumberOfSamples());
61+
sampleContainer.Reserve(this->GetNumberOfSamples());
6362

6463
/** Setup a random iterator over the input image. */
6564
using RandomIteratorType = ImageRandomConstIteratorWithIndex<InputImageType>;
66-
RandomIteratorType randIter(inputImage, this->GetCroppedInputImageRegion());
65+
RandomIteratorType randIter(&inputImage, this->GetCroppedInputImageRegion());
6766

6867
if (const auto optionalSeed = Superclass::GetOptionalSeed())
6968
{
@@ -73,19 +72,19 @@ ImageRandomSampler<TInputImage>::GenerateData()
7372

7473
/** Setup an iterator over the output, which is of ImageSampleContainerType. */
7574
typename ImageSampleContainerType::Iterator iter;
76-
typename ImageSampleContainerType::ConstIterator end = sampleContainer->End();
75+
typename ImageSampleContainerType::ConstIterator end = sampleContainer.End();
7776

7877
if (mask.IsNull())
7978
{
8079
/** number of samples + 1, because of the initial ++randIter. */
8180
randIter.SetNumberOfSamples(this->GetNumberOfSamples() + 1);
8281
/** Advance one, in order to generate the same sequence as when using a mask */
8382
++randIter;
84-
for (iter = sampleContainer->Begin(); iter != end; ++iter)
83+
for (iter = sampleContainer.Begin(); iter != end; ++iter)
8584
{
8685
/** Get the index, transform it to the physical coordinates and put it in the sample. */
8786
InputImageIndexType index = randIter.GetIndex();
88-
inputImage->TransformIndexToPhysicalPoint(index, iter->Value().m_ImageCoordinates);
87+
inputImage.TransformIndexToPhysicalPoint(index, iter->Value().m_ImageCoordinates);
8988
/** Get the value and put it in the sample. */
9089
iter->Value().m_ImageValue = randIter.Get();
9190
/** Jump to a random position. */
@@ -104,7 +103,7 @@ ImageRandomSampler<TInputImage>::GenerateData()
104103
/** Loop over the sample container. */
105104
InputImagePointType inputPoint;
106105
bool insideMask = false;
107-
for (iter = sampleContainer->Begin(); iter != end; ++iter)
106+
for (iter = sampleContainer.Begin(); iter != end; ++iter)
108107
{
109108
/** Loop until a valid sample is found. */
110109
do
@@ -115,16 +114,16 @@ ImageRandomSampler<TInputImage>::GenerateData()
115114
if (randIter.IsAtEnd())
116115
{
117116
/** Squeeze the sample container to the size that is still valid. */
118-
typename ImageSampleContainerType::iterator stlnow = sampleContainer->begin();
119-
typename ImageSampleContainerType::iterator stlend = sampleContainer->end();
117+
typename ImageSampleContainerType::iterator stlnow = sampleContainer.begin();
118+
typename ImageSampleContainerType::iterator stlend = sampleContainer.end();
120119
stlnow += iter.Index();
121-
sampleContainer->erase(stlnow, stlend);
120+
sampleContainer.erase(stlnow, stlend);
122121
itkExceptionMacro(
123122
<< "Could not find enough image samples within reasonable time. Probably the mask is too small");
124123
}
125124
/** Get the index, and transform it to the physical coordinates. */
126125
InputImageIndexType index = randIter.GetIndex();
127-
inputImage->TransformIndexToPhysicalPoint(index, inputPoint);
126+
inputImage.TransformIndexToPhysicalPoint(index, inputPoint);
128127
/** Check if it's inside the mask. */
129128
insideMask = mask->IsInsideInWorldSpace(inputPoint);
130129
} while (!insideMask);

Common/ImageSamplers/itkImageRandomSamplerSparseMask.hxx

+10-10
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ ImageRandomSamplerSparseMask<TInputImage>::GenerateData()
4242
}
4343

4444
/** Get handles to the input image and output sample container. */
45-
InputImageConstPointer inputImage = this->GetInput();
46-
ImageSampleContainerPointer sampleContainer = this->GetOutput();
45+
const InputImageType & inputImage = elastix::Deref(this->GetInput());
46+
ImageSampleContainerType & sampleContainer = elastix::Deref(this->GetOutput());
4747

4848
// Take capacity from the output container, and clear it.
4949
std::vector<ImageSampleType> sampleVector;
50-
sampleContainer->swap(sampleVector);
50+
sampleContainer.swap(sampleVector);
5151
sampleVector.clear();
5252

5353
/** Make sure the internal full sampler is up-to-date. */
54-
this->m_InternalFullSampler->SetInput(inputImage);
54+
this->m_InternalFullSampler->SetInput(&inputImage);
5555
this->m_InternalFullSampler->SetMask(mask);
5656
this->m_InternalFullSampler->SetInputImageRegion(this->GetCroppedInputImageRegion());
5757
this->m_InternalFullSampler->SetUseMultiThread(Superclass::m_UseMultiThread);
@@ -82,8 +82,8 @@ ImageRandomSamplerSparseMask<TInputImage>::GenerateData()
8282
}
8383

8484
/** Get a handle to the full sampler output. */
85-
typename ImageSampleContainerType::Pointer allValidSamples = this->m_InternalFullSampler->GetOutput();
86-
unsigned long numberOfValidSamples = allValidSamples->Size();
85+
const ImageSampleContainerType & allValidSamples = elastix::Deref(this->m_InternalFullSampler->GetOutput());
86+
unsigned long numberOfValidSamples = allValidSamples.Size();
8787

8888

8989
/** If desired we exercise a multi-threaded version. */
@@ -97,10 +97,10 @@ ImageRandomSamplerSparseMask<TInputImage>::GenerateData()
9797
m_RandomIndices.push_back(m_RandomGenerator->GetIntegerVariate(numberOfValidSamples - 1));
9898
}
9999

100-
auto & samples = elastix::Deref(sampleContainer).CastToSTLContainer();
100+
auto & samples = sampleContainer.CastToSTLContainer();
101101
samples.resize(m_RandomIndices.size());
102102

103-
m_OptionalUserData.emplace(elastix::Deref(allValidSamples).CastToSTLConstContainer(), m_RandomIndices, samples);
103+
m_OptionalUserData.emplace(allValidSamples.CastToSTLConstContainer(), m_RandomIndices, samples);
104104

105105
MultiThreaderBase & multiThreader = elastix::Deref(this->ProcessObject::GetMultiThreader());
106106
multiThreader.SetSingleMethod(&Self::ThreaderCallback, &*m_OptionalUserData);
@@ -112,11 +112,11 @@ ImageRandomSamplerSparseMask<TInputImage>::GenerateData()
112112
for (unsigned int i = 0; i < this->GetNumberOfSamples(); ++i)
113113
{
114114
unsigned long randomIndex = this->m_RandomGenerator->GetIntegerVariate(numberOfValidSamples - 1);
115-
sampleVector.push_back(allValidSamples->ElementAt(randomIndex));
115+
sampleVector.push_back(allValidSamples.ElementAt(randomIndex));
116116
}
117117

118118
// Move the samples from the vector into the output container.
119-
sampleContainer->swap(sampleVector);
119+
sampleContainer.swap(sampleVector);
120120

121121
} // end GenerateData()
122122

0 commit comments

Comments
 (0)