Skip to content

Commit

Permalink
experiment with pthreads - file dialogs - dynamic image open
Browse files Browse the repository at this point in the history
  • Loading branch information
aang7 committed Mar 11, 2018
1 parent 7975b26 commit 669f3af
Show file tree
Hide file tree
Showing 5 changed files with 6,148 additions and 47 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
TAGS
graphiclib/libs/gl3w/GL/gl3w.o
graphiclib/opengl2_example/imgui_impl_glfw_gl2.o
graphiclib/opengl2_example/main.o
graphiclib/sdl_opengl2_example/image.jpg
graphiclib/sdl_opengl2_example/image2.jpg
graphiclib/sdl_opengl2_example/imgui.ini
graphiclib/sdl_opengl2_example/imgui_impl_sdl_gl2.o
graphiclib/sdl_opengl2_example/main.o
graphiclib/sdl_opengl2_example/pixout
graphiclib/sdl_opengl2_example/sdl2example
imgui/imgui.o
imgui/imgui_demo.o
imgui/imgui_draw.o
pixui.o
tags
8 changes: 4 additions & 4 deletions graphiclib/sdl_opengl2_example/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# g++ `sdl2-config --cflags` -I ../.. main.cpp imgui_impl_sdl_gl2.cpp ../../imgui*.cpp `sdl2-config --libs` -lGL -o sdl2example

EXE = pixout
COMPILER = -std=c++11
OBJS = main.o imgui_impl_sdl_gl2.o
OBJS += ../../imgui/imgui.o ../../pixui.o ../../imgui/imgui_draw.o
OBJS += ../../imgui/imgui.o ../../pixui.o ../../tinyfiledialogs.o ../../imgui/imgui_draw.o

UNAME_S := $(shell uname -s)


ifeq ($(UNAME_S), Linux) #LINUX
ECHO_MESSAGE = "Linux"
LIBS = -lGL `sdl2-config --libs` `pkg-config --cflags --libs opencv` -std=c++11 -lpthread

LIBS = -lGL `sdl2-config --libs` `pkg-config --cflags --libs opencv` -lpthread
CXXFLAGS = -I ../../imgui `sdl2-config --cflags`
CXXFLAGS += -Wall -Wformat
CFLAGS = $(CXXFLAGS)
Expand All @@ -24,7 +24,7 @@ all: $(EXE)
@echo Build complete for $(ECHO_MESSAGE)

$(EXE): $(OBJS)
$(CXX) -o $(EXE) $(OBJS) $(CXXFLAGS) $(LIBS)
$(CXX) $(COMPILER) -o $(EXE) $(OBJS) $(CXXFLAGS) $(LIBS)

clean:
rm $(EXE) $(OBJS)
Expand Down
220 changes: 177 additions & 43 deletions pixui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
#include<opencv2/opencv.hpp>
#include <GLFW/glfw3.h>
#include<pthread.h>
#include<string>
#include <sstream>
#include <iostream>
#include "tinyfiledialogs.h"



#ifdef _MSC_VER
#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
Expand Down Expand Up @@ -53,10 +59,6 @@

#define IM_MAX(_A,_B) (((_A) >= (_B)) ? (_A) : (_B))

//-----------------------------------------------------------------------------
// DEMO CODE
//-----------------------------------------------------------------------------

#if !defined(IMGUI_DISABLE_OBSOLETE_FUNCTIONS) && defined(IMGUI_DISABLE_TEST_WINDOWS) && !defined(IMGUI_DISABLE_DEMO_WINDOWS) // Obsolete name since 1.53, TEST->DEMO
#define IMGUI_DISABLE_DEMO_WINDOWS
#endif
Expand All @@ -78,14 +80,17 @@ struct OpenCVImage

GLuint texture;
cv::Mat mat; // maybe i will use this later
bool open;
string name;

OpenCVImage() {

texture = 0;
}

void LoadCVMat(cv::Mat& frame) {
void LoadCVMat(cv::Mat frame) {
if (!texture)
{
//mat = frame; // maybe i should copy that frame (clone it)
cv::cvtColor(frame, frame, CV_BGR2RGB);

glGenTextures(1, &texture);
Expand All @@ -100,6 +105,30 @@ struct OpenCVImage

}
}

void LoadCVMat2(cv::Mat frame) {
if (!texture)
{
mat = frame; // maybe i should copy that frame (clone it)
if (mat.empty()) {
cout << "No image data ON LOADCVMAT2" << endl;
}else
cout << "We have image data ************" << endl;
cv::cvtColor(mat, mat, CV_BGR2RGB);

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);

// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mat.cols, mat.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, mat.ptr());

}else
cout << "We HAVE SOMETHING AT TEXTURE" << "\n";
}

void UpdateMat(cv::Mat& frame ) {
// image must have same size
Expand All @@ -112,28 +141,147 @@ struct OpenCVImage
void Clear() { glDeleteTextures(1, &texture); }

GLuint * getTexture() { return &texture; }


// return mat if is not empty otherwise return null
cv::Mat* GetMat() {

if (!mat.data)
{
if (!mat.empty())
return &mat;
else
return NULL;
} else
return NULL;

}

void switchOpen(){
if (open)
open = false;
else
open = true;
}

bool * getOpen()
{
return &open;
}

void SetName(const char* nme) {
name = std::string(nme);
}

//then GetName

const char * GetName(){
return name.c_str();
}
};

