Skip to content

Commit

Permalink
feat(cxx): add bufferedRegion to ImageJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jul 19, 2024
1 parent 11e398f commit 46551e7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 26 deletions.
27 changes: 25 additions & 2 deletions include/itkImageJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "glaze/glaze.hpp"

#include <optional>

namespace itk
{
/** \class ImageTypeJSON
Expand All @@ -46,6 +48,18 @@ namespace itk
unsigned int components { 1 };
};

/** \class ImageRegionJSON
*
* \brief Image region JSON representation data structure.
*
* \ingroup WebAssemblyInterface
*/
struct ImageRegionJSON
{
std::vector<IndexValueType> index {};
std::vector<SizeValueType> size {};
};

/** \class ImageJSON
*
* \brief Image JSON representation data structure.
Expand All @@ -61,7 +75,8 @@ namespace itk
std::vector<double> origin { 0.0, 0.0 };
std::vector<double> spacing { 1.0, 1.0 };
std::string direction;
std::vector<size_t> size { 0, 0 };
std::vector<SizeValueType> size { 0, 0 };
ImageRegionJSON bufferedRegion{};

std::string data;

Expand Down Expand Up @@ -93,6 +108,7 @@ auto imageToImageJSON(const TImage * image, const WasmImage<TImage> * wasmImage,

using PointType = typename ImageType::PointType;
PointType imageOrigin;
// largest region index is implicitly zeros
image->TransformIndexToPhysicalPoint(largestRegion.GetIndex(), imageOrigin);
imageJSON.origin.clear();
for (unsigned int ii = 0; ii < ImageType::ImageDimension; ++ii)
Expand Down Expand Up @@ -120,10 +136,17 @@ auto imageToImageJSON(const TImage * image, const WasmImage<TImage> * wasmImage,
imageJSON.direction = "data:application/vnd.itk.path,data/direction.raw";
}

imageJSON.bufferedRegion.index.clear();
imageJSON.bufferedRegion.size.clear();
imageJSON.size.clear();
const auto imageSize = image->GetBufferedRegion().GetSize();
// largest region index is implicitly zeros
const auto bufferedRegionIndex = image->GetBufferedRegion().GetIndex() - largestRegion.GetIndex();
const auto bufferedRegionSize = image->GetBufferedRegion().GetSize();
const auto imageSize = image->GetLargestPossibleRegion().GetSize();
for (unsigned int ii = 0; ii < ImageType::ImageDimension; ++ii)
{
imageJSON.bufferedRegion.index.push_back(bufferedRegionIndex[ii]);
imageJSON.bufferedRegion.size.push_back(bufferedRegionSize[ii]);
imageJSON.size.push_back(imageSize[ii]);
}

Expand Down
37 changes: 29 additions & 8 deletions include/itkImportVectorImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,47 @@ class ITK_TEMPLATE_EXPORT ImportVectorImageFilter : public ImageSource<TOutputIm
unsigned int vectorImageComponents = 1);

/** Set the region object that defines the size and starting index
* for the imported image. This will serve as the LargestPossibleRegion,
* for the imported image. This will serve as
* the BufferedRegion, and the RequestedRegion.
* \sa ImageRegion */
void
SetRegion(const RegionType & region)
SetBufferedRegion(const RegionType & region)
{
if (m_Region != region)
if (m_BufferedRegion != region)
{
m_Region = region;
m_BufferedRegion = region;
this->Modified();
}
}

/** Get the region object that defines the size and starting index
* for the imported image. This will serve as the LargestPossibleRegion,
* for the imported image. This will serve as the
* the BufferedRegion, and the RequestedRegion.
* \sa ImageRegion */
const RegionType &
GetRegion() const
GetBufferedRegion() const
{
return m_Region;
return m_BufferedRegion;
}

/** Set the LargestPossibleRegion
* \sa ImageRegion */
void
SetLargestPossibleRegion(const RegionType & region)
{
if (m_LargestPossibleRegion != region)
{
m_LargestPossibleRegion = region;
this->Modified();
}
}

/** Get the LargestPossibleRegion,
* \sa ImageRegion */
const RegionType &
GetLargestPossibleRegion() const
{
return m_LargestPossibleRegion;
}

/** Set the spacing (size of a pixel) of the image.
Expand Down Expand Up @@ -173,7 +193,8 @@ class ITK_TEMPLATE_EXPORT ImportVectorImageFilter : public ImageSource<TOutputIm
EnlargeOutputRequestedRegion(DataObject * output) override;

private:
RegionType m_Region;
RegionType m_BufferedRegion;
RegionType m_LargestPossibleRegion;
SpacingType m_Spacing;
OriginType m_Origin;
DirectionType m_Direction;
Expand Down
4 changes: 2 additions & 2 deletions include/itkImportVectorImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ ImportVectorImageFilter<TOutputImage>::EnlargeOutputRequestedRegion(DataObject *

// set the requested region to the largest possible region (in this case
// the amount of data that we have)
outputPtr->SetRequestedRegion(outputPtr->GetLargestPossibleRegion());
outputPtr->SetRequestedRegion(m_BufferedRegion);
}

template <typename TOutputImage>
Expand All @@ -136,7 +136,7 @@ ImportVectorImageFilter<TOutputImage>::GenerateOutputInformation()
outputPtr->SetSpacing(m_Spacing);
outputPtr->SetOrigin(m_Origin);
outputPtr->SetDirection(m_Direction);
outputPtr->SetLargestPossibleRegion(m_Region);
outputPtr->SetLargestPossibleRegion(m_LargestPossibleRegion);

if (outputPtr->GetNameOfClass() == "VectorImage")
{
Expand Down
8 changes: 4 additions & 4 deletions include/itkWasmImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ namespace itk
* \brief JSON representation for an itk::ImageBase
*
* JSON representation for an itk::ImageBase for interfacing across programming languages and runtimes.
*
*
* Pixel and Direction binary array buffer's are stored as strings with memory addresses or paths on disks or a virtual filesystem.
*
*
* Arrays:
*
*
* - 0: Pixel buffer `data`
* - 1: Orientation `direction`
*
*
* \ingroup WebAssemblyInterface
*/
template <typename TImage>
Expand Down
4 changes: 2 additions & 2 deletions include/itkWasmImageToImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ namespace itk
/**
*\class WasmImageToImageFilter
* \brief Convert an WasmImage to an Image object.
*
*
* TImage must match the type stored in the JSON representation or an exception will be shown.
*
*
* \ingroup WebAssemblyInterface
*/
template <typename TImage>
Expand Down
17 changes: 10 additions & 7 deletions include/itkWasmImageToImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,21 @@ WasmImageToImageFilter<TImage>
const DirectionType direction(vnlMatrix);
filter->SetDirection(direction);

using RegionType = typename ImageType::RegionType;
RegionType bufferedRegion;
RegionType largestRegion;
using SizeType = typename ImageType::SizeType;
SizeType size;
SizeType largestSize;
SizeValueType totalSize = 1;
for (unsigned int i = 0; i < Dimension; ++i)
{
size[i] = imageJSON.size[i];
totalSize *= size[i];
bufferedRegion.SetIndex(i, imageJSON.bufferedRegion.index[i]);
bufferedRegion.SetSize(i, imageJSON.bufferedRegion.size[i]);
largestRegion.SetSize(i, imageJSON.size[i]);
totalSize *= largestSize[i];
}
using RegionType = typename ImageType::RegionType;
RegionType region;
region.SetSize( size );
filter->SetRegion( region );
filter->SetBufferedRegion(bufferedRegion);
filter->SetLargestPossibleRegion(largestRegion);

const std::string dataString = imageJSON.data;
IOPixelType * dataPtr = reinterpret_cast< IOPixelType * >( std::strtoull(dataString.substr(35).c_str(), nullptr, 10) );
Expand Down
2 changes: 1 addition & 1 deletion test/itkWasmImageInterfaceWithNegativeIndexTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ itkWasmImageInterfaceWithNegativeIndexTest(int argc, char * argv[])
using ImageToWasmImageFilterType = itk::ImageToWasmImageFilter<ImageType>;
auto imageToJSON = ImageToWasmImageFilterType::New();
imageToJSON->SetInput(constantPad->GetOutput());
imageToJSON->Update();
imageToJSON->UpdateLargestPossibleRegion();
auto imageJSON = imageToJSON->GetOutput();
std::cout << "Image JSON: " << imageJSON->GetJSON() << std::endl;

Expand Down

0 comments on commit 46551e7

Please sign in to comment.