From 0a444df0479b12e076093a8941ff53b05bf474d1 Mon Sep 17 00:00:00 2001 From: Your Name <you@example.com> Date: Fri, 16 Oct 2020 21:27:00 +0200 Subject: [PATCH] Client version c_... of gtk_image --- CMakeLists.txt | 1 + c_gtk_image/CMakeLists.txt | 28 +++++ c_gtk_image/c_gtk_image.cpp | 138 +++++++++++++++++++++ {o_gtk_image/src => include}/common.h | 2 +- o_gtk_image/CMakeLists.txt | 4 +- o_gtk_image/{src => }/o_gtk_image.cpp | 18 +-- {o_gtk_image/src => src}/histogram.cpp | 8 +- {o_gtk_image/src => src}/image_display.cpp | 4 +- 8 files changed, 185 insertions(+), 18 deletions(-) create mode 100644 c_gtk_image/CMakeLists.txt create mode 100644 c_gtk_image/c_gtk_image.cpp rename {o_gtk_image/src => include}/common.h (95%) rename o_gtk_image/{src => }/o_gtk_image.cpp (93%) rename {o_gtk_image/src => src}/histogram.cpp (97%) rename {o_gtk_image/src => src}/image_display.cpp (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7900de2..abcd1d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,4 +4,5 @@ cmake_minimum_required(VERSION 2.6) project(gtk) subdirs(o_gtk_image) +subdirs(c_gtk_image) diff --git a/c_gtk_image/CMakeLists.txt b/c_gtk_image/CMakeLists.txt new file mode 100644 index 0000000..588efeb --- /dev/null +++ b/c_gtk_image/CMakeLists.txt @@ -0,0 +1,28 @@ +# Set the minimum version of cmake required to build this project +cmake_minimum_required(VERSION 2.6) + +project(c_gtk_image) + +#This is to be able to debug and recompile automatically the libs (shared_blc). It is slower, you can remove this line and use : ${BLAR_BUILD_DIR}/lib/libblc.dylib instead of shared_blc + +find_package(blc_network REQUIRED) +find_package(blc_channel REQUIRED) #For compatibilité with common.h + +find_package(blc_image REQUIRED) +find_package(blc_program REQUIRED) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK3 REQUIRED gtk+-3.0) +find_package(JPEG REQUIRED) + +add_definitions(-Wall ${BL_DEFINITIONS} -std=c++17 -Wno-deprecated-declarations) #device_manager (mouse) is deprecated +include_directories(${GTK3_INCLUDE_DIRS} ${BL_INCLUDE_DIRS} ${JPEG_INCLUDE_DIR} ../include) +link_directories(${GTK3_LIBRARY_DIRS}) +add_definitions(${GTK3_CFLAGS_OTHER}) +add_executable(c_gtk_image c_gtk_image.cpp ../src/image_display.cpp ../src/histogram.cpp) +target_link_libraries(c_gtk_image ${GTK3_LIBRARIES} ${BL_LIBRARIES} ${JPEG_LIBRARIES} ) + + + + + diff --git a/c_gtk_image/c_gtk_image.cpp b/c_gtk_image/c_gtk_image.cpp new file mode 100644 index 0000000..41365f6 --- /dev/null +++ b/c_gtk_image/c_gtk_image.cpp @@ -0,0 +1,138 @@ +#include "common.h" + +#include <fcntl.h> // O_RDONLY ... +#include <stdio.h> +#include <gtk/gtk.h> +#include <string.h> +#include <stdint.h> //uint32_t +//#include <jpeglib.h> +#include <sys/mman.h> +#include <errno.h> //errno +#include "blc_core.h" +#include "blc_program.h" +#include "blc_network.h" +#include <thread> + +using namespace std; + +float min_val, max_val; + +const char *channel_name, *fullscreen_option, *keyboard_mode; +GtkWidget *window; +GdkDisplay *main_display; +GdkDevice *pointer_device; +int interactive_mode=0; +GtkApplication *app; + +blc_channel mouse_channel; +blc_channel input; + +char const *input_name; + +int input_terminal; +int mode; + +char const *format_string; +uint32_t format; + +void ask_fullscreen(){ + gtk_window_fullscreen(GTK_WINDOW(window)); + +} + +void ask_quit() +{ + g_application_quit(G_APPLICATION(app)); + blc_quit(); +} + +void on_key_press(GtkWidget *widget, GdkEventKey *event){ + + char key; + + if (event->type == GDK_KEY_PRESS){ + key=event->keyval; + fwrite(&key, 1, 1, stdout); + fflush(stdout); + } +} + +void activate_cb(GApplication *app) +{ + GtkWidget *display=nullptr; + GtkWidget *grid; + + main_display = gdk_display_get_default (); + GdkDeviceManager *device_manager = gdk_display_get_device_manager (main_display); + pointer_device = gdk_device_manager_get_client_pointer (device_manager); + + window=gtk_application_window_new(GTK_APPLICATION(app)); + gtk_window_set_title(GTK_WINDOW(window), input.name); + + grid=gtk_grid_new(); + + // for(i=0; input_names[i]; i++){ This is for displaying multiple images + // input=new blc_channel(/*input_names[i]*/ input_name, BLC_CHANNEL_READ); + display=create_image_display(&input); + if (display==NULL) EXIT_ON_CHANNEL_ERROR(&input, "Format not managed."); + gtk_widget_set_hexpand(display, 1); + gtk_widget_set_vexpand(display, 1); + gtk_container_add(GTK_CONTAINER(grid), display); + // } + gtk_container_add(GTK_CONTAINER(window), grid); + gtk_widget_show_all(window); + if (keyboard_mode) g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK (on_key_press), NULL); + +} + +/** Classical GTK application. + * The first optional argument is the name of the experience. Otherwise all the existing shared memory are used. + * */ +int main(int argc, char *argv[]) +{ + int status=0; + char const *g_debug, *mouse_name, *min_str, *max_str; + + blc_program_set_description("Display the content of the blc_channel depending on its type on format"); + blc_program_add_option(&keyboard_mode, 'k', "keyboard", nullptr, "Send keyboard input to stdout", nullptr); + blc_program_add_option(&min_str, 'm', "min", "FL32", "minimal value", nullptr); + blc_program_add_option(&max_str, 'M', "max", "FL32", "maximal value", nullptr); + blc_program_add_option(&fullscreen_option, 'F', "fullscreen", nullptr, "Set the window in fullscreen", nullptr); + blc_program_add_option(&g_debug, 'g', "g-fatal-warnings", nullptr, "Debug gtk.", nullptr); + + blc_program_init(&argc, &argv, ask_quit); + blc_command_forward_blc_channels(); + + blc_array_tcp4_client input_client(&input, "127.0.0.1", "31440"); + input.allocate(); + fprintf(stderr, "Connecté\n"); + + min_val=0; + if (input.type=='UIN8') max_val=256; + if (input.type=='INT8'){ + min_val=-128; + max_val=128; + } + + if (input.type=='FL32' && input.format=='Y800'){ + if (min_str) SSCANF(1, min_str, "%f", &min_val); + else min_val=0; + if (max_str) SSCANF(1, max_str, "%f", &max_val); + else max_val=1; + }else if (min_str || max_str) EXIT_ON_ARRAY_ERROR(&input, "Min (-m) and max (-M) have only effect for type FL32 format Y800"); + + + thread input_thread([&input_client](){ + while(1) input_client.read_data(); + }); + + gtk_disable_setlocale(); + gtk_init(&argc, &argv); + app = gtk_application_new(nullptr, G_APPLICATION_FLAGS_NONE); + g_signal_connect (app, "activate", G_CALLBACK (activate_cb), nullptr); + status = g_application_run(G_APPLICATION(app), 0, nullptr); + g_object_unref(app); + + return EXIT_SUCCESS; +} + diff --git a/o_gtk_image/src/common.h b/include/common.h similarity index 95% rename from o_gtk_image/src/common.h rename to include/common.h index 90eef34..0189352 100644 --- a/o_gtk_image/src/common.h +++ b/include/common.h @@ -5,7 +5,7 @@ #include <blc_channel.h> -extern float min, max; +extern float min_val, max_val; extern blc_channel *channel; extern GtkWidget *legend; diff --git a/o_gtk_image/CMakeLists.txt b/o_gtk_image/CMakeLists.txt index 15bd5a7..a9f91ec 100644 --- a/o_gtk_image/CMakeLists.txt +++ b/o_gtk_image/CMakeLists.txt @@ -14,10 +14,10 @@ pkg_check_modules(GTK3 REQUIRED gtk+-3.0) find_package(JPEG REQUIRED) add_definitions(-Wall ${BL_DEFINITIONS} -Wno-deprecated-declarations) #device_manager (mouse) is deprecated -include_directories(${GTK3_INCLUDE_DIRS} ${BL_INCLUDE_DIRS} ${JPEG_INCLUDE_DIR}) +include_directories(${GTK3_INCLUDE_DIRS} ${BL_INCLUDE_DIRS} ${JPEG_INCLUDE_DIR} ../include) link_directories(${GTK3_LIBRARY_DIRS}) add_definitions(${GTK3_CFLAGS_OTHER}) -add_executable(o_gtk_image src/o_gtk_image.cpp src/image_display.cpp src/histogram.cpp) +add_executable(o_gtk_image o_gtk_image.cpp ../src/image_display.cpp ../src/histogram.cpp) target_link_libraries(o_gtk_image ${GTK3_LIBRARIES} ${BL_LIBRARIES} ${JPEG_LIBRARIES} ) diff --git a/o_gtk_image/src/o_gtk_image.cpp b/o_gtk_image/o_gtk_image.cpp similarity index 93% rename from o_gtk_image/src/o_gtk_image.cpp rename to o_gtk_image/o_gtk_image.cpp index 4ba398b..cc73094 100644 --- a/o_gtk_image/src/o_gtk_image.cpp +++ b/o_gtk_image/o_gtk_image.cpp @@ -10,7 +10,7 @@ #include <errno.h> //errno #include "blc_core.h" #include "blc_program.h" -float min, max; +float min_val, max_val; const char *channel_name, *fullscreen_option, *keyboard_mode; GtkWidget *window; @@ -104,18 +104,18 @@ int main(int argc, char *argv[]) input.open(input_name, mode); - min=0; - if (input.type=='UIN8') max=256; + min_val=0; + if (input.type=='UIN8') max_val=256; if (input.type=='INT8'){ - min=-128; - max=128; + min_val=-128; + max_val=128; } if (input.type=='FL32' && input.format=='Y800'){ - if (min_str) SSCANF(1, min_str, "%f", &min); - else min=0; - if (max_str) SSCANF(1, max_str, "%f", &max); - else max=1; + if (min_str) SSCANF(1, min_str, "%f", &min_val); + else min_val=0; + if (max_str) SSCANF(1, max_str, "%f", &max_val); + else max_val=1; }else if (min_str || max_str) EXIT_ON_ARRAY_ERROR(&input, "Min (-m) and max (-M) have only effect for type FL32 format Y800"); gtk_disable_setlocale(); diff --git a/o_gtk_image/src/histogram.cpp b/src/histogram.cpp similarity index 97% rename from o_gtk_image/src/histogram.cpp rename to src/histogram.cpp index 7f9464a..1591a40 100644 --- a/o_gtk_image/src/histogram.cpp +++ b/src/histogram.cpp @@ -63,7 +63,7 @@ static gboolean update_histogram_cb(GtkImage *image, GdkFrameClock *, gpointer p } break; case 'FL32': - FOR(i, channel->total_length) histogram[(int)CLIP_UCHAR(((channel->floats[i]-min)/(max-min)*256+0.5))]++; + FOR(i, channel->total_length) histogram[(int)CLIP_UCHAR(((channel->floats[i]-min_val)/(max_val-min_val)*256+0.5))]++; FOR(i, 256) hist_max=MAX(histogram[i], hist_max); FOR(i, 256) { @@ -73,7 +73,7 @@ static gboolean update_histogram_cb(GtkImage *image, GdkFrameClock *, gpointer p } if (y>=0 && y<256 && x >=0 && x<256) { - SPRINTF(text, "Y[%f]=%f.2%%", x*(max-min-0.5)/256+min, histogram[x]*100/(float)channel->total_length); + SPRINTF(text, "Y[%f]=%f.2%%", x*(max_val-min_val-0.5)/256+min_val, histogram[x]*100/(float)channel->total_length); gtk_statusbar_push(GTK_STATUSBAR(pointer_statusbar), 0, text); } break; @@ -189,10 +189,10 @@ void histogram_cb(GtkToolButton *toolbutton, gpointer pointer_statusbar ) gtk_container_add(GTK_CONTAINER(histogram), histogram_scrolled_window); gtk_container_add(GTK_CONTAINER(histogram), legend_box); - SPRINTF(text, "%f", min); + SPRINTF(text, "%f", min_val); label=gtk_label_new(text); gtk_box_pack_start(GTK_BOX(legend_box), label, FALSE, FALSE, 0); - SPRINTF(text, "%f", max); + SPRINTF(text, "%f", max_val); label=gtk_label_new(text); gtk_box_pack_end(GTK_BOX(legend_box), label, FALSE, FALSE, 0); diff --git a/o_gtk_image/src/image_display.cpp b/src/image_display.cpp similarity index 99% rename from o_gtk_image/src/image_display.cpp rename to src/image_display.cpp index a4c6d36..959a493 100644 --- a/o_gtk_image/src/image_display.cpp +++ b/src/image_display.cpp @@ -168,14 +168,14 @@ gboolean update_Y800_image(GtkImage *image, GdkFrameClock *, gpointer pointer_st uint32_t *values; char text[64]; - gain=1/(max-min); + gain=1/(max_val-min_val); if (blc_command_loop_start()){ values = (uint32_t*)image_buffer; if (channel->type=='UIN8') FOR(i, channel->size) values[i]=color_map[channel->uchars[i]]; else if (channel->type=='FL32') - FOR(i, channel->total_length) values[i]=color_map[CLIP_UCHAR((channel->floats[i]-min)*256*gain-0.5f)]; + FOR(i, channel->total_length) values[i]=color_map[CLIP_UCHAR((channel->floats[i]-min_val)*256*gain-0.5f)]; else EXIT_ON_ARRAY_ERROR(channel, "Type not managed"); gtk_image_set_from_surface(image, image_surface); -- GitLab