Skip to content

Commit

Permalink
Add ImageDisplayResizable
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed May 11, 2024
1 parent 934db18 commit fa219b0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 12 deletions.
19 changes: 15 additions & 4 deletions src/immvision/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace ImmVision
IMMVISION_API void Image(const std::string& label, const cv::Mat& mat, ImageParams* params);


// Only, display the image, with no decoration, and no user interaction (by default)
// ImageDisplay: Only, display the image, with no user interaction (by default)
//
// Parameters:
// :param label_id
Expand All @@ -263,6 +263,8 @@ namespace ImmVision
// To circumvent this, you can modify your label like this:
// "MyLabel##some_unique_id" (the part after "##" will not be displayed but will be part of the id)
// - To display an empty legend, use "##_some_unique_id"
// - if your legend is displayed (i.e. it does not start with "##"),
// then the total size of the widget will be larger than the imageDisplaySize.
//
// :param mat:
// An image you want to display, under the form of an OpenCV matrix. All types of dense matrices are supported.
Expand All @@ -271,9 +273,6 @@ namespace ImmVision
// Size of the displayed image (can be different from the mat size)
// If you specify only the width or height (e.g (300, 0), then the other dimension
// will be calculated automatically, respecting the original image w/h ratio.
// Warning:
// if your legend is displayed (i.e. it does not start with "##"),
// then the total size of the widget will be larger than the imageDisplaySize.
//
// :param refreshImage:
// images textures are cached. Set to true if your image matrix/buffer has changed
Expand Down Expand Up @@ -306,6 +305,18 @@ namespace ImmVision
bool isBgrOrBgra = true
);

// ImageDisplayResizable: display the image, with no user interaction (by default)
// The image can be resized by the user (and the new size will be stored in the size parameter)
// The label will not be displayed (but it will be used as an id, and must be unique)
IMMVISION_API cv::Point2d ImageDisplayResizable(
const std::string& label_id,
const cv::Mat& mat,
ImVec2* size,
bool refreshImage = false,
bool showOptionsButton = false,
bool isBgrOrBgra = true
);


// Return the list of the available color maps
// Taken from https://github.com/yuki-koyama/tinycolormap, thanks to Yuki Koyama
Expand Down
36 changes: 36 additions & 0 deletions src/immvision/internal/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,12 +668,48 @@ namespace ImmVision
params.ImageDisplaySize = imageDisplaySize;
params.RefreshImage = refreshImage;
params.IsColorOrderBGR = isBgrOrBgra;
params.ZoomPanMatrix = ZoomPanTransform::MakeFullView(mat.size(), params.ImageDisplaySize);
}

Image(label_id, mat, &params);
return params.MouseInfo.MousePosition;
}

IMMVISION_API cv::Point2d ImageDisplayResizable(
const std::string& label_id,
const cv::Mat& mat,
ImVec2* size,
bool refreshImage,
bool showOptionsButton,
bool isBgrOrBgra
)
{
ImGuiID id = ImGui::GetID(label_id.c_str());
static std::map<ImGuiID, ImageParams> s_Params;
if (s_Params.find(id) == s_Params.end())
{
ImageParams params = showOptionsButton ? ImageParams() : FactorImageParamsDisplayOnly();
s_Params[id] = params;
}

ImageParams& params = s_Params.at(id);
{
params.ShowOptionsButton = showOptionsButton;
params.ImageDisplaySize = cv::Size((int)size->x, (int)size->y);
params.CanResize = true;
params.RefreshImage = refreshImage;
params.IsColorOrderBGR = isBgrOrBgra;
params.ZoomPanMatrix = ZoomPanTransform::MakeFullView(mat.size(), params.ImageDisplaySize);
}
std::string hiddenLabel = std::string("##") + label_id;
Image(hiddenLabel, mat, &params);



*size = ImVec2((float)params.ImageDisplaySize.width, (float)params.ImageDisplaySize.height);
return params.MouseInfo.MousePosition;
}


ImageParams FactorImageParamsDisplayOnly()
{
Expand Down
52 changes: 48 additions & 4 deletions src_all_in_one/immvision/immvision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ namespace ImmVision
IMMVISION_API void Image(const std::string& label, const cv::Mat& mat, ImageParams* params);


// Only, display the image, with no decoration, and no user interaction (by default)
// ImageDisplay: Only, display the image, with no user interaction (by default)
//
// Parameters:
// :param label_id
Expand All @@ -276,6 +276,8 @@ namespace ImmVision
// To circumvent this, you can modify your label like this:
// "MyLabel##some_unique_id" (the part after "##" will not be displayed but will be part of the id)
// - To display an empty legend, use "##_some_unique_id"
// - if your legend is displayed (i.e. it does not start with "##"),
// then the total size of the widget will be larger than the imageDisplaySize.
//
// :param mat:
// An image you want to display, under the form of an OpenCV matrix. All types of dense matrices are supported.
Expand All @@ -284,9 +286,6 @@ namespace ImmVision
// Size of the displayed image (can be different from the mat size)
// If you specify only the width or height (e.g (300, 0), then the other dimension
// will be calculated automatically, respecting the original image w/h ratio.
// Warning:
// if your legend is displayed (i.e. it does not start with "##"),
// then the total size of the widget will be larger than the imageDisplaySize.
//
// :param refreshImage:
// images textures are cached. Set to true if your image matrix/buffer has changed
Expand Down Expand Up @@ -319,6 +318,18 @@ namespace ImmVision
bool isBgrOrBgra = true
);

