Skip to content

Commit

Permalink
Change the image acquisition behaviour of NiftiImage
Browse files Browse the repository at this point in the history
Don't own the image pointer if it's constructed by using a nifti_image pointer
  • Loading branch information
onurulgen committed Jul 12, 2024
1 parent dd78e8e commit cbbdd00
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 37 deletions.
2 changes: 1 addition & 1 deletion niftyreg_build_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
429
430
2 changes: 1 addition & 1 deletion reg-apps/reg_ppcnr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ int main(int argc, char **argv)
nifti_image_free(source);
makesource->ndim=makesource->dim[0] = 4;
makesource->nt = makesource->dim[4] = atoi(argv[++i]);
makesource->nvox = NiftiImage::calcVoxelNumber(makesource->nx, makesource->ndim);
makesource->nvox = NiftiImage::calcVoxelNumber(makesource, makesource->ndim);
makesource->data = malloc(makesource->nvox * makesource->nbyper);
char *temp_data = reinterpret_cast<char *>(makesource->data);
for(int ii=0; ii<makesource->nt; ii++) // fill with file data
Expand Down
18 changes: 10 additions & 8 deletions reg-io/RNifti/NiftiImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,8 @@ class NiftiImage
enum class Dim { NDim, X, Y, Z, T, U, V, W }; /**< Dimension enumeration */

