Skip to content

Commit

Permalink
Add json serialization for ImageParams
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Mar 22, 2024
1 parent 0e2864e commit 0a9e755
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ option(IMMVISION_FETCH_OPENCV OFF)
option(IMMVISION_FETCH_OPENCV_FULL OFF)
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# IMMVISION_SERIALIZE_JSON
# if ON, the ImageParams class will be able to serialize to JSON
# This requires nlohmann/json
option(IMMVISION_SERIALIZE_JSON OFF)
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# IMMVISION_BUILD_DEMOS
Expand Down
3 changes: 3 additions & 0 deletions src/immvision/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ find_package(OpenCV REQUIRED)

set(target_name immvision)
add_library(${target_name} ${sources})
if (IMMVISION_SERIALIZE_JSON)
target_compile_options(${target_name} PUBLIC -DIMMVISION_SERIALIZE_JSON)
endif()
target_include_directories(${target_name} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)

target_link_libraries(${target_name} PUBLIC
Expand Down
5 changes: 5 additions & 0 deletions src/immvision/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ namespace ImmVision
~ImageParams();
};

#ifdef IMMVISION_SERIALIZE_JSON
IMMVISION_API std::string ImageParamsToJson(const ImageParams& params);
IMMVISION_API void FillImageParamsFromJson(const std::string& json, ImageParams* params);
IMMVISION_API ImageParams ImageParamsFromJson(const std::string& json);
#endif

// Create ImageParams that display the image only, with no decoration, and no user interaction
IMMVISION_API ImageParams FactorImageParamsDisplayOnly();
Expand Down
184 changes: 184 additions & 0 deletions src/immvision/internal/image_params_serialize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#ifdef IMMVISION_SERIALIZE_JSON

#include "immvision/image.h"
#include "nlohmann/json.hpp"

namespace nlohmann
{
template <>
struct adl_serializer<cv::Size>
{
static void to_json(json& j, const cv::Size& size) {
j = json{{"width", size.width}, {"height", size.height}};
}

static void from_json(const json& j, cv::Size& size) {
j.at("width").get_to(size.width);
j.at("height").get_to(size.height);
}
};

template <>
struct adl_serializer<cv::Point>
{
static void to_json(json& j, const cv::Point& point) {
j = json{{"x", point.x}, {"y", point.y}};
}

static void from_json(const json& j, cv::Point& point) {
j.at("x").get_to(point.x);
j.at("y").get_to(point.y);
}
};

template <>
struct adl_serializer<cv::Matx33d>
{
static void to_json(json& j, const cv::Matx33d& mat) {
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
j[std::to_string(row) + std::to_string(col)] = mat(row, col);
}
}
}

static void from_json(const json& j, cv::Matx33d& mat) {
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
mat(row, col) = j[std::to_string(row) + std::to_string(col)];
}
}
}
};

template <>
struct adl_serializer<ImmVision::ColormapScaleFromStatsData>
{
static void to_json(json& j, const ImmVision::ColormapScaleFromStatsData& data) {
j = json{
{"ColorMapStatsType", data.ColorMapStatsType},
{"NbSigmas", data.NbSigmas},
{"UseStatsMin", data.UseStatsMin},
{"UseStatsMax", data.UseStatsMax}
};
}

static void from_json(const json& j, ImmVision::ColormapScaleFromStatsData& data) {
j.at("ColorMapStatsType").get_to(data.ColorMapStatsType);
j.at("NbSigmas").get_to(data.NbSigmas);
j.at("UseStatsMin").get_to(data.UseStatsMin);
j.at("UseStatsMax").get_to(data.UseStatsMax);
}
};

template <>
struct adl_serializer<ImmVision::ColormapSettingsData>
{
static void to_json(json& j, const ImmVision::ColormapSettingsData& data) {
j = json{
{"Colormap", data.Colormap},
{"ColormapScaleMin", data.ColormapScaleMin},
{"ColormapScaleMax", data.ColormapScaleMax},
{"ColormapScaleFromStats", data.ColormapScaleFromStats},
{"internal_ColormapHovered", data.internal_ColormapHovered}
};
}

static void from_json(const json& j, ImmVision::ColormapSettingsData& data) {
j.at("Colormap").get_to(data.Colormap);
j.at("ColormapScaleMin").get_to(data.ColormapScaleMin);
j.at("ColormapScaleMax").get_to(data.ColormapScaleMax);
j.at("ColormapScaleFromStats").get_to(data.ColormapScaleFromStats);
j.at("internal_ColormapHovered").get_to(data.internal_ColormapHovered);
}
};

