Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpDisplayGTK.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Image display.
32  *
33  * Authors:
34  * Christophe Collewet
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 
46 #include <visp3/core/vpConfig.h>
47 
48 #if ( defined(VISP_HAVE_GTK) )
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <iostream>
53 #include <cmath> // std::fabs
54 #include <limits> // numeric_limits
55 
56 // Display stuff
57 #include <visp3/core/vpDisplay.h>
58 #include <visp3/gui/vpDisplayGTK.h>
59 
60 //debug / exception
61 #include <visp3/core/vpDebug.h>
62 #include <visp3/core/vpDisplayException.h>
63 #include <visp3/core/vpMath.h>
64 #include <visp3/core/vpImageTools.h>
65 
83  : widget(NULL), m_background(NULL), m_gc(NULL),
84  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
85  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
86  purple(), lightGray(), gray(), darkGray(),
87  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
88 {
89  setScale(scaleType, I.getWidth(), I.getHeight());
90  init(I) ;
91 }
92 
112  int x,
113  int y,
114  const std::string &title, vpScaleType scaleType)
115  : widget(NULL), m_background(NULL), m_gc(NULL),
116  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
117  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
118  purple(), lightGray(), gray(), darkGray(),
119  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
120 {
121  setScale(scaleType, I.getWidth(), I.getHeight());
122  init(I, x, y, title) ;
123 }
124 
140  : widget(NULL), m_background(NULL), m_gc(NULL),
141  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
142  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
143  purple(), lightGray(), gray(), darkGray(),
144  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
145 {
146  setScale(scaleType, I.getWidth(), I.getHeight());
147  init(I) ;
148 }
149 
167  int x,
168  int y,
169  const std::string &title, vpScaleType scaleType)
170  : widget(NULL), m_background(NULL), m_gc(NULL),
171  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
172  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
173  purple(), lightGray(), gray(), darkGray(),
174  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
175 {
176  setScale(scaleType, I.getWidth(), I.getHeight());
177  init(I, x, y, title) ;
178 }
179 
202 vpDisplayGTK::vpDisplayGTK(int x, int y, const std::string &title)
203  : widget(NULL), m_background(NULL), m_gc(NULL),
204  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
205  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
206  purple(), lightGray(), gray(), darkGray(),
207  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
208 {
209  m_windowXPosition = x ;
210  m_windowYPosition = y ;
211  m_title = title;
212 }
213 
234  : vpDisplay(), widget(NULL), m_background(NULL), m_gc(NULL),
235  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
236  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
237  purple(), lightGray(), gray(), darkGray(),
238  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
239 
240 {
241 }
242 
247 {
248  closeDisplay() ;
249 }
250 
259 void
261  int x,
262  int y,
263  const std::string &title)
264 {
265  if ((I.getHeight() == 0) || (I.getWidth()==0))
266  {
268  "Image not initialized")) ;
269  }
270 
271  if (x != -1)
272  m_windowXPosition = x ;
273  if (y != -1)
274  m_windowYPosition = y ;
275 
276  if (! title.empty())
277  m_title = title;
278 
281 
282  I.display = this ;
284 }
285 
295 void
297  int x,
298  int y,
299  const std::string &title)
300 {
301  if ((I.getHeight() == 0) || (I.getWidth()==0))
302  {
304  "Image not initialized")) ;
305  }
306 
307  if (x != -1)
308  m_windowXPosition = x ;
309  if (y != -1)
310  m_windowYPosition = y ;
311 
312  if (! title.empty())
313  m_title = title;
314 
317 
318  I.display = this ;
320 }
329 void
330 vpDisplayGTK::init(unsigned int w, unsigned int h,
331  int x, int y,
332  const std::string &title)
333 {
334  /* Initialisation of thegdk et gdk_rgb library */
335  int *argc=NULL ;
336  char **argv ;
337 
338  gtk_init(argc, &argv);
339 
340  setScale(m_scaleType, w, h);
341 
342  m_width = w/m_scale;
343  m_height = h/m_scale;
344 
345  /* Create the window*/
346  widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
347 
348  gtk_widget_add_events(widget, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
349  | GDK_POINTER_MOTION_MASK);
350 
351  gtk_window_set_default_size(GTK_WINDOW(widget), (gint)m_width, (gint)m_height);
352 
353  if (x != -1)
354  m_windowXPosition = x ;
355  if (y != -1)
356  m_windowYPosition = y ;
357 
358  gtk_window_move(GTK_WINDOW(widget), m_windowXPosition, m_windowYPosition);
359 
360  gtk_widget_show(widget);
361 
362  gdk_rgb_init();
363 
364  /* Create background pixmap */
365  m_background = gdk_pixmap_new(widget->window, (gint)m_width, (gint)m_height, -1);
366 
367  /* Create graphic context */
368  m_gc = gdk_gc_new(widget->window);
369 
370  /* get the colormap */
371  colormap = gdk_window_get_colormap(widget->window);
372 
373  col = new GdkColor *[vpColor::id_unknown] ; // id_unknown = number of predefined colors
374 
375  /* Create color */
376  gdk_color_parse("light blue",&lightBlue);
377  gdk_colormap_alloc_color(colormap,&lightBlue,FALSE,TRUE);
378  col[vpColor::id_lightBlue] = &lightBlue ;
379 
380  gdk_color_parse("blue",&blue);
381  gdk_colormap_alloc_color(colormap,&blue,FALSE,TRUE);
382  col[vpColor::id_blue] = &blue ;
383 
384  gdk_color_parse("dark blue",&darkBlue);
385  gdk_colormap_alloc_color(colormap,&darkBlue,FALSE,TRUE);
386  col[vpColor::id_darkBlue] = &darkBlue ;
387 
388  gdk_color_parse("#FF8C8C",&lightRed);
389  gdk_colormap_alloc_color(colormap,&lightRed,FALSE,TRUE);
390  col[vpColor::id_lightRed] = &lightRed ;
391 
392  gdk_color_parse("red",&red);
393  gdk_colormap_alloc_color(colormap,&red,FALSE,TRUE);
394  col[vpColor::id_red] = &red ;
395 
396  gdk_color_parse("dark red",&darkRed);
397  gdk_colormap_alloc_color(colormap,&darkRed,FALSE,TRUE);
398  col[vpColor::id_darkRed] = &darkRed ;
399 
400  gdk_color_parse("light green",&lightGreen);
401  gdk_colormap_alloc_color(colormap,&lightGreen,FALSE,TRUE);
402  col[vpColor::id_lightGreen] = &lightGreen ;
403 
404  gdk_color_parse("green",&green);
405  gdk_colormap_alloc_color(colormap,&green,FALSE,TRUE);
406  col[vpColor::id_green] = &green ;
407 
408  gdk_color_parse("dark green",&darkGreen);
409  gdk_colormap_alloc_color(colormap,&darkGreen,FALSE,TRUE);
410  col[vpColor::id_darkGreen] = &darkGreen ;
411 
412  gdk_color_parse("yellow",&yellow);
413  gdk_colormap_alloc_color(colormap,&yellow,FALSE,TRUE);
414  col[vpColor::id_yellow] = &yellow ;
415 
416  gdk_color_parse("cyan",&cyan);
417  gdk_colormap_alloc_color(colormap,&cyan,FALSE,TRUE);
418  col[vpColor::id_cyan] = &cyan ;
419 
420  gdk_color_parse("orange",&orange);
421  gdk_colormap_alloc_color(colormap,&orange,FALSE,TRUE);
422  col[vpColor::id_orange] = &orange ;
423 
424  gdk_color_parse("purple",&purple);
425  gdk_colormap_alloc_color(colormap,&purple,FALSE,TRUE);
426  col[vpColor::id_purple] = &purple ;
427 
428  gdk_color_parse("white",&white);
429  gdk_colormap_alloc_color(colormap,&white,FALSE,TRUE);
430  col[vpColor::id_white] = &white ;
431 
432  gdk_color_parse("black",&black);
433  gdk_colormap_alloc_color(colormap,&black,FALSE,TRUE);
434  col[vpColor::id_black] = &black ;
435 
436  gdk_color_parse("#C0C0C0",&lightGray);
437  gdk_colormap_alloc_color(colormap,&lightGray,FALSE,TRUE);
438  col[vpColor::id_lightGray] = &lightGray ;
439 
440  gdk_color_parse("#808080",&gray);
441  gdk_colormap_alloc_color(colormap,&gray,FALSE,TRUE);
442  col[vpColor::id_gray] = &gray ;
443 
444  gdk_color_parse("#404040",&darkGray);
445  gdk_colormap_alloc_color(colormap,&darkGray,FALSE,TRUE);
446  col[vpColor::id_darkGray] = &darkGray ;
447 
448  // Try to load a default font
449  font = gdk_font_load("-*-times-medium-r-normal-*-16-*-*-*-*-*-*-*");
450  if (font == NULL)
451  font = gdk_font_load("-*-courier-bold-r-normal-*-*-140-*-*-*-*-*-*");
452  if (font == NULL)
453  font = gdk_font_load("-*-courier 10 pitch-medium-r-normal-*-16-*-*-*-*-*-*-*");
454 
455  m_title = title;
456  if(!title.empty())
457  gdk_window_set_title(widget->window, m_title.c_str());
458 
460 }
461 
462 
477 void
478 vpDisplayGTK::setFont(const std::string &fontname)
479 {
480  font = gdk_font_load((const gchar*)fontname.c_str());
481 }
482 
487 void
488 vpDisplayGTK::setTitle(const std::string &title)
489 {
491  {
492  m_title = title;
493  if(!title.empty())
494  gdk_window_set_title(widget->window, m_title.c_str());
495  }
496  else
497  {
499  "GTK not initialized")) ;
500  }
501 }
502 
511 void vpDisplayGTK::setWindowPosition(int winx, int winy)
512 {
513 
515  gtk_window_move(GTK_WINDOW(widget), winx, winy);
516  }
517  else
518  {
520  "GTK not initialized")) ;
521  }
522 }
523 
524 
537 {
538 
540  {
541  if(m_scale == 1) {
542  /* Copie de l'image dans le pixmap fond */
543  gdk_draw_gray_image(m_background,
544  m_gc, 0, 0, (gint)m_width, (gint)m_height,
545  GDK_RGB_DITHER_NONE,
546  I.bitmap,
547  (gint)m_width);
548  }
549  else{
550  vpImage<unsigned char> sampled;
551  I.subsample(m_scale, m_scale, sampled);
552  gdk_draw_gray_image(m_background,
553  m_gc, 0, 0, (gint)m_width, (gint)m_height,
554  GDK_RGB_DITHER_NONE,
555  sampled.bitmap,
556  (gint)m_width);
557  }
558 
559  /* Le pixmap background devient le fond de la zone de dessin */
560  gdk_window_set_back_pixmap(widget->window, m_background, FALSE);
561  }
562  else
563  {
565  "GTK not initialized")) ;
566  }
567 }
568 
569 
587 void vpDisplayGTK::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int w, const unsigned int h )
588 {
590  {
592  vpImageTools::crop(I, iP.get_i(), iP.get_j(), h, w, Itemp, m_scale, m_scale);
593 
594  /* Copie de l'image dans le pixmap fond */
595  int i_min = std::max((int)ceil(iP.get_i()/m_scale), 0);
596  int j_min = std::max((int)ceil(iP.get_j()/m_scale), 0);
597 
598  gdk_draw_gray_image(m_background,
599  m_gc, (gint)j_min, (gint)i_min, (gint)Itemp.getWidth(), (gint)Itemp.getHeight(),
600  GDK_RGB_DITHER_NONE,
601  Itemp.bitmap,
602  (gint)Itemp.getWidth());
603 
604  /* Le pixmap background devient le fond de la zone de dessin */
605  gdk_window_set_back_pixmap(widget->window, m_background, FALSE);
606  }
607  else
608  {
610  "GTK not initialized")) ;
611  }
612 }
613 
614 
627 {
628 
630  {
631  if (m_scale == 1) {
632  /* Copie de l'image dans le pixmap fond */
633  gdk_draw_rgb_32_image(m_background,
634  m_gc, 0, 0, (gint)m_width, (gint)m_height,
635  GDK_RGB_DITHER_NONE,
636  (unsigned char *)I.bitmap,
637  (gint)(4*m_width));
638  }
639  else {
640  vpImage<vpRGBa> sampled;
641  I.subsample(m_scale, m_scale, sampled);
642  gdk_draw_rgb_32_image(m_background,
643  m_gc, 0, 0, (gint)m_width, (gint)m_height,
644  GDK_RGB_DITHER_NONE,
645  (unsigned char *)sampled.bitmap,
646  (gint)(4*m_width));
647  }
648  /* Permet de fermer la fenetre si besoin (cas des sequences d'images) */
649  //while (g_main_iteration(FALSE));
650 
651  /* Le pixmap background devient le fond de la zone de dessin */
652  gdk_window_set_back_pixmap(widget->window, m_background, FALSE);
653  }
654  else
655  {
657  "GTK not initialized")) ;
658  }
659 }
660 
678 void vpDisplayGTK::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int w, const unsigned int h )
679 {
681  {
682  vpImage<vpRGBa> Itemp;
683  vpImageTools::crop(I, iP.get_i(), iP.get_j(), h, w, Itemp, m_scale, m_scale);
684 
685  /* Copie de l'image dans le pixmap fond */
686  int i_min = std::max((int)ceil(iP.get_i()/m_scale), 0);
687  int j_min = std::max((int)ceil(iP.get_j()/m_scale), 0);
688 
689  gdk_draw_rgb_32_image(m_background,
690  m_gc, (gint)j_min, (gint)i_min, (gint)Itemp.getWidth(), (gint)Itemp.getHeight(),
691  GDK_RGB_DITHER_NONE,
692  (unsigned char *)Itemp.bitmap,
693  (gint)Itemp.getWidth()*4);
694 
695  /* Le pixmap background devient le fond de la zone de dessin */
696  gdk_window_set_back_pixmap(widget->window, m_background, FALSE);
697  }
698  else
699  {
701  "GTK not initialized")) ;
702  }
703 }
704 
710 void vpDisplayGTK::displayImage(const unsigned char * /* I */)
711 {
712  vpTRACE(" not implemented ") ;
713 }
714 
721 {
722  if (col != NULL)
723  {
724  delete [] col ; col = NULL ;
725  }
726 
727  if (widget != NULL)
728  {
729  gdk_window_hide (widget->window);
730  gdk_window_destroy(widget->window);
731  gtk_widget_destroy(widget);
732  widget = NULL;
733  }
735 }
736 
737 
743 {
745  {
746  gdk_window_clear(widget->window);
747  gdk_flush();
748  }
749  else
750  {
752  "GTK not initialized")) ;
753  }
754 }
755 
756 
761 void vpDisplayGTK::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
762 {
764  {
765  gdk_window_clear(widget->window);
766  gdk_flush();
767  }
768  else
769  {
771  "GTK not initialized")) ;
772  }
773 }
774 
775 
779 void vpDisplayGTK::clearDisplay(const vpColor & /* color */)
780 {
781  vpTRACE("Not implemented") ;
782 }
783 
792  const vpImagePoint &ip2,
793  const vpColor &color,
794  unsigned int w, unsigned int h,
795  unsigned int thickness)
796 {
798  {
799  double a = ip2.get_i() - ip1.get_i() ;
800  double b = ip2.get_j() - ip1.get_j() ;
801  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
802 
803  //if ((a==0)&&(b==0))
804  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon() )&&(std::fabs(b) <= std::numeric_limits<double>::epsilon()) )
805  {
806  // DisplayCrossLarge(i1,j1,3,col) ;
807  }
808  else
809  {
810  a /= lg ;
811  b /= lg ;
812 
813  vpImagePoint ip3;
814  ip3.set_i(ip2.get_i() - w*a);
815  ip3.set_j(ip2.get_j() - w*b);
816 
817  vpImagePoint ip4;
818  ip4.set_i( ip3.get_i() - b*h );
819  ip4.set_j( ip3.get_j() + a*h );
820 
821  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
822  displayLine ( ip2, ip4, color, thickness ) ;
823 
824  ip4.set_i( ip3.get_i() + b*h );
825  ip4.set_j( ip3.get_j() - a*h );
826 
827  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
828  displayLine ( ip2, ip4, color, thickness ) ;
829 
830  displayLine ( ip1, ip2, color, thickness ) ;
831  }
832  }
833  else
834  {
836  "GTK not initialized")) ;
837  }
838 }
839 
840 
853  const char *text,
854  const vpColor &color )
855 {
857  {
858  if (color.id < vpColor::id_unknown)
859  gdk_gc_set_foreground(m_gc, col[color.id]);
860  else {
861  gdkcolor.red = 256 * color.R;
862  gdkcolor.green = 256 * color.G;
863  gdkcolor.blue = 256 * color.B;
864  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
865  gdk_gc_set_foreground(m_gc, &gdkcolor);
866  }
867  if (font != NULL)
868  gdk_draw_string(m_background, font, m_gc,
869  vpMath::round( ip.get_u()/m_scale ),
870  vpMath::round( ip.get_v()/m_scale ),
871  (const gchar *)text);
872  else
873  std::cout << "Cannot draw string: no font is selected" << std::endl;
874  }
875  else
876  {
878  "GTK not initialized")) ;
879  }
880 }
891  unsigned int radius,
892  const vpColor &color,
893  bool fill,
894  unsigned int thickness )
895 {
897  {
898  if ( thickness == 1 ) thickness = 0;
899 
900  if (color.id < vpColor::id_unknown)
901  gdk_gc_set_foreground(m_gc, col[color.id]);
902  else {
903  gdkcolor.red = 256 * color.R;
904  gdkcolor.green = 256 * color.G;
905  gdkcolor.blue = 256 * color.B;
906  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
907  gdk_gc_set_foreground(m_gc, &gdkcolor);
908  }
909 
910  gdk_gc_set_line_attributes(m_gc, (gint)thickness,
911  GDK_LINE_SOLID, GDK_CAP_BUTT,
912  GDK_JOIN_BEVEL) ;
913 
914  if (fill == false)
915  gdk_draw_arc(m_background, m_gc, FALSE,
916  vpMath::round( (center.get_u()-radius)/m_scale ),
917  vpMath::round( (center.get_v()-radius)/m_scale ),
918  (gint)(2.*radius/m_scale), (gint)(2.*radius/m_scale), 23040, 23040) ; /* 23040 = 360*64 */
919  else
920  gdk_draw_arc(m_background, m_gc, TRUE,
921  vpMath::round( (center.get_u()-radius)/m_scale ),
922  vpMath::round( (center.get_v()-radius)/m_scale ),
923  (gint)(2.*radius/m_scale), (gint)(2.*radius/m_scale), 23040, 23040) ; /* 23040 = 360*64 */
924  }
925  else
926  {
928  "GTK not initialized")) ;
929  }
930 }
939  unsigned int size,
940  const vpColor &color,
941  unsigned int thickness)
942 {
944  {
945  double i = ip.get_i();
946  double j = ip.get_j();
947  vpImagePoint ip1, ip2;
948 
949  ip1.set_i( i-size/2 );
950  ip1.set_j( j );
951  ip2.set_i( i+size/2 );
952  ip2.set_j( j );
953  displayLine ( ip1, ip2, color, thickness ) ;
954 
955  ip1.set_i( i );
956  ip1.set_j( j-size/2 );
957  ip2.set_i( i );
958  ip2.set_j( j+size/2 );
959 
960  displayLine ( ip1, ip2, color, thickness ) ;
961  }
962 
963  else
964  {
966  "GTK not initialized")) ;
967  }
968 }
976  const vpImagePoint &ip2,
977  const vpColor &color,
978  unsigned int thickness )
979 {
980 
982  {
983  if ( thickness == 1 ) thickness = 0;
984 
985  if (color.id < vpColor::id_unknown)
986  gdk_gc_set_foreground(m_gc, col[color.id]);
987  else {
988  gdkcolor.red = 256 * color.R;
989  gdkcolor.green = 256 * color.G;
990  gdkcolor.blue = 256 * color.B;
991  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
992  gdk_gc_set_foreground(m_gc, &gdkcolor);
993  }
994 
995  gdk_gc_set_line_attributes(m_gc, (gint)thickness,
996  GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT,
997  GDK_JOIN_BEVEL) ;
998  gdk_draw_line(m_background, m_gc,
999  vpMath::round( ip1.get_u()/m_scale ),
1000  vpMath::round( ip1.get_v()/m_scale ),
1001  vpMath::round( ip2.get_u()/m_scale ),
1002  vpMath::round( ip2.get_v()/m_scale ) );
1003  gdk_gc_set_line_attributes(m_gc, 0,
1004  GDK_LINE_SOLID, GDK_CAP_BUTT,
1005  GDK_JOIN_BEVEL) ;
1006  }
1007  else
1008  {
1010  "GTK not initialized")) ;
1011  }
1012 }
1013 
1021  const vpImagePoint &ip2,
1022  const vpColor &color,
1023  unsigned int thickness )
1024 {
1026  {
1027  if ( thickness == 1 ) thickness = 0;
1028 
1029  if (color.id < vpColor::id_unknown)
1030  gdk_gc_set_foreground(m_gc, col[color.id]);
1031  else {
1032  gdkcolor.red = 256 * color.R;
1033  gdkcolor.green = 256 * color.G;
1034  gdkcolor.blue = 256 * color.B;
1035  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1036  gdk_gc_set_foreground(m_gc, &gdkcolor);
1037  }
1038 
1039  gdk_gc_set_line_attributes(m_gc, (gint)thickness,
1040  GDK_LINE_SOLID, GDK_CAP_BUTT,
1041  GDK_JOIN_BEVEL) ;
1042  gdk_draw_line(m_background, m_gc,
1043  vpMath::round( ip1.get_u()/m_scale ),
1044  vpMath::round( ip1.get_v()/m_scale ),
1045  vpMath::round( ip2.get_u()/m_scale ),
1046  vpMath::round( ip2.get_v()/m_scale ) );
1047  }
1048  else
1049  {
1051  "GTK not initialized")) ;
1052  }
1053 }
1054 
1061 void vpDisplayGTK::displayPoint ( const vpImagePoint &ip, const vpColor &color, unsigned int thickness )
1062 {
1064  {
1065  if (color.id < vpColor::id_unknown)
1066  gdk_gc_set_foreground(m_gc, col[color.id]);
1067  else {
1068  gdkcolor.red = 256 * color.R;
1069  gdkcolor.green = 256 * color.G;
1070  gdkcolor.blue = 256 * color.B;
1071  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1072  gdk_gc_set_foreground(m_gc, &gdkcolor);
1073  }
1074 
1075  if (thickness == 1) {
1076  gdk_draw_point(m_background, m_gc,
1077  vpMath::round( ip.get_u()/m_scale ),
1078  vpMath::round( ip.get_v()/m_scale ) );
1079  }
1080  else {
1081  gdk_draw_rectangle( m_background, m_gc, TRUE,
1082  vpMath::round( ip.get_u()/m_scale ),
1083  vpMath::round( ip.get_v()/m_scale ),
1084  thickness, thickness );
1085  }
1086  }
1087  else
1088  {
1090  "GTK not initialized")) ;
1091  }
1092 }
1093 
1094 
1108 void
1110  unsigned int w, unsigned int h,
1111  const vpColor &color, bool fill,
1112  unsigned int thickness )
1113 {
1115  {
1116  if ( thickness == 1 ) thickness = 0;
1117 
1118  if (color.id < vpColor::id_unknown)
1119  gdk_gc_set_foreground(m_gc, col[color.id]);
1120  else {
1121  gdkcolor.red = 256 * color.R;
1122  gdkcolor.green = 256 * color.G;
1123  gdkcolor.blue = 256 * color.B;
1124  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1125  gdk_gc_set_foreground(m_gc, &gdkcolor);
1126  }
1127  gdk_gc_set_line_attributes(m_gc, (gint)thickness,
1128  GDK_LINE_SOLID, GDK_CAP_BUTT,
1129  GDK_JOIN_BEVEL) ;
1130 
1131  if (fill == false)
1132  gdk_draw_rectangle(m_background, m_gc, FALSE,
1133  vpMath::round( topLeft.get_u()/m_scale ),
1134  vpMath::round( topLeft.get_v()/m_scale ),
1135  (gint)w/m_scale, (gint)h/m_scale);
1136  else
1137  gdk_draw_rectangle(m_background, m_gc, TRUE,
1138  vpMath::round( topLeft.get_u()/m_scale ),
1139  vpMath::round( topLeft.get_v()/m_scale ),
1140  (gint)w/m_scale, (gint)h/m_scale);
1141 
1142  if (thickness > 1)
1143  gdk_gc_set_line_attributes(m_gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1144  GDK_JOIN_BEVEL) ;
1145  }
1146  else
1147  {
1149  "GTK not initialized")) ;
1150  }
1151 }
1152 
1165 void
1167  const vpImagePoint &bottomRight,
1168  const vpColor &color, bool fill,
1169  unsigned int thickness )
1170 {
1172  {
1173  if ( thickness == 1 ) thickness = 0;
1174 
1175  if (color.id < vpColor::id_unknown)
1176  gdk_gc_set_foreground(m_gc, col[color.id]);
1177  else {
1178  gdkcolor.red = 256 * color.R;
1179  gdkcolor.green = 256 * color.G;
1180  gdkcolor.blue = 256 * color.B;
1181  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1182  gdk_gc_set_foreground(m_gc, &gdkcolor);
1183  }
1184 
1185  gdk_gc_set_line_attributes(m_gc, (gint)thickness,
1186  GDK_LINE_SOLID, GDK_CAP_BUTT,
1187  GDK_JOIN_BEVEL) ;
1188 
1189  int w = vpMath::round( bottomRight.get_u() - topLeft.get_u() );
1190  int h = vpMath::round( bottomRight.get_v() - topLeft.get_v() );
1191 
1192  if (fill == false)
1193  gdk_draw_rectangle(m_background, m_gc, FALSE,
1194  vpMath::round( topLeft.get_u()/m_scale ),
1195  vpMath::round( topLeft.get_v()/m_scale ),
1196  w/m_scale,h/m_scale);
1197  else
1198  gdk_draw_rectangle(m_background, m_gc, TRUE,
1199  vpMath::round( topLeft.get_u()/m_scale ),
1200  vpMath::round( topLeft.get_v()/m_scale ),
1201  w/m_scale, h/m_scale);
1202 
1203  if (thickness > 1)
1204  gdk_gc_set_line_attributes(m_gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1205  GDK_JOIN_BEVEL) ;
1206  }
1207  else
1208  {
1210  "GTK not initialized")) ;
1211  }
1212 }
1213 
1226 void
1228  const vpColor &color, bool fill,
1229  unsigned int thickness )
1230 {
1232  {
1233  if (color.id < vpColor::id_unknown)
1234  gdk_gc_set_foreground(m_gc, col[color.id]);
1235  else {
1236  gdkcolor.red = 256 * color.R;
1237  gdkcolor.green = 256 * color.G;
1238  gdkcolor.blue = 256 * color.B;
1239  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1240  gdk_gc_set_foreground(m_gc, &gdkcolor);
1241  }
1242 
1243  if ( thickness == 1 ) thickness = 0;
1244 
1245  gdk_gc_set_line_attributes(m_gc, (gint)thickness, GDK_LINE_SOLID, GDK_CAP_BUTT,
1246  GDK_JOIN_BEVEL) ;
1247 
1248  if (fill == false)
1249  gdk_draw_rectangle(m_background, m_gc, FALSE,
1250  vpMath::round( rectangle.getLeft()/m_scale ),
1251  vpMath::round( rectangle.getTop()/m_scale ),
1252  vpMath::round( rectangle.getWidth()/m_scale ),
1253  vpMath::round( rectangle.getHeight()/m_scale ) );
1254 
1255  else
1256  gdk_draw_rectangle(m_background, m_gc, TRUE,
1257  vpMath::round( rectangle.getLeft()/m_scale ),
1258  vpMath::round( rectangle.getTop()/m_scale ),
1259  vpMath::round( rectangle.getWidth()/m_scale ),
1260  vpMath::round( rectangle.getHeight()/m_scale ) );
1261 
1262  if (thickness > 1)
1263  gdk_gc_set_line_attributes(m_gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1264  GDK_JOIN_BEVEL) ;
1265  }
1266  else
1267  {
1269  "GTK not initialized")) ;
1270  }
1271 }
1272 
1273 
1289 bool
1291 {
1292  bool ret = false;
1293 
1295 
1296  // flushDisplay() ;
1297  //int cpt =0;
1298  do {
1299  GdkEvent *ev = NULL;
1300  while ((ev = gdk_event_get())!=NULL){
1301  //cpt++;
1302  // printf("event %d type %d on window %p My window %p\n",
1303  //cpt, ev->type, ev->any.window, widget->window);
1304 
1305  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1306  ret = true ;
1307  //printf("Click detection\n");
1308  }
1309  gdk_event_free(ev) ;
1310  }
1311  if (blocking){
1312  flushDisplay();
1313  vpTime::wait(100);
1314  }
1315  } while ( ret == false && blocking == true);
1316  }
1317  else {
1319  "GTK not initialized")) ;
1320  }
1321  return ret;
1322 }
1323 
1340 bool
1342 {
1343  bool ret = false;
1344 
1346 
1347  double u, v ;
1348  do {
1349  GdkEvent *ev = NULL;
1350  while ((ev = gdk_event_get())!=NULL){
1351  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS) {
1352  u = ((GdkEventButton *)ev)->x ;
1353  v = ((GdkEventButton *)ev)->y ;
1354  ip.set_u( u*m_scale );
1355  ip.set_v( v*m_scale );
1356  ret = true ;
1357  }
1358  gdk_event_free(ev) ;
1359  }
1360  if (blocking){
1361  flushDisplay();
1362  vpTime::wait(100);
1363  }
1364  } while ( ret == false && blocking == true);
1365  }
1366  else {
1368  "GTK not initialized")) ;
1369  }
1370  return ret ;
1371 }
1372 
1373 
1393 bool
1396  bool blocking)
1397 {
1398  bool ret = false;
1399 
1401  double u, v ;
1402  do {
1403  GdkEvent *ev = NULL;
1404  while ((ev = gdk_event_get())){
1405  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1406  u = ((GdkEventButton *)ev)->x ;
1407  v = ((GdkEventButton *)ev)->y ;
1408  ip.set_u( u*m_scale );
1409  ip.set_v( v*m_scale );
1410 
1411  switch ((int)((GdkEventButton *)ev)->button) {
1412  case 1:
1413  button = vpMouseButton::button1; break;
1414  case 2:
1415  button = vpMouseButton::button2; break;
1416  case 3:
1417  button = vpMouseButton::button3; break;
1418  }
1419  ret = true ;
1420  }
1421  gdk_event_free(ev) ;
1422  }
1423  if (blocking){
1424  flushDisplay();
1425  vpTime::wait(100);
1426  }
1427 
1428  } while ( ret == false && blocking == true);
1429  }
1430  else {
1432  "GTK not initialized")) ;
1433  }
1434  return ret;
1435 }
1436 
1460 bool
1463  bool blocking)
1464 {
1465  bool ret = false;
1466 
1468 
1469  //flushDisplay() ;
1470  double u, v ;
1471  do {
1472  GdkEvent *ev = NULL;
1473  while ((ev = gdk_event_get())!=NULL){
1474  if ( ev->any.window == widget->window
1475  && ev->type == GDK_BUTTON_RELEASE) {
1476  u = ((GdkEventButton *)ev)->x ;
1477  v = ((GdkEventButton *)ev)->y ;
1478  ip.set_u( u*m_scale );
1479  ip.set_v( v*m_scale );
1480 
1481  switch ( ( int ) ( ( GdkEventButton * ) ev )->button ) {
1482  case 1:
1483  button = vpMouseButton::button1; break;
1484  case 2:
1485  button = vpMouseButton::button2; break;
1486  case 3:
1487  button = vpMouseButton::button3; break;
1488  }
1489  ret = true ;
1490  }
1491  gdk_event_free(ev) ;
1492  }
1493  if (blocking){
1494  flushDisplay();
1495  vpTime::wait(100);
1496  }
1497 
1498  } while ( ret == false && blocking == true);
1499  }
1500  else {
1502  "GTK not initialized" ) ) ;
1503  }
1504  return ret;
1505 }
1506 
1507 /*
1508  \brief gets the displayed image (including the overlay plane)
1509  and returns an RGBa image
1510 */
1512 {
1513  // should certainly be optimized.
1515  {
1516 
1517  GdkImage *ImageGtk;
1518  /*
1519  */
1520 
1521  ImageGtk = gdk_image_get(m_background, 0, 0, (gint)m_width, (gint)m_height);
1522 
1523  I.resize(m_height, m_width) ;
1524  guint32 pixel;
1525  gint x,y;
1526  guchar OctetRouge,OctetVert,OctetBleu,mask;
1527  mask = 0x000000FF;
1528 
1529  for (y=0;y<(gint)m_height;y++)
1530  {
1531  for (x=0;x<(gint)m_width;x++)
1532  {
1533  pixel = gdk_image_get_pixel(ImageGtk,x,y);
1534  OctetBleu = (guchar)pixel & mask;
1535  OctetVert = (guchar)(pixel>>8) & mask;
1536  OctetRouge = (guchar)(pixel>>16) & mask;
1537  I[y][x].R = OctetRouge;
1538  I[y][x].G = OctetVert;
1539  I[y][x].B = OctetBleu;
1540  I[y][x].A = vpRGBa::alpha_default; // default opacity
1541  }
1542  }
1543  }
1544  else
1545  {
1547  "GTK not initialized")) ;
1548  }
1549 
1550 }
1551 
1558 {
1559 
1560  unsigned int depth;
1561 
1562  depth = (unsigned int)gdk_window_get_visual(widget->window)->depth ;
1563 
1564  return (depth);
1565 }
1566 
1582 bool
1584 {
1585  bool ret = false;
1586 
1588 
1589  int cpt =0;
1590  do {
1591  GdkEvent *ev = NULL;
1592  while ((ev = gdk_event_get())!=NULL){
1593  cpt++;
1594  // printf("event %d type %d on window %p My window %p\n",
1595  //cpt, ev->type, ev->any.window, widget->window);
1596 
1597  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1598  ret = true ;
1599  //printf("Key press detection\n");
1600  }
1601  gdk_event_free(ev) ;
1602  }
1603  if (blocking){
1604  flushDisplay();
1605  vpTime::wait(100);
1606  }
1607  } while ( ret == false && blocking == true);
1608  }
1609  else {
1611  "GTK not initialized")) ;
1612  }
1613  return ret;
1614 }
1615 
1635 bool
1636 vpDisplayGTK::getKeyboardEvent(std::string &key, bool blocking)
1637 {
1638  bool ret = false;
1639 
1641 
1642  int cpt =0;
1643  do {
1644  GdkEvent *ev = NULL;
1645  while ((ev = gdk_event_get())!=NULL){
1646  cpt++;
1647  // printf("event %d type %d on window %p My window %p\n",
1648  //cpt, ev->type, ev->any.window, widget->window);
1649 
1650  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1651  //std::cout << "Key val: \"" << gdk_keyval_name (ev->key.keyval) /*ev->key.string*/ << "\"" << std::endl;
1652  key = gdk_keyval_name (ev->key.keyval);
1653  ret = true ;
1654  //printf("Key press detection\n");
1655  }
1656  gdk_event_free(ev) ;
1657  }
1658  if (blocking){
1659  flushDisplay();
1660  vpTime::wait(100);
1661  }
1662  } while ( ret == false && blocking == true);
1663  }
1664  else {
1666  "GTK not initialized")) ;
1667  }
1668  return ret;
1669 }
1670 
1683 bool
1685 {
1686  bool ret = false;
1687 
1689  GdkEvent *ev = NULL;
1690  if ((ev = gdk_event_get())){
1691  if (ev->any.window == widget->window && ev->type == GDK_MOTION_NOTIFY){
1692  double u = ((GdkEventMotion *)ev)->x ;
1693  double v = ((GdkEventMotion *)ev)->y ;
1694  ip.set_u( u*m_scale );
1695  ip.set_v( v*m_scale );
1696 
1697  ret = true ;
1698  }
1699  gdk_event_free(ev) ;
1700  }
1701  }
1702  else {
1704  "GTK not initialized")) ;
1705  }
1706  return ret;
1707 }
1708 
1719 bool
1721 {
1723  {
1724  int u,v;
1725  gdk_window_get_pointer(widget->window, &u, &v, NULL);
1726  ip.set_u( u*m_scale );
1727  ip.set_v( v*m_scale );
1728  }
1729  else {
1731  "GTK not initialized")) ;
1732  }
1733 
1734  return true;
1735 }
1736 
1741 void vpDisplayGTK::getScreenSize ( unsigned int &w, unsigned int &h )
1742 {
1743  w = h = 0;
1744 
1746  int *argc=NULL ;
1747  char **argv ;
1748 
1749  gtk_init(argc, &argv);
1750 
1751  GtkWidget *widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1752  gtk_window_set_default_size(GTK_WINDOW(widget_), 100, 100);
1753  gtk_widget_show(widget_);
1754 
1755  GdkScreen *screen_ = gdk_window_get_screen(widget_->window);
1756  w = (unsigned int)gdk_screen_get_width(screen_);
1757  h = (unsigned int)gdk_screen_get_height(screen_);
1758  gtk_widget_destroy(widget_);
1759  }
1760  else {
1761  GdkScreen *screen_ = gdk_window_get_screen(widget->window);
1762  w = (unsigned int)gdk_screen_get_width(screen_);
1763  h = (unsigned int)gdk_screen_get_height(screen_);
1764  }
1765 
1766 }
1767 
1772 {
1773  unsigned int width, height;
1774  getScreenSize(width, height);
1775  return width;
1776 }
1777 
1782 {
1783  unsigned int width, height;
1784  getScreenSize(width, height);
1785  return height;
1786 }
1787 
1788 #elif !defined(VISP_BUILD_SHARED_LIBS)
1789 // Work arround to avoid warning: libvisp_core.a(vpDisplayGTK.cpp.o) has no symbols
1790 void dummy_vpDisplayGTK() {};
1791 #endif
unsigned int m_height
Definition: vpDisplay.h:196
vpDisplay * display
Definition: vpImage.h:135
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
double getTop() const
Definition: vpRect.h:178
int m_windowYPosition
display position
Definition: vpDisplay.h:194
double get_v() const
Definition: vpImagePoint.h:268
void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:169
bool getClick(bool blocking=true)
void displayImageROI(const vpImage< unsigned char > &I, const vpImagePoint &iP, const unsigned int width, const unsigned int height)
double get_i() const
Definition: vpImagePoint.h:199
unsigned int getWidth() const
Definition: vpImage.h:226
unsigned char B
Blue component.
Definition: vpRGBa.h:155
Type * bitmap
points toward the bitmap
Definition: vpImage.h:134
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
double get_u() const
Definition: vpImagePoint.h:257
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
double getHeight() const
Definition: vpRect.h:152
unsigned char G
Green component.
Definition: vpRGBa.h:154
bool m_displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:190
static int round(const double x)
Definition: vpMath.h:249
double get_j() const
Definition: vpImagePoint.h:210
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
void displayPoint(const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
vpColorIdentifier id
Definition: vpColor.h:152
void set_i(const double ii)
Definition: vpImagePoint.h:163
double getWidth() const
Definition: vpRect.h:199
int m_windowXPosition
display position
Definition: vpDisplay.h:192
void flushDisplay()
unsigned int m_scale
Definition: vpDisplay.h:198
void setScale(vpScaleType scaleType, unsigned int width, unsigned int height)
Definition: vpDisplay.cpp:276
void set_u(const double u)
Definition: vpImagePoint.h:221
#define vpTRACE
Definition: vpDebug.h:414
static double sqr(double x)
Definition: vpMath.h:110
virtual ~vpDisplayGTK()
void set_v(const double v)
Definition: vpImagePoint.h:232
bool getPointerPosition(vpImagePoint &ip)
vpScaleType m_scaleType
Definition: vpDisplay.h:199
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
std::string m_title
Definition: vpDisplay.h:197
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:903
void displayImage(const vpImage< vpRGBa > &I)
unsigned int getScreenWidth()
bool getPointerMotionEvent(vpImagePoint &ip)
void getScreenSize(unsigned int &width, unsigned int &height)
void setTitle(const std::string &title)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
void getImage(vpImage< vpRGBa > &I)
get the window pixmap and put it in vpRGBa image
void closeDisplay()
void set_j(const double jj)
Definition: vpImagePoint.h:174
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)
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
Definition: vpImage.h:1338
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)
void setFont(const std::string &fontname)
unsigned int m_width
Definition: vpDisplay.h:195
void setWindowPosition(int winx, int winy)
static void crop(const vpImage< Type > &I, double roi_top, double roi_left, unsigned int roi_height, unsigned int roi_width, vpImage< Type > &crop, unsigned int v_scale=1, unsigned int h_scale=1)
Definition: vpImageTools.h:266
vpScaleType
Values that could be applied to a display to down scale the size of the display.
Definition: vpDisplay.h:173
unsigned char R
Red component.
Definition: vpRGBa.h:153
unsigned int getScreenDepth()
get the window depth (8,16,24,32)
unsigned int getHeight() const
Definition: vpImage.h:175
Defines a rectangle in the plane.
Definition: vpRect.h:82
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 ...
Definition: vpImagePoint.h:88
bool getKeyboardEvent(bool blocking=true)
double getLeft() const
Definition: vpRect.h:159
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
Definition: vpImagePoint.h:279