// ImageDisplayResizable: display the image, with no user interaction (by default)
// The image can be resized by the user (and the new size will be stored in the size parameter)
// The label will not be displayed (but it will be used as an id, and must be unique)
IMMVISION_API cv::Point2d ImageDisplayResizable(
const std::string& label_id,
const cv::Mat& mat,
ImVec2* size,
bool refreshImage = false,
bool showOptionsButton = false,
bool isBgrOrBgra = true
);


// Return the list of the available color maps
// Taken from https://github.com/yuki-koyama/tinycolormap, thanks to Yuki Koyama
Expand Down Expand Up @@ -10109,6 +10120,7 @@ namespace ImmVision
{
params.ShowOptionsButton = showOptionsButton;
params.ImageDisplaySize = imageDisplaySize;
// params.CanResize = true;
params.RefreshImage = refreshImage;
params.IsColorOrderBGR = isBgrOrBgra;
}
Expand All @@ -10117,6 +10129,38 @@ namespace ImmVision
return params.MouseInfo.MousePosition;
}

IMMVISION_API cv::Point2d ImageDisplayResizable(
const std::string& label_id,
const cv::Mat& mat,
ImVec2* size,
bool refreshImage,
bool showOptionsButton,
bool isBgrOrBgra
)
{
ImGuiID id = ImGui::GetID(label_id.c_str());
static std::map<ImGuiID, ImageParams> s_Params;
if (s_Params.find(id) == s_Params.end())
{
ImageParams params = showOptionsButton ? ImageParams() : FactorImageParamsDisplayOnly();
s_Params[id] = params;
}

ImageParams& params = s_Params.at(id);
{
params.ShowOptionsButton = showOptionsButton;
params.ImageDisplaySize = cv::Size((int)size->x, (int)size->y);
params.CanResize = true;
params.RefreshImage = refreshImage;
params.IsColorOrderBGR = isBgrOrBgra;
}
std::string hiddenLabel = std::string("##") + label_id;
Image(hiddenLabel, mat, &params);

*size = ImVec2((float)params.ImageDisplaySize.width, (float)params.ImageDisplaySize.height);
return params.MouseInfo.MousePosition;
}


ImageParams FactorImageParamsDisplayOnly()
{
Expand Down
19 changes: 15 additions & 4 deletions src_all_in_one/immvision/immvision.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ namespace ImmVision
IMMVISION_API void Image(const std::string& label, const cv::Mat& mat, ImageParams* params);


// Only, display the image, with no decoration, and no user interaction (by default)
// ImageDisplay: Only, display the image, with no user interaction (by default)
//
// Parameters:
// :param label_id
Expand All @@ -271,6 +271,8 @@ namespace ImmVision
// To circumvent this, you can modify your label like this:
// "MyLabel##some_unique_id" (the part after "##" will not be displayed but will be part of the id)
// - To display an empty legend, use "##_some_unique_id"
// - if your legend is displayed (i.e. it does not start with "##"),
// then the total size of the widget will be larger than the imageDisplaySize.
//
// :param mat:
// An image you want to display, under the form of an OpenCV matrix. All types of dense matrices are supported.
Expand All @@ -279,9 +281,6 @@ namespace ImmVision
// Size of the displayed image (can be different from the mat size)
// If you specify only the width or height (e.g (300, 0), then the other dimension
// will be calculated automatically, respecting the original image w/h ratio.
// Warning:
// if your legend is displayed (i.e. it does not start with "##"),
// then the total size of the widget will be larger than the imageDisplaySize.
//
// :param refreshImage:
// images textures are cached. Set to true if your image matrix/buffer has changed
Expand Down Expand Up @@ -314,6 +313,18 @@ namespace ImmVision
bool isBgrOrBgra = true
);

// ImageDisplayResizable: display the image, with no user interaction (by default)
// The image can be resized by the user (and the new size will be stored in the size parameter)
// The label will not be displayed (but it will be used as an id, and must be unique)
IMMVISION_API cv::Point2d ImageDisplayResizable(
const std::string& label_id,
const cv::Mat& mat,
ImVec2* size,
bool refreshImage = false,
bool showOptionsButton = false,
bool isBgrOrBgra = true
);


// Return the list of the available color maps
// Taken from https://github.com/yuki-koyama/tinycolormap, thanks to Yuki Koyama
Expand Down

0 comments on commit fa219b0

Please sign in to comment.