diff --git a/.clang-format b/.clang-format index 65e9a04..d98801b 100644 --- a/.clang-format +++ b/.clang-format @@ -93,7 +93,7 @@ BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon BreakStringLiterals: true -ColumnLimit: 80 +ColumnLimit: 120 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerIndentWidth: 4 diff --git a/lib/rendering/CvMatRender.cpp b/lib/rendering/CvMatRender.cpp index 9482ed9..a632e5f 100644 --- a/lib/rendering/CvMatRender.cpp +++ b/lib/rendering/CvMatRender.cpp @@ -1,6 +1,12 @@ #include "CvMatRender.hpp" +#include + #include #include +#include +#include +#include +#include "imaging/Image.hpp" using namespace pixelarium::imaging; @@ -12,9 +18,15 @@ pixelarium::render::CvMatRender::CvMatRender(const std::shared_ptr& 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(sz.width), height / static_cast(sz.height)); + // cv::resize(this->_base->GetImage(), this->_img, cv::Size(0,0), factor, factor, cv::INTER_LINEAR_EXACT); + + return this->Render(factor); +} + diff --git a/lib/rendering/CvMatRender.hpp b/lib/rendering/CvMatRender.hpp index 2bdd6c3..6a94bcb 100644 --- a/lib/rendering/CvMatRender.hpp +++ b/lib/rendering/CvMatRender.hpp @@ -25,6 +25,8 @@ class CvMatRender CvMatRender() = default; explicit CvMatRender(const std::shared_ptr& img); GLuint* Render(); + GLuint* Render(float factor); + GLuint* Render(size_t width, size_t height); private: cv::Mat _img; diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index d769fe7..3710a7d 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -1,16 +1,37 @@ #include "AppGLFW.hpp" -#include + #include -#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(curr_dim.x) / img.GetImage().cols); + const auto h_fact = (static_cast(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(this->_curr_dim.x), + static_cast(this->_curr_dim.y)) + : this->_render.Render(); + + this->_curr_dim = new_dim; + + // random aspect ratio + // ImGui::Image(reinterpret_cast( + // reinterpret_cast(*texture)), + // this->_curr_dim); + ImVec2 dim(this->_img->GetImage().cols, this->_img->GetImage().rows); + // aspect ratio constant render + ImGui::Image(reinterpret_cast(reinterpret_cast(*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), diff --git a/src/AppGLFW.hpp b/src/AppGLFW.hpp index cc6ab24..f648929 100644 --- a/src/AppGLFW.hpp +++ b/src/AppGLFW.hpp @@ -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(0); + // LogLevelSelection log_level_ = static_cast(0); GLFWwindow* window = nullptr; ImGuiWindowFlags window_flags = 0; std::shared_ptr _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 \ No newline at end of file +} // namespace pixelarium::ui \ No newline at end of file