fix mem runaway

This commit is contained in:
Kueffner, Maximilian
2025-03-17 10:44:56 +01:00
parent 0b541348b0
commit 8d8480bac1
4 changed files with 32 additions and 17 deletions
+11 -7
View File
@@ -1,14 +1,15 @@
#include "CvMatRender.hpp" #include "CvMatRender.hpp"
#include <cstdint> #include <cstdint>
#include <memory>
using namespace pixelarium::imaging; using namespace pixelarium::imaging;
pixelarium::render::CvMatRender::CvMatRender(const Image& img) pixelarium::render::CvMatRender::CvMatRender(const std::shared_ptr<Image>& img)
: _base(img), _texture(0)
{ {
this->_img = img.GetImage(); this->_img = this->_base->GetImage().clone();
this->_texture = 0; // // storing a copy of the to-be-rendered image with a "well-behaved"
// cv::cvtColor(this->_img, this->_img, cv::COLOR_BGR2RGBA);
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)
@@ -42,8 +43,11 @@ pixelarium::render::CvMatRender::CvMatRender(const Image& img)
} }
} }
GLuint pixelarium::render::CvMatRender::Render() 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; return &this->_texture;
} }
+10 -5
View File
@@ -1,4 +1,8 @@
#pragma once #pragma once
// windows. must come before GL/GL.h here.
// clang format would change this, effectively rendering the build broken.
// clang-format off
#include <memory>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include <GL/GL.h> #include <GL/GL.h>
@@ -10,20 +14,21 @@
#include <GLFW/glfw3.h> // Will drag system OpenGL headers #include <GLFW/glfw3.h> // Will drag system OpenGL headers
#endif #endif
#include "imaging/Image.hpp" #include "imaging/Image.hpp"
// clang-format on
namespace pixelarium::render namespace pixelarium::render
{ {
static void matToTexture(const cv::Mat& image, static void matToTexture(const cv::Mat& image, GLuint* texture);
GLuint* texture);
class CvMatRender class CvMatRender
{ {
public: public:
explicit CvMatRender(const pixelarium::imaging::Image& img); CvMatRender() = default;
// void* Render(); explicit CvMatRender(const std::shared_ptr<pixelarium::imaging::Image>& img);
GLuint Render(); GLuint* Render();
private: private:
cv::Mat _img; cv::Mat _img;
std::shared_ptr<pixelarium::imaging::Image> _base;
GLuint _texture; GLuint _texture;
}; };
+7 -3
View File
@@ -1,4 +1,6 @@
#include "AppGLFW.hpp" #include "AppGLFW.hpp"
#include <cstdint>
#include <memory>
#include "imgui.h" #include "imgui.h"
@@ -115,9 +117,9 @@ int pixelarium::ui::AppGLFW::Run()
if (this->_imagep) if (this->_imagep)
{ {
auto render = render::CvMatRender(this->_img); // auto render = render::CvMatRender(this->_img);
ImGui::Begin("An image", &this->_imagep, NULL); ImGui::Begin("An image", &this->_imagep, NULL);
ImGui::Image(render.Render(), ImVec2(this->_img.GetImage().cols, this->_img.GetImage().rows)); ImGui::Image(*this->_render.Render(), ImVec2(this->_img->GetImage().cols, this->_img->GetImage().rows));
ImGui::End(); ImGui::End();
} }
@@ -185,7 +187,9 @@ void pixelarium::ui::AppGLFW::LoadImageProt()
// lg::Logger::Debug("Adding image from " + std::string(p), // lg::Logger::Debug("Adding image from " + std::string(p),
// __FUNCTION__); // __FUNCTION__);
this->_img = Image(p); // this->_img = Image(p);
this->_img = std::make_shared<Image>(p);
this->_render = pixelarium::render::CvMatRender(this->_img);
this->_imagep = true; this->_imagep = true;
} }
} }
+3 -1
View File
@@ -8,6 +8,7 @@
#include "imgui.h" #include "imgui.h"
#include "imgui_impl_glfw.h" #include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h" #include "imgui_impl_opengl3.h"
#include "rendering/CvMatRender.hpp"
namespace pixelarium::ui namespace pixelarium::ui
{ {
@@ -33,7 +34,8 @@ class AppGLFW
LogLevelSelection log_level_ = static_cast<LogLevelSelection>(0); LogLevelSelection log_level_ = static_cast<LogLevelSelection>(0);
GLFWwindow* window = nullptr; GLFWwindow* window = nullptr;
ImGuiWindowFlags window_flags = 0; ImGuiWindowFlags window_flags = 0;
pixelarium::imaging::Image _img; std::shared_ptr<pixelarium::imaging::Image> _img;
pixelarium::render::CvMatRender _render;
bool _imagep { false }; bool _imagep { false };
}; };