Skip to content

Commit

Permalink
STYLE: Declare RGBGibbsPriorFilter data members as unique_ptr<T[]>
Browse files Browse the repository at this point in the history
Declared m_LabelStatus, m_Region, and m_RegionCount as `unique_ptr<T[]>`, which
improved exception-safety. Defaulted the destructor of `RGBGibbsPriorFilter`.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Sep 14, 2022
1 parent 3cdfee3 commit bb7d2f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "itkMRFImageFilter.h"

#include <memory> // For unique_ptr.

namespace itk
{
/**
Expand Down Expand Up @@ -194,7 +196,7 @@ class ITK_TEMPLATE_EXPORT RGBGibbsPriorFilter : public MRFImageFilter<TInputImag

protected:
RGBGibbsPriorFilter();
~RGBGibbsPriorFilter() override;
~RGBGibbsPriorFilter() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;

Expand Down Expand Up @@ -237,13 +239,13 @@ class ITK_TEMPLATE_EXPORT RGBGibbsPriorFilter : public MRFImageFilter<TInputImag

typename ClassifierType::Pointer m_ClassifierPtr;

unsigned int m_BoundaryGradient{ 7 }; /** the threshold for the existence of a
boundary. */
double m_BoundaryWeight{ 1 }; /** weight for H_1 */
double m_GibbsPriorWeight{ 1 }; /** weight for H_2 */
int m_StartRadius{ 10 }; /** define the start region of the object. */
int m_RecursiveNumber{ 0 }; /** number of SA iterations. */
LabelType * m_LabelStatus{ nullptr }; /** array for the state of each pixel. */
unsigned int m_BoundaryGradient{ 7 }; /** the threshold for the existence of a
boundary. */
double m_BoundaryWeight{ 1 }; /** weight for H_1 */
double m_GibbsPriorWeight{ 1 }; /** weight for H_2 */
int m_StartRadius{ 10 }; /** define the start region of the object. */
int m_RecursiveNumber{ 0 }; /** number of SA iterations. */
std::unique_ptr<LabelType[]> m_LabelStatus{ nullptr }; /** array for the state of each pixel. */

InputImagePointer m_MediumImage; /** the medium image to store intermedium
result */
Expand All @@ -261,8 +263,8 @@ class ITK_TEMPLATE_EXPORT RGBGibbsPriorFilter : public MRFImageFilter<TInputImag
InputPixelType m_LowPoint; /** the point give lowest value of H-1 in
neighbor. */

unsigned short * m_Region{ nullptr }; /** for region erase. */
unsigned short * m_RegionCount{ nullptr }; /** for region erase. */
std::unique_ptr<unsigned short[]> m_Region{ nullptr }; /** for region erase. */
std::unique_ptr<unsigned short[]> m_RegionCount{ nullptr }; /** for region erase. */

/** weights for different clique configuration. */
double m_CliqueWeight_1{ 0.0 }; /** weight for cliques that v/h smooth boundary */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ RGBGibbsPriorFilter<TInputImage, TClassifiedImage>::RGBGibbsPriorFilter()
m_StartPoint.Fill(0);
}

template <typename TInputImage, typename TClassifiedImage>
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>::~RGBGibbsPriorFilter()
{
delete[] m_Region;
delete[] m_RegionCount;
delete[] m_LabelStatus;
}

/* Set the labelled image. */
template <typename TInputImage, typename TClassifiedImage>
void
Expand Down Expand Up @@ -87,7 +79,7 @@ RGBGibbsPriorFilter<TInputImage, TClassifiedImage>::Allocate()
m_ImageDepth = inputImageSize[2];

const unsigned int numberOfPixels = m_ImageWidth * m_ImageHeight * m_ImageDepth;
m_LabelStatus = new LabelType[numberOfPixels];
m_LabelStatus = make_unique_for_overwrite<LabelType[]>(numberOfPixels);
for (unsigned int index = 0; index < numberOfPixels; ++index)
{
m_LabelStatus[index] = 1;
Expand Down Expand Up @@ -623,11 +615,8 @@ RGBGibbsPriorFilter<TInputImage, TClassifiedImage>::RegionEraser()
{
const unsigned int size = m_ImageWidth * m_ImageHeight * m_ImageDepth;

delete[] m_Region;
m_Region = new unsigned short[size];

delete[] m_RegionCount;
m_RegionCount = new unsigned short[size];
m_Region = make_unique_for_overwrite<unsigned short[]>(size);
m_RegionCount = make_unique_for_overwrite<unsigned short[]>(size);

const auto valid_region_counter = make_unique_for_overwrite<unsigned short[]>(size);

Expand Down

0 comments on commit bb7d2f2

Please sign in to comment.