static void showImage(const char *windowName,bool *open, GLuint *textura) {


if (ImGui::Begin(windowName, open, ImGuiWindowFlags_ResizeFromAnySide))
if (*open)
{
ImVec2 pos = ImGui::GetCursorScreenPos(); // actual position
ImGui::GetWindowDrawList()->AddImage((void*)*textura, pos, ImVec2(ImGui::GetContentRegionAvail().x + pos.x, ImGui::GetContentRegionAvail().y + pos.y));

}
ImGui::End();
if (ImGui::Begin(windowName, open, ImGuiWindowFlags_ResizeFromAnySide))
{
ImVec2 pos = ImGui::GetCursorScreenPos(); // actual position
ImGui::GetWindowDrawList()->AddImage((void*)*textura, pos, ImVec2(ImGui::GetContentRegionAvail().x + pos.x, ImGui::GetContentRegionAvail().y + pos.y));

}
ImGui::End();

}
}

static std::ostringstream ss;

char const * lTheOpenFileName;
char const * lFilterPatterns[2] = { "*.jpg", "*.png" };

void *call_from_thread(void *) {
lTheOpenFileName = tinyfd_openFileDialog(
"let us read the password back",
"",
2,
lFilterPatterns,
NULL,
0);
if (! lTheOpenFileName)
{
tinyfd_messageBox(
"Error",
"Open file name is NULL",
"ok",
"error",
1);
return NULL;

}
else {cout << "file choosed: " << lTheOpenFileName << endl; return (void *)lTheOpenFileName;}


}

void ImGui::ShowPixui(bool *p_open)
{
static std::vector<OpenCVImage> data;
static bool window1 = true;
static bool window2 = false;

ImGui::ShowMetricsWindow(&window2);
if (ImGui::Begin("nombre", p_open, ImVec2(90, 100), ImGuiWindowFlags_MenuBar ))
{

static int count;
/* Sample */
if (ImGui::Button("Create new OpenCVImage"))
{
data.push_back(OpenCVImage());
static pthread_t t;

//Launch a thread
pthread_create(&t, NULL, call_from_thread, NULL);

void* temp = NULL;
pthread_join(t, &temp);

if (temp != NULL) {
const char* returnValue = (const char *) temp;
cout << "RETURNED VALUE: " << returnValue << endl;
cv::Mat mmm = cv::imread(returnValue);
data.back().LoadCVMat2(mmm);
data.at(data.size() - 1).SetName(returnValue);
mmm.release();
}

}

static std::vector<string> names;


for(int i = 0;i < data.size(); i++)
{

//long num = i;
//ss << num;
//ImGui::Selectable("ss0" , &selection[0]);
//names.emplace_back(ss.str());
// if (ImGui::Button(data.at(i).c_str())) {
// data.at(i).switchOpen();
// }
// ImGui::SameLine();
// ImGui::Text("value: %s", *(data.at(i).getOpen()) ? "true" : "false");

ImGui::PushID(i);
if (ImGui::Button(data.at(i).GetName()))
data.at(i).switchOpen();
ImGui::PopID();
ImGui::Text("value: %s", *(data.at(i).getOpen()) ? "true" : "false");
}

/* END OF SAMPLE */
static OpenCVImage myglopencv;
static OpenCVImage glcv2;

Expand All @@ -153,15 +301,15 @@ void ImGui::ShowPixui(bool *p_open)
if (!image2.data)
image2 = cv::imread("image2.jpg", 1);

static bool window1;
static bool window2 = false;

glcv2.LoadCVMat(image);
showImage("imagen", &window1, glcv2.getTexture());
//showImage("imagen", &window1, glcv2.getTexture());
//const char* names[2] = {"window1", "window2"};
// mostrar imagenes

myglopencv.LoadCVMat(image2);

{

static int value = 0;
static int oldvalue = 0;

Expand All @@ -176,41 +324,27 @@ void ImGui::ShowPixui(bool *p_open)
myglopencv.UpdateMat(smooth);

}

showImage("imagen2",&window2, myglopencv.getTexture());
}



//showImage("imagen2", &window2, myglopencv.getTexture());
for(int i = 0; i < data.size(); i++)
{

//showImage(names[i], data.at(i).getOpen(), myglopencv.getTexture());
ImGui::PushID(i);
showImage(data.at(i).GetName(), data.at(i).getOpen(), data.at(i).getTexture());
ImGui::PopID();

}
}

ImGui::End();

}


void load_opencv_Mat(cv::Mat frame, GLuint *mytexture) {

void getVideoCapture() {

if (!*mytexture)
{

cv::cvtColor(frame, frame, CV_BGR2RGB);

glGenTextures(1, mytexture);
glBindTexture(GL_TEXTURE_2D, *mytexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame.cols, frame.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, frame.ptr());

}

}


#else

void ImGui::ShowDemoWindow(bool*) {}
Expand Down
Loading

0 comments on commit 669f3af

Please sign in to comment.