Skip to content

Commit

Permalink
feat(SupportInputImageTypes): Support VectorImage as template special…
Browse files Browse the repository at this point in the history
…ization

Reduces binary sizes, simplifies usage for pipelines that cannot support
VectorImage.
  • Loading branch information
thewtex committed Oct 27, 2022
1 parent 045abe8 commit 0ec818e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
2 changes: 2 additions & 0 deletions include/itkPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <CLI/Error.hpp>

#include "itkMacro.h"
#include "itkImage.h"
#include "itkVectorImage.h"

// Short circuit help output without raising an exception (currently not
// available in WASI)
Expand Down
73 changes: 73 additions & 0 deletions include/itkSpecializedImagePipelineFunctor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkSpecializedImagePipelineFunctor_h
#define itkSpecializedImagePipelineFunctor_h
#include "itkPipeline.h"
#include "itkImage.h"
#include "itkVectorImage.h"

namespace itk
{

namespace wasm
{

/** \class SpecializedImagePipelineFunctor
*
* \brief Internal class to dispatch for pipeline execution on itk::Image or itk::VectorImage.
*
* \ingroup ITKWebAssemblyInterface
*/
template<template <typename TImage> class TPipelineFunctor, unsigned int VDimension, typename TPixel>
class
SpecializedImagePipelineFunctor
{
public:
int operator()(itk::wasm::Pipeline & pipeline)
{
using ImageType = itk::Image<TPixel, VDimension>;

using PipelineType = TPipelineFunctor<ImageType>;
return PipelineType()(pipeline);
}
};

/** \class SpecializedImagePipelineFunctor
*
* \brief Internal class to dispatch for pipeline execution on itk::Image or itk::VectorImage.
*
* \ingroup ITKWebAssemblyInterface
*/
template<template <typename TImage> class TPipelineFunctor, unsigned int VDimension, typename TComponent>
class
SpecializedImagePipelineFunctor<TPipelineFunctor, VDimension, itk::VariableLengthVector<TComponent>>
{
public:
int operator()(itk::wasm::Pipeline & pipeline)
{
using ImageType = itk::VectorImage<TComponent, VDimension>;

using PipelineType = TPipelineFunctor<ImageType>;
return PipelineType()(pipeline);
}
};

} // end namespace wasm
} // end namespace itk

#endif
21 changes: 7 additions & 14 deletions include/itkSupportInputImageTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "itkVectorImage.h"
#include "itkImageIOBase.h"
#include "itkImageIOFactory.h"
#include "itkSpecializedImagePipelineFunctor.h"

namespace itk
{
Expand Down Expand Up @@ -76,7 +77,7 @@ main(int argc, char * argv[])
{
itk::wasm::Pipeline pipeline("support-multiple", "Test supporting multiple input image types", argc, argv);
// Supports the pixels types uint8_t, float
// Supports the pixels types uint8_t, float for itk::Image, itk::VectorImage
// Supports the image dimensions 2, 3
return itk::wasm::SupportInputImageTypes<PipelineFunctor,
uint8_t,
Expand Down Expand Up @@ -132,21 +133,13 @@ SupportInputImageTypes
using PixelType = TPixel;
using ConvertPixelTraits = DefaultConvertPixelTraits<PixelType>;

if (passThrough || imageType.componentType == MapComponentType<typename ConvertPixelTraits::ComponentType>::ComponentString && imageType.pixelType == MapPixelType<PixelType>::PixelString)
if (passThrough ||
imageType.componentType == MapComponentType<typename ConvertPixelTraits::ComponentType>::ComponentString &&
imageType.pixelType == MapPixelType<PixelType>::PixelString)
{
if (imageType.pixelType == "VariableLengthVector" || imageType.pixelType == "VariableSizeMatrix" )
if (passThrough || imageType.pixelType == "VariableLengthVector" || imageType.pixelType == "VariableSizeMatrix" || imageType.components == ConvertPixelTraits::GetNumberOfComponents() )
{
using ImageType = itk::VectorImage<typename ConvertPixelTraits::ComponentType, Dimension>;

using PipelineType = TPipelineFunctor<ImageType>;
return PipelineType()(pipeline);
}
else if(passThrough || imageType.components == ConvertPixelTraits::GetNumberOfComponents() )
{
using ImageType = Image<PixelType, Dimension>;

using PipelineType = TPipelineFunctor<ImageType>;
return PipelineType()(pipeline);
return SpecializedImagePipelineFunctor<TPipelineFunctor, Dimension, PixelType>()(pipeline);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ set(WebAssemblyInterface_SRCS
itkWASMComponentTypeFromIOComponentEnum.cxx
itkWASMPixelTypeFromIOPixelEnum.cxx
itkSupportInputImageTypes.cxx
itkSupportInputImageTypesNoVectorImage.cxx
itkSupportInputMeshTypes.cxx
itkSupportInputPolyDataTypes.cxx
)
Expand Down
3 changes: 2 additions & 1 deletion test/itkSupportInputImageTypesTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ itkSupportInputImageTypesTest(int argc, char * argv[])

return itk::wasm::SupportInputImageTypes<PipelineFunctor,
uint8_t,
float>
float,
itk::VariableLengthVector<uint8_t>>
::Dimensions<2U,3U>("input-image", pipeline);
}

0 comments on commit 0ec818e

Please sign in to comment.