ViSP  2.6.2
vpDisplayGTK.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayGTK.cpp 3590 2012-03-05 09:48:31Z ayol $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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) : vpDisplay()
84 {
85  col = NULL;
86  title = NULL ;
87  widget = NULL ;
88  init(I, x, y, title) ;
89 }
90 
91 
101  int x,
102  int y,
103  const char *title)
104 {
105  col = NULL;
106  title = NULL ;
107  widget = NULL ;
108  init(I, x, y, title) ;
109 }
110 
111 
112 
135 vpDisplayGTK::vpDisplayGTK(int x, int y, const char *title)
136 {
137  windowXPosition = x ;
138  windowYPosition = y ;
139 
140  col = NULL;
141  title = NULL ;
142  widget = NULL ;
143 
144  if (title != NULL)
145  {
146  this->title = new char[strlen(title) + 1] ; // Modif Fabien le 19/04/02
147  strcpy(this->title, title) ;
148  }
149 
150  displayHasBeenInitialized = false ;
151 }
152 
173 {
174  windowXPosition = windowYPosition = -1 ;
175 
176  col = NULL;
177  title = NULL ;
178  widget = NULL ;
179  if (title != NULL)
180  {
181  delete [] title ;
182  title = NULL ;
183  }
184  title = new char[1] ;
185  strcpy(title,"") ;
186 
187  displayHasBeenInitialized = false ;
188 }
189 
194 {
195  closeDisplay() ;
196 }
197 
206 void
208  int x,
209  int y,
210  const char *title)
211 {
212 
213  if ((I.getHeight() == 0) || (I.getWidth()==0))
214  {
215  vpERROR_TRACE("Image not initialized " ) ;
217  "Image not initialized")) ;
218  }
219  init (I.getWidth(), I.getHeight(), x, y, title) ;
220  I.display = this ;
222 }
223 
233 void
235  int x,
236  int y,
237  const char *title)
238 {
239  if ((I.getHeight() == 0) || (I.getWidth()==0))
240  {
241  vpERROR_TRACE("Image not initialized " ) ;
243  "Image not initialized")) ;
244  }
245 
246  init (I.getWidth(), I.getHeight(), x, y, title) ;
247  I.display = this ;
249 }
258 void
259 vpDisplayGTK::init(unsigned int width, unsigned int height,
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 = width;
269  this->height = height;
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  gtk_window_move(GTK_WINDOW(widget), x, y);
280 
281  gtk_widget_show(widget);
282 
283  gdk_rgb_init();
284 
285  /* Create background pixmap */
286  background = gdk_pixmap_new(widget->window, (gint)width, (gint)height, -1);
287 
288  /* Create graphic context */
289  gc = gdk_gc_new(widget->window);
290 
291  /* get the colormap */
292  colormap = gdk_window_get_colormap(widget->window);
293 
294  col = new GdkColor *[vpColor::id_unknown] ; // id_unknown = number of predefined colors
295 
296  /* Create color */
297  gdk_color_parse("light blue",&lightBlue);
298  gdk_colormap_alloc_color(colormap,&lightBlue,FALSE,TRUE);
299  col[vpColor::id_lightBlue] = &lightBlue ;
300 
301  gdk_color_parse("blue",&blue);
302  gdk_colormap_alloc_color(colormap,&blue,FALSE,TRUE);
303  col[vpColor::id_blue] = &blue ;
304 
305  gdk_color_parse("dark blue",&darkBlue);
306  gdk_colormap_alloc_color(colormap,&darkBlue,FALSE,TRUE);
307  col[vpColor::id_darkBlue] = &darkBlue ;
308 
309  gdk_color_parse("#FF8C8C",&lightRed);
310  gdk_colormap_alloc_color(colormap,&lightRed,FALSE,TRUE);
311  col[vpColor::id_lightRed] = &lightRed ;
312 
313  gdk_color_parse("red",&red);
314  gdk_colormap_alloc_color(colormap,&red,FALSE,TRUE);
315  col[vpColor::id_red] = &red ;
316 
317  gdk_color_parse("dark red",&darkRed);
318  gdk_colormap_alloc_color(colormap,&darkRed,FALSE,TRUE);
319  col[vpColor::id_darkRed] = &darkRed ;
320 
321  gdk_color_parse("light green",&lightGreen);
322  gdk_colormap_alloc_color(colormap,&lightGreen,FALSE,TRUE);
323  col[vpColor::id_lightGreen] = &lightGreen ;
324 
325  gdk_color_parse("green",&green);
326  gdk_colormap_alloc_color(colormap,&green,FALSE,TRUE);
327  col[vpColor::id_green] = &green ;
328 
329  gdk_color_parse("dark green",&darkGreen);
330  gdk_colormap_alloc_color(colormap,&darkGreen,FALSE,TRUE);
331  col[vpColor::id_darkGreen] = &darkGreen ;
332 
333  gdk_color_parse("yellow",&yellow);
334  gdk_colormap_alloc_color(colormap,&yellow,FALSE,TRUE);
335  col[vpColor::id_yellow] = &yellow ;
336 
337  gdk_color_parse("cyan",&cyan);
338  gdk_colormap_alloc_color(colormap,&cyan,FALSE,TRUE);
339  col[vpColor::id_cyan] = &cyan ;
340 
341  gdk_color_parse("orange",&orange);
342  gdk_colormap_alloc_color(colormap,&orange,FALSE,TRUE);
343  col[vpColor::id_orange] = &orange ;
344 
345  gdk_color_parse("purple",&purple);
346  gdk_colormap_alloc_color(colormap,&purple,FALSE,TRUE);
347  col[vpColor::id_purple] = &purple ;
348 
349  gdk_color_parse("white",&white);
350  gdk_colormap_alloc_color(colormap,&white,FALSE,TRUE);
351  col[vpColor::id_white] = &white ;
352 
353  gdk_color_parse("black",&black);
354  gdk_colormap_alloc_color(colormap,&black,FALSE,TRUE);
355  col[vpColor::id_black] = &black ;
356 
357  gdk_color_parse("#C0C0C0",&lightGray);
358  gdk_colormap_alloc_color(colormap,&lightGray,FALSE,TRUE);
359  col[vpColor::id_lightGray] = &lightGray ;
360 
361  gdk_color_parse("#808080",&gray);
362  gdk_colormap_alloc_color(colormap,&gray,FALSE,TRUE);
363  col[vpColor::id_gray] = &gray ;
364 
365  gdk_color_parse("#404040",&darkGray);
366  gdk_colormap_alloc_color(colormap,&darkGray,FALSE,TRUE);
367  col[vpColor::id_darkGray] = &darkGray ;
368 
369  /* Chargement des polices */
370  Police1 = gdk_font_load("-*-times-medium-r-normal-*-16-*-*-*-*-*-*-*");
371  Police2 = gdk_font_load("-*-courier-bold-r-normal-*-*-140-*-*-*-*-*-*");
372 
373  if (title != NULL)
374  {
375  if (this->title != NULL)
376  {
377  delete [] this->title ;
378  this->title = NULL ;
379  }
380  this->title = new char[strlen(title) + 1] ;
381  strcpy(this->title, title) ;
382  }
384  setTitle(title) ;
385 }
386 
387 
402 void
403 vpDisplayGTK::setFont(const char * /* font */)
404 {
405  vpERROR_TRACE("Not yet implemented" ) ;
406 }
407 
412 void
413 vpDisplayGTK::setTitle(const char *title)
414 {
416  {
417  if (title != NULL)
418  gdk_window_set_title(widget->window,(char *)title);
419  }
420  else
421  {
422  vpERROR_TRACE("GTK not initialized " ) ;
424  "GTK not initialized")) ;
425  }
426 }
427 
436 void vpDisplayGTK::setWindowPosition(int winx, int winy)
437 {
438 
440  gtk_window_move(GTK_WINDOW(widget), winx, winy);
441  }
442  else
443  {
444  vpERROR_TRACE("GTK not initialized " ) ;
446  "GTK not initialized")) ;
447  }
448 }
449 
450 
463 {
464 
466  {
467  /* Copie de l'image dans le pixmap fond */
468  gdk_draw_gray_image(background,
469  gc, 0, 0, (gint)width, (gint)height,
470  GDK_RGB_DITHER_NONE,
471  I.bitmap,
472  (gint)width);
473 
474  /* Le pixmap background devient le fond de la zone de dessin */
475  gdk_window_set_back_pixmap(widget->window, background, FALSE);
476 
477  /* Affichage */
478  //gdk_window_clear(GTK_WINDOW(widget));
479  //gdk_flush();
480  }
481  else
482  {
483  vpERROR_TRACE("GTK not initialized " ) ;
485  "GTK not initialized")) ;
486  }
487 }
488 
489 
507 void vpDisplayGTK::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
508 {
510  {
512  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Itemp);
513  /* Copie de l'image dans le pixmap fond */
514  gdk_draw_gray_image(background,
515  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)width, (gint)height,
516  GDK_RGB_DITHER_NONE,
517  I.bitmap,
518  (gint)width);
519 
520  /* Le pixmap background devient le fond de la zone de dessin */
521  gdk_window_set_back_pixmap(widget->window, background, FALSE);
522 
523  /* Affichage */
524  //gdk_window_clear(GTK_WINDOW(widget));
525  //gdk_flush();
526  }
527  else
528  {
529  vpERROR_TRACE("GTK not initialized " ) ;
531  "GTK not initialized")) ;
532  }
533 }
534 
535 
548 {
549 
551  {
552 
553  /* Copie de l'image dans le pixmap fond */
554  gdk_draw_rgb_32_image(background,
555  gc, 0, 0, (gint)width, (gint)height,
556  GDK_RGB_DITHER_NONE,
557  (unsigned char *)I.bitmap,
558  (gint)(4*width));
559 
560  /* Permet de fermer la fen�tre si besoin (cas des s�quences d'images) */
561  //while (g_main_iteration(FALSE));
562 
563  /* Le pixmap background devient le fond de la zone de dessin */
564  gdk_window_set_back_pixmap(widget->window, background, FALSE);
565 
566  /* Affichage */
567  //gdk_window_clear(GTK_WINDOW(widget));
568  //flushDisplay() ;
569 
570  }
571  else
572  {
573  vpERROR_TRACE("GTK not initialized " ) ;
575  "GTK not initialized")) ;
576  }
577 }
578 
596 void vpDisplayGTK::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
597 {
599  {
600  vpImage<vpRGBa> Itemp;
601  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Itemp);
602  /* Copie de l'image dans le pixmap fond */
603  gdk_draw_rgb_32_image(background,
604  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)width, (gint)height,
605  GDK_RGB_DITHER_NONE,
606  (unsigned char *)Itemp.bitmap,
607  (gint)(4*width));
608 
609  /* Permet de fermer la fen�tre si besoin (cas des s�quences d'images) */
610  //while (g_main_iteration(FALSE));
611 
612  /* Le pixmap background devient le fond de la zone de dessin */
613  gdk_window_set_back_pixmap(widget->window, background, FALSE);
614 
615  /* Affichage */
616  //gdk_window_clear(GTK_WINDOW(widget));
617  //flushDisplay() ;
618  }
619  else
620  {
621  vpERROR_TRACE("GTK not initialized " ) ;
623  "GTK not initialized")) ;
624  }
625 }
626 
632 void vpDisplayGTK::displayImage(const unsigned char * /* I */)
633 {
634  vpTRACE(" not implemented ") ;
635 }
636 
643 {
644  if (col != NULL)
645  {
646  delete [] col ; col = NULL ;
647  }
648  if (title != NULL)
649  {
650  delete [] title ;
651  title = NULL ;
652  }
653 
654  if (widget != NULL)
655  {
656  gdk_window_hide (widget->window);
657  gdk_window_destroy(widget->window);
658  widget = NULL;
659  }
661 }
662 
663 
669 {
671  {
672  gdk_window_clear(widget->window);
673  gdk_flush();
674  }
675  else
676  {
677  vpERROR_TRACE("GTK not initialized " ) ;
679  "GTK not initialized")) ;
680  }
681 }
682 
683 
688 void vpDisplayGTK::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
689 {
691  {
692  gdk_window_clear(widget->window);
693  gdk_flush();
694  }
695  else
696  {
697  vpERROR_TRACE("GTK not initialized " ) ;
699  "GTK not initialized")) ;
700  }
701 }
702 
703 
707 void vpDisplayGTK::clearDisplay(const vpColor & /* color */)
708 {
709  vpTRACE("Not implemented") ;
710 }
711 
720  const vpImagePoint &ip2,
721  const vpColor &color,
722  unsigned int w, unsigned int h,
723  unsigned int thickness)
724 {
726  {
727  try{
728  double a = ip2.get_i() - ip1.get_i() ;
729  double b = ip2.get_j() - ip1.get_j() ;
730  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
731 
732  //if ((a==0)&&(b==0))
733  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon() )&&(std::fabs(b) <= std::numeric_limits<double>::epsilon()) )
734  {
735  // DisplayCrossLarge(i1,j1,3,col) ;
736  }
737  else
738  {
739  a /= lg ;
740  b /= lg ;
741 
742  vpImagePoint ip3;
743  ip3.set_i(ip2.get_i() - w*a);
744  ip3.set_j(ip2.get_j() - w*b);
745 
746  vpImagePoint ip4;
747  ip4.set_i( ip3.get_i() - b*h );
748  ip4.set_j( ip3.get_j() + a*h );
749 
750  displayLine ( ip2, ip4, color, thickness ) ;
751 
752  ip4.set_i( ip3.get_i() + b*h );
753  ip4.set_j( ip3.get_j() - a*h );
754 
755  displayLine ( ip2, ip4, color, thickness ) ;
756  displayLine ( ip1, ip2, color, thickness ) ;
757  }
758  }
759  catch (...)
760  {
761  vpERROR_TRACE("Error caught") ;
762  throw ;
763  }
764  }
765  else
766  {
767  vpERROR_TRACE("GTK not initialized " ) ;
769  "GTK not initialized")) ;
770  }
771 }
772 
773 
786  const char *text,
787  const vpColor &color )
788 {
790  {
791  if (color.id < vpColor::id_unknown)
792  gdk_gc_set_foreground(gc, col[color.id]);
793  else {
794  gdkcolor.red = 256 * color.R;
795  gdkcolor.green = 256 * color.G;
796  gdkcolor.blue = 256 * color.B;
797  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
798  gdk_gc_set_foreground(gc, &gdkcolor);
799  }
800 
801  gdk_draw_string(background, Police2, gc,
802  vpMath::round( ip.get_u() ),
803  vpMath::round( ip.get_v() ),
804  (const gchar *)text);
805 
806  }
807  else
808  {
809  vpERROR_TRACE("GTK not initialized " ) ;
811  "GTK not initialized")) ;
812  }
813 }
824  unsigned int radius,
825  const vpColor &color,
826  bool fill,
827  unsigned int thickness )
828 {
830  {
831  if ( thickness == 1 ) thickness = 0;
832 
833  if (color.id < vpColor::id_unknown)
834  gdk_gc_set_foreground(gc, col[color.id]);
835  else {
836  gdkcolor.red = 256 * color.R;
837  gdkcolor.green = 256 * color.G;
838  gdkcolor.blue = 256 * color.B;
839  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
840  gdk_gc_set_foreground(gc, &gdkcolor);
841  }
842 
843  gdk_gc_set_line_attributes(gc, (gint)thickness,
844  GDK_LINE_SOLID, GDK_CAP_BUTT,
845  GDK_JOIN_BEVEL) ;
846 
847  if (fill == false)
848  gdk_draw_arc(background, gc, FALSE,
849  vpMath::round( center.get_u()-radius ),
850  vpMath::round( center.get_v()-radius ),
851  (gint)(2*radius), (gint)(2*radius), 23040, 23040) ; /* 23040 = 360*64 */
852  else
853  gdk_draw_arc(background, gc, TRUE,
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  }
858  else
859  {
860  vpERROR_TRACE("GTK not initialized " ) ;
862  "GTK not initialized")) ;
863  }
864 }
873  unsigned int size,
874  const vpColor &color,
875  unsigned int thickness)
876 {
878  {
879  try{
880  double i = ip.get_i();
881  double j = ip.get_j();
882  vpImagePoint ip1, ip2;
883 
884  ip1.set_i( i-size/2 );
885  ip1.set_j( j );
886  ip2.set_i( i+size/2 );
887  ip2.set_j( j );
888  displayLine ( ip1, ip2, color, thickness ) ;
889 
890  ip1.set_i( i );
891  ip1.set_j( j-size/2 );
892  ip2.set_i( i );
893  ip2.set_j( j+size/2 );
894 
895  displayLine ( ip1, ip2, color, thickness ) ;
896  }
897  catch (...)
898  {
899  vpERROR_TRACE("Error caught") ;
900  throw ;
901  }
902  }
903 
904  else
905  {
906  vpERROR_TRACE("GTK not initialized " ) ;
908  "GTK not initialized")) ;
909  }
910 }
918  const vpImagePoint &ip2,
919  const vpColor &color,
920  unsigned int thickness )
921 {
922 
924  {
925  if ( thickness == 1 ) thickness = 0;
926 
927  if (color.id < vpColor::id_unknown)
928  gdk_gc_set_foreground(gc, col[color.id]);
929  else {
930  gdkcolor.red = 256 * color.R;
931  gdkcolor.green = 256 * color.G;
932  gdkcolor.blue = 256 * color.B;
933  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
934  gdk_gc_set_foreground(gc, &gdkcolor);
935  }
936 
937  gdk_gc_set_line_attributes(gc, (gint)thickness,
938  GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT,
939  GDK_JOIN_BEVEL) ;
940  gdk_draw_line(background, gc,
941  vpMath::round( ip1.get_u() ),
942  vpMath::round( ip1.get_v() ),
943  vpMath::round( ip2.get_u() ),
944  vpMath::round( ip2.get_v() ) );
945  gdk_gc_set_line_attributes(gc, 0,
946  GDK_LINE_SOLID, GDK_CAP_BUTT,
947  GDK_JOIN_BEVEL) ;
948  }
949  else
950  {
951  vpERROR_TRACE("GTK not initialized " ) ;
953  "GTK not initialized")) ;
954  }
955 }
956 
964  const vpImagePoint &ip2,
965  const vpColor &color,
966  unsigned int thickness )
967 {
969  {
970  if ( thickness == 1 ) thickness = 0;
971 
972  if (color.id < vpColor::id_unknown)
973  gdk_gc_set_foreground(gc, col[color.id]);
974  else {
975  gdkcolor.red = 256 * color.R;
976  gdkcolor.green = 256 * color.G;
977  gdkcolor.blue = 256 * color.B;
978  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
979  gdk_gc_set_foreground(gc, &gdkcolor);
980  }
981 
982  gdk_gc_set_line_attributes(gc, (gint)thickness,
983  GDK_LINE_SOLID, GDK_CAP_BUTT,
984  GDK_JOIN_BEVEL) ;
985  gdk_draw_line(background, gc,
986  vpMath::round( ip1.get_u() ),
987  vpMath::round( ip1.get_v() ),
988  vpMath::round( ip2.get_u() ),
989  vpMath::round( ip2.get_v() ) );
990  }
991  else
992  {
993  vpERROR_TRACE("GTK not initialized " ) ;
995  "GTK not initialized")) ;
996  }
997 }
998 
1005  const vpColor &color )
1006 {
1008  {
1009  if (color.id < vpColor::id_unknown)
1010  gdk_gc_set_foreground(gc, col[color.id]);
1011  else {
1012  gdkcolor.red = 256 * color.R;
1013  gdkcolor.green = 256 * color.G;
1014  gdkcolor.blue = 256 * color.B;
1015  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1016  gdk_gc_set_foreground(gc, &gdkcolor);
1017  }
1018 
1019  gdk_draw_point(background,gc,
1020  vpMath::round( ip.get_u() ),
1021  vpMath::round( ip.get_v() ) );
1022  }
1023  else
1024  {
1025  vpERROR_TRACE("GTK not initialized " ) ;
1027  "GTK not initialized")) ;
1028  }
1029 }
1030 
1031 
1045 void
1047  unsigned int width, unsigned int height,
1048  const vpColor &color, bool fill,
1049  unsigned int thickness )
1050 {
1052  {
1053  if ( thickness == 1 ) thickness = 0;
1054 
1055  if (color.id < vpColor::id_unknown)
1056  gdk_gc_set_foreground(gc, col[color.id]);
1057  else {
1058  gdkcolor.red = 256 * color.R;
1059  gdkcolor.green = 256 * color.G;
1060  gdkcolor.blue = 256 * color.B;
1061  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1062  gdk_gc_set_foreground(gc, &gdkcolor);
1063  }
1064  gdk_gc_set_line_attributes(gc, (gint)thickness,
1065  GDK_LINE_SOLID, GDK_CAP_BUTT,
1066  GDK_JOIN_BEVEL) ;
1067 
1068  if (fill == false)
1069  gdk_draw_rectangle(background, gc, FALSE,
1070  vpMath::round( topLeft.get_u() ),
1071  vpMath::round( topLeft.get_v() ),
1072  (gint)width-1, (gint)height-1);
1073  else
1074  gdk_draw_rectangle(background, gc, TRUE,
1075  vpMath::round( topLeft.get_u() ),
1076  vpMath::round( topLeft.get_v() ),
1077  (gint)width, (gint)height);
1078 
1079  if (thickness > 1)
1080  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1081  GDK_JOIN_BEVEL) ;
1082  }
1083  else
1084  {
1085  vpERROR_TRACE("GTK not initialized " ) ;
1087  "GTK not initialized")) ;
1088  }
1089 }
1090 
1103 void
1105  const vpImagePoint &bottomRight,
1106  const vpColor &color, bool fill,
1107  unsigned int thickness )
1108 {
1110  {
1111  if ( thickness == 1 ) thickness = 0;
1112 
1113  if (color.id < vpColor::id_unknown)
1114  gdk_gc_set_foreground(gc, col[color.id]);
1115  else {
1116  gdkcolor.red = 256 * color.R;
1117  gdkcolor.green = 256 * color.G;
1118  gdkcolor.blue = 256 * color.B;
1119  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1120  gdk_gc_set_foreground(gc, &gdkcolor);
1121  }
1122 
1123  gdk_gc_set_line_attributes(gc, (gint)thickness,
1124  GDK_LINE_SOLID, GDK_CAP_BUTT,
1125  GDK_JOIN_BEVEL) ;
1126 
1127  int width = vpMath::round( bottomRight.get_u() - topLeft.get_u() );
1128  int height = vpMath::round( bottomRight.get_v() - topLeft.get_v() );
1129 
1130  if (fill == false)
1131  gdk_draw_rectangle(background, gc, FALSE,
1132  vpMath::round( topLeft.get_u() ),
1133  vpMath::round( topLeft.get_v() ),
1134  width-1,height-1);
1135  else
1136  gdk_draw_rectangle(background, gc, TRUE,
1137  vpMath::round( topLeft.get_u() ),
1138  vpMath::round( topLeft.get_v() ),
1139  width, height);
1140 
1141  if (thickness > 1)
1142  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1143  GDK_JOIN_BEVEL) ;
1144  }
1145  else
1146  {
1147  vpERROR_TRACE("GTK not initialized " ) ;
1149  "GTK not initialized")) ;
1150  }
1151 }
1152 
1165 void
1167  const vpColor &color, bool fill,
1168  unsigned int thickness )
1169 {
1171  {
1172  if (color.id < vpColor::id_unknown)
1173  gdk_gc_set_foreground(gc, col[color.id]);
1174  else {
1175  gdkcolor.red = 256 * color.R;
1176  gdkcolor.green = 256 * color.G;
1177  gdkcolor.blue = 256 * color.B;
1178  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1179  gdk_gc_set_foreground(gc, &gdkcolor);
1180  }
1181 
1182  if ( thickness == 1 ) thickness = 0;
1183 
1184  gdk_gc_set_line_attributes(gc, (gint)thickness, GDK_LINE_SOLID, GDK_CAP_BUTT,
1185  GDK_JOIN_BEVEL) ;
1186 
1187  if (fill == false)
1188  gdk_draw_rectangle(background, gc, FALSE,
1189  vpMath::round( rectangle.getLeft() ),
1190  vpMath::round( rectangle.getTop() ),
1191  vpMath::round( rectangle.getWidth()-1 ),
1192  vpMath::round( rectangle.getHeight()-1 ) );
1193 
1194  else
1195  gdk_draw_rectangle(background, gc, TRUE,
1196  vpMath::round( rectangle.getLeft() ),
1197  vpMath::round( rectangle.getTop() ),
1198  vpMath::round( rectangle.getWidth()-1 ),
1199  vpMath::round( rectangle.getHeight()-1 ) );
1200 
1201  if (thickness > 1)
1202  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1203  GDK_JOIN_BEVEL) ;
1204  }
1205  else
1206  {
1207  vpERROR_TRACE("GTK not initialized " ) ;
1209  "GTK not initialized")) ;
1210  }
1211 }
1212 
1213 
1229 bool
1231 {
1232  bool ret = false;
1233 
1234  int cpt =0;
1236 
1237 // flushDisplay() ;
1238  GdkEvent *ev = NULL;
1239  do {
1240  while ((ev = gdk_event_get())!=NULL){
1241  cpt++;
1242  // printf("event %d type %d on window %p My window %p\n",
1243  //cpt, ev->type, ev->any.window, widget->window);
1244 
1245  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1246  ret = true ;
1247  //printf("Click detection\n");
1248  }
1249  gdk_event_free(ev) ;
1250  }
1251  if (blocking){
1252  flushDisplay();
1253  vpTime::wait(100);
1254  }
1255  } while ( ret == false && blocking == true);
1256  }
1257  else {
1258  vpERROR_TRACE("GTK not initialized " ) ;
1260  "GTK not initialized")) ;
1261  }
1262  return ret;
1263 }
1264 
1281 bool
1283 {
1284  bool ret = false;
1285 
1287 
1288  GdkEvent *ev = NULL;
1289  double u, v ;
1290  do {
1291  while ((ev = gdk_event_get())!=NULL){
1292  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS) {
1293  u = ((GdkEventButton *)ev)->x ;
1294  v = ((GdkEventButton *)ev)->y ;
1295  ip.set_u( u );
1296  ip.set_v( v );
1297  ret = true ;
1298  }
1299  gdk_event_free(ev) ;
1300  }
1301  if (blocking){
1302  flushDisplay();
1303  vpTime::wait(100);
1304  }
1305  } while ( ret == false && blocking == true);
1306  }
1307  else {
1308  vpERROR_TRACE("GTK not initialized " ) ;
1310  "GTK not initialized")) ;
1311  }
1312  return ret ;
1313 }
1314 
1315 
1335 bool
1338  bool blocking)
1339 {
1340  bool ret = false;
1341 
1343  GdkEvent *ev = NULL;
1344  double u, v ;
1345  do {
1346  while ((ev = gdk_event_get())){
1347  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1348  u = ((GdkEventButton *)ev)->x ;
1349  v = ((GdkEventButton *)ev)->y ;
1350  ip.set_u( u );
1351  ip.set_v( v );
1352 
1353  switch ((int)((GdkEventButton *)ev)->button) {
1354  case 1:
1355  button = vpMouseButton::button1; break;
1356  case 2:
1357  button = vpMouseButton::button2; break;
1358  case 3:
1359  button = vpMouseButton::button3; break;
1360  }
1361  ret = true ;
1362  }
1363  gdk_event_free(ev) ;
1364  }
1365  if (blocking){
1366  flushDisplay();
1367  vpTime::wait(100);
1368  }
1369 
1370  } while ( ret == false && blocking == true);
1371  }
1372  else {
1373  vpERROR_TRACE("GTK not initialized " ) ;
1375  "GTK not initialized")) ;
1376  }
1377  return ret;
1378 }
1379 
1403 bool
1406  bool blocking)
1407 {
1408  bool ret = false;
1409 
1410  if ( displayHasBeenInitialized ) {
1411 
1412  //flushDisplay() ;
1413  GdkEvent *ev = NULL;
1414  double u, v ;
1415  do {
1416  while ((ev = gdk_event_get())!=NULL){
1417  if ( ev->any.window == widget->window
1418  && ev->type == GDK_BUTTON_RELEASE) {
1419  u = ((GdkEventButton *)ev)->x ;
1420  v = ((GdkEventButton *)ev)->y ;
1421  ip.set_u( u );
1422  ip.set_v( v );
1423 
1424  switch ( ( int ) ( ( GdkEventButton * ) ev )->button ) {
1425  case 1:
1426  button = vpMouseButton::button1; break;
1427  case 2:
1428  button = vpMouseButton::button2; break;
1429  case 3:
1430  button = vpMouseButton::button3; break;
1431  }
1432  ret = true ;
1433  }
1434  gdk_event_free(ev) ;
1435  }
1436  if (blocking){
1437  flushDisplay();
1438  vpTime::wait(100);
1439  }
1440 
1441  } while ( ret == false && blocking == true);
1442  }
1443  else {
1444  vpERROR_TRACE ( "GTK not initialized " ) ;
1446  "GTK not initialized" ) ) ;
1447  }
1448  return ret;
1449 }
1450 
1451 /*
1452  \brief gets the displayed image (including the overlay plane)
1453  and returns an RGBa image
1454 */
1456 {
1457 
1458 
1459  // shoudl certainly be optimized.
1460  // doesn't work
1462  {
1463 
1464  GdkImage *ImageGtk;
1465  /*
1466  */
1467 
1468  ImageGtk = gdk_image_get(background, 0, 0, (gint)width, (gint)height);
1469 
1470 
1471  I.resize(height,width) ;
1472  guchar *pos;
1473  guint32 pixel;
1474  gint x,y;
1475  guchar OctetRouge,OctetVert,OctetBleu,mask;
1476  mask = 0x000000FF;
1477 
1478  pos = (unsigned char *)I.bitmap;
1479  for (y=0;y<(gint)height;y++)
1480  {
1481  for (x=0;x<(gint)width;x++)
1482  {
1483  pixel = gdk_image_get_pixel(ImageGtk,x,y);
1484  OctetBleu = (guchar)pixel & mask;
1485  OctetVert = (guchar)(pixel>>8) & mask;
1486  OctetRouge = (guchar)(pixel>>16) & mask;
1487  *pos++ = OctetRouge;
1488  *pos++ = OctetVert;
1489  *pos++ = OctetBleu;
1490  *pos++ = 0;
1491  }
1492  }
1493 
1494 
1495  }
1496  else
1497  {
1498  vpERROR_TRACE("GTK not initialized " ) ;
1500  "GTK not initialized")) ;
1501  }
1502 
1503 }
1504 
1511 {
1512 
1513  unsigned int depth;
1514 
1515  depth = (unsigned int)gdk_window_get_visual(widget->window)->depth ;
1516 
1517  return (depth);
1518 }
1519 
1525 void vpDisplayGTK::getScreenSize(unsigned int &width, unsigned int &height)
1526 {
1527  vpTRACE("Not implemented") ;
1528  width = 0;
1529  height = 0;
1530 }
1531 
1547 bool
1549 {
1550  bool ret = false;
1551 
1552  int cpt =0;
1554 
1555  GdkEvent *ev = NULL;
1556  do {
1557  while ((ev = gdk_event_get())!=NULL){
1558  cpt++;
1559  // printf("event %d type %d on window %p My window %p\n",
1560  //cpt, ev->type, ev->any.window, widget->window);
1561 
1562  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1563  ret = true ;
1564  //printf("Key press detection\n");
1565  }
1566  gdk_event_free(ev) ;
1567  }
1568  if (blocking){
1569  flushDisplay();
1570  vpTime::wait(100);
1571  }
1572  } while ( ret == false && blocking == true);
1573  }
1574  else {
1575  vpERROR_TRACE("GTK not initialized " ) ;
1577  "GTK not initialized")) ;
1578  }
1579  return ret;
1580 }
1581 
1601 bool
1602 vpDisplayGTK::getKeyboardEvent(char *string, bool blocking)
1603 {
1604  bool ret = false;
1605 
1606  int cpt =0;
1608 
1609  GdkEvent *ev = NULL;
1610  do {
1611  while ((ev = gdk_event_get())!=NULL){
1612  cpt++;
1613  // printf("event %d type %d on window %p My window %p\n",
1614  //cpt, ev->type, ev->any.window, widget->window);
1615 
1616  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1617  //std::cout << "Key val: \"" << gdk_keyval_name (ev->key.keyval) /*ev->key.string*/ << "\"" << std::endl;
1618  sprintf(string, "%s", gdk_keyval_name (ev->key.keyval));
1619  ret = true ;
1620  //printf("Key press detection\n");
1621  }
1622  gdk_event_free(ev) ;
1623  }
1624  if (blocking){
1625  flushDisplay();
1626  vpTime::wait(100);
1627  }
1628  } while ( ret == false && blocking == true);
1629  }
1630  else {
1631  vpERROR_TRACE("GTK not initialized " ) ;
1633  "GTK not initialized")) ;
1634  }
1635  return ret;
1636 }
1637 
1650 bool
1652 {
1653  bool ret = false;
1654 
1656  GdkEvent *ev = NULL;
1657  double u, v ;
1658  if ((ev = gdk_event_get())){
1659  if (ev->any.window == widget->window && ev->type == GDK_MOTION_NOTIFY){
1660  u = ((GdkEventMotion *)ev)->x ;
1661  v = ((GdkEventMotion *)ev)->y ;
1662  ip.set_u( u );
1663  ip.set_v( v );
1664 
1665  ret = true ;
1666  }
1667  gdk_event_free(ev) ;
1668  }
1669  }
1670  else {
1671  vpERROR_TRACE("GTK not initialized " ) ;
1673  "GTK not initialized")) ;
1674  }
1675  return ret;
1676 }
1677 
1688 bool
1690 {
1692  {
1693  int u,v;
1694  gdk_window_get_pointer(widget->window, &u, &v, NULL);
1695  ip.set_u( u );
1696  ip.set_v( v );
1697  }
1698  else {
1699  vpERROR_TRACE("GTK not initialized " ) ;
1701  "GTK not initialized")) ;
1702  }
1703 
1704  return true;
1705 }
1706 
1707 #endif
1708 
1709 /*
1710  * Local variables:
1711  * c-basic-offset: 2
1712  * End:
1713  */
void set_j(const double j)
Definition: vpImagePoint.h:156
vpDisplay * display
Definition: vpImage.h:116
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
double getTop() const
Definition: vpRect.h:169
double get_v() const
Definition: vpImagePoint.h:250
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:167
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:181
unsigned int getWidth() const
Definition: vpImage.h:154
#define vpERROR_TRACE
Definition: vpDebug.h:379
unsigned char B
Blue component.
Definition: vpRGBa.h:155
#define vpTRACE
Definition: vpDebug.h:401
Type * bitmap
points toward the bitmap
Definition: vpImage.h:115
Class to define colors available for display functionnalities.
Definition: vpColor.h:123
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
double get_u() const
Definition: vpImagePoint.h:239
void resize(const unsigned int height, const unsigned int width)
set the size of the image
Definition: vpImage.h:530
void set_i(const double i)
Definition: vpImagePoint.h:145
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:150
void setFont(const char *font)
char * title
display title
Definition: vpDisplay.h:177
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:171
unsigned char G
Green component.
Definition: vpRGBa.h:154
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:192
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
vpColorIdentifier id
Definition: vpColor.h:154
double getWidth() const
Definition: vpRect.h:188
void flushDisplay()
void set_u(const double u)
Definition: vpImagePoint.h:203
static double sqr(double x)
Definition: vpMath.h:106
virtual ~vpDisplayGTK()
unsigned int height
Definition: vpDisplay.h:180
void set_v(const double v)
Definition: vpImagePoint.h:214
bool getPointerPosition(vpImagePoint &ip)
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
void displayImage(const vpImage< vpRGBa > &I)
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 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:129
void setWindowPosition(int winx, int winy)
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:145
Defines a rectangle in the plane.
Definition: vpRect.h:82
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:92
void setTitle(const char *title)
bool getKeyboardEvent(bool blocking=true)
double getLeft() const
Definition: vpRect.h:156