Skip to content

Commit 282db1b

Browse files
committed
feat(Image): support metadata serialization
1 parent f9ac56e commit 282db1b

7 files changed

+896
-3
lines changed

include/itkImageToWASMImageFilter.hxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "itkImageToWASMImageFilter.h"
2222

2323
#include "itkDefaultConvertPixelTraits.h"
24+
#include "itkMetaDataDictionaryJSON.h"
2425

2526
#include "itkWASMMapComponentType.h"
2627
#include "itkWASMMapPixelType.h"
@@ -203,6 +204,11 @@ ImageToWASMImageFilter<TImage>
203204
dataString.SetString( dataStream.str().c_str(), allocator );
204205
document.AddMember( "data", dataString.Move(), allocator );
205206

207+
auto dictionary = image->GetMetaDataDictionary();
208+
rapidjson::Value metadataJson(rapidjson::kArrayType);
209+
wasm::ConvertMetaDataDictionaryToJSON(dictionary, metadataJson, allocator);
210+
document.AddMember( "metadata", metadataJson.Move(), allocator );
211+
206212
rapidjson::StringBuffer stringBuffer;
207213
rapidjson::Writer<rapidjson::StringBuffer> writer(stringBuffer);
208214
document.Accept(writer);

include/itkMetaDataDictionaryJSON.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
#ifndef itkMetaDataDictionaryJSON_h
19+
#define itkMetaDataDictionaryJSON_h
20+
21+
#include "itkMetaDataDictionary.h"
22+
#include "itkDefaultConvertPixelTraits.h"
23+
#include "itkMetaDataObject.h"
24+
#include "itkArray.h"
25+
#include "itkMatrix.h"
26+
27+
#include "rapidjson/document.h"
28+
29+
namespace itk
30+
{
31+
32+
namespace wasm
33+
{
34+
35+
void ConvertMetaDataDictionaryToJSON(const itk::MetaDataDictionary & dictionary, rapidjson::Value & metadataJson, rapidjson::Document::AllocatorType& allocator);
36+
37+
void ConvertJSONToMetaDataDictionary(const rapidjson::Value & metadataJson, itk::MetaDataDictionary & dictionary);
38+
39+
} // end namespace wasm
40+
} // end namespace itk
41+
42+
#endif

include/itkWASMImageToImageFilter.hxx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
#include "itkWASMImageToImageFilter.h"
2222

23+
#include "itkMetaDataDictionaryJSON.h"
2324
#include "itkImportVectorImageFilter.h"
2425
#include <exception>
2526
#include "itkWASMMapComponentType.h"
2627
#include "itkWASMMapPixelType.h"
2728
#include "itkDefaultConvertPixelTraits.h"
29+
#include "itkMetaDataObject.h"
2830

2931
#include "rapidjson/document.h"
3032

@@ -229,9 +231,16 @@ WASMImageToImageFilter<TImage>
229231
{
230232
filter->SetImportPointer( dataPtr, totalSize, letImageContainerManageMemory);
231233
}
232-
233234
filter->Update();
234235
image->Graft(filter->GetOutput());
236+
237+
if (document.HasMember("metadata"))
238+
{
239+
auto dictionary = image->GetMetaDataDictionary();
240+
const rapidjson::Value & metadataJson = document["metadata"];
241+
wasm::ConvertJSONToMetaDataDictionary(metadataJson, dictionary);
242+
}
243+
235244
}
236245

237246
template <typename TImage>

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include_directories(${WebAssemblyInterface_INCLUDE_DIRS})
22

33
set(WebAssemblyInterface_SRCS
44
itkPipeline.cxx
5+
itkMetaDataDictionaryJSON.cxx
56
itkWASMExports.cxx
67
itkWASMDataObject.cxx
78
itkWASMImageIOBase.cxx

0 commit comments

Comments
 (0)