template <>
struct adl_serializer<ImmVision::ImageParams>
{
static void to_json(json& j, const ImmVision::ImageParams& params)
{
j = json{
{"RefreshImage", params.RefreshImage},
{"ImageDisplaySize", params.ImageDisplaySize},
{"ZoomPanMatrix", params.ZoomPanMatrix},
{"ZoomKey", params.ZoomKey},
{"ColormapSettings", params.ColormapSettings},
{"ColormapKey", params.ColormapKey},
{"PanWithMouse", params.PanWithMouse},
{"ZoomWithMouseWheel", params.ZoomWithMouseWheel},
{"IsColorOrderBGR", params.IsColorOrderBGR},
{"SelectedChannel", params.SelectedChannel},
{"ShowSchoolPaperBackground", params.ShowSchoolPaperBackground},
{"ShowAlphaChannelCheckerboard", params.ShowAlphaChannelCheckerboard},
{"ShowGrid", params.ShowGrid},
{"DrawValuesOnZoomedPixels", params.DrawValuesOnZoomedPixels},
{"ShowImageInfo", params.ShowImageInfo},
{"ShowPixelInfo", params.ShowPixelInfo},
{"ShowZoomButtons", params.ShowZoomButtons},
{"ShowOptionsPanel", params.ShowOptionsPanel},
{"ShowOptionsInTooltip", params.ShowOptionsInTooltip},
{"ShowOptionsButton", params.ShowOptionsButton},
{"WatchedPixels", params.WatchedPixels},
{"AddWatchedPixelOnDoubleClick", params.AddWatchedPixelOnDoubleClick},
{"HighlightWatchedPixels", params.HighlightWatchedPixels}
};
}

static void from_json(const json& j, ImmVision::ImageParams& params)
{
j.at("RefreshImage").get_to(params.RefreshImage);
j.at("ImageDisplaySize").get_to(params.ImageDisplaySize);
j.at("ZoomPanMatrix").get_to(params.ZoomPanMatrix);
j.at("ZoomKey").get_to(params.ZoomKey);
j.at("ColormapSettings").get_to(params.ColormapSettings);
j.at("ColormapKey").get_to(params.ColormapKey);
j.at("PanWithMouse").get_to(params.PanWithMouse);
j.at("ZoomWithMouseWheel").get_to(params.ZoomWithMouseWheel);
j.at("IsColorOrderBGR").get_to(params.IsColorOrderBGR);
j.at("SelectedChannel").get_to(params.SelectedChannel);
j.at("ShowSchoolPaperBackground").get_to(params.ShowSchoolPaperBackground);
j.at("ShowAlphaChannelCheckerboard").get_to(params.ShowAlphaChannelCheckerboard);
j.at("ShowGrid").get_to(params.ShowGrid);
j.at("DrawValuesOnZoomedPixels").get_to(params.DrawValuesOnZoomedPixels);
j.at("ShowImageInfo").get_to(params.ShowImageInfo);
j.at("ShowPixelInfo").get_to(params.ShowPixelInfo);
j.at("ShowZoomButtons").get_to(params.ShowZoomButtons);
j.at("ShowOptionsPanel").get_to(params.ShowOptionsPanel);
j.at("ShowOptionsInTooltip").get_to(params.ShowOptionsInTooltip);
j.at("ShowOptionsButton").get_to(params.ShowOptionsButton);
j.at("WatchedPixels").get_to(params.WatchedPixels);
j.at("AddWatchedPixelOnDoubleClick").get_to(params.AddWatchedPixelOnDoubleClick);
j.at("HighlightWatchedPixels").get_to(params.HighlightWatchedPixels);
}

};
} // namespace nlohmann


namespace ImmVision
{
using json = nlohmann::json;


std::string ImageParamsToJson(const ImageParams& params)
{
json j = params;
return j.dump(4);
}

void FillImageParamsFromJson(const std::string& json, ImageParams* params)
{
nlohmann::json j = nlohmann::json::parse(json);
nlohmann::adl_serializer<ImageParams>::from_json(j, *params);
}

ImageParams ImageParamsFromJson(const std::string& json)
{
ImageParams params;
FillImageParamsFromJson(json, &params);
return params;
}

}
#endif
Loading

0 comments on commit 0a9e755

Please sign in to comment.