cameraview

This commit is contained in:
milek7
2019-04-09 00:58:36 +02:00
parent c0b11a4517
commit 317af2e5ba
4 changed files with 87 additions and 5 deletions

View File

@@ -1,3 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_JPEG
#include "stb/stb_image.h"

View File

@@ -110,6 +110,9 @@ RECENT REVISION HISTORY:
#ifndef STBI_INCLUDE_STB_IMAGE_H
#define STBI_INCLUDE_STB_IMAGE_H
#define STBI_ONLY_JPEG
#define STBI_NO_STDIO
// DOCUMENTATION
//
// Limitations:

View File

@@ -1,15 +1,27 @@
#include "widgets/cameraview.h"
#include "extras/VS_Dev.h"
#include "stb/stb_image.h"
#include "Globals.h"
#include "Logs.h"
ui::cameraview_panel::cameraview_panel()
: ui_panel(LOC_STR(cameraview_window), false)
{
size_min = {200, 200};
size_max = {2000, 2000};
}
void ui::cameraview_panel::render()
{
if (!is_open && !exit_thread)
if (!is_open && !exit_thread) {
exit_thread = true;
{
std::lock_guard<std::mutex> lock(mutex);
stbi_image_free(image_ptr);
image_ptr = nullptr;
}
cv.notify_all();
}
ui_panel::render();
}
@@ -21,10 +33,70 @@ void ui::cameraview_panel::render_contents()
workthread = std::thread(&cameraview_panel::workthread_func, this);
exit_thread = false;
}
if (!texture) {
texture.emplace();
texture->make_stub();
}
texture->bind(0);
{
std::lock_guard<std::mutex> lock(mutex);
if (image_ptr) {
glActiveTexture(GL_TEXTURE0);
glTexImage2D(
GL_TEXTURE_2D, 0,
GL_SRGB8_ALPHA8,
image_w, image_h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image_ptr);
stbi_image_free(image_ptr);
if (Global.python_mipmaps) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
image_ptr = nullptr;
}
}
cv.notify_all();
ImVec2 surface_size = ImGui::GetContentRegionAvail();
ImGui::Image(reinterpret_cast<void*>(texture->id), surface_size);
}
void ui::cameraview_panel::workthread_func()
{
while (!exit_thread);
std::this_thread::sleep_for(std::chrono::seconds(1));
VSDev vsdev;
while (!exit_thread) {
uint8_t *buffer = nullptr;
size_t len = vsdev.GetFrameFromStream((char**)&buffer);
if (buffer) {
int w, h;
uint8_t *image = stbi_load_from_memory(buffer, len, &w, &h, nullptr, 4);
if (!image)
ErrorLog(std::string(stbi_failure_reason()));
vsdev.FreeFrameBuffer((char*)buffer);
std::unique_lock<std::mutex> lock(mutex);
if (image_ptr)
cv.wait(lock, [this]{return !image_ptr;});
image_w = w;
image_h = h;
image_ptr = image;
}
else
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}

View File

@@ -9,6 +9,14 @@ class cameraview_panel : public ui_panel
std::atomic_bool exit_thread = true;
std::thread workthread;
uint8_t* image_ptr = nullptr;
int image_w, image_h;
std::mutex mutex;
std::condition_variable cv;
std::optional<opengl_texture> texture;
void workthread_func();
public: