Skip to content

Commit

Permalink
Publish GlTexture in the API
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Sep 17, 2024
1 parent 6cabb2b commit 3b851b0
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 133 deletions.
49 changes: 49 additions & 0 deletions src/immvision/gl_texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once
#include "imgui.h"
#include <opencv2/core.hpp>


namespace ImmVision
{
// GlTexture contains an OpenGL texture which can be created or updated from a cv::Mat (C++), or numpy array (Python)
struct GlTexture
{
//
// Constructors
//

// Create an empty texture
GlTexture();
// Create a texture from an image (cv::Mat in C++, numpy array in Python)
GlTexture(const cv::Mat& image, bool isColorOrderBGR);
// The destructor will delete the texture from the GPU
~GlTexture();

// GlTextureCv is non copiable (since it holds a reference to a texture stored on the GPU),
// but it is movable.
GlTexture(const GlTexture& ) = delete;
GlTexture& operator=(const GlTexture& ) = delete;
GlTexture(GlTexture&& other) noexcept = default;
GlTexture& operator=(GlTexture&& other) noexcept = default;


//
// Methods
//

// Update the texture from a new image (cv::Mat in C++, numpy array in Python).
void UpdateFromImage(const cv::Mat& image, bool isColorOrderBGR);
// Returns the size as ImVec2
ImVec2 SizeImVec2() const;


//
// Members
//

// OpenGL texture ID on the GPU
ImTextureID TextureId;
// Image size in pixels
cv::Size Size;
};
} // namespace ImmVision
1 change: 1 addition & 0 deletions src/immvision/immvision.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#include "immvision/image.h"
#include "immvision/inspector.h"
#include "immvision/gl_texture.h"
16 changes: 8 additions & 8 deletions src/immvision/internal/cv/colormap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "immvision/internal/misc/tinycolormap.hpp"
#include "immvision/internal/misc/magic_enum.hpp"
#include "immvision/internal/misc/math_utils.h"
#include "immvision/internal/gl/gl_texture.h"
#include "immvision/gl_texture.h"
#include "immvision/imgui_imm.h"
#include "imgui.h"
#include "imgui_internal.h"
Expand All @@ -23,18 +23,18 @@ namespace ImmVision
{
ImVec2 size_(size);
if (size.x == 0.f)
size_ = texture.mImageSize;
ImGui::Image(texture.mImTextureId, size_);
size_ = texture.SizeImVec2();
ImGui::Image(texture.TextureId, size_);
}

bool GlTexture_DrawButton(const GlTexture& texture, const ImVec2& size)
{
ImVec2 size_(size);
if (size.x == 0.f)
size_ = texture.mImageSize;
size_ = texture.SizeImVec2();
char id[64];
snprintf(id, 64, "##%p", &texture);
return ImGui::ImageButton(id, texture.mImTextureId, size_);
return ImGui::ImageButton(id, texture.TextureId, size_);
}
}

Expand Down Expand Up @@ -143,7 +143,7 @@ namespace ImmVision
}


static insertion_order_map<std::string, std::unique_ptr<GlTextureCv>> sColormapsTexturesCache;
static insertion_order_map<std::string, std::unique_ptr<GlTexture>> sColormapsTexturesCache;


void FillTextureCache()
Expand All @@ -154,7 +154,7 @@ namespace ImmVision
for (const auto& k: images.insertion_order_keys())
{
cv::Mat& m = images.get(k);
auto texture = std::make_unique<GlTextureCv>(m, true);
auto texture = std::make_unique<GlTexture>(m, true);
sColormapsTexturesCache.insert(k, std::move(texture));
}
}
Expand All @@ -169,7 +169,7 @@ namespace ImmVision
if (cache.empty())
{
for (const auto& k: sColormapsTexturesCache.insertion_order_keys())
cache.insert(k, sColormapsTexturesCache.get(k)->mImTextureId);
cache.insert(k, sColormapsTexturesCache.get(k)->TextureId);
}
return cache;
}
Expand Down
4 changes: 2 additions & 2 deletions src/immvision/internal/drawing/image_drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ namespace ImmVision
const cv::Mat& image,
cv::Mat& in_out_rgba_image_cache,
bool shall_refresh_rgba,
GlTextureCv* outTexture
GlTexture* outTexture
)
{
if (image.empty())
Expand Down Expand Up @@ -301,7 +301,7 @@ namespace ImmVision
//
// Blit
//
outTexture->BlitMat(finalImage, false);
outTexture->UpdateFromImage(finalImage, false);
}

bool HasColormapParam(const ImageParams &params)
Expand Down
4 changes: 2 additions & 2 deletions src/immvision/internal/drawing/image_drawing.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "immvision/image.h"
#include "immvision/internal/gl/gl_texture.h"
#include "immvision/gl_texture.h"
#include <opencv2/core.hpp>

