Files

124 lines
4.0 KiB
C++
Raw Permalink Normal View History

2025-03-14 19:32:40 +01:00
#include "CvMatRender.hpp"
2025-03-17 17:33:31 +01:00
#include <opencv2/core/mat.hpp>
2025-05-23 15:15:01 +02:00
#include <opencv2/imgproc.hpp>
2025-06-13 22:23:20 +00:00
#include "IPixelariumImage.hpp"
2025-03-14 19:32:40 +01:00
using namespace pixelarium::imaging;
2025-06-13 22:23:20 +00:00
/// @brief Constructor for the CvMatRender class.
/// @param img A shared pointer to the PixelariumImage to be rendered.
2026-01-23 23:00:35 +00:00
pixelarium::application::CvMatRender::CvMatRender(const cv::Mat& img) : base_(img), texture_(0)
2025-06-13 22:23:20 +00:00
{
// storing a copy of the to-be-rendered image
// because it will be resized and filtered eventually which we absolutely
// must not do on the original image
this->ResetRenderImage();
cv::cvtColor(this->img_, this->img_, cv::COLOR_BGR2RGBA);
}
/// @brief Destructor for the CvMatRender class.
/// Deallocates the OpenGL texture if it exists.
2026-01-23 23:00:35 +00:00
pixelarium::application::CvMatRender::~CvMatRender()
2025-03-14 19:32:40 +01:00
{
2025-06-13 22:23:20 +00:00
if (texture_)
{
glDeleteTextures(1, &texture_);
texture_ = 0;
}
2025-03-14 19:32:40 +01:00
}
2025-06-13 22:23:20 +00:00
/// @brief Uploads the current image data to an OpenGL texture.
/// @return The ID of the uploaded OpenGL texture.
/// @throws std::runtime_error if the image data is empty or if there is an OpenGL error.
2026-01-23 23:00:35 +00:00
GLuint pixelarium::application::CvMatRender::uploadTexture()
2025-06-13 22:23:20 +00:00
{
if (img_.empty())
{
throw std::runtime_error("Image data is empty.");
}
if (!this->texture_)
2025-03-17 17:33:31 +01:00
{
2025-06-13 22:23:20 +00:00
glGenTextures(1, &this->texture_);
if (this->texture_ == 0)
{
throw std::runtime_error("Failed to generate OpenGL texture.");
}
2025-03-17 17:33:31 +01:00
}
2025-06-13 22:23:20 +00:00
glBindTexture(GL_TEXTURE_2D, this->texture_);
2025-03-14 19:32:40 +01:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2025-06-13 22:23:20 +00:00
const int width = img_.cols;
const int height = img_.rows;
2025-10-07 12:18:00 +02:00
// see
// https://stackoverflow.com/questions/10167534/how-to-find-out-what-type-of-a-mat-object-is-with-mattype-in-opencv
// for pixel a pixel type table
switch (img_.type())
{
case CV_8U:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_BYTE, img_.data);
break;
2025-09-23 21:57:08 +02:00
case CV_16U:
case CV_16UC3:
2025-10-07 12:18:00 +02:00
case CV_16UC4:
2025-09-23 21:57:08 +02:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT, img_.data);
break;
2025-10-07 12:18:00 +02:00
case CV_32F:
case CV_32FC4:
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
img_.data);
2025-09-23 21:57:08 +02:00
break;
default:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_.cols, img_.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_.data);
break;
2025-03-14 19:32:40 +01:00
}
2025-06-13 22:23:20 +00:00
GLenum error = glGetError();
if (error != GL_NO_ERROR)
{
throw std::runtime_error("OpenGL error during texture upload: " + std::to_string(error));
}
return this->texture_;
2025-03-14 19:32:40 +01:00
}
2025-03-17 17:33:31 +01:00
2025-06-13 22:23:20 +00:00
/// @brief Renders the image by uploading it as a texture.
/// @return The ID of the OpenGL texture.
2026-01-23 23:00:35 +00:00
GLuint pixelarium::application::CvMatRender::Render() { return this->uploadTexture(); }
2025-06-13 22:23:20 +00:00
/// @brief Renders the image with a specified scaling factor.
/// @param factor The scaling factor for resizing the image.
/// @return The ID of the OpenGL texture.
2026-01-23 23:00:35 +00:00
GLuint pixelarium::application::CvMatRender::Render(float factor)
2025-03-17 17:33:31 +01:00
{
2025-09-26 21:09:51 +02:00
cv::resize(this->base_, this->img_, cv::Size(0, 0), factor, factor, cv::INTER_LINEAR_EXACT);
2025-03-17 17:33:31 +01:00
2025-06-13 22:23:20 +00:00
return this->uploadTexture();
2025-03-17 17:33:31 +01:00
}
2025-06-13 22:23:20 +00:00
/// @brief Renders the image to fit within the specified width and height.
/// @param width The maximum width of the rendered image.
/// @param height The maximum height of the rendered image.
/// @return The ID of the OpenGL texture.
2026-01-23 23:00:35 +00:00
GLuint pixelarium::application::CvMatRender::Render(size_t width, size_t height)
2025-03-17 17:33:31 +01:00
{
2025-09-26 21:09:51 +02:00
const auto sz{this->base_.size()};
2025-03-17 17:33:31 +01:00
2025-06-13 22:23:20 +00:00
const auto get_factor = [](auto opt1, auto opt2) -> float { return opt1 < opt2 ? opt1 : opt2; };
2025-03-17 17:33:31 +01:00
auto factor = get_factor(width / static_cast<float>(sz.width), height / static_cast<float>(sz.height));
return this->Render(factor);
}
2025-09-22 23:13:28 +02:00
2026-01-23 23:00:35 +00:00
void pixelarium::application::CvMatRender::ResetRenderImage()
2025-09-22 23:13:28 +02:00
{
// we copy here
2025-09-26 21:09:51 +02:00
this->img_ = this->base_.clone();
2025-09-22 23:13:28 +02:00
}