Skip to content

Commit ebf267a

Browse files
committed
refactor(SupportInputImageTypes): rapidjson to glaze
1 parent 3fc591c commit ebf267a

File tree

4 files changed

+30
-39
lines changed

4 files changed

+30
-39
lines changed

include/itkSupportInputImageTypes.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,15 @@
3131
#include "itkSpecializedImagePipelineFunctor.h"
3232
#include "WebAssemblyInterfaceExport.h"
3333

34+
#include "itkImageJSON.h"
35+
3436
namespace itk
3537
{
3638

37-
namespace wasm
38-
{
39+
WebAssemblyInterface_EXPORT bool lexical_cast(const std::string &input, ImageTypeJSON & imageType);
3940

40-
struct InterfaceImageType
41+
namespace wasm
4142
{
42-
unsigned int dimension{2};
43-
std::string componentType{"uint8"};
44-
std::string pixelType{"Scalar"};
45-
unsigned int components{1};
46-
};
47-
48-
WebAssemblyInterface_EXPORT bool lexical_cast(const std::string &input, InterfaceImageType & imageType);
4943

5044
/** \class SupportInputImageTypes
5145
*
@@ -98,7 +92,7 @@ SupportInputImageTypes
9892
static int
9993
Dimensions(const std::string & inputImageOptionName, Pipeline & pipeline)
10094
{
101-
InterfaceImageType imageType;
95+
ImageTypeJSON imageType;
10296

10397
const auto iwpArgc = pipeline.get_argc();
10498
const auto iwpArgv = pipeline.get_argv();
@@ -128,17 +122,20 @@ SupportInputImageTypes
128122
private:
129123
template<unsigned int VDimension, typename TPixel, typename ...TPixelsRest>
130124
static int
131-
IteratePixelTypes(Pipeline & pipeline, const InterfaceImageType & imageType, bool passThrough = false)
125+
IteratePixelTypes(Pipeline & pipeline, const ImageTypeJSON & imageType, bool passThrough = false)
132126
{
133127
constexpr unsigned int Dimension = VDimension;
134128
using PixelType = TPixel;
135129
using ConvertPixelTraits = DefaultConvertPixelTraits<PixelType>;
136130

137131
if (passThrough ||
138-
imageType.componentType == MapComponentType<typename ConvertPixelTraits::ComponentType>::ComponentString &&
139-
imageType.pixelType == MapPixelType<PixelType>::PixelString)
132+
imageType.componentType == MapComponentType<typename ConvertPixelTraits::ComponentType>::JSONComponentEnum &&
133+
imageType.pixelType == MapPixelType<PixelType>::JSONPixelEnum)
140134
{
141-
if (passThrough || imageType.pixelType == "VariableLengthVector" || imageType.pixelType == "VariableSizeMatrix" || imageType.components == ConvertPixelTraits::GetNumberOfComponents() )
135+
if (passThrough ||
136+
imageType.pixelType == JSONPixelTypesEnum::VariableLengthVector ||
137+
imageType.pixelType == JSONPixelTypesEnum::VariableSizeMatrix ||
138+
imageType.components == ConvertPixelTraits::GetNumberOfComponents() )
142139
{
143140
return SpecializedImagePipelineFunctor<TPipelineFunctor, Dimension, PixelType>()(pipeline);
144141
}
@@ -149,14 +146,15 @@ SupportInputImageTypes
149146
}
150147

151148
std::ostringstream ostrm;
152-
ostrm << "Unsupported pixel type: " << imageType.pixelType << " with component type: " << imageType.componentType << " and components: " << imageType.components;
149+
std::string imageTypeString = glz::write_json(imageType).value_or("error");
150+
ostrm << "Unsupported image type: " << imageTypeString << std::endl;
153151
CLI::Error err("Runtime error", ostrm.str(), 1);
154152
return pipeline.exit(err);
155153
}
156154

157155
template<unsigned int VDimension, unsigned int ...VDimensions>
158156
static int
159-
IterateDimensions(Pipeline & pipeline, const InterfaceImageType & imageType, bool passThrough = false)
157+
IterateDimensions(Pipeline & pipeline, const ImageTypeJSON & imageType, bool passThrough = false)
160158
{
161159
if (passThrough || VDimension == imageType.dimension)
162160
{

src/itkOutputBinaryStream.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#ifndef ITK_WASM_NO_MEMORY_IO
2222
#include "itkWasmExports.h"
2323
#include <sstream>
24-
#include "rapidjson/document.h"
2524
#include "itkWasmStringStream.h"
2625
#endif
2726

src/itkOutputTextStream.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#ifndef ITK_WASM_NO_MEMORY_IO
2222
#include "itkWasmExports.h"
2323
#include <sstream>
24-
#include "rapidjson/document.h"
2524
#include "itkWasmStringStream.h"
2625
#endif
2726

src/itkSupportInputImageTypes.cxx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,28 @@
1818
#include "itkSupportInputImageTypes.h"
1919
#include "itkWasmExports.h"
2020

21-
#include "rapidjson/document.h"
21+
#include "itkjsonFromIOComponentEnum.h"
22+
#include "itkjsonFromIOPixelEnum.h"
2223

2324
namespace itk
2425
{
2526

26-
namespace wasm
27-
{
28-
29-
bool lexical_cast(const std::string &input, InterfaceImageType & imageType)
27+
bool lexical_cast(const std::string &input, ImageTypeJSON & imageType)
3028
{
3129
if (wasm::Pipeline::get_use_memory_io())
3230
{
3331
#ifndef ITK_WASM_NO_MEMORY_IO
3432
const unsigned int index = std::stoi(input);
35-
auto json = getMemoryStoreInputJSON(0, index);
36-
rapidjson::Document document;
37-
if (document.Parse(json.c_str()).HasParseError())
38-
{
39-
throw std::runtime_error("Could not parse JSON");
40-
}
41-
42-
const rapidjson::Value & jsonImageType = document["imageType"];
43-
imageType.dimension = jsonImageType["dimension"].GetInt();
44-
imageType.componentType = jsonImageType["componentType"].GetString();
45-
imageType.pixelType = jsonImageType["pixelType"].GetString();
46-
imageType.components = jsonImageType["components"].GetInt();
33+
auto json = wasm::getMemoryStoreInputJSON(0, index);
34+
std::string deserialized;
35+
auto deserializedAttempt = glz::read_json<itk::ImageJSON>(json);
36+
if (!deserializedAttempt)
37+
{
38+
const std::string descriptiveError = glz::format_error(deserializedAttempt, json);
39+
throw std::runtime_error("Failed to deserialize ImageJSON: " + descriptiveError);
40+
}
41+
auto imageJSON = deserializedAttempt.value();
42+
imageType = imageJSON.imageType;
4743
#else
4844
return false;
4945
#endif
@@ -64,11 +60,11 @@ bool lexical_cast(const std::string &input, InterfaceImageType & imageType)
6460

6561
using IOComponentType = itk::IOComponentEnum;
6662
const IOComponentType ioComponentEnum = imageIO->GetComponentType();
67-
imageType.componentType = WasmComponentTypeFromIOComponentEnum( ioComponentEnum );
63+
imageType.componentType = itk::jsonComponentTypeFromIOComponentEnum( ioComponentEnum );
6864

6965
using IOPixelType = itk::IOPixelEnum;
7066
const IOPixelType ioPixelEnum = imageIO->GetPixelType();
71-
imageType.pixelType = WasmPixelTypeFromIOPixelEnum( ioPixelEnum );
67+
imageType.pixelType = itk::jsonFromIOPixelEnum( ioPixelEnum );
7268

7369
imageType.components = imageIO->GetNumberOfComponents();
7470
#else
@@ -78,5 +74,4 @@ bool lexical_cast(const std::string &input, InterfaceImageType & imageType)
7874
return true;
7975
}
8076

81-
} // end namespace wasm
8277
} // end namespace itk

0 commit comments

Comments
 (0)