namespace ImmVision
Expand All @@ -21,7 +21,7 @@ namespace ImmVision
const cv::Mat& image,
cv::Mat& in_out_rgba_image_cache,
bool shall_refresh_rgba,
GlTextureCv* outTexture
GlTexture* outTexture
);

bool HasColormapParam(const ImageParams& params);
Expand Down
8 changes: 4 additions & 4 deletions src/immvision/internal/drawing/internal_icons.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "immvision/internal/drawing/internal_icons.h"
#include "immvision/internal/cv/cv_drawing_utils.h"
#include "immvision/internal/gl/gl_texture.h"
#include "immvision/gl_texture.h"
#include "immvision/image.h"
#include "immvision/imgui_imm.h"
#include <opencv2/core.hpp>
Expand Down Expand Up @@ -228,7 +228,7 @@ namespace ImmVision
}


static std::map<IconType, std::unique_ptr<GlTextureCv>> sIconsTextureCache;
static std::map<IconType, std::unique_ptr<GlTexture>> sIconsTextureCache;
//static cv::Size gIconSize(20, 20);

cv::Size IconSize()
Expand All @@ -253,10 +253,10 @@ namespace ImmVision

cv::Mat resized = m;
cv::resize(m, resized, cv::Size(IconSize().width * 2, IconSize().height * 2), 0., 0., cv::INTER_AREA);
auto texture = std::make_unique<GlTextureCv>(resized, true);
auto texture = std::make_unique<GlTexture>(resized, true);
sIconsTextureCache[iconType] = std::move(texture);
}
return sIconsTextureCache[iconType]->mImTextureId;
return sIconsTextureCache[iconType]->TextureId;
}

bool IconButton(IconType iconType, bool disabled)
Expand Down
30 changes: 16 additions & 14 deletions src/immvision/internal/gl/gl_texture.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "immvision/internal/gl/gl_texture.h"

#include "immvision/gl_texture.h"
#include "immvision/internal/cv/cv_drawing_utils.h"
#include "immvision/internal/gl/gl_provider.h"

Expand All @@ -9,29 +8,32 @@ namespace ImmVision
GlTexture::GlTexture()
{
ImTextureID textureId_Gl = ImmVision_GlProvider::GenTexture();
this->mImTextureId = textureId_Gl;
this->TextureId = textureId_Gl;
}

GlTexture::~GlTexture()
{
ImmVision_GlProvider::DeleteTexture(mImTextureId);
ImmVision_GlProvider::DeleteTexture(TextureId);
}

//
// ImageTextureCv
//
GlTextureCv::GlTextureCv(const cv::Mat& mat, bool isBgrOrder) : GlTextureCv()
GlTexture::GlTexture(const cv::Mat& image, bool isColorOrderBGR) : GlTexture()
{
BlitMat(mat, isBgrOrder);
UpdateFromImage(image, isColorOrderBGR);
}

void GlTextureCv::BlitMat(const cv::Mat& mat, bool isBgrOrder)
void GlTexture::UpdateFromImage(const cv::Mat& image, bool isColorOrderBGR)
{
if (mat.empty())
if (image.empty())
return;
cv::Mat mat_rgba = CvDrawingUtils::converted_to_rgba_image(mat, isBgrOrder);
cv::Mat mat_rgba = CvDrawingUtils::converted_to_rgba_image(image, isColorOrderBGR);

ImmVision_GlProvider::Blit_RGBA_Buffer(mat_rgba.data, mat_rgba.cols, mat_rgba.rows, mImTextureId);
this->mImageSize = ImVec2((float)mat_rgba.cols, (float) mat_rgba.rows);
ImmVision_GlProvider::Blit_RGBA_Buffer(mat_rgba.data, mat_rgba.cols, mat_rgba.rows, TextureId);
this->Size = mat_rgba.size();
}

