From fa219b0c3042067ab8e34976b82b47d75ab97522 Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Sat, 11 May 2024 08:27:40 +0200 Subject: [PATCH] Add ImageDisplayResizable --- src/immvision/image.h | 19 ++++++++-- src/immvision/internal/image.cpp | 36 ++++++++++++++++++ src_all_in_one/immvision/immvision.cpp | 52 ++++++++++++++++++++++++-- src_all_in_one/immvision/immvision.h | 19 ++++++++-- 4 files changed, 114 insertions(+), 12 deletions(-) diff --git a/src/immvision/image.h b/src/immvision/image.h index 8dfad11..b3091d6 100644 --- a/src/immvision/image.h +++ b/src/immvision/image.h @@ -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 @@ -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. @@ -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 @@ -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 diff --git a/src/immvision/internal/image.cpp b/src/immvision/internal/image.cpp index 333c93c..feeaa9d 100644 --- a/src/immvision/internal/image.cpp +++ b/src/immvision/internal/image.cpp @@ -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, ¶ms); 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 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, ¶ms); + + + + *size = ImVec2((float)params.ImageDisplaySize.width, (float)params.ImageDisplaySize.height); + return params.MouseInfo.MousePosition; + } + ImageParams FactorImageParamsDisplayOnly() { diff --git a/src_all_in_one/immvision/immvision.cpp b/src_all_in_one/immvision/immvision.cpp index 3810a78..12058a8 100644 --- a/src_all_in_one/immvision/immvision.cpp +++ b/src_all_in_one/immvision/immvision.cpp @@ -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 @@ -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. @@ -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 @@ -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 @@ -10109,6 +10120,7 @@ namespace ImmVision { params.ShowOptionsButton = showOptionsButton; params.ImageDisplaySize = imageDisplaySize; +// params.CanResize = true; params.RefreshImage = refreshImage; params.IsColorOrderBGR = isBgrOrBgra; } @@ -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 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, ¶ms); + + *size = ImVec2((float)params.ImageDisplaySize.width, (float)params.ImageDisplaySize.height); + return params.MouseInfo.MousePosition; + } + ImageParams FactorImageParamsDisplayOnly() { diff --git a/src_all_in_one/immvision/immvision.h b/src_all_in_one/immvision/immvision.h index 1bbeb63..7d7663e 100644 --- a/src_all_in_one/immvision/immvision.h +++ b/src_all_in_one/immvision/immvision.h @@ -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 @@ -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. @@ -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 @@ -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