resizable images

This commit is contained in:
Kueffner, Maximilian
2025-03-17 17:33:31 +01:00
parent 8d8480bac1
commit ee39b254b4
5 changed files with 101 additions and 27 deletions
+1 -1
View File
@@ -93,7 +93,7 @@ BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
ColumnLimit: 80
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
+40 -4
View File
@@ -1,6 +1,12 @@
#include "CvMatRender.hpp"
#include <gl/GL.h>
#include <cstdint>
#include <memory>
#include <opencv2/core/mat.hpp>
#include <tuple>
#include <utility>
#include "imaging/Image.hpp"
using namespace pixelarium::imaging;
@@ -12,9 +18,15 @@ pixelarium::render::CvMatRender::CvMatRender(const std::shared_ptr<Image>& img)
// cv::cvtColor(this->_img, this->_img, cv::COLOR_BGR2RGBA);
}
/*static*/ void pixelarium::render::matToTexture(const cv::Mat& image, GLuint* texture)
/*static*/ void pixelarium::render::matToTexture(const cv::Mat& image,
GLuint* texture)
{
glGenTextures(1, texture);
// only generate the texture when it's not already present
if (*texture == 0)
{
glGenTextures(1, texture);
}
glBindTexture(GL_TEXTURE_2D, *texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@@ -47,7 +59,31 @@ GLuint* pixelarium::render::CvMatRender::Render()
{
// storing a copy of the to-be-rendered image with a "well-behaved"
cv::cvtColor(this->_img, this->_img, cv::COLOR_BGR2RGBA);
if (this->_texture == 0)
matToTexture(this->_img, &this->_texture);
matToTexture(this->_img, &this->_texture);
return &this->_texture;
}
GLuint* pixelarium::render::CvMatRender::Render(float factor)
{
cv::resize(this->_base->GetImage(), this->_img, cv::Size(0,0), factor, factor, cv::INTER_LINEAR_EXACT);
return this->Render();
}
GLuint* pixelarium::render::CvMatRender::Render(size_t width, size_t height)
{
// this is nasty as it knows about what Render is doing
const auto sz{this->_base->GetImage().size()};
const auto get_factor = [](auto opt1, auto opt2) -> float
{
return opt1 < opt2 ? opt1 : opt2;
};
auto factor = get_factor(width / static_cast<float>(sz.width), height / static_cast<float>(sz.height));
// cv::resize(this->_base->GetImage(), this->_img, cv::Size(0,0), factor, factor, cv::INTER_LINEAR_EXACT);
return this->Render(factor);
}
+2
View File
@@ -25,6 +25,8 @@ class CvMatRender
CvMatRender() = default;
explicit CvMatRender(const std::shared_ptr<pixelarium::imaging::Image>& img);
GLuint* Render();
GLuint* Render(float factor);
GLuint* Render(size_t width, size_t height);
private:
cv::Mat _img;
+50 -17
View File
@@ -1,16 +1,37 @@
#include "AppGLFW.hpp"
#include <cstdint>
#include <memory>
#include "imgui.h"
#include "imaging/Image.hpp"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "portable-file-dialogs.h"
#include "rendering/CvMatRender.hpp"
#include "uiresources.h"
using namespace pixelarium::imaging;
/*static*/ bool pixelarium::ui::dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect)
{
if (std::abs(ref_rect.y - new_rect.y) > 5 || std::abs(ref_rect.x - new_rect.x))
{
return true;
}
return false;
}
/*static*/ ImVec2 pixelarium::ui::ascpet_const_dimensions(const pixelarium::imaging::Image& img, const ImVec2& curr_dim)
{
const auto w_fact = (static_cast<float>(curr_dim.x) / img.GetImage().cols);
const auto h_fact = (static_cast<float>(curr_dim.y) / img.GetImage().rows);
const auto fact = w_fact < h_fact ? w_fact : h_fact;
return ImVec2(img.GetImage().cols * fact, img.GetImage().rows * fact);
}
pixelarium::ui::AppGLFW::AppGLFW()
{
glfwSetErrorCallback(glfw_error_callback);
@@ -32,7 +53,7 @@ pixelarium::ui::AppGLFW::AppGLFW()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
#ifdef __linux__
@@ -72,14 +93,11 @@ pixelarium::ui::AppGLFW::AppGLFW()
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
io.ConfigFlags |=
ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |=
ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |=
ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform
// Windows
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform
// Windows
// io.ConfigViewportsNoAutoMerge = true;
// io.ConfigViewportsNoTaskBarIcon = true;
@@ -118,8 +136,25 @@ int pixelarium::ui::AppGLFW::Run()
if (this->_imagep)
{
// auto render = render::CvMatRender(this->_img);
ImGui::Begin("An image", &this->_imagep, NULL);
ImGui::Image(*this->_render.Render(), ImVec2(this->_img->GetImage().cols, this->_img->GetImage().rows));
ImGui::Begin("An image", &this->_imagep, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar);
this->_curr_dim = ImGui::GetContentRegionAvail();
auto new_dim = ImGui::GetContentRegionAvail();
auto texture = dim_changed_p(this->_curr_dim, new_dim)
? this->_render.Render(static_cast<size_t>(this->_curr_dim.x),
static_cast<size_t>(this->_curr_dim.y))
: this->_render.Render();
this->_curr_dim = new_dim;
// random aspect ratio
// ImGui::Image(reinterpret_cast<Textured>(
// reinterpret_cast<void*>(*texture)),
// this->_curr_dim);
ImVec2 dim(this->_img->GetImage().cols, this->_img->GetImage().rows);
// aspect ratio constant render
ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(*texture)),
ascpet_const_dimensions(*this->_img, new_dim));
ImGui::End();
}
@@ -179,9 +214,7 @@ void pixelarium::ui::AppGLFW::MenuBar()
void pixelarium::ui::AppGLFW::LoadImageProt()
{
auto res{pfd::open_file("Load Inputs", pfd::path::home(),
{"All Files", "*"}, pfd::opt::multiselect)
.result()};
auto res{pfd::open_file("Load Inputs", pfd::path::home(), {"All Files", "*"}, pfd::opt::multiselect).result()};
for (auto& p : res)
{
// lg::Logger::Debug("Adding image from " + std::string(p),
+8 -5
View File
@@ -6,12 +6,14 @@
#include "Image.hpp"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "rendering/CvMatRender.hpp"
namespace pixelarium::ui
{
static bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect);
static ImVec2 ascpet_const_dimensions(const pixelarium::imaging::Image& img, const ImVec2& curr_dim);
enum LogLevelSelection
{
Debug = 0,
@@ -31,16 +33,17 @@ class AppGLFW
void LoadImageProt();
private:
LogLevelSelection log_level_ = static_cast<LogLevelSelection>(0);
// LogLevelSelection log_level_ = static_cast<LogLevelSelection>(0);
GLFWwindow* window = nullptr;
ImGuiWindowFlags window_flags = 0;
std::shared_ptr<pixelarium::imaging::Image> _img;
pixelarium::render::CvMatRender _render;
bool _imagep { false };
bool _imagep{false};
ImVec2 _curr_dim;
};
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
}
} // namespace ui
} // namespace pixelarium::ui