Skip to content

Commit

Permalink
widget: updated image widget
Browse files Browse the repository at this point in the history
  • Loading branch information
rxdu committed Oct 25, 2024
1 parent bf40895 commit d3fd227
Show file tree
Hide file tree
Showing 17 changed files with 425 additions and 220 deletions.
9 changes: 5 additions & 4 deletions src/imview/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ add_library(imview
src/box.cpp
src/layer.cpp
# src/popup.cpp
# src/cairo_context.cpp
# src/data_buffer.cpp
# src/cairo_widget.cpp
# src/cairo_draw.cpp
# utils
src/utils/image_utils.cpp
# widgets
# src/widget/image_widget.cpp
src/widget/cv_image_widget.cpp
src/widget/buffered_cv_image_widget.cpp
src/widget/cairo/cairo_context.cpp
src/widget/cairo/cairo_widget.cpp
src/widget/cairo/cairo_draw.cpp
# data buffer
src/buffer/buffer_registry.cpp
# event handling
Expand Down
1 change: 1 addition & 0 deletions src/imview/include/imview/buffer/buffer_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <mutex>
#include <memory>
#include <unordered_map>
#include <stdexcept>

#include "imview/buffer/buffer_interface.hpp"

Expand Down
39 changes: 39 additions & 0 deletions src/imview/include/imview/widget/buffered_cv_image_widget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* @file image_widget.hpp
* @date 10/10/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#ifndef QUICKVIZ_CV_IMAGE_WIDGET_HPP
#define QUICKVIZ_IMAGE_WIDGET_HPP

#include <functional>

#include "imview/panel.hpp"
#include "imview/buffer/buffer_registry.hpp"

#include "glad/glad.h"

#include <opencv2/opencv.hpp>

namespace quickviz {
class BufferedCvImageWidget : public Panel {
public:
BufferedCvImageWidget(const std::string& widget_name,
const std::string& buffer_name);
~BufferedCvImageWidget();

// public methods
void SetKeepAspectRatio(bool keep);
void Draw() override;

private:
GLuint image_texture_;
std::shared_ptr<BufferInterface<cv::Mat>> buffer_;
bool keep_aspect_ratio_ = false;
};
} // namespace quickviz

#endif // QUICKVIZ_CV_IMAGE_WIDGET_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
#ifndef CAIRO_CONTEXT_HPP
#define CAIRO_CONTEXT_HPP

#include <cstdint>
#include <stack>

#include <cairo.h>
#include <GL/gl.h>

#include <cstdint>
#include <stack>

namespace quickviz {
namespace swviz {
class CairoContext {
struct Scaler {
double x;
Expand Down Expand Up @@ -74,7 +73,6 @@ class CairoContext {
void CreateSurface();
void GenGlTexture();
};
} // namespace swviz
} // namespace xmotion
} // namespace quickviz

#endif /* CAIRO_CONTEXT_HPP */
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "imgui.h"

namespace quickviz {
namespace swviz {
enum ColorName {
BLACK = 0,
WHITE,
Expand Down Expand Up @@ -65,7 +64,6 @@ void DrawRing(cairo_t *cr, ImVec2 center, float inner_radius,

void DrawRectangle(cairo_t *cr, ImVec2 pos1, ImVec2 pos2, double thickness = 2,
ImVec4 color = colors[BLACK], bool fill = false);
} // namespace swviz
} // namespace xmotion

#endif /* CAIRO_DRAW_HPP */
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,30 @@

#include "imgui.h"

#include "imview/details/cairo_context.hpp"
#include "imview/panel.hpp"
#include "imview/widget/cairo/cairo_context.hpp"

namespace quickviz {
namespace swviz {
class CairoWidget {
class CairoWidget : public Panel {
public:
CairoWidget(uint32_t width, uint32_t height,
CairoWidget(const std::string& widget_name, uint32_t width, uint32_t height,
bool normalize_coordinate = false);
~CairoWidget();
~CairoWidget() override;

// load image texture (before entering rendering loop)
void LoadImage(std::string png_file);
void Draw() override;
void OnResize(float width, float height) override;

float GetAspectRatio() const;

// resize/fill cairo surface
void Resize(uint32_t width, uint32_t height);
void Fill(ImVec4 color = {1, 1, 1, 0.6});
void Clear();

float GetAspectRatio() const;

// draw vector graphics with user function
using CairoDrawFunc = std::function<void(cairo_t*)>;
void Draw(CairoDrawFunc DrawFunc);

// draw from png image (avoid if possible, slow)
enum class ScaleMode { MANUAL, AUTO_STRETCH, AUTO_KEEP_ASPECT_RATIO };
void Draw(std::string png_file, double pos_x, double pos_y,
double angle = 0.0,
ScaleMode scale_mode = ScaleMode::AUTO_KEEP_ASPECT_RATIO,
double scale_x = 1.0, double scale_y = 1.0);

// draw text to cairo surface
void DrawText(std::string text, double pos_x, double pos_y,
double angle = 0.0, ImVec4 color = {0, 0, 0, 1},
Expand All @@ -64,11 +57,7 @@ class CairoWidget {

private:
std::unique_ptr<CairoContext> ctx_;
std::unordered_map<std::string, cairo_surface_t*> images_;

cairo_surface_t* GetImageSurface(std::string png_file);
};
} // namespace swviz
} // namespace xmotion
} // namespace quickviz

#endif /* CAIRO_WIDGET_HPP */
40 changes: 40 additions & 0 deletions src/imview/include/imview/widget/cv_image_widget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* @file image_widget.hpp
* @date 10/10/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#ifndef QUICKVIZ_CV_IMAGE_WIDGET_HPP
#define QUICKVIZ_CV_IMAGE_WIDGET_HPP

#include "glad/glad.h"

#include <mutex>
#include <functional>

#include <opencv2/opencv.hpp>

#include "imview/panel.hpp"

namespace quickviz {
class CvImageWidget : public Panel {
public:
CvImageWidget(const std::string& widget_name);
~CvImageWidget();

// public methods
void SetKeepAspectRatio(bool keep);
void UpdateImage(const cv::Mat& image);
void Draw() override;

private:
std::mutex image_mutex_;
cv::Mat image_mat_;
GLuint image_texture_;
bool keep_aspect_ratio_ = false;
};
} // namespace quickviz

#endif // QUICKVIZ_CV_IMAGE_WIDGET_HPP
98 changes: 0 additions & 98 deletions src/imview/include/imview/widget/image_widget.hpp

This file was deleted.

83 changes: 83 additions & 0 deletions src/imview/src/widget/buffered_cv_image_widget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* @file buffered_cv_image_widget.cpp
* @date 10/25/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#include "imview/widget/buffered_cv_image_widget.hpp"

#include "imview/utils/image_utils.hpp"

namespace quickviz {
BufferedCvImageWidget::BufferedCvImageWidget(const std::string& widget_name,
const std::string& buffer_name)
: Panel(widget_name) {
this->SetAutoLayout(false);
// this->SetNoResize(true);
// this->SetNoMove(true);
this->SetWindowNoMenuButton();
this->SetNoBackground(true);

auto& buffer_registry = BufferRegistry::GetInstance();
buffer_ = buffer_registry.GetBuffer<cv::Mat>(buffer_name);

glGenTextures(1, &image_texture_);
}

BufferedCvImageWidget::~BufferedCvImageWidget() {
glDeleteTextures(1, &image_texture_);
}

void BufferedCvImageWidget::SetKeepAspectRatio(bool keep) {
keep_aspect_ratio_ = keep;
}

void BufferedCvImageWidget::Draw() {
Begin();
{
ImVec2 contentSize = ImGui::GetContentRegionAvail();
float width = contentSize.x;
float height = contentSize.y;

cv::Mat mat;
buffer_->Read(mat);

if (!mat.empty()) {
if (keep_aspect_ratio_) {
cv::Mat proc = mat;
float aspect_ratio = mat.cols / (float)mat.rows;
float img_width = width;
float img_height = height;
if (width / height > aspect_ratio) {
img_width = height * aspect_ratio;
} else {
img_height = width / aspect_ratio;
}
if (img_width > width) {
// need to scale down
img_width = width;
img_height = width / aspect_ratio;
} else {
// need to scale down
img_height = height;
img_width = height * aspect_ratio;
}
cv::resize(mat, proc, cv::Size(img_width, img_height), 0, 0,
cv::INTER_CUBIC);
// copy display to center of image
cv::Mat display = cv::Mat::zeros(height, width, mat.type());
cv::Rect roi((width - proc.cols) / 2, (height - proc.rows) / 2,
proc.cols, proc.rows);
proc.copyTo(display(roi));
CopyTextureFromCvMat(display, image_texture_);
} else {
CopyTextureFromCvMat(mat, image_texture_);
}
}
ImGui::Image((void*)(intptr_t)image_texture_, ImVec2(width, height));
}
End();
}
} // namespace quickviz
Loading

0 comments on commit d3fd227

Please sign in to comment.