46 #include <visp3/core/vpConfig.h> 48 #if (defined(VISP_HAVE_GTK)) 57 #include <visp3/core/vpDisplay.h> 58 #include <visp3/gui/vpDisplayGTK.h> 61 #include <visp3/core/vpDebug.h> 62 #include <visp3/core/vpDisplayException.h> 63 #include <visp3/core/vpImageConvert.h> 64 #include <visp3/core/vpImageTools.h> 65 #include <visp3/core/vpMath.h> 67 #ifndef DOXYGEN_SHOULD_SKIP_THIS 71 #include <gdk/gdkrgb.h> 73 class vpDisplayGTK::Impl
77 : m_widget(NULL), m_background(NULL), m_gc(NULL), m_blue(), m_red(), m_yellow(), m_green(), m_cyan(), m_orange(),
78 m_white(), m_black(), m_gdkcolor(), m_lightBlue(), m_darkBlue(), m_lightRed(), m_darkRed(), m_lightGreen(), m_darkGreen(),
79 m_purple(), m_lightGray(), m_gray(), m_darkGray(), m_colormap(NULL), m_font(NULL), m_vectgtk(NULL), m_col(NULL)
86 void init(
unsigned int win_width,
unsigned int win_height,
int win_x,
int win_y,
const std::string &title)
88 gint width =
static_cast<gint
>(win_width);
89 gint height =
static_cast<gint
>(win_height);
95 gtk_init(argc, &argv);
98 m_widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
100 gtk_widget_add_events(m_widget, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
102 gtk_window_set_default_size(GTK_WINDOW(m_widget), width, height);
104 gtk_window_move(GTK_WINDOW(m_widget), win_x, win_y);
106 gtk_widget_show(m_widget);
111 m_background = gdk_pixmap_new(m_widget->window, width, height, -1);
114 m_gc = gdk_gc_new(m_widget->window);
117 m_colormap = gdk_window_get_colormap(m_widget->window);
122 gdk_color_parse(
"light blue", &m_lightBlue);
123 gdk_colormap_alloc_color(m_colormap, &m_lightBlue, FALSE, TRUE);
126 gdk_color_parse(
"blue", &m_blue);
127 gdk_colormap_alloc_color(m_colormap, &m_blue, FALSE, TRUE);
130 gdk_color_parse(
"dark blue", &m_darkBlue);
131 gdk_colormap_alloc_color(m_colormap, &m_darkBlue, FALSE, TRUE);
134 gdk_color_parse(
"#FF8C8C", &m_lightRed);
135 gdk_colormap_alloc_color(m_colormap, &m_lightRed, FALSE, TRUE);
138 gdk_color_parse(
"red", &m_red);
139 gdk_colormap_alloc_color(m_colormap, &m_red, FALSE, TRUE);
142 gdk_color_parse(
"dark red", &m_darkRed);
143 gdk_colormap_alloc_color(m_colormap, &m_darkRed, FALSE, TRUE);
146 gdk_color_parse(
"light green", &m_lightGreen);
147 gdk_colormap_alloc_color(m_colormap, &m_lightGreen, FALSE, TRUE);
150 gdk_color_parse(
"green", &m_green);
151 gdk_colormap_alloc_color(m_colormap, &m_green, FALSE, TRUE);
154 gdk_color_parse(
"dark green", &m_darkGreen);
155 gdk_colormap_alloc_color(m_colormap, &m_darkGreen, FALSE, TRUE);
158 gdk_color_parse(
"yellow", &m_yellow);
159 gdk_colormap_alloc_color(m_colormap, &m_yellow, FALSE, TRUE);
162 gdk_color_parse(
"cyan", &m_cyan);
163 gdk_colormap_alloc_color(m_colormap, &m_cyan, FALSE, TRUE);
166 gdk_color_parse(
"orange", &m_orange);
167 gdk_colormap_alloc_color(m_colormap, &m_orange, FALSE, TRUE);
170 gdk_color_parse(
"purple", &m_purple);
171 gdk_colormap_alloc_color(m_colormap, &m_purple, FALSE, TRUE);
174 gdk_color_parse(
"white", &m_white);
175 gdk_colormap_alloc_color(m_colormap, &m_white, FALSE, TRUE);
178 gdk_color_parse(
"black", &m_black);
179 gdk_colormap_alloc_color(m_colormap, &m_black, FALSE, TRUE);
182 gdk_color_parse(
"#C0C0C0", &m_lightGray);
183 gdk_colormap_alloc_color(m_colormap, &m_lightGray, FALSE, TRUE);
186 gdk_color_parse(
"#808080", &m_gray);
187 gdk_colormap_alloc_color(m_colormap, &m_gray, FALSE, TRUE);
190 gdk_color_parse(
"#404040", &m_darkGray);
191 gdk_colormap_alloc_color(m_colormap, &m_darkGray, FALSE, TRUE);
195 m_font = gdk_font_load(
"-*-times-medium-r-normal-*-16-*-*-*-*-*-*-*");
197 m_font = gdk_font_load(
"-*-courier-bold-r-normal-*-*-140-*-*-*-*-*-*");
199 m_font = gdk_font_load(
"-*-courier 10 pitch-medium-r-normal-*-16-*-*-*-*-*-*-*");
202 gdk_window_set_title(m_widget->window, title.c_str());
205 void setFont(
const std::string &fontname)
207 m_font = gdk_font_load((
const gchar *)fontname.c_str());
210 void setTitle(
const std::string &title)
212 gdk_window_set_title(m_widget->window, title.c_str());
217 gtk_window_move(GTK_WINDOW(m_widget), win_x, win_y);
224 gdk_draw_gray_image(m_background, m_gc, 0, 0, width, height, GDK_RGB_DITHER_NONE, I.
bitmap, width);
228 gdk_draw_gray_image(m_background, m_gc, 0, 0, width, height, GDK_RGB_DITHER_NONE, sampled.
bitmap, width);
232 gdk_window_set_back_pixmap(m_widget->window, m_background, FALSE);
239 gdk_draw_rgb_32_image(m_background, m_gc, 0, 0, width, height, GDK_RGB_DITHER_NONE, (
unsigned char *)I.
bitmap, 4 * width);
243 gdk_draw_rgb_32_image(m_background, m_gc, 0, 0, width, height, GDK_RGB_DITHER_NONE, (
unsigned char *)sampled.
bitmap, 4 * width);
247 gdk_window_set_back_pixmap(m_widget->window, m_background, FALSE);
252 gdk_draw_gray_image(m_background, m_gc, j_min, i_min,
253 width, height, GDK_RGB_DITHER_NONE, I.
bitmap, width);
255 gdk_window_set_back_pixmap(m_widget->window, m_background, FALSE);
260 gdk_draw_rgb_32_image(m_background, m_gc, j_min, i_min, width, height, GDK_RGB_DITHER_NONE,
261 (
unsigned char *)I.
bitmap, width * 4);
264 gdk_window_set_back_pixmap(m_widget->window, m_background, FALSE);
274 if (m_widget != NULL) {
275 gdk_window_hide(m_widget->window);
276 gdk_window_destroy(m_widget->window);
277 gtk_widget_destroy(m_widget);
284 gdk_window_clear(m_widget->window);
291 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
293 m_gdkcolor.red = 256 * color.
R;
294 m_gdkcolor.green = 256 * color.
G;
295 m_gdkcolor.blue = 256 * color.
B;
296 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
297 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
303 std::cout <<
"Cannot draw string: no font is selected" << std::endl;
307 unsigned int thickness,
unsigned int scale)
310 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
312 m_gdkcolor.red = 256 * color.
R;
313 m_gdkcolor.green = 256 * color.
G;
314 m_gdkcolor.blue = 256 * color.
B;
315 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
316 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
319 gdk_gc_set_line_attributes(m_gc, static_cast<gint>(thickness), GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
322 gdk_draw_arc(m_background, m_gc, FALSE,
vpMath::round((center.
get_u() - radius) / scale),
323 vpMath::round((center.
get_v() - radius) / scale),
static_cast<gint
>(2. * radius / scale),
324 static_cast<gint>(2. * radius / scale), 23040, 23040);
326 gdk_draw_arc(m_background, m_gc, TRUE,
vpMath::round((center.
get_u() - radius) / scale),
327 vpMath::round((center.
get_v() - radius) / scale),
static_cast<gint
>(2. * radius / scale),
328 static_cast<gint>(2. * radius / scale), 23040, 23040);
334 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
336 m_gdkcolor.red = 256 * color.
R;
337 m_gdkcolor.green = 256 * color.
G;
338 m_gdkcolor.blue = 256 * color.
B;
339 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
340 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
343 gdk_gc_set_line_attributes(m_gc, static_cast<gint>(thickness), GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
346 gdk_gc_set_line_attributes(m_gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
352 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
354 m_gdkcolor.red = 256 * color.
R;
355 m_gdkcolor.green = 256 * color.
G;
356 m_gdkcolor.blue = 256 * color.
B;
357 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
358 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
361 gdk_gc_set_line_attributes(m_gc, static_cast<gint>(thickness), GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
369 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
371 m_gdkcolor.red = 256 * color.
R;
372 m_gdkcolor.green = 256 * color.
G;
373 m_gdkcolor.blue = 256 * color.
B;
374 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
375 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
378 if (thickness == 1) {
382 vpMath::round(ip.
get_v() / scale), static_cast<gint>(thickness),
static_cast<gint
>(thickness));
387 bool fill,
unsigned int thickness,
unsigned int scale)
390 gdk_gc_set_foreground(m_gc, m_col[color.
id]);
392 m_gdkcolor.red = 256 * color.
R;
393 m_gdkcolor.green = 256 * color.
G;
394 m_gdkcolor.blue = 256 * color.
B;
395 gdk_colormap_alloc_color(m_colormap, &m_gdkcolor, FALSE, TRUE);
396 gdk_gc_set_foreground(m_gc, &m_gdkcolor);
398 gdk_gc_set_line_attributes(m_gc, static_cast<gint>(thickness), GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
402 vpMath::round(topLeft.
get_v() / scale), static_cast<gint>(w / scale),
static_cast<gint
>(h / scale));
405 vpMath::round(topLeft.
get_v() / scale), static_cast<gint>(w / scale),
static_cast<gint
>(h / scale));
408 gdk_gc_set_line_attributes(m_gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_BEVEL);
416 while ((ev = gdk_event_get())) {
417 if (ev->any.window == m_widget->window && ev->type == event_type) {
418 double u = ((GdkEventButton *)ev)->x;
419 double v = ((GdkEventButton *)ev)->y;
423 switch (static_cast<int>(((GdkEventButton *)ev)->button)) {
443 }
while (ret ==
false && blocking ==
true);
450 ImageGtk = gdk_image_get(m_background, 0, 0, width, height);
455 guchar OctetRouge, OctetVert, OctetBleu, mask;
458 for (gint y = 0; y < height; y++) {
459 for (gint x = 0; x < width; x++) {
460 pixel = gdk_image_get_pixel(ImageGtk, x, y);
461 OctetBleu =
static_cast<guchar
>(pixel) & mask;
462 OctetVert =
static_cast<guchar
>(pixel >> 8) & mask;
463 OctetRouge =
static_cast<guchar
>(pixel >> 16) & mask;
464 I[y][x].R = OctetRouge;
465 I[y][x].G = OctetVert;
466 I[y][x].B = OctetBleu;
474 return static_cast<unsigned int>(gdk_window_get_visual(m_widget->window)->depth);
483 while ((ev = gdk_event_get()) != NULL) {
488 if (ev->any.window == m_widget->window && ev->type == GDK_KEY_PRESS) {
491 key = gdk_keyval_name(ev->key.keyval);
501 }
while (ret ==
false && blocking ==
true);
509 if ((ev = gdk_event_get())) {
510 if (ev->any.window == m_widget->window && ev->type == GDK_MOTION_NOTIFY) {
511 double u = ((GdkEventMotion *)ev)->x;
512 double v = ((GdkEventMotion *)ev)->y;
526 gdk_window_get_pointer(m_widget->window, &u, &v, NULL);
527 ip.
set_u(static_cast<double>(u) * scale);
528 ip.
set_v(static_cast<double>(v) * scale);
531 void getScreenSize(
bool is_init,
unsigned int &w,
unsigned int &h)
537 gtk_init(argc, &argv);
539 GtkWidget *widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
540 gtk_window_set_default_size(GTK_WINDOW(widget_), 100, 100);
541 gtk_widget_show(widget_);
543 GdkScreen *screen_ = gdk_window_get_screen(widget_->window);
544 w =
static_cast<unsigned int>(gdk_screen_get_width(screen_));
545 h =
static_cast<unsigned int>(gdk_screen_get_height(screen_));
546 gtk_widget_destroy(widget_);
548 GdkScreen *screen_ = gdk_window_get_screen(m_widget->window);
549 w =
static_cast<unsigned int>(gdk_screen_get_width(screen_));
550 h =
static_cast<unsigned int>(gdk_screen_get_height(screen_));
556 GdkPixmap *m_background;
558 GdkColor m_blue, m_red, m_yellow, m_green, m_cyan, m_orange, m_white, m_black, m_gdkcolor;
559 GdkColor m_lightBlue, m_darkBlue, m_lightRed, m_darkRed, m_lightGreen, m_darkGreen, m_purple;
560 GdkColor m_lightGray, m_gray, m_darkGray;
561 GdkColormap *m_colormap;
568 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS 625 init(I, win_x, win_y, win_title);
679 init(I, win_x, win_y, win_title);
764 if (!win_title.empty())
794 if (!win_title.empty())
812 void vpDisplayGTK::init(
unsigned int win_width,
unsigned int win_height,
int win_x,
int win_y,
const std::string &win_title)
845 m_impl->setFont(fontname);
856 if (!title.empty()) {
857 m_impl->setTitle(title);
876 m_impl->setWindowPosition(win_x, win_y);
927 int i_min = (std::max)(static_cast<int>(ceil(iP.
get_i() /
m_scale)), 0);
928 int j_min = (std::max)(static_cast<int>(ceil(iP.
get_j() /
m_scale)), 0);
930 m_impl->displayImageROI(Itemp, static_cast<gint>(j_min), static_cast<gint>(i_min),
931 static_cast<gint>(Itemp.getWidth()), static_cast<gint>(Itemp.getHeight()));
982 int i_min = (std::max)(static_cast<int>(ceil(iP.
get_i() /
m_scale)), 0);
983 int j_min = (std::max)(static_cast<int>(ceil(iP.
get_j() /
m_scale)), 0);
985 m_impl->displayImageROI(Itemp, static_cast<gint>(j_min), static_cast<gint>(i_min),
986 static_cast<gint>(Itemp.getWidth()), static_cast<gint>(Itemp.getHeight()));
1007 m_impl->closeDisplay();
1020 m_impl->flushDisplay();
1031 const unsigned int )
1034 m_impl->flushDisplay();
1053 unsigned int h,
unsigned int thickness)
1060 if ((std::fabs(a) > std::numeric_limits<double>::epsilon()) &&
1061 (std::fabs(b) > std::numeric_limits<double>::epsilon())) {
1103 m_impl->displayCharString(ip, text, color,
m_scale);
1118 unsigned int thickness)
1124 m_impl->displayCircle(center, radius, color, fill, thickness,
m_scale);
1139 double i = ip.
get_i();
1140 double j = ip.
get_j();
1143 ip1.
set_i(i - size / 2);
1145 ip2.
set_i(i + size / 2);
1150 ip1.
set_j(j - size / 2);
1152 ip2.
set_j(j + size / 2);
1168 unsigned int thickness)
1175 m_impl->displayDotLine(ip1, ip2, color, thickness,
m_scale);
1188 unsigned int thickness)
1194 m_impl->displayLine(ip1, ip2, color, thickness,
m_scale);
1209 m_impl->displayPoint(ip, color, thickness,
m_scale);
1229 bool fill,
unsigned int thickness)
1235 m_impl->displayRectangle(topLeft, w, h, color, fill, thickness,
m_scale);
1254 bool fill,
unsigned int thickness)
1263 m_impl->displayRectangle(topLeft, w, h, color, fill, thickness,
m_scale);
1290 m_impl->displayRectangle(topLeft, w, h, color, fill, thickness,
m_scale);
1318 ret = m_impl->getClick(ip, button, blocking,
m_scale, GDK_BUTTON_PRESS);
1347 ret = m_impl->getClick(ip, button, blocking,
m_scale, GDK_BUTTON_PRESS);
1378 ret = m_impl->getClick(ip, button, blocking,
m_scale, GDK_BUTTON_PRESS);
1413 ret = m_impl->getClick(ip, button, blocking,
m_scale, GDK_BUTTON_RELEASE);
1428 m_impl->getImage(I, static_cast<gint>(
m_width), static_cast<gint>(
m_height));
1441 unsigned int depth = m_impl->getScreenDepth();
1467 ret = m_impl->getKeyboardEvent(key, blocking);
1498 ret = m_impl->getKeyboardEvent(key, blocking);
1522 ret = m_impl->getPointerMotionEvent(ip,
m_scale);
1542 m_impl->getPointerPosition(ip,
m_scale);
1566 unsigned int width, height;
1576 unsigned int width, height;
1581 #elif !defined(VISP_BUILD_SHARED_LIBS) 1584 void dummy_vpDisplayGTK(){};
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
VISP_EXPORT int wait(double t0, double t)
void setWindowPosition(int win_x, int win_y)
int m_windowYPosition
display position
void displayCircle(const vpImagePoint ¢er, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
Class that defines generic functionnalities for display.
bool getClick(bool blocking=true)
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
unsigned char B
Blue component.
Type * bitmap
points toward the bitmap
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
Class to define RGB colors available for display functionnalities.
void flushDisplayROI(const vpImagePoint &iP, unsigned int width, unsigned int height)
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
unsigned char G
Green component.
bool m_displayHasBeenInitialized
display has been initialized
vpImagePoint getTopLeft() const
void getScreenSize(unsigned int &screen_width, unsigned int &screen_height)
void displayPoint(const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
int m_windowXPosition
display position
void setScale(vpScaleType scaleType, unsigned int width, unsigned int height)
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
static double sqr(double x)
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
bool getPointerPosition(vpImagePoint &ip)
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
void displayImage(const vpImage< vpRGBa > &I)
unsigned int getScreenWidth()
bool getPointerMotionEvent(vpImagePoint &ip)
void getImage(vpImage< vpRGBa > &I)
get the window pixmap and put it in vpRGBa image
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)
static int round(double x)
void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)
Error that can be emited by the vpDisplay class and its derivates.
void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
unsigned int getHeight() const
void setFont(const std::string &fontname)
void setTitle(const std::string &win_title)
unsigned char R
Red component.
unsigned int getScreenDepth()
get the window depth (8,16,24,32)
Defines a rectangle in the plane.
unsigned int getScreenHeight()
void clearDisplay(const vpColor &color=vpColor::white)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
bool getKeyboardEvent(bool blocking=true)
unsigned int getWidth() const
void displayImageROI(const vpImage< unsigned char > &I, const vpImagePoint &iP, unsigned int width, unsigned int height)