Skip to content

Commit

Permalink
feat(Image): support metadata serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 14, 2022
1 parent f9ac56e commit 282db1b
Show file tree
Hide file tree
Showing 7 changed files with 896 additions and 3 deletions.
6 changes: 6 additions & 0 deletions include/itkImageToWASMImageFilter.hxx
Expand Up @@ -21,6 +21,7 @@
#include "itkImageToWASMImageFilter.h"

#include "itkDefaultConvertPixelTraits.h"
#include "itkMetaDataDictionaryJSON.h"

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

auto dictionary = image->GetMetaDataDictionary();
rapidjson::Value metadataJson(rapidjson::kArrayType);
wasm::ConvertMetaDataDictionaryToJSON(dictionary, metadataJson, allocator);
document.AddMember( "metadata", metadataJson.Move(), allocator );

rapidjson::StringBuffer stringBuffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(stringBuffer);
document.Accept(writer);
Expand Down
42 changes: 42 additions & 0 deletions include/itkMetaDataDictionaryJSON.h
@@ -0,0 +1,42 @@
/*=========================================================================
*
* 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 itkMetaDataDictionaryJSON_h
#define itkMetaDataDictionaryJSON_h

#include "itkMetaDataDictionary.h"
#include "itkDefaultConvertPixelTraits.h"
#include "itkMetaDataObject.h"
#include "itkArray.h"
#include "itkMatrix.h"

#include "rapidjson/document.h"

namespace itk
{

namespace wasm
{

void ConvertMetaDataDictionaryToJSON(const itk::MetaDataDictionary & dictionary, rapidjson::Value & metadataJson, rapidjson::Document::AllocatorType& allocator);

void ConvertJSONToMetaDataDictionary(const rapidjson::Value & metadataJson, itk::MetaDataDictionary & dictionary);

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

#endif
11 changes: 10 additions & 1 deletion include/itkWASMImageToImageFilter.hxx
Expand Up @@ -20,11 +20,13 @@

#include "itkWASMImageToImageFilter.h"

#include "itkMetaDataDictionaryJSON.h"
#include "itkImportVectorImageFilter.h"
#include <exception>
#include "itkWASMMapComponentType.h"
#include "itkWASMMapPixelType.h"
#include "itkDefaultConvertPixelTraits.h"
#include "itkMetaDataObject.h"

#include "rapidjson/document.h"

Expand Down Expand Up @@ -229,9 +231,16 @@ WASMImageToImageFilter<TImage>
{
filter->SetImportPointer( dataPtr, totalSize, letImageContainerManageMemory);
}

filter->Update();
image->Graft(filter->GetOutput());

if (document.HasMember("metadata"))
{
auto dictionary = image->GetMetaDataDictionary();
const rapidjson::Value & metadataJson = document["metadata"];
wasm::ConvertJSONToMetaDataDictionary(metadataJson, dictionary);
}

}

template <typename TImage>
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ include_directories(${WebAssemblyInterface_INCLUDE_DIRS})

set(WebAssemblyInterface_SRCS
itkPipeline.cxx
itkMetaDataDictionaryJSON.cxx
itkWASMExports.cxx
itkWASMDataObject.cxx
itkWASMImageIOBase.cxx
Expand Down

0 comments on commit 282db1b

Please sign in to comment.