Visual Servoing Platform  version 3.0.0
vpDisplayGTK.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 
77  int x,
78  int y,
79  const char *title)
80  : widget(NULL), background(NULL), gc(NULL),
81  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
82  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
83  purple(), lightGray(), gray(), darkGray(),
84  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
85 {
86  init(I, x, y, title) ;
87 }
88 
89 
99  int x,
100  int y,
101  const char *title)
102  : widget(NULL), background(NULL), gc(NULL),
103  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
104  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
105  purple(), lightGray(), gray(), darkGray(),
106  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
107 {
108  init(I, x, y, title) ;
109 }
110 
111 
112 
135 vpDisplayGTK::vpDisplayGTK(int x, int y, const char *title)
136  : widget(NULL), background(NULL), gc(NULL),
137  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
138  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
139  purple(), lightGray(), gray(), darkGray(),
140  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
141 {
142  windowXPosition = x ;
143  windowYPosition = y ;
144 
145  if(title != NULL)
146  title_ = std::string(title);
147  else
148  title_ = std::string(" ");
149 }
150 
171  : vpDisplay(), widget(NULL), background(NULL), gc(NULL),
172  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
173  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
174  purple(), lightGray(), gray(), darkGray(),
175  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
176 
177 {
178 }
179 
184 {
185  closeDisplay() ;
186 }
187 
196 void
198  int x,
199  int y,
200  const char *title)
201 {
202  if ((I.getHeight() == 0) || (I.getWidth()==0))
203  {
204  vpERROR_TRACE("Image not initialized " ) ;
206  "Image not initialized")) ;
207  }
208 
209  if (x != -1)
210  windowXPosition = x ;
211  if (y != -1)
212  windowYPosition = y ;
213 
215  I.display = this ;
217 }
218 
228 void
230  int x,
231  int y,
232  const char *title)
233 {
234  if ((I.getHeight() == 0) || (I.getWidth()==0))
235  {
236  vpERROR_TRACE("Image not initialized " ) ;
238  "Image not initialized")) ;
239  }
240 
241  if (x != -1)
242  windowXPosition = x ;
243  if (y != -1)
244  windowYPosition = y ;
245 
247  I.display = this ;
249 }
258 void
259 vpDisplayGTK::init(unsigned int w, unsigned int h,
260  int x, int y,
261  const char *title)
262 {
263  /* Initialisation of thegdk et gdk_rgb library */
264  int *argc=NULL ;
265  char **argv ;
266  gtk_init(argc,&argv);
267 
268  this->width = w;
269  this->height = h;
270 
271  /* Create the window*/
272  widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
273 
274  gtk_widget_add_events(widget, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
275  | GDK_POINTER_MOTION_MASK);
276 
277  gtk_window_set_default_size(GTK_WINDOW(widget), (gint)width, (gint)height);
278 
279  if (x != -1)
280  windowXPosition = x ;
281  if (y != -1)
282  windowYPosition = y ;
283 
284  gtk_window_move(GTK_WINDOW(widget), windowXPosition, windowYPosition);
285 
286  gtk_widget_show(widget);
287 
288  gdk_rgb_init();
289 
290  /* Create background pixmap */
291  background = gdk_pixmap_new(widget->window, (gint)width, (gint)height, -1);
292 
293  /* Create graphic context */
294  gc = gdk_gc_new(widget->window);
295 
296  /* get the colormap */
297  colormap = gdk_window_get_colormap(widget->window);
298 
299  col = new GdkColor *[vpColor::id_unknown] ; // id_unknown = number of predefined colors
300 
301  /* Create color */
302  gdk_color_parse("light blue",&lightBlue);
303  gdk_colormap_alloc_color(colormap,&lightBlue,FALSE,TRUE);
304  col[vpColor::id_lightBlue] = &lightBlue ;
305 
306  gdk_color_parse("blue",&blue);
307  gdk_colormap_alloc_color(colormap,&blue,FALSE,TRUE);
308  col[vpColor::id_blue] = &blue ;
309 
310  gdk_color_parse("dark blue",&darkBlue);
311  gdk_colormap_alloc_color(colormap,&darkBlue,FALSE,TRUE);
312  col[vpColor::id_darkBlue] = &darkBlue ;
313 
314  gdk_color_parse("#FF8C8C",&lightRed);
315  gdk_colormap_alloc_color(colormap,&lightRed,FALSE,TRUE);
316  col[vpColor::id_lightRed] = &lightRed ;
317 
318  gdk_color_parse("red",&red);
319  gdk_colormap_alloc_color(colormap,&red,FALSE,TRUE);
320  col[vpColor::id_red] = &red ;
321 
322  gdk_color_parse("dark red",&darkRed);
323  gdk_colormap_alloc_color(colormap,&darkRed,FALSE,TRUE);
324  col[vpColor::id_darkRed] = &darkRed ;
325 
326  gdk_color_parse("light green",&lightGreen);
327  gdk_colormap_alloc_color(colormap,&lightGreen,FALSE,TRUE);
328  col[vpColor::id_lightGreen] = &lightGreen ;
329 
330  gdk_color_parse("green",&green);
331  gdk_colormap_alloc_color(colormap,&green,FALSE,TRUE);
332  col[vpColor::id_green] = &green ;
333 
334  gdk_color_parse("dark green",&darkGreen);
335  gdk_colormap_alloc_color(colormap,&darkGreen,FALSE,TRUE);
336  col[vpColor::id_darkGreen] = &darkGreen ;
337 
338  gdk_color_parse("yellow",&yellow);
339  gdk_colormap_alloc_color(colormap,&yellow,FALSE,TRUE);
340  col[vpColor::id_yellow] = &yellow ;
341 
342  gdk_color_parse("cyan",&cyan);
343  gdk_colormap_alloc_color(colormap,&cyan,FALSE,TRUE);
344  col[vpColor::id_cyan] = &cyan ;
345 
346  gdk_color_parse("orange",&orange);
347  gdk_colormap_alloc_color(colormap,&orange,FALSE,TRUE);
348  col[vpColor::id_orange] = &orange ;
349 
350  gdk_color_parse("purple",&purple);
351  gdk_colormap_alloc_color(colormap,&purple,FALSE,TRUE);
352  col[vpColor::id_purple] = &purple ;
353 
354  gdk_color_parse("white",&white);
355  gdk_colormap_alloc_color(colormap,&white,FALSE,TRUE);
356  col[vpColor::id_white] = &white ;
357 
358  gdk_color_parse("black",&black);
359  gdk_colormap_alloc_color(colormap,&black,FALSE,TRUE);
360  col[vpColor::id_black] = &black ;
361 
362  gdk_color_parse("#C0C0C0",&lightGray);
363  gdk_colormap_alloc_color(colormap,&lightGray,FALSE,TRUE);
364  col[vpColor::id_lightGray] = &lightGray ;
365 
366  gdk_color_parse("#808080",&gray);
367  gdk_colormap_alloc_color(colormap,&gray,FALSE,TRUE);
368  col[vpColor::id_gray] = &gray ;
369 
370  gdk_color_parse("#404040",&darkGray);
371  gdk_colormap_alloc_color(colormap,&darkGray,FALSE,TRUE);
372  col[vpColor::id_darkGray] = &darkGray ;
373 
374  // Try to load a default font
375  font = gdk_font_load("-*-times-medium-r-normal-*-16-*-*-*-*-*-*-*");
376  if (font == NULL)
377  font = gdk_font_load("-*-courier-bold-r-normal-*-*-140-*-*-*-*-*-*");
378  if (font == NULL)
379  font = gdk_font_load("-*-courier 10 pitch-medium-r-normal-*-16-*-*-*-*-*-*-*");
380 
381  if(title != NULL)
382  title_ = std::string(title);
383  else
384  title_ = std::string(" ");
385 
387  gdk_window_set_title(widget->window, title_.c_str());
388 }
389 
390 
405 void
406 vpDisplayGTK::setFont(const char *fontname)
407 {
408  font = gdk_font_load((const gchar*)fontname);
409 }
410 
415 void
416 vpDisplayGTK::setTitle(const char *title)
417 {
419  {
420  if(title != NULL)
421  title_ = std::string(title);
422  else
423  title_ = std::string(" ");
424  gdk_window_set_title(widget->window, title_.c_str());
425  }
426  else
427  {
428  vpERROR_TRACE("GTK not initialized " ) ;
430  "GTK not initialized")) ;
431  }
432 }
433 
442 void vpDisplayGTK::setWindowPosition(int winx, int winy)
443 {
444 
446  gtk_window_move(GTK_WINDOW(widget), winx, winy);
447  }
448  else
449  {
450  vpERROR_TRACE("GTK not initialized " ) ;
452  "GTK not initialized")) ;
453  }
454 }
455 
456 
469 {
470 
472  {
473  /* Copie de l'image dans le pixmap fond */
474  gdk_draw_gray_image(background,
475  gc, 0, 0, (gint)width, (gint)height,
476  GDK_RGB_DITHER_NONE,
477  I.bitmap,
478  (gint)width);
479 
480  /* Le pixmap background devient le fond de la zone de dessin */
481  gdk_window_set_back_pixmap(widget->window, background, FALSE);
482 
483  /* Affichage */
484  //gdk_window_clear(GTK_WINDOW(widget));
485  //gdk_flush();
486  }
487  else
488  {
489  vpERROR_TRACE("GTK not initialized " ) ;
491  "GTK not initialized")) ;
492  }
493 }
494 
495 
513 void vpDisplayGTK::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int w, const unsigned int h )
514 {
516  {
518  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(), h, w,Itemp);
519  /* Copie de l'image dans le pixmap fond */
520  gdk_draw_gray_image(background,
521  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)w, (gint)h,
522  GDK_RGB_DITHER_NONE,
523  I.bitmap,
524  (gint)w);
525 
526  /* Le pixmap background devient le fond de la zone de dessin */
527  gdk_window_set_back_pixmap(widget->window, background, FALSE);
528 
529  /* Affichage */
530  //gdk_window_clear(GTK_WINDOW(widget));
531  //gdk_flush();
532  }
533  else
534  {
535  vpERROR_TRACE("GTK not initialized " ) ;
537  "GTK not initialized")) ;
538  }
539 }
540 
541 
554 {
555 
557  {
558 
559  /* Copie de l'image dans le pixmap fond */
560  gdk_draw_rgb_32_image(background,
561  gc, 0, 0, (gint)width, (gint)height,
562  GDK_RGB_DITHER_NONE,
563  (unsigned char *)I.bitmap,
564  (gint)(4*width));
565 
566  /* Permet de fermer la fenetre si besoin (cas des sequences d'images) */
567  //while (g_main_iteration(FALSE));
568 
569  /* Le pixmap background devient le fond de la zone de dessin */
570  gdk_window_set_back_pixmap(widget->window, background, FALSE);
571 
572  /* Affichage */
573  //gdk_window_clear(GTK_WINDOW(widget));
574  //flushDisplay() ;
575 
576  }
577  else
578  {
579  vpERROR_TRACE("GTK not initialized " ) ;
581  "GTK not initialized")) ;
582  }
583 }
584 
602 void vpDisplayGTK::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int w, const unsigned int h )
603 {
605  {
606  vpImage<vpRGBa> Itemp;
607  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(), (unsigned int)iP.get_j(), h, w, Itemp);
608  /* Copie de l'image dans le pixmap fond */
609  gdk_draw_rgb_32_image(background,
610  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)w, (gint)h,
611  GDK_RGB_DITHER_NONE,
612  (unsigned char *)Itemp.bitmap,
613  (gint)(4*w));
614 
615  /* Permet de fermer la fenetre si besoin (cas des sequences d'images) */
616  //while (g_main_iteration(FALSE));
617 
618  /* Le pixmap background devient le fond de la zone de dessin */
619  gdk_window_set_back_pixmap(widget->window, background, FALSE);
620 
621  /* Affichage */
622  //gdk_window_clear(GTK_WINDOW(widget));
623  //flushDisplay() ;
624  }
625  else
626  {
627  vpERROR_TRACE("GTK not initialized " ) ;
629  "GTK not initialized")) ;
630  }
631 }
632 
638 void vpDisplayGTK::displayImage(const unsigned char * /* I */)
639 {
640  vpTRACE(" not implemented ") ;
641 }
642 
649 {
650  if (col != NULL)
651  {
652  delete [] col ; col = NULL ;
653  }
654 
655  if (widget != NULL)
656  {
657  gdk_window_hide (widget->window);
658  gdk_window_destroy(widget->window);
659  widget = NULL;
660  }
662 }
663 
664 
670 {
672  {
673  gdk_window_clear(widget->window);
674  gdk_flush();
675  }
676  else
677  {
678  vpERROR_TRACE("GTK not initialized " ) ;
680  "GTK not initialized")) ;
681  }
682 }
683 
684 
689 void vpDisplayGTK::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
690 {
692  {
693  gdk_window_clear(widget->window);
694  gdk_flush();
695  }
696  else
697  {
698  vpERROR_TRACE("GTK not initialized " ) ;
700  "GTK not initialized")) ;
701  }
702 }
703 
704 
708 void vpDisplayGTK::clearDisplay(const vpColor & /* color */)
709 {
710  vpTRACE("Not implemented") ;
711 }
712 
721  const vpImagePoint &ip2,
722  const vpColor &color,
723  unsigned int w, unsigned int h,
724  unsigned int thickness)
725 {
727  {
728  try{
729  double a = ip2.get_i() - ip1.get_i() ;
730  double b = ip2.get_j() - ip1.get_j() ;
731  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
732 
733  //if ((a==0)&&(b==0))
734  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon() )&&(std::fabs(b) <= std::numeric_limits<double>::epsilon()) )
735  {
736  // DisplayCrossLarge(i1,j1,3,col) ;
737  }
738  else
739  {
740  a /= lg ;
741  b /= lg ;
742 
743  vpImagePoint ip3;
744  ip3.set_i(ip2.get_i() - w*a);
745  ip3.set_j(ip2.get_j() - w*b);
746 
747  vpImagePoint ip4;
748  ip4.set_i( ip3.get_i() - b*h );
749  ip4.set_j( ip3.get_j() + a*h );
750 
751  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
752  displayLine ( ip2, ip4, color, thickness ) ;
753 
754  ip4.set_i( ip3.get_i() + b*h );
755  ip4.set_j( ip3.get_j() - a*h );
756 
757  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
758  displayLine ( ip2, ip4, color, thickness ) ;
759 
760  displayLine ( ip1, ip2, color, thickness ) ;
761  }
762  }
763  catch (...)
764  {
765  vpERROR_TRACE("Error caught") ;
766  throw ;
767  }
768  }
769  else
770  {
771  vpERROR_TRACE("GTK not initialized " ) ;
773  "GTK not initialized")) ;
774  }
775 }
776 
777 
790  const char *text,
791  const vpColor &color )
792 {
794  {
795  if (color.id < vpColor::id_unknown)
796  gdk_gc_set_foreground(gc, col[color.id]);
797  else {
798  gdkcolor.red = 256 * color.R;
799  gdkcolor.green = 256 * color.G;
800  gdkcolor.blue = 256 * color.B;
801  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
802  gdk_gc_set_foreground(gc, &gdkcolor);
803  }
804  if (font != NULL)
805  gdk_draw_string(background, font, gc,
806  vpMath::round( ip.get_u() ),
807  vpMath::round( ip.get_v() ),
808  (const gchar *)text);
809  else
810  std::cout << "Cannot draw string: no font is selected" << std::endl;
811  }
812  else
813  {
814  vpERROR_TRACE("GTK not initialized " ) ;
816  "GTK not initialized")) ;
817  }
818 }
829  unsigned int radius,
830  const vpColor &color,
831  bool fill,
832  unsigned int thickness )
833 {
835  {
836  if ( thickness == 1 ) thickness = 0;
837 
838  if (color.id < vpColor::id_unknown)
839  gdk_gc_set_foreground(gc, col[color.id]);
840  else {
841  gdkcolor.red = 256 * color.R;
842  gdkcolor.green = 256 * color.G;
843  gdkcolor.blue = 256 * color.B;
844  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
845  gdk_gc_set_foreground(gc, &gdkcolor);
846  }
847 
848  gdk_gc_set_line_attributes(gc, (gint)thickness,
849  GDK_LINE_SOLID, GDK_CAP_BUTT,
850  GDK_JOIN_BEVEL) ;
851 
852  if (fill == false)
853  gdk_draw_arc(background, gc, FALSE,
854  vpMath::round( center.get_u()-radius ),
855  vpMath::round( center.get_v()-radius ),
856  (gint)(2*radius), (gint)(2*radius), 23040, 23040) ; /* 23040 = 360*64 */
857  else
858  gdk_draw_arc(background, gc, TRUE,
859  vpMath::round( center.get_u()-radius ),
860  vpMath::round( center.get_v()-radius ),
861  (gint)(2*radius), (gint)(2*radius), 23040, 23040) ; /* 23040 = 360*64 */
862  }
863  else
864  {
865  vpERROR_TRACE("GTK not initialized " ) ;
867  "GTK not initialized")) ;
868  }
869 }
878  unsigned int size,
879  const vpColor &color,
880  unsigned int thickness)
881 {
883  {
884  try{
885  double i = ip.get_i();
886  double j = ip.get_j();
887  vpImagePoint ip1, ip2;
888 
889  ip1.set_i( i-size/2 );
890  ip1.set_j( j );
891  ip2.set_i( i+size/2 );
892  ip2.set_j( j );
893  displayLine ( ip1, ip2, color, thickness ) ;
894 
895  ip1.set_i( i );
896  ip1.set_j( j-size/2 );
897  ip2.set_i( i );
898  ip2.set_j( j+size/2 );
899 
900  displayLine ( ip1, ip2, color, thickness ) ;
901  }
902  catch (...)
903  {
904  vpERROR_TRACE("Error caught") ;
905  throw ;
906  }
907  }
908 
909  else
910  {
911  vpERROR_TRACE("GTK not initialized " ) ;
913  "GTK not initialized")) ;
914  }
915 }
923  const vpImagePoint &ip2,
924  const vpColor &color,
925  unsigned int thickness )
926 {
927 
929  {
930  if ( thickness == 1 ) thickness = 0;
931 
932  if (color.id < vpColor::id_unknown)
933  gdk_gc_set_foreground(gc, col[color.id]);
934  else {
935  gdkcolor.red = 256 * color.R;
936  gdkcolor.green = 256 * color.G;
937  gdkcolor.blue = 256 * color.B;
938  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
939  gdk_gc_set_foreground(gc, &gdkcolor);
940  }
941 
942  gdk_gc_set_line_attributes(gc, (gint)thickness,
943  GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT,
944  GDK_JOIN_BEVEL) ;
945  gdk_draw_line(background, gc,
946  vpMath::round( ip1.get_u() ),
947  vpMath::round( ip1.get_v() ),
948  vpMath::round( ip2.get_u() ),
949  vpMath::round( ip2.get_v() ) );
950  gdk_gc_set_line_attributes(gc, 0,
951  GDK_LINE_SOLID, GDK_CAP_BUTT,
952  GDK_JOIN_BEVEL) ;
953  }
954  else
955  {
956  vpERROR_TRACE("GTK not initialized " ) ;
958  "GTK not initialized")) ;
959  }
960 }
961 
969  const vpImagePoint &ip2,
970  const vpColor &color,
971  unsigned int thickness )
972 {
974  {
975  if ( thickness == 1 ) thickness = 0;
976 
977  if (color.id < vpColor::id_unknown)
978  gdk_gc_set_foreground(gc, col[color.id]);
979  else {
980  gdkcolor.red = 256 * color.R;
981  gdkcolor.green = 256 * color.G;
982  gdkcolor.blue = 256 * color.B;
983  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
984  gdk_gc_set_foreground(gc, &gdkcolor);
985  }
986 
987  gdk_gc_set_line_attributes(gc, (gint)thickness,
988  GDK_LINE_SOLID, GDK_CAP_BUTT,
989  GDK_JOIN_BEVEL) ;
990  gdk_draw_line(background, gc,
991  vpMath::round( ip1.get_u() ),
992  vpMath::round( ip1.get_v() ),
993  vpMath::round( ip2.get_u() ),
994  vpMath::round( ip2.get_v() ) );
995  }
996  else
997  {
998  vpERROR_TRACE("GTK not initialized " ) ;
1000  "GTK not initialized")) ;
1001  }
1002 }
1003 
1010  const vpColor &color )
1011 {
1013  {
1014  if (color.id < vpColor::id_unknown)
1015  gdk_gc_set_foreground(gc, col[color.id]);
1016  else {
1017  gdkcolor.red = 256 * color.R;
1018  gdkcolor.green = 256 * color.G;
1019  gdkcolor.blue = 256 * color.B;
1020  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1021  gdk_gc_set_foreground(gc, &gdkcolor);
1022  }
1023 
1024  gdk_draw_point(background,gc,
1025  vpMath::round( ip.get_u() ),
1026  vpMath::round( ip.get_v() ) );
1027  }
1028  else
1029  {
1030  vpERROR_TRACE("GTK not initialized " ) ;
1032  "GTK not initialized")) ;
1033  }
1034 }
1035 
1036 
1050 void
1052  unsigned int w, unsigned int h,
1053  const vpColor &color, bool fill,
1054  unsigned int thickness )
1055 {
1057  {
1058  if ( thickness == 1 ) thickness = 0;
1059 
1060  if (color.id < vpColor::id_unknown)
1061  gdk_gc_set_foreground(gc, col[color.id]);
1062  else {
1063  gdkcolor.red = 256 * color.R;
1064  gdkcolor.green = 256 * color.G;
1065  gdkcolor.blue = 256 * color.B;
1066  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1067  gdk_gc_set_foreground(gc, &gdkcolor);
1068  }
1069  gdk_gc_set_line_attributes(gc, (gint)thickness,
1070  GDK_LINE_SOLID, GDK_CAP_BUTT,
1071  GDK_JOIN_BEVEL) ;
1072 
1073  if (fill == false)
1074  gdk_draw_rectangle(background, gc, FALSE,
1075  vpMath::round( topLeft.get_u() ),
1076  vpMath::round( topLeft.get_v() ),
1077  (gint)w-1, (gint)h-1);
1078  else
1079  gdk_draw_rectangle(background, gc, TRUE,
1080  vpMath::round( topLeft.get_u() ),
1081  vpMath::round( topLeft.get_v() ),
1082  (gint)w, (gint)h);
1083 
1084  if (thickness > 1)
1085  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1086  GDK_JOIN_BEVEL) ;
1087  }
1088  else
1089  {
1090  vpERROR_TRACE("GTK not initialized " ) ;
1092  "GTK not initialized")) ;
1093  }
1094 }
1095 
1108 void
1110  const vpImagePoint &bottomRight,
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(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(gc, &gdkcolor);
1126  }
1127 
1128  gdk_gc_set_line_attributes(gc, (gint)thickness,
1129  GDK_LINE_SOLID, GDK_CAP_BUTT,
1130  GDK_JOIN_BEVEL) ;
1131 
1132  int w = vpMath::round( bottomRight.get_u() - topLeft.get_u() );
1133  int h = vpMath::round( bottomRight.get_v() - topLeft.get_v() );
1134 
1135  if (fill == false)
1136  gdk_draw_rectangle(background, gc, FALSE,
1137  vpMath::round( topLeft.get_u() ),
1138  vpMath::round( topLeft.get_v() ),
1139  w-1,h-1);
1140  else
1141  gdk_draw_rectangle(background, gc, TRUE,
1142  vpMath::round( topLeft.get_u() ),
1143  vpMath::round( topLeft.get_v() ),
1144  w, h);
1145 
1146  if (thickness > 1)
1147  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1148  GDK_JOIN_BEVEL) ;
1149  }
1150  else
1151  {
1152  vpERROR_TRACE("GTK not initialized " ) ;
1154  "GTK not initialized")) ;
1155  }
1156 }
1157 
1170 void
1172  const vpColor &color, bool fill,
1173  unsigned int thickness )
1174 {
1176  {
1177  if (color.id < vpColor::id_unknown)
1178  gdk_gc_set_foreground(gc, col[color.id]);
1179  else {
1180  gdkcolor.red = 256 * color.R;
1181  gdkcolor.green = 256 * color.G;
1182  gdkcolor.blue = 256 * color.B;
1183  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1184  gdk_gc_set_foreground(gc, &gdkcolor);
1185  }
1186 
1187  if ( thickness == 1 ) thickness = 0;
1188 
1189  gdk_gc_set_line_attributes(gc, (gint)thickness, GDK_LINE_SOLID, GDK_CAP_BUTT,
1190  GDK_JOIN_BEVEL) ;
1191 
1192  if (fill == false)
1193  gdk_draw_rectangle(background, gc, FALSE,
1194  vpMath::round( rectangle.getLeft() ),
1195  vpMath::round( rectangle.getTop() ),
1196  vpMath::round( rectangle.getWidth()-1 ),
1197  vpMath::round( rectangle.getHeight()-1 ) );
1198 
1199  else
1200  gdk_draw_rectangle(background, gc, TRUE,
1201  vpMath::round( rectangle.getLeft() ),
1202  vpMath::round( rectangle.getTop() ),
1203  vpMath::round( rectangle.getWidth()-1 ),
1204  vpMath::round( rectangle.getHeight()-1 ) );
1205 
1206  if (thickness > 1)
1207  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1208  GDK_JOIN_BEVEL) ;
1209  }
1210  else
1211  {
1212  vpERROR_TRACE("GTK not initialized " ) ;
1214  "GTK not initialized")) ;
1215  }
1216 }
1217 
1218 
1234 bool
1236 {
1237  bool ret = false;
1238 
1239  int cpt =0;
1241 
1242 // flushDisplay() ;
1243  GdkEvent *ev = NULL;
1244  do {
1245  while ((ev = gdk_event_get())!=NULL){
1246  cpt++;
1247  // printf("event %d type %d on window %p My window %p\n",
1248  //cpt, ev->type, ev->any.window, widget->window);
1249 
1250  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1251  ret = true ;
1252  //printf("Click detection\n");
1253  }
1254  gdk_event_free(ev) ;
1255  }
1256  if (blocking){
1257  flushDisplay();
1258  vpTime::wait(100);
1259  }
1260  } while ( ret == false && blocking == true);
1261  }
1262  else {
1263  vpERROR_TRACE("GTK not initialized " ) ;
1265  "GTK not initialized")) ;
1266  }
1267  return ret;
1268 }
1269 
1286 bool
1288 {
1289  bool ret = false;
1290 
1292 
1293  GdkEvent *ev = NULL;
1294  double u, v ;
1295  do {
1296  while ((ev = gdk_event_get())!=NULL){
1297  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS) {
1298  u = ((GdkEventButton *)ev)->x ;
1299  v = ((GdkEventButton *)ev)->y ;
1300  ip.set_u( u );
1301  ip.set_v( v );
1302  ret = true ;
1303  }
1304  gdk_event_free(ev) ;
1305  }
1306  if (blocking){
1307  flushDisplay();
1308  vpTime::wait(100);
1309  }
1310  } while ( ret == false && blocking == true);
1311  }
1312  else {
1313  vpERROR_TRACE("GTK not initialized " ) ;
1315  "GTK not initialized")) ;
1316  }
1317  return ret ;
1318 }
1319 
1320 
1340 bool
1343  bool blocking)
1344 {
1345  bool ret = false;
1346 
1348  GdkEvent *ev = NULL;
1349  double u, v ;
1350  do {
1351  while ((ev = gdk_event_get())){
1352  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1353  u = ((GdkEventButton *)ev)->x ;
1354  v = ((GdkEventButton *)ev)->y ;
1355  ip.set_u( u );
1356  ip.set_v( v );
1357 
1358  switch ((int)((GdkEventButton *)ev)->button) {
1359  case 1:
1360  button = vpMouseButton::button1; break;
1361  case 2:
1362  button = vpMouseButton::button2; break;
1363  case 3:
1364  button = vpMouseButton::button3; break;
1365  }
1366  ret = true ;
1367  }
1368  gdk_event_free(ev) ;
1369  }
1370  if (blocking){
1371  flushDisplay();
1372  vpTime::wait(100);
1373  }
1374 
1375  } while ( ret == false && blocking == true);
1376  }
1377  else {
1378  vpERROR_TRACE("GTK not initialized " ) ;
1380  "GTK not initialized")) ;
1381  }
1382  return ret;
1383 }
1384 
1408 bool
1411  bool blocking)
1412 {
1413  bool ret = false;
1414 
1415  if ( displayHasBeenInitialized ) {
1416 
1417  //flushDisplay() ;
1418  GdkEvent *ev = NULL;
1419  double u, v ;
1420  do {
1421  while ((ev = gdk_event_get())!=NULL){
1422  if ( ev->any.window == widget->window
1423  && ev->type == GDK_BUTTON_RELEASE) {
1424  u = ((GdkEventButton *)ev)->x ;
1425  v = ((GdkEventButton *)ev)->y ;
1426  ip.set_u( u );
1427  ip.set_v( v );
1428 
1429  switch ( ( int ) ( ( GdkEventButton * ) ev )->button ) {
1430  case 1:
1431  button = vpMouseButton::button1; break;
1432  case 2:
1433  button = vpMouseButton::button2; break;
1434  case 3:
1435  button = vpMouseButton::button3; break;
1436  }
1437  ret = true ;
1438  }
1439  gdk_event_free(ev) ;
1440  }
1441  if (blocking){
1442  flushDisplay();
1443  vpTime::wait(100);
1444  }
1445 
1446  } while ( ret == false && blocking == true);
1447  }
1448  else {
1449  vpERROR_TRACE ( "GTK not initialized " ) ;
1451  "GTK not initialized" ) ) ;
1452  }
1453  return ret;
1454 }
1455 
1456 /*
1457  \brief gets the displayed image (including the overlay plane)
1458  and returns an RGBa image
1459 */
1461 {
1462 
1463 
1464  // shoudl certainly be optimized.
1465  // doesn't work
1467  {
1468 
1469  GdkImage *ImageGtk;
1470  /*
1471  */
1472 
1473  ImageGtk = gdk_image_get(background, 0, 0, (gint)width, (gint)height);
1474 
1475 
1476  I.resize(height,width) ;
1477  guchar *pos;
1478  guint32 pixel;
1479  gint x,y;
1480  guchar OctetRouge,OctetVert,OctetBleu,mask;
1481  mask = 0x000000FF;
1482 
1483  pos = (unsigned char *)I.bitmap;
1484  for (y=0;y<(gint)height;y++)
1485  {
1486  for (x=0;x<(gint)width;x++)
1487  {
1488  pixel = gdk_image_get_pixel(ImageGtk,x,y);
1489  OctetBleu = (guchar)pixel & mask;
1490  OctetVert = (guchar)(pixel>>8) & mask;
1491  OctetRouge = (guchar)(pixel>>16) & mask;
1492  *pos++ = OctetRouge;
1493  *pos++ = OctetVert;
1494  *pos++ = OctetBleu;
1495  *pos++ = 0;
1496  }
1497  }
1498 
1499 
1500  }
1501  else
1502  {
1503  vpERROR_TRACE("GTK not initialized " ) ;
1505  "GTK not initialized")) ;
1506  }
1507 
1508 }
1509 
1516 {
1517 
1518  unsigned int depth;
1519 
1520  depth = (unsigned int)gdk_window_get_visual(widget->window)->depth ;
1521 
1522  return (depth);
1523 }
1524 
1530 void vpDisplayGTK::getScreenSize(unsigned int &w, unsigned int &h)
1531 {
1532  vpTRACE("Not implemented") ;
1533  w = 0;
1534  h = 0;
1535 }
1536 
1552 bool
1554 {
1555  bool ret = false;
1556 
1557  int cpt =0;
1559 
1560  GdkEvent *ev = NULL;
1561  do {
1562  while ((ev = gdk_event_get())!=NULL){
1563  cpt++;
1564  // printf("event %d type %d on window %p My window %p\n",
1565  //cpt, ev->type, ev->any.window, widget->window);
1566 
1567  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1568  ret = true ;
1569  //printf("Key press detection\n");
1570  }
1571  gdk_event_free(ev) ;
1572  }
1573  if (blocking){
1574  flushDisplay();
1575  vpTime::wait(100);
1576  }
1577  } while ( ret == false && blocking == true);
1578  }
1579  else {
1580  vpERROR_TRACE("GTK not initialized " ) ;
1582  "GTK not initialized")) ;
1583  }
1584  return ret;
1585 }
1586 
1606 bool
1607 vpDisplayGTK::getKeyboardEvent(char *string, bool blocking)
1608 {
1609  bool ret = false;
1610 
1611  int cpt =0;
1613 
1614  GdkEvent *ev = NULL;
1615  do {
1616  while ((ev = gdk_event_get())!=NULL){
1617  cpt++;
1618  // printf("event %d type %d on window %p My window %p\n",
1619  //cpt, ev->type, ev->any.window, widget->window);
1620 
1621  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1622  //std::cout << "Key val: \"" << gdk_keyval_name (ev->key.keyval) /*ev->key.string*/ << "\"" << std::endl;
1623  sprintf(string, "%s", gdk_keyval_name (ev->key.keyval));
1624  ret = true ;
1625  //printf("Key press detection\n");
1626  }
1627  gdk_event_free(ev) ;
1628  }
1629  if (blocking){
1630  flushDisplay();
1631  vpTime::wait(100);
1632  }
1633  } while ( ret == false && blocking == true);
1634  }
1635  else {
1636  vpERROR_TRACE("GTK not initialized " ) ;
1638  "GTK not initialized")) ;
1639  }
1640  return ret;
1641 }
1642 
1655 bool
1657 {
1658  bool ret = false;
1659 
1661  GdkEvent *ev = NULL;
1662  double u, v ;
1663  if ((ev = gdk_event_get())){
1664  if (ev->any.window == widget->window && ev->type == GDK_MOTION_NOTIFY){
1665  u = ((GdkEventMotion *)ev)->x ;
1666  v = ((GdkEventMotion *)ev)->y ;
1667  ip.set_u( u );
1668  ip.set_v( v );
1669 
1670  ret = true ;
1671  }
1672  gdk_event_free(ev) ;
1673  }
1674  }
1675  else {
1676  vpERROR_TRACE("GTK not initialized " ) ;
1678  "GTK not initialized")) ;
1679  }
1680  return ret;
1681 }
1682 
1693 bool
1695 {
1697  {
1698  int u,v;
1699  gdk_window_get_pointer(widget->window, &u, &v, NULL);
1700  ip.set_u( u );
1701  ip.set_v( v );
1702  }
1703  else {
1704  vpERROR_TRACE("GTK not initialized " ) ;
1706  "GTK not initialized")) ;
1707  }
1708 
1709  return true;
1710 }
1711 
1712 #elif !defined(VISP_BUILD_SHARED_LIBS)
1713 // Work arround to avoid warning: libvisp_core.a(vpDisplayGTK.cpp.o) has no symbols
1714 void dummy_vpDisplayGTK() {};
1715 #endif
vpDisplay * display
Definition: vpImage.h:117
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:150
double getTop() const
Definition: vpRect.h:176
double get_v() const
Definition: vpImagePoint.h:259
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:170
bool getClick(bool blocking=true)
void displayImageROI(const vpImage< unsigned char > &I, const vpImagePoint &iP, const unsigned int width, const unsigned int height)
unsigned int width
Definition: vpDisplay.h:179
double get_i() const
Definition: vpImagePoint.h:190
unsigned int getWidth() const
Definition: vpImage.h:161
unsigned char B
Blue component.
Definition: vpRGBa.h:144
Type * bitmap
points toward the bitmap
Definition: vpImage.h:116
void setFont(const char *fontname)
#define vpERROR_TRACE
Definition: vpDebug.h:391
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:248
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
double getHeight() const
Definition: vpRect.h:151
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:174
unsigned char G
Green component.
Definition: vpRGBa.h:143
static int round(const double x)
Definition: vpMath.h:248
double get_j() const
Definition: vpImagePoint.h:201
std::string title_
Definition: vpDisplay.h:181
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
vpColorIdentifier id
Definition: vpColor.h:152
void set_i(const double ii)
Definition: vpImagePoint.h:154
double getWidth() const
Definition: vpRect.h:195
void flushDisplay()
void set_u(const double u)
Definition: vpImagePoint.h:212
#define vpTRACE
Definition: vpDebug.h:414
static double sqr(double x)
Definition: vpMath.h:110
virtual ~vpDisplayGTK()
unsigned int height
Definition: vpDisplay.h:180
void set_v(const double v)
Definition: vpImagePoint.h:223
bool getPointerPosition(vpImagePoint &ip)
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
void resize(const unsigned int h, const unsigned int w)
set the size of the image without initializing it.
Definition: vpImage.h:616
void displayImage(const vpImage< vpRGBa > &I)
void displayPoint(const vpImagePoint &ip, const vpColor &color)
bool getPointerMotionEvent(vpImagePoint &ip)
void getScreenSize(unsigned int &width, unsigned int &height)
get the window size
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:165
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 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)
static void createSubImage(const vpImage< Type > &I, unsigned int i_sub, unsigned int j_sub, unsigned int nrow_sub, unsigned int ncol_sub, vpImage< Type > &S)
Definition: vpImageTools.h:132
void setWindowPosition(int winx, int winy)
int windowXPosition
display position
Definition: vpDisplay.h:176
unsigned char R
Red component.
Definition: vpRGBa.h:142
unsigned int getScreenDepth()
get the window depth (8,16,24,32)
unsigned int getHeight() const
Definition: vpImage.h:152
Defines a rectangle in the plane.
Definition: vpRect.h:81
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
void setTitle(const char *title)
bool getKeyboardEvent(bool blocking=true)
int windowYPosition
display position
Definition: vpDisplay.h:178
double getLeft() const
Definition: vpRect.h:157
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
Definition: vpImagePoint.h:270