44#include <visp3/core/vpConfig.h>
46#if (defined(VISP_HAVE_GTK))
55#include <visp3/core/vpDisplay.h>
56#include <visp3/gui/vpDisplayGTK.h>
59#include <visp3/core/vpDebug.h>
60#include <visp3/core/vpDisplayException.h>
61#include <visp3/core/vpImageConvert.h>
62#include <visp3/core/vpImageTools.h>
63#include <visp3/core/vpMath.h>
65#ifndef DOXYGEN_SHOULD_SKIP_THIS
68#include <gdk/gdkrgb.h>
71class vpDisplayGTK::Impl
75 : m_widget(NULL), m_background(NULL), m_gc(NULL), m_blue(), m_red(), m_yellow(), m_green(), m_cyan(), m_orange(),
76 m_white(), m_black(), m_gdkcolor(), m_lightBlue(), m_darkBlue(), m_lightRed(), m_darkRed(), m_lightGreen(),
77 m_darkGreen(), m_purple(), m_lightGray(), m_gray(), m_darkGray(), m_colormap(NULL), m_font(NULL), m_vectgtk(NULL),
84 void init(
unsigned int win_width,
unsigned int win_height,
int win_x,
int win_y,
const std::string &title)
86 gint width =
static_cast<gint
>(win_width);
87 gint height =
static_cast<gint
>(win_height);
93 gtk_init(argc, &argv);
96 m_widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
98 gtk_widget_add_events(m_widget, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
100 gtk_window_set_default_size(GTK_WINDOW(m_widget), width, height);
102 gtk_window_move(GTK_WINDOW(m_widget), win_x, win_y);
104 gtk_widget_show(m_widget);
109 m_background = gdk_pixmap_new(m_widget->window, width, height, -1);
112 m_gc = gdk_gc_new(m_widget->window);
115 m_colormap = gdk_window_get_colormap(m_widget->window);
120 gdk_color_parse(
"light blue", &m_lightBlue);
121 gdk_colormap_alloc_color(m_colormap, &m_lightBlue, FALSE, TRUE);
124 gdk_color_parse(
"blue", &m_blue);
125 gdk_colormap_alloc_color(m_colormap, &m_blue, FALSE, TRUE);
128 gdk_color_parse(
"dark blue", &m_darkBlue);
129 gdk_colormap_alloc_color(m_colormap, &m_darkBlue, FALSE, TRUE);
132 gdk_color_parse(
"#FF8C8C", &m_lightRed);
133 gdk_colormap_alloc_color(m_colormap, &m_lightRed, FALSE, TRUE);
136 gdk_color_parse(
"red", &m_red);
137 gdk_colormap_alloc_color(m_colormap, &m_red, FALSE, TRUE);
140 gdk_color_parse(
"dark red", &m_darkRed);
141 gdk_colormap_alloc_color(m_colormap, &m_darkRed, FALSE, TRUE);
144 gdk_color_parse(
"light green", &m_lightGreen);
145 gdk_colormap_alloc_color(m_colormap, &m_lightGreen, FALSE, TRUE);
148 gdk_color_parse(
"green", &m_green);
149 gdk_colormap_alloc_color(m_colormap, &m_green, FALSE, TRUE);
152 gdk_color_parse(
"dark green", &m_darkGreen);
153 gdk_colormap_alloc_color(m_colormap, &m_darkGreen, FALSE, TRUE);
156 gdk_color_parse(
"yellow", &m_yellow);
157 gdk_colormap_alloc_color(m_colormap, &m_yellow, FALSE, TRUE);
160 gdk_color_parse(
"cyan", &m_cyan);
161 gdk_colormap_alloc_color(m_colormap, &m_cyan, FALSE, TRUE);
164 gdk_color_parse(
"orange", &m_orange);
165 gdk_colormap_alloc_color(m_colormap, &m_orange, FALSE, TRUE);
168 gdk_color_parse(
"purple", &m_purple);
169 gdk_colormap_alloc_color(m_colormap, &m_purple, FALSE, TRUE);
172 gdk_color_parse(
"white", &m_white);
173 gdk_colormap_alloc_color(m_colormap, &m_white, FALSE, TRUE);
176 gdk_color_parse(
"black", &m_black);
177 gdk_colormap_alloc_color(m_colormap, &m_black, FALSE, TRUE);
180 gdk_color_parse(
"#C0C0C0", &m_lightGray);
181 gdk_colormap_alloc_color(m_colormap, &m_lightGray, FALSE, TRUE);
184 gdk_color_parse(
"#808080", &m_gray);
185 gdk_colormap_alloc_color(m_colormap, &m_gray, FALSE, TRUE);
188 gdk_color_parse(
"#404040", &m_darkGray);
189 gdk_colormap_alloc_color(m_colormap, &m_darkGray, FALSE, TRUE);
193 m_font = gdk_font_load(
"-*-times-medium-r-normal-*-16-*-*-*-*-*-*-*");
195 m_font = gdk_font_load(
"-*-courier-bold-r-normal-*-*-140-*-*-*-*-*-*");
197 m_font = gdk_font_load(
"-*-courier 10 pitch-medium-r-normal-*-16-*-*-*-*-*-*-*");
200 gdk_window_set_title(m_widget->window, title.c_str());
203 void setFont(
const std::string &fontname) { m_font = gdk_font_load((
const gchar *)fontname.c_str()); }
205 void setTitle(
const std::string &title) { gdk_window_set_title(m_widget->window, title.c_str()); }
207 void setWindowPosition(
int win_x,
int win_y) { gtk_window_move(GTK_WINDOW(m_widget), win_x, win_y); }
213 gdk_draw_gray_image(m_background, m_gc, 0, 0, width, height, GDK_RGB_DITHER_NONE, I.
bitmap, width);
217 gdk_draw_gray_image(m_background, m_gc, 0, 0, width, height, GDK_RGB_DITHER_NONE, sampled.
bitmap, width);
221 gdk_window_set_back_pixmap(m_widget->window, m_background, FALSE);
224 void displayImage(
const vpImage<vpRGBa> &I,
unsigned int scale, gint width, gint height)
228 gdk_draw_rgb_32_image(m_background, m_gc, 0, 0, width, height, GDK_RGB_DITHER_NONE, (
unsigned char *)I.
bitmap,
233 gdk_draw_rgb_32_image(m_background, m_gc, 0, 0, width, height, GDK_RGB_DITHER_NONE,
234 (
unsigned char *)sampled.
bitmap, 4 * width);
238 gdk_window_set_back_pixmap(m_widget->window, m_background, FALSE);
243 gdk_draw_gray_image(m_background, m_gc, j_min, i_min, width, height, GDK_RGB_DITHER_NONE, I.
bitmap, width);
245 gdk_window_set_back_pixmap(m_widget->window, m_background, FALSE);
248 void displayImageROI(
const vpImage<vpRGBa> &I, gint j_min, gint i_min, gint width, gint height)
250 gdk_draw_rgb_32_image(m_background, m_gc, j_min, i_min, width, height, GDK_RGB_DITHER_NONE,
251 (
unsigned char *)I.
bitmap, width * 4);
254 gdk_window_set_back_pixmap(m_widget->window, m_background, FALSE);
264 if (m_widget != NULL) {
265 gdk_window_hide(m_widget->window);
266 gdk_window_destroy(m_widget->window);
267 gtk_widget_destroy(m_widget);
274 gdk_window_clear(m_widget->window);
278 void displayCharString(
const vpImagePoint &ip,
const char *text,
const vpColor &color,
unsigned int scale)
281 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
283 m_gdkcolor.red = 256 * color.
R;
284 m_gdkcolor.green = 256 * color.
G;
285 m_gdkcolor.blue = 256 * color.
B;
286 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
287 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
291 (
const gchar *)text);
293 std::cout <<
"Cannot draw string: no font is selected" << std::endl;
296 void displayCircle(
const vpImagePoint ¢er,
unsigned int radius,
const vpColor &color,
bool fill,
297 unsigned int thickness,
unsigned int scale)
300 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
302 m_gdkcolor.red = 256 * color.
R;
303 m_gdkcolor.green = 256 * color.
G;
304 m_gdkcolor.blue = 256 * color.
B;
305 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
306 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
309 gdk_gc_set_line_attributes(m_gc,
static_cast<gint
>(thickness), GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
312 gdk_draw_arc(m_background, m_gc, FALSE,
vpMath::round((center.
get_u() - radius) / scale),
313 vpMath::round((center.
get_v() - radius) / scale),
static_cast<gint
>(2. * radius / scale),
314 static_cast<gint
>(2. * radius / scale), 23040, 23040);
316 gdk_draw_arc(m_background, m_gc, TRUE,
vpMath::round((center.
get_u() - radius) / scale),
317 vpMath::round((center.
get_v() - radius) / scale),
static_cast<gint
>(2. * radius / scale),
318 static_cast<gint
>(2. * radius / scale), 23040, 23040);
325 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
327 m_gdkcolor.red = 256 * color.
R;
328 m_gdkcolor.green = 256 * color.
G;
329 m_gdkcolor.blue = 256 * color.
B;
330 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
331 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
334 gdk_gc_set_line_attributes(m_gc,
static_cast<gint
>(thickness), GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
337 gdk_gc_set_line_attributes(m_gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
344 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
346 m_gdkcolor.red = 256 * color.
R;
347 m_gdkcolor.green = 256 * color.
G;
348 m_gdkcolor.blue = 256 * color.
B;
349 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
350 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
353 gdk_gc_set_line_attributes(m_gc,
static_cast<gint
>(thickness), GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
358 void displayPoint(
const vpImagePoint &ip,
const vpColor &color,
unsigned int thickness,
unsigned int scale)
361 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
363 m_gdkcolor.red = 256 * color.
R;
364 m_gdkcolor.green = 256 * color.
G;
365 m_gdkcolor.blue = 256 * color.
B;
366 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
367 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
370 if (thickness == 1) {
374 static_cast<gint
>(thickness),
static_cast<gint
>(thickness));
378 void displayRectangle(
const vpImagePoint &topLeft,
unsigned int w,
unsigned int h,
const vpColor &color,
bool fill,
379 unsigned int thickness,
unsigned int scale)
382 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
384 m_gdkcolor.red = 256 * color.
R;
385 m_gdkcolor.green = 256 * color.
G;
386 m_gdkcolor.blue = 256 * color.
B;
387 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
388 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
390 gdk_gc_set_line_attributes(m_gc,
static_cast<gint
>(thickness), GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
395 static_cast<gint
>(h / scale));
399 static_cast<gint
>(h / scale));
402 gdk_gc_set_line_attributes(m_gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
406 const GdkEventType &event_type)
411 while ((ev = gdk_event_get())) {
412 if (ev->any.window == m_widget->window && ev->type == event_type) {
413 double u = ((GdkEventButton *)ev)->x;
414 double v = ((GdkEventButton *)ev)->y;
418 switch (
static_cast<int>(((GdkEventButton *)ev)->button)) {
438 }
while (ret ==
false && blocking ==
true);
445 ImageGtk = gdk_image_get(m_background, 0, 0, width, height);
450 guchar OctetRouge, OctetVert, OctetBleu, mask;
453 for (gint y = 0; y < height; y++) {
454 for (gint x = 0; x < width; x++) {
455 pixel = gdk_image_get_pixel(ImageGtk, x, y);
456 OctetBleu =
static_cast<guchar
>(pixel) & mask;
457 OctetVert =
static_cast<guchar
>(pixel >> 8) & mask;
458 OctetRouge =
static_cast<guchar
>(pixel >> 16) & mask;
459 I[y][x].R = OctetRouge;
460 I[y][x].G = OctetVert;
461 I[y][x].B = OctetBleu;
467 unsigned int getScreenDepth() {
return static_cast<unsigned int>(gdk_window_get_visual(m_widget->window)->depth); }
469 bool getKeyboardEvent(std::string &key,
bool blocking)
475 while ((ev = gdk_event_get()) != NULL) {
478 if (ev->any.window == m_widget->window && ev->type == GDK_KEY_PRESS) {
479 key = gdk_keyval_name(ev->key.keyval);
488 }
while (ret ==
false && blocking ==
true);
492 bool getPointerMotionEvent(
vpImagePoint &ip,
unsigned int scale)
496 if ((ev = gdk_event_get())) {
497 if (ev->any.window == m_widget->window && ev->type == GDK_MOTION_NOTIFY) {
498 double u = ((GdkEventMotion *)ev)->x;
499 double v = ((GdkEventMotion *)ev)->y;
510 void getPointerPosition(
vpImagePoint &ip,
unsigned int scale)
513 gdk_window_get_pointer(m_widget->window, &u, &v, NULL);
514 ip.
set_u(
static_cast<double>(u) * scale);
515 ip.
set_v(
static_cast<double>(v) * scale);
518 void getScreenSize(
bool is_init,
unsigned int &w,
unsigned int &h)
524 gtk_init(argc, &argv);
526 GtkWidget *widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
527 gtk_window_set_default_size(GTK_WINDOW(widget_), 100, 100);
528 gtk_widget_show(widget_);
530 GdkScreen *screen_ = gdk_window_get_screen(widget_->window);
531 w =
static_cast<unsigned int>(gdk_screen_get_width(screen_));
532 h =
static_cast<unsigned int>(gdk_screen_get_height(screen_));
533 gtk_widget_destroy(widget_);
535 GdkScreen *screen_ = gdk_window_get_screen(m_widget->window);
536 w =
static_cast<unsigned int>(gdk_screen_get_width(screen_));
537 h =
static_cast<unsigned int>(gdk_screen_get_height(screen_));
543 GdkPixmap *m_background;
545 GdkColor m_blue, m_red, m_yellow, m_green, m_cyan, m_orange, m_white, m_black, m_gdkcolor;
546 GdkColor m_lightBlue, m_darkBlue, m_lightRed, m_darkRed, m_lightGreen, m_darkGreen, m_purple;
547 GdkColor m_lightGray, m_gray, m_darkGray;
548 GdkColormap *m_colormap;
612 init(I, win_x, win_y, win_title);
666 init(I, win_x, win_y, win_title);
747 if (!win_title.empty())
777 if (!win_title.empty())
796 const std::string &win_title)
837 if (!title.empty()) {
838 m_impl->setTitle(title);
857 m_impl->setWindowPosition(win_x, win_y);
908 int i_min = (std::max)(
static_cast<int>(ceil(iP.
get_i() /
m_scale)), 0);
909 int j_min = (std::max)(
static_cast<int>(ceil(iP.
get_j() /
m_scale)), 0);
911 m_impl->displayImageROI(Itemp,
static_cast<gint
>(j_min),
static_cast<gint
>(i_min),
962 int i_min = (std::max)(
static_cast<int>(ceil(iP.
get_i() /
m_scale)), 0);
963 int j_min = (std::max)(
static_cast<int>(ceil(iP.
get_j() /
m_scale)), 0);
965 m_impl->displayImageROI(Itemp,
static_cast<gint
>(j_min),
static_cast<gint
>(i_min),
987 m_impl->closeDisplay();
1000 m_impl->flushDisplay();
1011 const unsigned int )
1014 m_impl->flushDisplay();
1033 unsigned int h,
unsigned int thickness)
1040 if ((std::fabs(a) > std::numeric_limits<double>::epsilon()) &&
1041 (std::fabs(b) > std::numeric_limits<double>::epsilon())) {
1083 m_impl->displayCharString(ip, text, color,
m_scale);
1098 unsigned int thickness)
1104 m_impl->displayCircle(center, radius, color, fill, thickness,
m_scale);
1119 double i = ip.
get_i();
1120 double j = ip.
get_j();
1123 ip1.
set_i(i - size / 2);
1125 ip2.
set_i(i + size / 2);
1130 ip1.
set_j(j - size / 2);
1132 ip2.
set_j(j + size / 2);
1148 unsigned int thickness)
1155 m_impl->displayDotLine(ip1, ip2, color, thickness,
m_scale);
1168 unsigned int thickness)
1174 m_impl->displayLine(ip1, ip2, color, thickness,
m_scale);
1189 m_impl->displayPoint(ip, color, thickness,
m_scale);
1209 bool fill,
unsigned int thickness)
1215 m_impl->displayRectangle(topLeft, w, h, color, fill, thickness,
m_scale);
1234 bool fill,
unsigned int thickness)
1243 m_impl->displayRectangle(topLeft, w, h, color, fill, thickness,
m_scale);
1270 m_impl->displayRectangle(topLeft, w, h, color, fill, thickness,
m_scale);
1298 ret = m_impl->getClick(ip, button, blocking,
m_scale, GDK_BUTTON_PRESS);
1327 ret = m_impl->getClick(ip, button, blocking,
m_scale, GDK_BUTTON_PRESS);
1358 ret = m_impl->getClick(ip, button, blocking,
m_scale, GDK_BUTTON_PRESS);
1393 ret = m_impl->getClick(ip, button, blocking,
m_scale, GDK_BUTTON_RELEASE);
1408 m_impl->getImage(I,
static_cast<gint
>(
m_width),
static_cast<gint
>(
m_height));
1421 unsigned int depth = m_impl->getScreenDepth();
1447 ret = m_impl->getKeyboardEvent(key, blocking);
1478 ret = m_impl->getKeyboardEvent(key, blocking);
1502 ret = m_impl->getPointerMotionEvent(ip,
m_scale);
1522 m_impl->getPointerPosition(ip,
m_scale);
1546 unsigned int width, height;
1556 unsigned int width, height;
1561#elif !defined(VISP_BUILD_SHARED_LIBS)
1564void dummy_vpDisplayGTK(){};
Class to define RGB colors available for display functionalities.
Error that can be emitted by the vpDisplay class and its derivatives.
@ notInitializedError
Display not initialized.
void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
void setFont(const std::string &fontname)
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
void displayImageROI(const vpImage< unsigned char > &I, const vpImagePoint &iP, unsigned int width, unsigned int height)
void flushDisplayROI(const vpImagePoint &iP, unsigned int width, unsigned int height)
void displayPoint(const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
bool getClick(bool blocking=true)
bool getKeyboardEvent(bool blocking=true)
bool getPointerMotionEvent(vpImagePoint &ip)
void displayArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
void getScreenSize(unsigned int &screen_width, unsigned int &screen_height)
void getImage(vpImage< vpRGBa > &I)
get the window pixmap and put it in vpRGBa image
void clearDisplay(const vpColor &color=vpColor::white)
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
unsigned int getScreenDepth()
get the window depth (8,16,24,32)
unsigned int getScreenWidth()
void setTitle(const std::string &win_title)
bool getPointerPosition(vpImagePoint &ip)
unsigned int getScreenHeight()
void setWindowPosition(int win_x, int win_y)
void displayImage(const vpImage< vpRGBa > &I)
void displayCircle(const vpImagePoint ¢er, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)
Class that defines generic functionalities for display.
int m_windowXPosition
display position
int m_windowYPosition
display position
bool m_displayHasBeenInitialized
display has been initialized
void setScale(vpScaleType scaleType, unsigned int width, unsigned int height)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
Definition of the vpImage class member functions.
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
unsigned int getWidth() const
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
static double sqr(double x)
static int round(double x)
unsigned char B
Blue component.
unsigned char R
Red component.
unsigned char G
Green component.
Defines a rectangle in the plane.
vpImagePoint getTopLeft() const
VISP_EXPORT int wait(double t0, double t)