ImVec2 GlTexture::SizeImVec2() const
{
return {(float)Size.width, (float)Size.height};
}

} // namespace ImmVision
34 changes: 0 additions & 34 deletions src/immvision/internal/gl/gl_texture.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/immvision/internal/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ namespace ImmVision
//
// Lambda / Show image
//
auto fnShowImage = [&params](const GlTextureCv& glTexture) -> MouseInformation
auto fnShowImage = [&params](const GlTexture& glTexture) -> MouseInformation
{
bool disableDragWindow = params->PanWithMouse;
cv::Point2d mouseLocation = ImageWidgets::DisplayTexture_TrackMouse(
Expand Down
4 changes: 2 additions & 2 deletions src/immvision/internal/image_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace ImmVision
{
mCacheImages.AddKey(key);
isNewEntry = true;
mCacheImages.Get(key).GlTexture = std::make_unique<GlTextureCv>();
mCacheImages.Get(key).GlTexture = std::make_unique<GlTexture>();
}
return isNewEntry;
}
Expand Down Expand Up @@ -137,7 +137,7 @@ namespace ImmVision
bool fullRefresh =
( userRefresh
|| isNewEntry
|| (cachedImage.GlTexture->mImageSize.x == 0.f)
|| (cachedImage.GlTexture->Size.empty())
|| ShallRefreshRgbaCache(oldParams, *params));
if (fullRefresh)
{
Expand Down
4 changes: 2 additions & 2 deletions src/immvision/internal/image_cache.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "immvision/image.h"
#include "immvision/internal/gl/gl_texture.h"
#include "immvision/gl_texture.h"
#include "immvision/internal/gl/short_lived_cache.h"


Expand Down Expand Up @@ -31,7 +31,7 @@ namespace ImmVision
// These caches are heavy and will be destroyed
// if not used (after about 5 seconds)
cv::Mat ImageRgbaCache; // Image with applied colormap, alpha grid & paper background
std::unique_ptr<GlTextureCv> GlTexture;
std::unique_ptr<GlTexture> GlTexture;
};

// returns true if new entry
Expand Down
8 changes: 4 additions & 4 deletions src/immvision/internal/imgui/image_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ namespace ImmVision
{
ImVec2 size_(size);
if (size.x == 0.f)
size_ = texture.mImageSize;
size_ = texture.SizeImVec2();

ImVec2 imageTl = ImGui::GetCursorScreenPos();
ImVec2 imageBr(imageTl.x + size.x, imageTl.y + size.y);
std::stringstream id;
id << "##" << texture.mImTextureId;
id << "##" << texture.TextureId;
if (disableDragWindow)
ImGui::InvisibleButton(id.str().c_str(), size);
else
ImGui::Dummy(size);
ImGui::GetWindowDrawList()->AddImage(texture.mImTextureId, imageTl, imageBr);
ImGui::GetWindowDrawList()->AddImage(texture.TextureId, imageTl, imageBr);
}

float FontSizeRatio()
Expand All @@ -30,7 +30,7 @@ namespace ImmVision
return r;
}

cv::Point2d DisplayTexture_TrackMouse(const GlTextureCv& texture, ImVec2 displaySize, bool disableDragWindow)
cv::Point2d DisplayTexture_TrackMouse(const GlTexture& texture, ImVec2 displaySize, bool disableDragWindow)
{
ImVec2 imageTopLeft = ImGui::GetCursorScreenPos();
GlTexture_Draw_DisableDragWindow(texture, displaySize, disableDragWindow);
Expand Down
4 changes: 2 additions & 2 deletions src/immvision/internal/imgui/image_widgets.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "immvision/internal/gl/gl_texture.h"
#include "immvision/gl_texture.h"
#include "immvision/image.h"
#include "imgui.h"
#include <opencv2/core.hpp>
Expand All @@ -8,7 +8,7 @@ namespace ImmVision
{
namespace ImageWidgets
{
cv::Point2d DisplayTexture_TrackMouse(const GlTextureCv& texture, ImVec2 displaySize, bool disableDragWindow);
cv::Point2d DisplayTexture_TrackMouse(const GlTexture& texture, ImVec2 displaySize, bool disableDragWindow);
void ShowImageInfo(const cv::Mat &image, double zoomFactor);
void ShowPixelColorWidget(const cv::Mat &image, cv::Point pt, const ImageParams& params);

Expand Down
4 changes: 2 additions & 2 deletions src/immvision/internal/inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ namespace ImmVision
if (ImGui::Selectable(id_selectable.c_str(), is_selected, 0, itemSize))
s_Inspector_CurrentIndex = i;

float imageRatio = cacheImage.GlTexture->mImageSize.x / cacheImage.GlTexture->mImageSize.y;
float imageRatio = cacheImage.GlTexture->SizeImVec2().x / cacheImage.GlTexture->SizeImVec2().y;
ImVec2 image_tl(pos.x, pos.y + ImGui::GetTextLineHeight());
ImVec2 image_br(pos.x + imageRatio * imageHeight, image_tl.y + imageHeight);

ImGui::GetWindowDrawList()->AddImage(cacheImage.GlTexture->mImTextureId, image_tl, image_br);
ImGui::GetWindowDrawList()->AddImage(cacheImage.GlTexture->TextureId, image_tl, image_br);

ImGui::PopID();
}
Expand Down
Loading

0 comments on commit 3b851b0

Please sign in to comment.