ViSP  2.10.0
vpDisplayGTK.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayGTK.cpp 5126 2015-01-05 22:07:11Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Image display.
36  *
37  * Authors:
38  * Christophe Collewet
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
44 
50 #include <visp/vpConfig.h>
51 
52 #if ( defined(VISP_HAVE_GTK) )
53 
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <iostream>
57 #include <cmath> // std::fabs
58 #include <limits> // numeric_limits
59 
60 // Display stuff
61 #include <visp/vpDisplay.h>
62 #include <visp/vpDisplayGTK.h>
63 
64 //debug / exception
65 #include <visp/vpDebug.h>
66 #include <visp/vpDisplayException.h>
67 #include <visp/vpMath.h>
68 #include <visp/vpImageTools.h>
69 
81  int x,
82  int y,
83  const char *title)
84  : widget(NULL), background(NULL), gc(NULL),
85  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
86  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
87  purple(), lightGray(), gray(), darkGray(),
88  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
89 {
90  init(I, x, y, title) ;
91 }
92 
93 
103  int x,
104  int y,
105  const char *title)
106  : widget(NULL), background(NULL), gc(NULL),
107  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
108  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
109  purple(), lightGray(), gray(), darkGray(),
110  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
111 {
112  init(I, x, y, title) ;
113 }
114 
115 
116 
139 vpDisplayGTK::vpDisplayGTK(int x, int y, const char *title)
140  : widget(NULL), background(NULL), 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  windowXPosition = x ;
147  windowYPosition = y ;
148 
149  if(title != NULL)
150  title_ = std::string(title);
151  else
152  title_ = std::string(" ");
153 }
154 
175  : vpDisplay(), widget(NULL), background(NULL), gc(NULL),
176  blue(), red(), yellow(), green(), cyan(), orange(), white(), black(), gdkcolor(),
177  lightBlue(), darkBlue(), lightRed(), darkRed(),lightGreen(), darkGreen(),
178  purple(), lightGray(), gray(), darkGray(),
179  colormap(NULL), font(NULL), vectgtk(NULL), col(NULL), ncol(0), nrow(0)
180 
181 {
182 }
183 
188 {
189  closeDisplay() ;
190 }
191 
200 void
202  int x,
203  int y,
204  const char *title)
205 {
206  if ((I.getHeight() == 0) || (I.getWidth()==0))
207  {
208  vpERROR_TRACE("Image not initialized " ) ;
210  "Image not initialized")) ;
211  }
212 
213  if (x != -1)
214  windowXPosition = x ;
215  if (y != -1)
216  windowYPosition = y ;
217 
219  I.display = this ;
221 }
222 
232 void
234  int x,
235  int y,
236  const char *title)
237 {
238  if ((I.getHeight() == 0) || (I.getWidth()==0))
239  {
240  vpERROR_TRACE("Image not initialized " ) ;
242  "Image not initialized")) ;
243  }
244 
245  if (x != -1)
246  windowXPosition = x ;
247  if (y != -1)
248  windowYPosition = y ;
249 
251  I.display = this ;
253 }
262 void
263 vpDisplayGTK::init(unsigned int w, unsigned int h,
264  int x, int y,
265  const char *title)
266 {
267  /* Initialisation of thegdk et gdk_rgb library */
268  int *argc=NULL ;
269  char **argv ;
270  gtk_init(argc,&argv);
271 
272  this->width = w;
273  this->height = h;
274 
275  /* Create the window*/
276  widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
277 
278  gtk_widget_add_events(widget, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
279  | GDK_POINTER_MOTION_MASK);
280 
281  gtk_window_set_default_size(GTK_WINDOW(widget), (gint)width, (gint)height);
282 
283  if (x != -1)
284  windowXPosition = x ;
285  if (y != -1)
286  windowYPosition = y ;
287 
288  gtk_window_move(GTK_WINDOW(widget), windowXPosition, windowYPosition);
289 
290  gtk_widget_show(widget);
291 
292  gdk_rgb_init();
293 
294  /* Create background pixmap */
295  background = gdk_pixmap_new(widget->window, (gint)width, (gint)height, -1);
296 
297  /* Create graphic context */
298  gc = gdk_gc_new(widget->window);
299 
300  /* get the colormap */
301  colormap = gdk_window_get_colormap(widget->window);
302 
303  col = new GdkColor *[vpColor::id_unknown] ; // id_unknown = number of predefined colors
304 
305  /* Create color */
306  gdk_color_parse("light blue",&lightBlue);
307  gdk_colormap_alloc_color(colormap,&lightBlue,FALSE,TRUE);
308  col[vpColor::id_lightBlue] = &lightBlue ;
309 
310  gdk_color_parse("blue",&blue);
311  gdk_colormap_alloc_color(colormap,&blue,FALSE,TRUE);
312  col[vpColor::id_blue] = &blue ;
313 
314  gdk_color_parse("dark blue",&darkBlue);
315  gdk_colormap_alloc_color(colormap,&darkBlue,FALSE,TRUE);
316  col[vpColor::id_darkBlue] = &darkBlue ;
317 
318  gdk_color_parse("#FF8C8C",&lightRed);
319  gdk_colormap_alloc_color(colormap,&lightRed,FALSE,TRUE);
320  col[vpColor::id_lightRed] = &lightRed ;
321 
322  gdk_color_parse("red",&red);
323  gdk_colormap_alloc_color(colormap,&red,FALSE,TRUE);
324  col[vpColor::id_red] = &red ;
325 
326  gdk_color_parse("dark red",&darkRed);
327  gdk_colormap_alloc_color(colormap,&darkRed,FALSE,TRUE);
328  col[vpColor::id_darkRed] = &darkRed ;
329 
330  gdk_color_parse("light green",&lightGreen);
331  gdk_colormap_alloc_color(colormap,&lightGreen,FALSE,TRUE);
332  col[vpColor::id_lightGreen] = &lightGreen ;
333 
334  gdk_color_parse("green",&green);
335  gdk_colormap_alloc_color(colormap,&green,FALSE,TRUE);
336  col[vpColor::id_green] = &green ;
337 
338  gdk_color_parse("dark green",&darkGreen);
339  gdk_colormap_alloc_color(colormap,&darkGreen,FALSE,TRUE);
340  col[vpColor::id_darkGreen] = &darkGreen ;
341 
342  gdk_color_parse("yellow",&yellow);
343  gdk_colormap_alloc_color(colormap,&yellow,FALSE,TRUE);
344  col[vpColor::id_yellow] = &yellow ;
345 
346  gdk_color_parse("cyan",&cyan);
347  gdk_colormap_alloc_color(colormap,&cyan,FALSE,TRUE);
348  col[vpColor::id_cyan] = &cyan ;
349 
350  gdk_color_parse("orange",&orange);
351  gdk_colormap_alloc_color(colormap,&orange,FALSE,TRUE);
352  col[vpColor::id_orange] = &orange ;
353 
354  gdk_color_parse("purple",&purple);
355  gdk_colormap_alloc_color(colormap,&purple,FALSE,TRUE);
356  col[vpColor::id_purple] = &purple ;
357 
358  gdk_color_parse("white",&white);
359  gdk_colormap_alloc_color(colormap,&white,FALSE,TRUE);
360  col[vpColor::id_white] = &white ;
361 
362  gdk_color_parse("black",&black);
363  gdk_colormap_alloc_color(colormap,&black,FALSE,TRUE);
364  col[vpColor::id_black] = &black ;
365 
366  gdk_color_parse("#C0C0C0",&lightGray);
367  gdk_colormap_alloc_color(colormap,&lightGray,FALSE,TRUE);
368  col[vpColor::id_lightGray] = &lightGray ;
369 
370  gdk_color_parse("#808080",&gray);
371  gdk_colormap_alloc_color(colormap,&gray,FALSE,TRUE);
372  col[vpColor::id_gray] = &gray ;
373 
374  gdk_color_parse("#404040",&darkGray);
375  gdk_colormap_alloc_color(colormap,&darkGray,FALSE,TRUE);
376  col[vpColor::id_darkGray] = &darkGray ;
377 
378  // Try to load a default font
379  font = gdk_font_load("-*-times-medium-r-normal-*-16-*-*-*-*-*-*-*");
380  if (font == NULL)
381  font = gdk_font_load("-*-courier-bold-r-normal-*-*-140-*-*-*-*-*-*");
382  if (font == NULL)
383  font = gdk_font_load("-*-courier 10 pitch-medium-r-normal-*-16-*-*-*-*-*-*-*");
384 
385  if(title != NULL)
386  title_ = std::string(title);
387  else
388  title_ = std::string(" ");
389 
391  gdk_window_set_title(widget->window, title_.c_str());
392 }
393 
394 
409 void
410 vpDisplayGTK::setFont(const char *fontname)
411 {
412  font = gdk_font_load((const gchar*)fontname);
413 }
414 
419 void
420 vpDisplayGTK::setTitle(const char *title)
421 {
423  {
424  if(title != NULL)
425  title_ = std::string(title);
426  else
427  title_ = std::string(" ");
428  gdk_window_set_title(widget->window, title_.c_str());
429  }
430  else
431  {
432  vpERROR_TRACE("GTK not initialized " ) ;
434  "GTK not initialized")) ;
435  }
436 }
437 
446 void vpDisplayGTK::setWindowPosition(int winx, int winy)
447 {
448 
450  gtk_window_move(GTK_WINDOW(widget), winx, winy);
451  }
452  else
453  {
454  vpERROR_TRACE("GTK not initialized " ) ;
456  "GTK not initialized")) ;
457  }
458 }
459 
460 
473 {
474 
476  {
477  /* Copie de l'image dans le pixmap fond */
478  gdk_draw_gray_image(background,
479  gc, 0, 0, (gint)width, (gint)height,
480  GDK_RGB_DITHER_NONE,
481  I.bitmap,
482  (gint)width);
483 
484  /* Le pixmap background devient le fond de la zone de dessin */
485  gdk_window_set_back_pixmap(widget->window, background, FALSE);
486 
487  /* Affichage */
488  //gdk_window_clear(GTK_WINDOW(widget));
489  //gdk_flush();
490  }
491  else
492  {
493  vpERROR_TRACE("GTK not initialized " ) ;
495  "GTK not initialized")) ;
496  }
497 }
498 
499 
517 void vpDisplayGTK::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int w, const unsigned int h )
518 {
520  {
522  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(), h, w,Itemp);
523  /* Copie de l'image dans le pixmap fond */
524  gdk_draw_gray_image(background,
525  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)w, (gint)h,
526  GDK_RGB_DITHER_NONE,
527  I.bitmap,
528  (gint)w);
529 
530  /* Le pixmap background devient le fond de la zone de dessin */
531  gdk_window_set_back_pixmap(widget->window, background, FALSE);
532 
533  /* Affichage */
534  //gdk_window_clear(GTK_WINDOW(widget));
535  //gdk_flush();
536  }
537  else
538  {
539  vpERROR_TRACE("GTK not initialized " ) ;
541  "GTK not initialized")) ;
542  }
543 }
544 
545 
558 {
559 
561  {
562 
563  /* Copie de l'image dans le pixmap fond */
564  gdk_draw_rgb_32_image(background,
565  gc, 0, 0, (gint)width, (gint)height,
566  GDK_RGB_DITHER_NONE,
567  (unsigned char *)I.bitmap,
568  (gint)(4*width));
569 
570  /* Permet de fermer la fen�tre si besoin (cas des s�quences d'images) */
571  //while (g_main_iteration(FALSE));
572 
573  /* Le pixmap background devient le fond de la zone de dessin */
574  gdk_window_set_back_pixmap(widget->window, background, FALSE);
575 
576  /* Affichage */
577  //gdk_window_clear(GTK_WINDOW(widget));
578  //flushDisplay() ;
579 
580  }
581  else
582  {
583  vpERROR_TRACE("GTK not initialized " ) ;
585  "GTK not initialized")) ;
586  }
587 }
588 
606 void vpDisplayGTK::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int w, const unsigned int h )
607 {
609  {
610  vpImage<vpRGBa> Itemp;
611  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(), (unsigned int)iP.get_j(), h, w, Itemp);
612  /* Copie de l'image dans le pixmap fond */
613  gdk_draw_rgb_32_image(background,
614  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)w, (gint)h,
615  GDK_RGB_DITHER_NONE,
616  (unsigned char *)Itemp.bitmap,
617  (gint)(4*w));
618 
619  /* Permet de fermer la fen�tre si besoin (cas des s�quences d'images) */
620  //while (g_main_iteration(FALSE));
621 
622  /* Le pixmap background devient le fond de la zone de dessin */
623  gdk_window_set_back_pixmap(widget->window, background, FALSE);
624 
625  /* Affichage */
626  //gdk_window_clear(GTK_WINDOW(widget));
627  //flushDisplay() ;
628  }
629  else
630  {
631  vpERROR_TRACE("GTK not initialized " ) ;
633  "GTK not initialized")) ;
634  }
635 }
636 
642 void vpDisplayGTK::displayImage(const unsigned char * /* I */)
643 {
644  vpTRACE(" not implemented ") ;
645 }
646 
653 {
654  if (col != NULL)
655  {
656  delete [] col ; col = NULL ;
657  }
658 
659  if (widget != NULL)
660  {
661  gdk_window_hide (widget->window);
662  gdk_window_destroy(widget->window);
663  widget = NULL;
664  }
666 }
667 
668 
674 {
676  {
677  gdk_window_clear(widget->window);
678  gdk_flush();
679  }
680  else
681  {
682  vpERROR_TRACE("GTK not initialized " ) ;
684  "GTK not initialized")) ;
685  }
686 }
687 
688 
693 void vpDisplayGTK::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
694 {
696  {
697  gdk_window_clear(widget->window);
698  gdk_flush();
699  }
700  else
701  {
702  vpERROR_TRACE("GTK not initialized " ) ;
704  "GTK not initialized")) ;
705  }
706 }
707 
708 
712 void vpDisplayGTK::clearDisplay(const vpColor & /* color */)
713 {
714  vpTRACE("Not implemented") ;
715 }
716 
725  const vpImagePoint &ip2,
726  const vpColor &color,
727  unsigned int w, unsigned int h,
728  unsigned int thickness)
729 {
731  {
732  try{
733  double a = ip2.get_i() - ip1.get_i() ;
734  double b = ip2.get_j() - ip1.get_j() ;
735  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
736 
737  //if ((a==0)&&(b==0))
738  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon() )&&(std::fabs(b) <= std::numeric_limits<double>::epsilon()) )
739  {
740  // DisplayCrossLarge(i1,j1,3,col) ;
741  }
742  else
743  {
744  a /= lg ;
745  b /= lg ;
746 
747  vpImagePoint ip3;
748  ip3.set_i(ip2.get_i() - w*a);
749  ip3.set_j(ip2.get_j() - w*b);
750 
751  vpImagePoint ip4;
752  ip4.set_i( ip3.get_i() - b*h );
753  ip4.set_j( ip3.get_j() + a*h );
754 
755  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
756  displayLine ( ip2, ip4, color, thickness ) ;
757 
758  ip4.set_i( ip3.get_i() + b*h );
759  ip4.set_j( ip3.get_j() - a*h );
760 
761  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
762  displayLine ( ip2, ip4, color, thickness ) ;
763 
764  displayLine ( ip1, ip2, color, thickness ) ;
765  }
766  }
767  catch (...)
768  {
769  vpERROR_TRACE("Error caught") ;
770  throw ;
771  }
772  }
773  else
774  {
775  vpERROR_TRACE("GTK not initialized " ) ;
777  "GTK not initialized")) ;
778  }
779 }
780 
781 
794  const char *text,
795  const vpColor &color )
796 {
798  {
799  if (color.id < vpColor::id_unknown)
800  gdk_gc_set_foreground(gc, col[color.id]);
801  else {
802  gdkcolor.red = 256 * color.R;
803  gdkcolor.green = 256 * color.G;
804  gdkcolor.blue = 256 * color.B;
805  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
806  gdk_gc_set_foreground(gc, &gdkcolor);
807  }
808  if (font != NULL)
809  gdk_draw_string(background, font, gc,
810  vpMath::round( ip.get_u() ),
811  vpMath::round( ip.get_v() ),
812  (const gchar *)text);
813  else
814  std::cout << "Cannot draw string: no font is selected" << std::endl;
815  }
816  else
817  {
818  vpERROR_TRACE("GTK not initialized " ) ;
820  "GTK not initialized")) ;
821  }
822 }
833  unsigned int radius,
834  const vpColor &color,
835  bool fill,
836  unsigned int thickness )
837 {
839  {
840  if ( thickness == 1 ) thickness = 0;
841 
842  if (color.id < vpColor::id_unknown)
843  gdk_gc_set_foreground(gc, col[color.id]);
844  else {
845  gdkcolor.red = 256 * color.R;
846  gdkcolor.green = 256 * color.G;
847  gdkcolor.blue = 256 * color.B;
848  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
849  gdk_gc_set_foreground(gc, &gdkcolor);
850  }
851 
852  gdk_gc_set_line_attributes(gc, (gint)thickness,
853  GDK_LINE_SOLID, GDK_CAP_BUTT,
854  GDK_JOIN_BEVEL) ;
855 
856  if (fill == false)
857  gdk_draw_arc(background, gc, FALSE,
858  vpMath::round( center.get_u()-radius ),
859  vpMath::round( center.get_v()-radius ),
860  (gint)(2*radius), (gint)(2*radius), 23040, 23040) ; /* 23040 = 360*64 */
861  else
862  gdk_draw_arc(background, gc, TRUE,
863  vpMath::round( center.get_u()-radius ),
864  vpMath::round( center.get_v()-radius ),
865  (gint)(2*radius), (gint)(2*radius), 23040, 23040) ; /* 23040 = 360*64 */
866  }
867  else
868  {
869  vpERROR_TRACE("GTK not initialized " ) ;
871  "GTK not initialized")) ;
872  }
873 }
882  unsigned int size,
883  const vpColor &color,
884  unsigned int thickness)
885 {
887  {
888  try{
889  double i = ip.get_i();
890  double j = ip.get_j();
891  vpImagePoint ip1, ip2;
892 
893  ip1.set_i( i-size/2 );
894  ip1.set_j( j );
895  ip2.set_i( i+size/2 );
896  ip2.set_j( j );
897  displayLine ( ip1, ip2, color, thickness ) ;
898 
899  ip1.set_i( i );
900  ip1.set_j( j-size/2 );
901  ip2.set_i( i );
902  ip2.set_j( j+size/2 );
903 
904  displayLine ( ip1, ip2, color, thickness ) ;
905  }
906  catch (...)
907  {
908  vpERROR_TRACE("Error caught") ;
909  throw ;
910  }
911  }
912 
913  else
914  {
915  vpERROR_TRACE("GTK not initialized " ) ;
917  "GTK not initialized")) ;
918  }
919 }
927  const vpImagePoint &ip2,
928  const vpColor &color,
929  unsigned int thickness )
930 {
931 
933  {
934  if ( thickness == 1 ) thickness = 0;
935 
936  if (color.id < vpColor::id_unknown)
937  gdk_gc_set_foreground(gc, col[color.id]);
938  else {
939  gdkcolor.red = 256 * color.R;
940  gdkcolor.green = 256 * color.G;
941  gdkcolor.blue = 256 * color.B;
942  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
943  gdk_gc_set_foreground(gc, &gdkcolor);
944  }
945 
946  gdk_gc_set_line_attributes(gc, (gint)thickness,
947  GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT,
948  GDK_JOIN_BEVEL) ;
949  gdk_draw_line(background, gc,
950  vpMath::round( ip1.get_u() ),
951  vpMath::round( ip1.get_v() ),
952  vpMath::round( ip2.get_u() ),
953  vpMath::round( ip2.get_v() ) );
954  gdk_gc_set_line_attributes(gc, 0,
955  GDK_LINE_SOLID, GDK_CAP_BUTT,
956  GDK_JOIN_BEVEL) ;
957  }
958  else
959  {
960  vpERROR_TRACE("GTK not initialized " ) ;
962  "GTK not initialized")) ;
963  }
964 }
965 
973  const vpImagePoint &ip2,
974  const vpColor &color,
975  unsigned int thickness )
976 {
978  {
979  if ( thickness == 1 ) thickness = 0;
980 
981  if (color.id < vpColor::id_unknown)
982  gdk_gc_set_foreground(gc, col[color.id]);
983  else {
984  gdkcolor.red = 256 * color.R;
985  gdkcolor.green = 256 * color.G;
986  gdkcolor.blue = 256 * color.B;
987  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
988  gdk_gc_set_foreground(gc, &gdkcolor);
989  }
990 
991  gdk_gc_set_line_attributes(gc, (gint)thickness,
992  GDK_LINE_SOLID, GDK_CAP_BUTT,
993  GDK_JOIN_BEVEL) ;
994  gdk_draw_line(background, gc,
995  vpMath::round( ip1.get_u() ),
996  vpMath::round( ip1.get_v() ),
997  vpMath::round( ip2.get_u() ),
998  vpMath::round( ip2.get_v() ) );
999  }
1000  else
1001  {
1002  vpERROR_TRACE("GTK not initialized " ) ;
1004  "GTK not initialized")) ;
1005  }
1006 }
1007 
1014  const vpColor &color )
1015 {
1017  {
1018  if (color.id < vpColor::id_unknown)
1019  gdk_gc_set_foreground(gc, col[color.id]);
1020  else {
1021  gdkcolor.red = 256 * color.R;
1022  gdkcolor.green = 256 * color.G;
1023  gdkcolor.blue = 256 * color.B;
1024  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1025  gdk_gc_set_foreground(gc, &gdkcolor);
1026  }
1027 
1028  gdk_draw_point(background,gc,
1029  vpMath::round( ip.get_u() ),
1030  vpMath::round( ip.get_v() ) );
1031  }
1032  else
1033  {
1034  vpERROR_TRACE("GTK not initialized " ) ;
1036  "GTK not initialized")) ;
1037  }
1038 }
1039 
1040 
1054 void
1056  unsigned int w, unsigned int h,
1057  const vpColor &color, bool fill,
1058  unsigned int thickness )
1059 {
1061  {
1062  if ( thickness == 1 ) thickness = 0;
1063 
1064  if (color.id < vpColor::id_unknown)
1065  gdk_gc_set_foreground(gc, col[color.id]);
1066  else {
1067  gdkcolor.red = 256 * color.R;
1068  gdkcolor.green = 256 * color.G;
1069  gdkcolor.blue = 256 * color.B;
1070  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1071  gdk_gc_set_foreground(gc, &gdkcolor);
1072  }
1073  gdk_gc_set_line_attributes(gc, (gint)thickness,
1074  GDK_LINE_SOLID, GDK_CAP_BUTT,
1075  GDK_JOIN_BEVEL) ;
1076 
1077  if (fill == false)
1078  gdk_draw_rectangle(background, gc, FALSE,
1079  vpMath::round( topLeft.get_u() ),
1080  vpMath::round( topLeft.get_v() ),
1081  (gint)w-1, (gint)h-1);
1082  else
1083  gdk_draw_rectangle(background, gc, TRUE,
1084  vpMath::round( topLeft.get_u() ),
1085  vpMath::round( topLeft.get_v() ),
1086  (gint)w, (gint)h);
1087 
1088  if (thickness > 1)
1089  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1090  GDK_JOIN_BEVEL) ;
1091  }
1092  else
1093  {
1094  vpERROR_TRACE("GTK not initialized " ) ;
1096  "GTK not initialized")) ;
1097  }
1098 }
1099 
1112 void
1114  const vpImagePoint &bottomRight,
1115  const vpColor &color, bool fill,
1116  unsigned int thickness )
1117 {
1119  {
1120  if ( thickness == 1 ) thickness = 0;
1121 
1122  if (color.id < vpColor::id_unknown)
1123  gdk_gc_set_foreground(gc, col[color.id]);
1124  else {
1125  gdkcolor.red = 256 * color.R;
1126  gdkcolor.green = 256 * color.G;
1127  gdkcolor.blue = 256 * color.B;
1128  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1129  gdk_gc_set_foreground(gc, &gdkcolor);
1130  }
1131 
1132  gdk_gc_set_line_attributes(gc, (gint)thickness,
1133  GDK_LINE_SOLID, GDK_CAP_BUTT,
1134  GDK_JOIN_BEVEL) ;
1135 
1136  int w = vpMath::round( bottomRight.get_u() - topLeft.get_u() );
1137  int h = vpMath::round( bottomRight.get_v() - topLeft.get_v() );
1138 
1139  if (fill == false)
1140  gdk_draw_rectangle(background, gc, FALSE,
1141  vpMath::round( topLeft.get_u() ),
1142  vpMath::round( topLeft.get_v() ),
1143  w-1,h-1);
1144  else
1145  gdk_draw_rectangle(background, gc, TRUE,
1146  vpMath::round( topLeft.get_u() ),
1147  vpMath::round( topLeft.get_v() ),
1148  w, h);
1149 
1150  if (thickness > 1)
1151  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1152  GDK_JOIN_BEVEL) ;
1153  }
1154  else
1155  {
1156  vpERROR_TRACE("GTK not initialized " ) ;
1158  "GTK not initialized")) ;
1159  }
1160 }
1161 
1174 void
1176  const vpColor &color, bool fill,
1177  unsigned int thickness )
1178 {
1180  {
1181  if (color.id < vpColor::id_unknown)
1182  gdk_gc_set_foreground(gc, col[color.id]);
1183  else {
1184  gdkcolor.red = 256 * color.R;
1185  gdkcolor.green = 256 * color.G;
1186  gdkcolor.blue = 256 * color.B;
1187  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1188  gdk_gc_set_foreground(gc, &gdkcolor);
1189  }
1190 
1191  if ( thickness == 1 ) thickness = 0;
1192 
1193  gdk_gc_set_line_attributes(gc, (gint)thickness, GDK_LINE_SOLID, GDK_CAP_BUTT,
1194  GDK_JOIN_BEVEL) ;
1195 
1196  if (fill == false)
1197  gdk_draw_rectangle(background, gc, FALSE,
1198  vpMath::round( rectangle.getLeft() ),
1199  vpMath::round( rectangle.getTop() ),
1200  vpMath::round( rectangle.getWidth()-1 ),
1201  vpMath::round( rectangle.getHeight()-1 ) );
1202 
1203  else
1204  gdk_draw_rectangle(background, gc, TRUE,
1205  vpMath::round( rectangle.getLeft() ),
1206  vpMath::round( rectangle.getTop() ),
1207  vpMath::round( rectangle.getWidth()-1 ),
1208  vpMath::round( rectangle.getHeight()-1 ) );
1209 
1210  if (thickness > 1)
1211  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1212  GDK_JOIN_BEVEL) ;
1213  }
1214  else
1215  {
1216  vpERROR_TRACE("GTK not initialized " ) ;
1218  "GTK not initialized")) ;
1219  }
1220 }
1221 
1222 
1238 bool
1240 {
1241  bool ret = false;
1242 
1243  int cpt =0;
1245 
1246 // flushDisplay() ;
1247  GdkEvent *ev = NULL;
1248  do {
1249  while ((ev = gdk_event_get())!=NULL){
1250  cpt++;
1251  // printf("event %d type %d on window %p My window %p\n",
1252  //cpt, ev->type, ev->any.window, widget->window);
1253 
1254  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1255  ret = true ;
1256  //printf("Click detection\n");
1257  }
1258  gdk_event_free(ev) ;
1259  }
1260  if (blocking){
1261  flushDisplay();
1262  vpTime::wait(100);
1263  }
1264  } while ( ret == false && blocking == true);
1265  }
1266  else {
1267  vpERROR_TRACE("GTK not initialized " ) ;
1269  "GTK not initialized")) ;
1270  }
1271  return ret;
1272 }
1273 
1290 bool
1292 {
1293  bool ret = false;
1294 
1296 
1297  GdkEvent *ev = NULL;
1298  double u, v ;
1299  do {
1300  while ((ev = gdk_event_get())!=NULL){
1301  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS) {
1302  u = ((GdkEventButton *)ev)->x ;
1303  v = ((GdkEventButton *)ev)->y ;
1304  ip.set_u( u );
1305  ip.set_v( v );
1306  ret = true ;
1307  }
1308  gdk_event_free(ev) ;
1309  }
1310  if (blocking){
1311  flushDisplay();
1312  vpTime::wait(100);
1313  }
1314  } while ( ret == false && blocking == true);
1315  }
1316  else {
1317  vpERROR_TRACE("GTK not initialized " ) ;
1319  "GTK not initialized")) ;
1320  }
1321  return ret ;
1322 }
1323 
1324 
1344 bool
1347  bool blocking)
1348 {
1349  bool ret = false;
1350 
1352  GdkEvent *ev = NULL;
1353  double u, v ;
1354  do {
1355  while ((ev = gdk_event_get())){
1356  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1357  u = ((GdkEventButton *)ev)->x ;
1358  v = ((GdkEventButton *)ev)->y ;
1359  ip.set_u( u );
1360  ip.set_v( v );
1361 
1362  switch ((int)((GdkEventButton *)ev)->button) {
1363  case 1:
1364  button = vpMouseButton::button1; break;
1365  case 2:
1366  button = vpMouseButton::button2; break;
1367  case 3:
1368  button = vpMouseButton::button3; break;
1369  }
1370  ret = true ;
1371  }
1372  gdk_event_free(ev) ;
1373  }
1374  if (blocking){
1375  flushDisplay();
1376  vpTime::wait(100);
1377  }
1378 
1379  } while ( ret == false && blocking == true);
1380  }
1381  else {
1382  vpERROR_TRACE("GTK not initialized " ) ;
1384  "GTK not initialized")) ;
1385  }
1386  return ret;
1387 }
1388 
1412 bool
1415  bool blocking)
1416 {
1417  bool ret = false;
1418 
1419  if ( displayHasBeenInitialized ) {
1420 
1421  //flushDisplay() ;
1422  GdkEvent *ev = NULL;
1423  double u, v ;
1424  do {
1425  while ((ev = gdk_event_get())!=NULL){
1426  if ( ev->any.window == widget->window
1427  && ev->type == GDK_BUTTON_RELEASE) {
1428  u = ((GdkEventButton *)ev)->x ;
1429  v = ((GdkEventButton *)ev)->y ;
1430  ip.set_u( u );
1431  ip.set_v( v );
1432 
1433  switch ( ( int ) ( ( GdkEventButton * ) ev )->button ) {
1434  case 1:
1435  button = vpMouseButton::button1; break;
1436  case 2:
1437  button = vpMouseButton::button2; break;
1438  case 3:
1439  button = vpMouseButton::button3; break;
1440  }
1441  ret = true ;
1442  }
1443  gdk_event_free(ev) ;
1444  }
1445  if (blocking){
1446  flushDisplay();
1447  vpTime::wait(100);
1448  }
1449 
1450  } while ( ret == false && blocking == true);
1451  }
1452  else {
1453  vpERROR_TRACE ( "GTK not initialized " ) ;
1455  "GTK not initialized" ) ) ;
1456  }
1457  return ret;
1458 }
1459 
1460 /*
1461  \brief gets the displayed image (including the overlay plane)
1462  and returns an RGBa image
1463 */
1465 {
1466 
1467 
1468  // shoudl certainly be optimized.
1469  // doesn't work
1471  {
1472 
1473  GdkImage *ImageGtk;
1474  /*
1475  */
1476 
1477  ImageGtk = gdk_image_get(background, 0, 0, (gint)width, (gint)height);
1478 
1479 
1480  I.resize(height,width) ;
1481  guchar *pos;
1482  guint32 pixel;
1483  gint x,y;
1484  guchar OctetRouge,OctetVert,OctetBleu,mask;
1485  mask = 0x000000FF;
1486 
1487  pos = (unsigned char *)I.bitmap;
1488  for (y=0;y<(gint)height;y++)
1489  {
1490  for (x=0;x<(gint)width;x++)
1491  {
1492  pixel = gdk_image_get_pixel(ImageGtk,x,y);
1493  OctetBleu = (guchar)pixel & mask;
1494  OctetVert = (guchar)(pixel>>8) & mask;
1495  OctetRouge = (guchar)(pixel>>16) & mask;
1496  *pos++ = OctetRouge;
1497  *pos++ = OctetVert;
1498  *pos++ = OctetBleu;
1499  *pos++ = 0;
1500  }
1501  }
1502 
1503 
1504  }
1505  else
1506  {
1507  vpERROR_TRACE("GTK not initialized " ) ;
1509  "GTK not initialized")) ;
1510  }
1511 
1512 }
1513 
1520 {
1521 
1522  unsigned int depth;
1523 
1524  depth = (unsigned int)gdk_window_get_visual(widget->window)->depth ;
1525 
1526  return (depth);
1527 }
1528 
1534 void vpDisplayGTK::getScreenSize(unsigned int &w, unsigned int &h)
1535 {
1536  vpTRACE("Not implemented") ;
1537  w = 0;
1538  h = 0;
1539 }
1540 
1556 bool
1558 {
1559  bool ret = false;
1560 
1561  int cpt =0;
1563 
1564  GdkEvent *ev = NULL;
1565  do {
1566  while ((ev = gdk_event_get())!=NULL){
1567  cpt++;
1568  // printf("event %d type %d on window %p My window %p\n",
1569  //cpt, ev->type, ev->any.window, widget->window);
1570 
1571  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1572  ret = true ;
1573  //printf("Key press detection\n");
1574  }
1575  gdk_event_free(ev) ;
1576  }
1577  if (blocking){
1578  flushDisplay();
1579  vpTime::wait(100);
1580  }
1581  } while ( ret == false && blocking == true);
1582  }
1583  else {
1584  vpERROR_TRACE("GTK not initialized " ) ;
1586  "GTK not initialized")) ;
1587  }
1588  return ret;
1589 }
1590 
1610 bool
1611 vpDisplayGTK::getKeyboardEvent(char *string, bool blocking)
1612 {
1613  bool ret = false;
1614 
1615  int cpt =0;
1617 
1618  GdkEvent *ev = NULL;
1619  do {
1620  while ((ev = gdk_event_get())!=NULL){
1621  cpt++;
1622  // printf("event %d type %d on window %p My window %p\n",
1623  //cpt, ev->type, ev->any.window, widget->window);
1624 
1625  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1626  //std::cout << "Key val: \"" << gdk_keyval_name (ev->key.keyval) /*ev->key.string*/ << "\"" << std::endl;
1627  sprintf(string, "%s", gdk_keyval_name (ev->key.keyval));
1628  ret = true ;
1629  //printf("Key press detection\n");
1630  }
1631  gdk_event_free(ev) ;
1632  }
1633  if (blocking){
1634  flushDisplay();
1635  vpTime::wait(100);
1636  }
1637  } while ( ret == false && blocking == true);
1638  }
1639  else {
1640  vpERROR_TRACE("GTK not initialized " ) ;
1642  "GTK not initialized")) ;
1643  }
1644  return ret;
1645 }
1646 
1659 bool
1661 {
1662  bool ret = false;
1663 
1665  GdkEvent *ev = NULL;
1666  double u, v ;
1667  if ((ev = gdk_event_get())){
1668  if (ev->any.window == widget->window && ev->type == GDK_MOTION_NOTIFY){
1669  u = ((GdkEventMotion *)ev)->x ;
1670  v = ((GdkEventMotion *)ev)->y ;
1671  ip.set_u( u );
1672  ip.set_v( v );
1673 
1674  ret = true ;
1675  }
1676  gdk_event_free(ev) ;
1677  }
1678  }
1679  else {
1680  vpERROR_TRACE("GTK not initialized " ) ;
1682  "GTK not initialized")) ;
1683  }
1684  return ret;
1685 }
1686 
1697 bool
1699 {
1701  {
1702  int u,v;
1703  gdk_window_get_pointer(widget->window, &u, &v, NULL);
1704  ip.set_u( u );
1705  ip.set_v( v );
1706  }
1707  else {
1708  vpERROR_TRACE("GTK not initialized " ) ;
1710  "GTK not initialized")) ;
1711  }
1712 
1713  return true;
1714 }
1715 
1716 #endif
1717 
1718 /*
1719  * Local variables:
1720  * c-basic-offset: 2
1721  * End:
1722  */
vpDisplay * display
Definition: vpImage.h:121
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
double getTop() const
Definition: vpRect.h:180
double get_v() const
Definition: vpImagePoint.h:264
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:174
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:183
double get_i() const
Definition: vpImagePoint.h:195
unsigned int getWidth() const
Definition: vpImage.h:161
#define vpERROR_TRACE
Definition: vpDebug.h:395
unsigned char B
Blue component.
Definition: vpRGBa.h:148
#define vpTRACE
Definition: vpDebug.h:418
Type * bitmap
points toward the bitmap
Definition: vpImage.h:120
void setFont(const char *fontname)
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
double get_u() const
Definition: vpImagePoint.h:253
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:155
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:178
unsigned char G
Green component.
Definition: vpRGBa.h:147
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static int round(const double x)
Definition: vpMath.h:228
double get_j() const
Definition: vpImagePoint.h:206
std::string title_
Definition: vpDisplay.h:185
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
vpColorIdentifier id
Definition: vpColor.h:156
void set_i(const double ii)
Definition: vpImagePoint.h:159
double getWidth() const
Definition: vpRect.h:199
void flushDisplay()
void set_u(const double u)
Definition: vpImagePoint.h:217
static double sqr(double x)
Definition: vpMath.h:106
virtual ~vpDisplayGTK()
unsigned int height
Definition: vpDisplay.h:184
void set_v(const double v)
Definition: vpImagePoint.h:228
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:536
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:170
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:133
void setWindowPosition(int winx, int winy)
int windowXPosition
display position
Definition: vpDisplay.h:180
unsigned char R
Red component.
Definition: vpRGBa.h:146
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:85
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:93
void setTitle(const char *title)
bool getKeyboardEvent(bool blocking=true)
int windowYPosition
display position
Definition: vpDisplay.h:182
double getLeft() const
Definition: vpRect.h:161
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
Definition: vpImagePoint.h:275