Skip to content

Commit

Permalink
Merge pull request opencv#25561 from Kumataro:fix25560
Browse files Browse the repository at this point in the history
highgui: wayland: expand image width if title bar cannot be shown

Close opencv#25560

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
  • Loading branch information
Kumataro authored and klatism committed May 17, 2024
1 parent 1746e55 commit b6a4c12
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions modules/highgui/src/window_wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,9 @@ class cv_wl_viewer : public cv_wl_widget {
cv::Rect last_img_area_;
bool image_changed_ = false;

int real_img_width = 0;
cv::Scalar const outarea_color_ = CV_RGB(0, 0, 0);

void *param_ = nullptr;
CvMouseCallback callback_ = nullptr;
};
Expand Down Expand Up @@ -1582,6 +1585,28 @@ cv_wl_viewer::cv_wl_viewer(cv_wl_window *window, int flags)
void cv_wl_viewer::set_image(cv::Mat const &image) {
image_ = image.clone();
image_changed_ = true;

// See https://github.com/opencv/opencv/issues/25560
// If image_ width is too small enough to show title and buttons, expand it.

// Keep real image width to limit x position for callback functions
real_img_width = image_.size().width;

// Minimum width of title is not defined, so use button width * 3 instead of it.
const int view_min_width = cv_wl_titlebar::btn_width * 3 + cv_wl_titlebar::titlebar_min_width;

const int margin = view_min_width - real_img_width;
if(margin > 0)
{
copyMakeBorder(image_, // src
image_, // dst
0, // top
0, // bottom
0, // left
margin, // right
cv::BORDER_CONSTANT, // borderType
outarea_color_ ); // value(color)
}
}

void cv_wl_viewer::set_mouse_callback(CvMouseCallback callback, void *param) {
Expand Down Expand Up @@ -1634,6 +1659,8 @@ void cv_wl_viewer::on_mouse(int event, cv::Point const &p, int flag) {
int x = static_cast<int>((p.x - last_img_area_.x) * ((double) image_.size().width / last_img_area_.width));
int y = static_cast<int>((p.y - last_img_area_.y) *
((double) image_.size().height / last_img_area_.height));

x = cv::min(x, real_img_width);
callback_(event, x, y, flag, param_);
}
}
Expand Down
15 changes: 15 additions & 0 deletions modules/highgui/test/test_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@ TEST(Highgui_GUI, trackbar)
EXPECT_NO_THROW(destroyAllWindows());
}

// See https://github.com/opencv/opencv/issues/25560
#if !defined(ENABLE_PLUGINS)
TEST(Highgui_GUI, DISABLED_small_width_image)
#else
TEST(Highgui_GUI, small_width_image)
#endif
{
const std::string window_name("trackbar_test_window");
cv::Mat src(1,1,CV_8UC3,cv::Scalar(0));
EXPECT_NO_THROW(destroyAllWindows());
ASSERT_NO_THROW(namedWindow(window_name));
ASSERT_NO_THROW(imshow(window_name, src));
EXPECT_NO_THROW(waitKey(10));
EXPECT_NO_THROW(destroyAllWindows());
}

TEST(Highgui_GUI, currentUIFramework)
{
Expand Down

0 comments on commit b6a4c12

Please sign in to comment.