Skip to content

Commit

Permalink
Merge pull request #4328 from N-Dekker/clamp-Colormap
Browse files Browse the repository at this point in the history
Replace `std::min` and `std::max` calls with `std::clamp`,  in "Colormap", `ProgressTransformer`, `SliceImageFilter`, and `MultiThreaderBase`.
  • Loading branch information
jhlegarreta authored Nov 18, 2023
2 parents 5942002 + 2ee5c8e commit 0fcffbc
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 42 deletions.
4 changes: 1 addition & 3 deletions Modules/Core/Common/src/itkMultiThreaderBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,8 @@ MultiThreaderBase::GetGlobalDefaultNumberOfThreads()
}

// limit the number of threads to m_GlobalMaximumNumberOfThreads
threadCount = std::min(threadCount, ThreadIdType{ ITK_MAX_THREADS });

// verify that the default number of threads is larger than zero
threadCount = std::max(threadCount, NumericTraits<ThreadIdType>::OneValue());
threadCount = std::clamp(threadCount, ThreadIdType{ 1 }, ThreadIdType{ ITK_MAX_THREADS });

m_PimplGlobals->m_GlobalDefaultNumberOfThreads = threadCount;
}
Expand Down
9 changes: 3 additions & 6 deletions Modules/Core/Common/src/itkProgressTransformer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ ProgressTransformer::ProgressTransformer(float start, float end, ProcessObject *
: m_TargetFilter(targetFilter)
, m_ProgressTag(0)
{
m_Start = std::max(start, 0.0f);
m_Start = std::min(m_Start, 1.0f);
m_End = std::max(end, 0.0f);
m_End = std::min(m_End, 1.0f);
m_Start = std::clamp(start, 0.0f, 1.0f);
m_End = std::clamp(end, 0.0f, 1.0f);
m_Dummy = DummyProcess::New();

m_ProgressCommand = CommandType::New();
Expand All @@ -56,8 +54,7 @@ void
ProgressTransformer::UpdateProgress()
{
float progress = m_Dummy->GetProgress();
progress = std::max(progress, 0.0f);
progress = std::min(progress, 1.0f);
progress = std::clamp(progress, 0.0f, 1.0f);

progress = m_Start + progress * (m_End - m_Start); // transform progress
m_TargetFilter->UpdateProgress(progress);
Expand Down
4 changes: 1 addition & 3 deletions Modules/Filtering/Colormap/include/itkColormapFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ class ColormapFunction : public Object

auto d = static_cast<RealType>(maxInputValue - minInputValue);
RealType value = (static_cast<RealType>(v) - static_cast<RealType>(minInputValue)) / d;

value = std::max(0.0, value);
value = std::min(1.0, value);
value = std::clamp(value, 0.0, 1.0);
return value;
}

Expand Down
10 changes: 3 additions & 7 deletions Modules/Filtering/Colormap/include/itkHSVColormapFunction.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ HSVColormapFunction<TScalar, TRGBPixel>::operator()(const TScalar & v) const ->
// Apply the color mapping.
// Apply the color mapping.
RealType red = itk::Math::abs(5.0 * (value - 0.5)) - 5.0 / 6.0;

red = std::min(red, 1.0);
red = std::max(0.0, red);
red = std::clamp(red, 0.0, 1.0);

RealType green = -itk::Math::abs(5.0 * (value - 11.0 / 30.0)) + 11.0 / 6.0;
green = std::min(green, 1.0);
green = std::max(0.0, green);
green = std::clamp(green, 0.0, 1.0);

RealType blue = -itk::Math::abs(5.0 * (value - 19.0 / 30.0)) + 11.0 / 6.0;
blue = std::min(blue, 1.0);
blue = std::max(0.0, blue);
blue = std::clamp(blue, 0.0, 1.0);

// Set the rgb components after rescaling the values.
RGBPixelType pixel;
Expand Down
10 changes: 3 additions & 7 deletions Modules/Filtering/Colormap/include/itkHotColormapFunction.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@ HotColormapFunction<TScalar, TRGBPixel>::operator()(const TScalar & v) const ->

// Apply the color mapping.
RealType red = 63.0 / 26.0 * value - 1.0 / 13.0;

red = std::max(0.0, red);
red = std::min(1.0, red);
red = std::clamp(red, 0.0, 1.0);

RealType green = 63.0 / 26.0 * value - 11.0 / 13.0;
green = std::max(0.0, green);
green = std::min(1.0, green);
green = std::clamp(green, 0.0, 1.0);

RealType blue = 4.5 * value - 3.5;
blue = std::max(0.0, blue);
blue = std::min(1.0, blue);
blue = std::clamp(blue, 0.0, 1.0);

// Set the rgb components after rescaling the values.
RGBPixelType pixel;
Expand Down
10 changes: 3 additions & 7 deletions Modules/Filtering/Colormap/include/itkJetColormapFunction.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@ JetColormapFunction<TScalar, TRGBPixel>::operator()(const TScalar & v) const ->

// Apply the color mapping.
RealType red = -itk::Math::abs(3.95 * (value - 0.7460)) + 1.5;

red = std::min(red, 1.0);
red = std::max(0.0, red);
red = std::clamp(red, 0.0, 1.0);

RealType green = -itk::Math::abs(3.95 * (value - 0.492)) + 1.5;
green = std::min(green, 1.0);
green = std::max(0.0, green);
green = std::clamp(green, 0.0, 1.0);

RealType blue = -itk::Math::abs(3.95 * (value - 0.2385)) + 1.5;
blue = std::min(blue, 1.0);
blue = std::max(0.0, blue);
blue = std::clamp(blue, 0.0, 1.0);

// Set the rgb components after rescaling the values.
RGBPixelType pixel;
Expand Down
19 changes: 10 additions & 9 deletions Modules/Filtering/ImageGrid/include/itkSliceImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ SliceImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData(
InputIndexType start;
for (unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
{
start[i] = std::max(m_Start[i], inputIndex[i]);
start[i] = std::min(start[i], static_cast<IndexValueType>(inputIndex[i] + inputSize[i] - 1));
start[i] = std::clamp(m_Start[i], inputIndex[i], static_cast<IndexValueType>(inputIndex[i] + inputSize[i] - 1));
}

// Define/declare an iterator that will walk the output region for this thread
Expand Down Expand Up @@ -179,8 +178,7 @@ SliceImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion()
{
// clamp to valid index range and don't include one past end, so
// that a zero size RR would be valid
start[i] = std::max(m_Start[i], inputIndex[i]);
start[i] = std::min(start[i], static_cast<IndexValueType>(inputIndex[i] + inputSize[i] - 1));
start[i] = std::clamp(m_Start[i], inputIndex[i], static_cast<IndexValueType>(inputIndex[i] + inputSize[i] - 1));
}


Expand Down Expand Up @@ -247,14 +245,17 @@ SliceImageFilter<TInputImage, TOutputImage>::GenerateOutputInformation()
outputSpacing[i] = inputSpacing[i] * itk::Math::abs(m_Step[i]);

// clamp start, inclusive start interval
IndexValueType start = std::max(m_Start[i], inputIndex[i] - static_cast<int>(m_Step[i] < 0));
start =
std::min(start, static_cast<IndexValueType>(inputIndex[i] + inputSize[i]) - static_cast<int>(m_Step[i] < 0));
IndexValueType start =
std::clamp(m_Start[i],
inputIndex[i] - static_cast<int>(m_Step[i] < 0),
static_cast<IndexValueType>(inputIndex[i] + inputSize[i]) - static_cast<int>(m_Step[i] < 0));

// clamp stop as open interval
// Based on the sign of the step include 1 after the end.
IndexValueType stop = std::max(m_Stop[i], inputIndex[i] - static_cast<int>(m_Step[i] < 0));
stop = std::min(stop, static_cast<IndexValueType>(inputIndex[i] + inputSize[i]) - static_cast<int>(m_Step[i] < 0));
IndexValueType stop =
std::clamp(m_Stop[i],
inputIndex[i] - static_cast<int>(m_Step[i] < 0),
static_cast<IndexValueType>(inputIndex[i] + inputSize[i]) - static_cast<int>(m_Step[i] < 0));

// If both the numerator and the denominator have the same sign,
// then the range is a valid and non-zero sized. Truncation is the
Expand Down

0 comments on commit 0fcffbc

Please sign in to comment.