Skip to content

Commit

Permalink
STYLE: Make casts of buffer pointer in PyBuffer clearer, using std::byte
Browse files Browse the repository at this point in the history
Removed the local variable `itkImageBuffer`.
  • Loading branch information
N-Dekker committed Oct 17, 2024
1 parent 36bd2b1 commit 83116c9
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions Modules/Bridge/NumPy/include/itkPyBuffer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "itkImportImageContainer.h"
#include <algorithm> // For reverse.
#include <cstddef> // For byte.
#include <memory> // For unique_ptr.

namespace itk
Expand All @@ -39,17 +40,14 @@ PyBuffer<TImage>::_GetArrayViewFromImage(ImageType * image)

image->Update();

ComponentType * buffer =
const_cast<ComponentType *>(reinterpret_cast<const ComponentType *>(image->GetBufferPointer()));

void * itkImageBuffer = buffer;
void * buffer = const_cast<std::byte *>(reinterpret_cast<const std::byte *>(image->GetBufferPointer()));

// Computing the length of data
const unsigned int numberOfComponents = image->GetNumberOfComponentsPerPixel();
const SizeValueType numberOfPixels = image->GetBufferedRegion().GetNumberOfPixels();
const auto len = static_cast<Py_ssize_t>(numberOfPixels * numberOfComponents * sizeof(ComponentType));

PyBuffer_FillInfo(&pyBuffer, nullptr, itkImageBuffer, len, 0, PyBUF_CONTIG);
PyBuffer_FillInfo(&pyBuffer, nullptr, buffer, len, 0, PyBUF_CONTIG);
return PyMemoryView_FromBuffer(&pyBuffer);
}

Expand Down

0 comments on commit 83116c9

Please sign in to comment.