enum class Copy { /**< Enumeration of copy options of the constructor */
None, /**< Do not copy the image */
None, /**< Do not copy the image but acquire the pointer without ownership */
Acquire, /**< Do not copy the image but acquire the pointer with ownership */
Image, /**< Copy the entire image */
ImageInfo, /**< Copy only the image info, and do not allocate data */
ImageInfoAndAllocData /**< Copy only the image info, and allocate and zero the data */
Expand Down Expand Up @@ -1306,8 +1307,9 @@ class NiftiImage
* responsibility for freeing the associated memory. If the object currently wraps another
* pointer, it will be released
* @param image The pointer to wrap
* @param own If \c true, the object will take responsibility for freeing the memory
**/
void acquire (nifti_image * const image);
void acquire (nifti_image * const image, const bool own = true);

/**
* Acquire the same pointer as another \c NiftiImage, incrementing the shared reference count
Expand All @@ -1316,7 +1318,7 @@ class NiftiImage
void acquire (const NiftiImage &source)
{
refCount = source.refCount;
acquire(source.image);
acquire(source.image, refCount);
}

/**
Expand Down Expand Up @@ -1420,12 +1422,12 @@ class NiftiImage
/**
* Copy constructor
* @param source Another \c NiftiImage object
* @param copy If \c Copy::None, the new object just wraps the same pointer as \c source; otherwise the image data is copied
* @param copy If \c Copy::None or \c Copy::Acquire, the new object just wraps the same pointer as \c source; otherwise the image data is copied
**/
NiftiImage (const NiftiImage &source, const Copy copy = Copy::Image)
: NiftiImage()
{
if (copy != Copy::None) {
if (copy != Copy::None && copy != Copy::Acquire) {
this->copy(source, copy);
} else {
acquire(source);
Expand Down Expand Up @@ -1458,15 +1460,15 @@ class NiftiImage
/**
* Initialise using an existing \c nifti_image pointer
* @param image An existing \c nifti_image pointer, possibly \c nullptr
* @param copy If \c Copy::None, the new object just wraps the pointer passed to it; otherwise the image data is copied
* @param copy If \c Copy::None or \c Copy::Acquire, the new object just wraps the pointer passed to it with or without ownership; otherwise the image data is copied
**/
NiftiImage (nifti_image * const image, const Copy copy = Copy::None)
: NiftiImage()
{
if (copy != Copy::None)
if (copy != Copy::None && copy != Copy::Acquire)
this->copy(image, copy);
else
acquire(image);
acquire(image, copy == Copy::Acquire);
RN_DEBUG("Creating NiftiImage (v%d) with pointer %p (from pointer)", RNIFTI_NIFTILIB_VERSION, this->image);
}

Expand Down
15 changes: 10 additions & 5 deletions reg-io/RNifti/NiftiImage_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ inline int NiftiImage::fileVersion (const std::string &path)
#endif
}

inline void NiftiImage::acquire (nifti_image * const image)
inline void NiftiImage::acquire (nifti_image * const image, const bool own)
{
// If we're taking ownership of a new image, release the old one
if (this->image != nullptr && this->image != image)
Expand All @@ -715,11 +715,16 @@ inline void NiftiImage::acquire (nifti_image * const image)
this->image = image;
if (image != nullptr)
{
if (this->refCount == nullptr)
this->refCount = new int(1);
if (own) {
if (this->refCount == nullptr)
this->refCount = new int(1);
else
(*this->refCount)++;
}
if (this->refCount != nullptr)
RN_DEBUG("Acquiring pointer %p (v%d; reference count is %d)", this->image, RNIFTI_NIFTILIB_VERSION, *this->refCount);
else
(*this->refCount)++;
RN_DEBUG("Acquiring pointer %p (v%d; reference count is %d)", this->image, RNIFTI_NIFTILIB_VERSION, *this->refCount);
RN_DEBUG("Acquiring pointer %p without ownership (v%d)", this->image, RNIFTI_NIFTILIB_VERSION);
}
}

Expand Down
2 changes: 1 addition & 1 deletion reg-io/_reg_ReadWriteImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ NiftiImage reg_io_ReadImageFile(const char *filename, const bool onlyHeader) {
reg_checkAndCorrectDimension(image);

// Return the nifti image
return image;
return NiftiImage(image, NiftiImage::Copy::Acquire);
}
/* *************************************************************** */
void reg_io_WriteImageFile(nifti_image *image, const char *filename) {
Expand Down
2 changes: 0 additions & 2 deletions reg-lib/Compute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ void Compute::NormaliseGradient(double maxGradLength, bool optimiseX, bool optim
const size_t voxelsPerVolume = transformationGradient.nVoxelsPerVolume();
#endif

transformationGradient.disown();

#ifdef _OPENMP
#pragma omp parallel for default(none) \
shared(voxelsPerVolume, ptrX, ptrY, ptrZ, hasZ, optimiseX, optimiseY, optimiseZ, maxGradLenInv)
Expand Down
8 changes: 0 additions & 8 deletions reg-test/reg_test_conjugateGradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,14 @@ TEST_CASE_METHOD(ConjugateGradientTest, "Conjugate Gradient", "[unit]") {
NiftiImage img = content->GetControlPointGrid();
// Use bestControlPointGrid to store bestDof during initialisation of the optimiser
img.copyData(bestControlPointGrid);
img.disown();
content->UpdateControlPointGrid();

// Set the transformation gradients
img = content->GetTransformationGradient();
img.copyData(transGrad);
img.disown();
content->UpdateTransformationGradient();
img = contentBw->GetTransformationGradient();
img.copyData(transGradBw);
img.disown();
contentBw->UpdateTransformationGradient();

// Create a copy of the control point grid for expected results
Expand All @@ -272,7 +269,6 @@ TEST_CASE_METHOD(ConjugateGradientTest, "Conjugate Gradient", "[unit]") {
img = content->GetControlPointGrid();
const auto cppPtr = img.data();
const auto cppExpPtr = controlPointGridExpected.data();
img.disown();
for (size_t i = 0; i < controlPointGridExpected.nVoxels(); ++i) {
const float cppVal = cppPtr[i];
const float cppExpVal = cppExpPtr[i];
Expand Down Expand Up @@ -312,12 +308,10 @@ TEST_CASE_METHOD(ConjugateGradientTest, "Conjugate Gradient", "[unit]") {
// Update the transformation gradients
img = content->GetTransformationGradient();
img.copyData(transGrad);
img.disown();
content->UpdateTransformationGradient();
if (isSymmetric) {
img = contentBw->GetTransformationGradient();
img.copyData(transGradBw);
img.disown();
contentBw->UpdateTransformationGradient();
}

Expand All @@ -329,13 +323,11 @@ TEST_CASE_METHOD(ConjugateGradientTest, "Conjugate Gradient", "[unit]") {
img = content->GetTransformationGradient();
const auto gradPtr = img.data();
const auto gradExpPtr = transGrad.data();
img.disown();
NiftiImageData gradBwPtr, gradExpBwPtr;
if (isSymmetric) {
img = contentBw->GetTransformationGradient();
gradBwPtr = img.data();
gradExpBwPtr = transGradBw.data();
img.disown();
}
for (size_t i = 0; i < transGrad.nVoxels(); ++i) {
const float gradVal = gradPtr[i];
Expand Down
2 changes: 0 additions & 2 deletions reg-test/reg_test_imageGradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ TEST_CASE("Image Gradient", "[unit]") {
warpedGradient.setDim(NiftiDim::Z, 1);
warpedGradient.setDim(NiftiDim::U, defField->nu);
warpedGradient.recalcVoxelNumber();
warpedGradient.disown();

// Set the deformation field
content->SetDeformationField(defField.disown());
Expand All @@ -199,7 +198,6 @@ TEST_CASE("Image Gradient", "[unit]") {
warpedGradient = content->GetWarpedGradient();
const auto warpedGradPtr = warpedGradient.data();
const size_t nVoxels = warpedGradient.nVoxels();
warpedGradient.disown();
for (size_t i = 0; i < nVoxels; ++i) {
const float warpedGradVal = warpedGradPtr[i];
const auto diff = abs(warpedGradVal - testResult[i]);
Expand Down
1 change: 0 additions & 1 deletion reg-test/reg_test_interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ TEST_CASE("Interpolation", "[unit]") {
warped = content->GetWarped();
const auto warpedPtr = warped.data();
const size_t nVoxels = warped.nVoxels();
warped.disown();
for (size_t i = 0; i < nVoxels; ++i) {
const float warpedValue = warpedPtr[i];
const float diff = abs(warpedValue - testResult[i]);
Expand Down
1 change: 0 additions & 1 deletion reg-test/reg_test_normaliseGradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class NormaliseGradientTest {
// Set the transformation gradient image to host the computation
NiftiImage transGrad = content->GetTransformationGradient();
transGrad.copyData(expTransGrad);
transGrad.disown();
content->UpdateTransformationGradient();

// Calculate the maximal length
Expand Down
2 changes: 0 additions & 2 deletions reg-test/reg_test_regr_exponentiateGradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class ExponentiateGradientTest {
voxelGrad->sto_ijk = voxelBasedGrad->sto_ijk;
voxelGrad->sto_xyz = voxelBasedGrad->sto_xyz;
voxelGrad.copyData(voxelBasedGrad);
voxelGrad.disown();
contentCpu->UpdateVoxelBasedMeasureGradient();
voxelGrad = contentCuda->DefContent::GetVoxelBasedMeasureGradient();
voxelGrad->sform_code = voxelBasedGrad->sform_code;
Expand All @@ -135,7 +134,6 @@ class ExponentiateGradientTest {
voxelGrad->sto_ijk = voxelBasedGrad->sto_ijk;
voxelGrad->sto_xyz = voxelBasedGrad->sto_xyz;
voxelGrad.copyData(voxelBasedGrad);
voxelGrad.disown();
contentCuda->UpdateVoxelBasedMeasureGradient();

// Create the computes
Expand Down
2 changes: 0 additions & 2 deletions reg-test/reg_test_regr_resampleGradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class ResampleGradientTest {
voxelGrad->sto_ijk = voxelBasedGrad->sto_ijk;
voxelGrad->sto_xyz = voxelBasedGrad->sto_xyz;
voxelGrad.copyData(voxelBasedGrad);
voxelGrad.disown();
contentCpu->UpdateVoxelBasedMeasureGradient();
voxelGrad = contentCuda->DefContent::GetVoxelBasedMeasureGradient();
voxelGrad->sform_code = voxelBasedGrad->sform_code;
Expand All @@ -112,7 +111,6 @@ class ResampleGradientTest {
voxelGrad->sto_ijk = voxelBasedGrad->sto_ijk;
voxelGrad->sto_xyz = voxelBasedGrad->sto_xyz;
voxelGrad.copyData(voxelBasedGrad);
voxelGrad.disown();
contentCuda->UpdateVoxelBasedMeasureGradient();

// Create the computes
Expand Down
2 changes: 0 additions & 2 deletions reg-test/reg_test_regr_updateVelocityField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@ class UpdateVelocityFieldTest {
// Set the transformation gradient image to host the computation
NiftiImage transGradCpu = contentCpu->GetTransformationGradient();
transGradCpu.copyData(transGrad);
transGradCpu.disown();
contentCpu->UpdateTransformationGradient();
NiftiImage transGradCuda = contentCuda->GetTransformationGradient();
transGradCuda.copyData(transGrad);
transGradCuda.disown();
contentCuda->UpdateTransformationGradient();

// Create the computes
Expand Down
1 change: 0 additions & 1 deletion reg-test/reg_test_voxelCentricToNodeCentric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class VoxelCentricToNodeCentricTest {
const float weight = distr(gen);
NiftiImage expTransGrad(transGrad, NiftiImage::Copy::ImageInfoAndAllocData);
VoxelCentricToNodeCentric<float>(floating, expTransGrad, voxelGrad, weight);
transGrad.disown(); voxelGrad.disown();

// Extract the node-based NMI gradient from the voxel-based NMI gradient
unique_ptr<Compute> compute{ platform->CreateCompute(*content) };
Expand Down

0 comments on commit cbbdd00

Please sign in to comment.