ViSP  2.8.0
vpDisplayGTK.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayGTK.cpp 4174 2013-03-22 10:28:41Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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  widget = NULL ;
87  init(I, x, y, title) ;
88 }
89 
90 
100  int x,
101  int y,
102  const char *title) : vpDisplay()
103 {
104  col = NULL;
105  widget = NULL ;
106  init(I, x, y, title) ;
107 }
108 
109 
110 
133 vpDisplayGTK::vpDisplayGTK(int x, int y, const char *title) : vpDisplay()
134 {
135  windowXPosition = x ;
136  windowYPosition = y ;
137 
138  col = NULL;
139  widget = NULL ;
140 
141  if (title != NULL)
142  strcpy(this->title, title) ;
143 }
144 
165 {
166  col = NULL;
167  widget = NULL ;
168 }
169 
174 {
175  closeDisplay() ;
176 }
177 
186 void
188  int x,
189  int y,
190  const char *title)
191 {
192  if ((I.getHeight() == 0) || (I.getWidth()==0))
193  {
194  vpERROR_TRACE("Image not initialized " ) ;
196  "Image not initialized")) ;
197  }
198 
199  if (x != -1)
200  windowXPosition = x ;
201  if (y != -1)
202  windowYPosition = y ;
203 
205  I.display = this ;
207 }
208 
218 void
220  int x,
221  int y,
222  const char *title)
223 {
224  if ((I.getHeight() == 0) || (I.getWidth()==0))
225  {
226  vpERROR_TRACE("Image not initialized " ) ;
228  "Image not initialized")) ;
229  }
230 
231  if (x != -1)
232  windowXPosition = x ;
233  if (y != -1)
234  windowYPosition = y ;
235 
237  I.display = this ;
239 }
248 void
249 vpDisplayGTK::init(unsigned int width, unsigned int height,
250  int x, int y,
251  const char *title)
252 {
253  /* Initialisation of thegdk et gdk_rgb library */
254  int *argc=NULL ;
255  char **argv ;
256  gtk_init(argc,&argv);
257 
258  this->width = width;
259  this->height = height;
260 
261  /* Create the window*/
262  widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
263 
264  gtk_widget_add_events(widget, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
265  | GDK_POINTER_MOTION_MASK);
266 
267  gtk_window_set_default_size(GTK_WINDOW(widget), (gint)width, (gint)height);
268 
269  if (x != -1)
270  windowXPosition = x ;
271  if (y != -1)
272  windowYPosition = y ;
273 
274  gtk_window_move(GTK_WINDOW(widget), windowXPosition, windowYPosition);
275 
276  gtk_widget_show(widget);
277 
278  gdk_rgb_init();
279 
280  /* Create background pixmap */
281  background = gdk_pixmap_new(widget->window, (gint)width, (gint)height, -1);
282 
283  /* Create graphic context */
284  gc = gdk_gc_new(widget->window);
285 
286  /* get the colormap */
287  colormap = gdk_window_get_colormap(widget->window);
288 
289  col = new GdkColor *[vpColor::id_unknown] ; // id_unknown = number of predefined colors
290 
291  /* Create color */
292  gdk_color_parse("light blue",&lightBlue);
293  gdk_colormap_alloc_color(colormap,&lightBlue,FALSE,TRUE);
294  col[vpColor::id_lightBlue] = &lightBlue ;
295 
296  gdk_color_parse("blue",&blue);
297  gdk_colormap_alloc_color(colormap,&blue,FALSE,TRUE);
298  col[vpColor::id_blue] = &blue ;
299 
300  gdk_color_parse("dark blue",&darkBlue);
301  gdk_colormap_alloc_color(colormap,&darkBlue,FALSE,TRUE);
302  col[vpColor::id_darkBlue] = &darkBlue ;
303 
304  gdk_color_parse("#FF8C8C",&lightRed);
305  gdk_colormap_alloc_color(colormap,&lightRed,FALSE,TRUE);
306  col[vpColor::id_lightRed] = &lightRed ;
307 
308  gdk_color_parse("red",&red);
309  gdk_colormap_alloc_color(colormap,&red,FALSE,TRUE);
310  col[vpColor::id_red] = &red ;
311 
312  gdk_color_parse("dark red",&darkRed);
313  gdk_colormap_alloc_color(colormap,&darkRed,FALSE,TRUE);
314  col[vpColor::id_darkRed] = &darkRed ;
315 
316  gdk_color_parse("light green",&lightGreen);
317  gdk_colormap_alloc_color(colormap,&lightGreen,FALSE,TRUE);
318  col[vpColor::id_lightGreen] = &lightGreen ;
319 
320  gdk_color_parse("green",&green);
321  gdk_colormap_alloc_color(colormap,&green,FALSE,TRUE);
322  col[vpColor::id_green] = &green ;
323 
324  gdk_color_parse("dark green",&darkGreen);
325  gdk_colormap_alloc_color(colormap,&darkGreen,FALSE,TRUE);
326  col[vpColor::id_darkGreen] = &darkGreen ;
327 
328  gdk_color_parse("yellow",&yellow);
329  gdk_colormap_alloc_color(colormap,&yellow,FALSE,TRUE);
330  col[vpColor::id_yellow] = &yellow ;
331 
332  gdk_color_parse("cyan",&cyan);
333  gdk_colormap_alloc_color(colormap,&cyan,FALSE,TRUE);
334  col[vpColor::id_cyan] = &cyan ;
335 
336  gdk_color_parse("orange",&orange);
337  gdk_colormap_alloc_color(colormap,&orange,FALSE,TRUE);
338  col[vpColor::id_orange] = &orange ;
339 
340  gdk_color_parse("purple",&purple);
341  gdk_colormap_alloc_color(colormap,&purple,FALSE,TRUE);
342  col[vpColor::id_purple] = &purple ;
343 
344  gdk_color_parse("white",&white);
345  gdk_colormap_alloc_color(colormap,&white,FALSE,TRUE);
346  col[vpColor::id_white] = &white ;
347 
348  gdk_color_parse("black",&black);
349  gdk_colormap_alloc_color(colormap,&black,FALSE,TRUE);
350  col[vpColor::id_black] = &black ;
351 
352  gdk_color_parse("#C0C0C0",&lightGray);
353  gdk_colormap_alloc_color(colormap,&lightGray,FALSE,TRUE);
354  col[vpColor::id_lightGray] = &lightGray ;
355 
356  gdk_color_parse("#808080",&gray);
357  gdk_colormap_alloc_color(colormap,&gray,FALSE,TRUE);
358  col[vpColor::id_gray] = &gray ;
359 
360  gdk_color_parse("#404040",&darkGray);
361  gdk_colormap_alloc_color(colormap,&darkGray,FALSE,TRUE);
362  col[vpColor::id_darkGray] = &darkGray ;
363 
364  /* Chargement des polices */
365  Police1 = gdk_font_load("-*-times-medium-r-normal-*-16-*-*-*-*-*-*-*");
366  Police2 = gdk_font_load("-*-courier-bold-r-normal-*-*-140-*-*-*-*-*-*");
367 
368  if (title != NULL)
369  strcpy(this->title, title) ;
370 
372  setTitle(this->title) ;
373 }
374 
375 
390 void
391 vpDisplayGTK::setFont(const char * /* font */)
392 {
393  vpERROR_TRACE("Not yet implemented" ) ;
394 }
395 
400 void
401 vpDisplayGTK::setTitle(const char *title)
402 {
404  {
405  if (title != NULL)
406  gdk_window_set_title(widget->window,(char *)title);
407  }
408  else
409  {
410  vpERROR_TRACE("GTK not initialized " ) ;
412  "GTK not initialized")) ;
413  }
414 }
415 
424 void vpDisplayGTK::setWindowPosition(int winx, int winy)
425 {
426 
428  gtk_window_move(GTK_WINDOW(widget), winx, winy);
429  }
430  else
431  {
432  vpERROR_TRACE("GTK not initialized " ) ;
434  "GTK not initialized")) ;
435  }
436 }
437 
438 
451 {
452 
454  {
455  /* Copie de l'image dans le pixmap fond */
456  gdk_draw_gray_image(background,
457  gc, 0, 0, (gint)width, (gint)height,
458  GDK_RGB_DITHER_NONE,
459  I.bitmap,
460  (gint)width);
461 
462  /* Le pixmap background devient le fond de la zone de dessin */
463  gdk_window_set_back_pixmap(widget->window, background, FALSE);
464 
465  /* Affichage */
466  //gdk_window_clear(GTK_WINDOW(widget));
467  //gdk_flush();
468  }
469  else
470  {
471  vpERROR_TRACE("GTK not initialized " ) ;
473  "GTK not initialized")) ;
474  }
475 }
476 
477 
495 void vpDisplayGTK::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
496 {
498  {
500  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Itemp);
501  /* Copie de l'image dans le pixmap fond */
502  gdk_draw_gray_image(background,
503  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)width, (gint)height,
504  GDK_RGB_DITHER_NONE,
505  I.bitmap,
506  (gint)width);
507 
508  /* Le pixmap background devient le fond de la zone de dessin */
509  gdk_window_set_back_pixmap(widget->window, background, FALSE);
510 
511  /* Affichage */
512  //gdk_window_clear(GTK_WINDOW(widget));
513  //gdk_flush();
514  }
515  else
516  {
517  vpERROR_TRACE("GTK not initialized " ) ;
519  "GTK not initialized")) ;
520  }
521 }
522 
523 
536 {
537 
539  {
540 
541  /* Copie de l'image dans le pixmap fond */
542  gdk_draw_rgb_32_image(background,
543  gc, 0, 0, (gint)width, (gint)height,
544  GDK_RGB_DITHER_NONE,
545  (unsigned char *)I.bitmap,
546  (gint)(4*width));
547 
548  /* Permet de fermer la fen�tre si besoin (cas des s�quences d'images) */
549  //while (g_main_iteration(FALSE));
550 
551  /* Le pixmap background devient le fond de la zone de dessin */
552  gdk_window_set_back_pixmap(widget->window, background, FALSE);
553 
554  /* Affichage */
555  //gdk_window_clear(GTK_WINDOW(widget));
556  //flushDisplay() ;
557 
558  }
559  else
560  {
561  vpERROR_TRACE("GTK not initialized " ) ;
563  "GTK not initialized")) ;
564  }
565 }
566 
584 void vpDisplayGTK::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
585 {
587  {
588  vpImage<vpRGBa> Itemp;
589  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Itemp);
590  /* Copie de l'image dans le pixmap fond */
591  gdk_draw_rgb_32_image(background,
592  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)width, (gint)height,
593  GDK_RGB_DITHER_NONE,
594  (unsigned char *)Itemp.bitmap,
595  (gint)(4*width));
596 
597  /* Permet de fermer la fen�tre si besoin (cas des s�quences d'images) */
598  //while (g_main_iteration(FALSE));
599 
600  /* Le pixmap background devient le fond de la zone de dessin */
601  gdk_window_set_back_pixmap(widget->window, background, FALSE);
602 
603  /* Affichage */
604  //gdk_window_clear(GTK_WINDOW(widget));
605  //flushDisplay() ;
606  }
607  else
608  {
609  vpERROR_TRACE("GTK not initialized " ) ;
611  "GTK not initialized")) ;
612  }
613 }
614 
620 void vpDisplayGTK::displayImage(const unsigned char * /* I */)
621 {
622  vpTRACE(" not implemented ") ;
623 }
624 
631 {
632  if (col != NULL)
633  {
634  delete [] col ; col = NULL ;
635  }
636 
637  if (widget != NULL)
638  {
639  gdk_window_hide (widget->window);
640  gdk_window_destroy(widget->window);
641  widget = NULL;
642  }
644 }
645 
646 
652 {
654  {
655  gdk_window_clear(widget->window);
656  gdk_flush();
657  }
658  else
659  {
660  vpERROR_TRACE("GTK not initialized " ) ;
662  "GTK not initialized")) ;
663  }
664 }
665 
666 
671 void vpDisplayGTK::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
672 {
674  {
675  gdk_window_clear(widget->window);
676  gdk_flush();
677  }
678  else
679  {
680  vpERROR_TRACE("GTK not initialized " ) ;
682  "GTK not initialized")) ;
683  }
684 }
685 
686 
690 void vpDisplayGTK::clearDisplay(const vpColor & /* color */)
691 {
692  vpTRACE("Not implemented") ;
693 }
694 
703  const vpImagePoint &ip2,
704  const vpColor &color,
705  unsigned int w, unsigned int h,
706  unsigned int thickness)
707 {
709  {
710  try{
711  double a = ip2.get_i() - ip1.get_i() ;
712  double b = ip2.get_j() - ip1.get_j() ;
713  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
714 
715  //if ((a==0)&&(b==0))
716  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon() )&&(std::fabs(b) <= std::numeric_limits<double>::epsilon()) )
717  {
718  // DisplayCrossLarge(i1,j1,3,col) ;
719  }
720  else
721  {
722  a /= lg ;
723  b /= lg ;
724 
725  vpImagePoint ip3;
726  ip3.set_i(ip2.get_i() - w*a);
727  ip3.set_j(ip2.get_j() - w*b);
728 
729  vpImagePoint ip4;
730  ip4.set_i( ip3.get_i() - b*h );
731  ip4.set_j( ip3.get_j() + a*h );
732 
733  displayLine ( ip2, ip4, color, thickness ) ;
734 
735  ip4.set_i( ip3.get_i() + b*h );
736  ip4.set_j( ip3.get_j() - a*h );
737 
738  displayLine ( ip2, ip4, color, thickness ) ;
739  displayLine ( ip1, ip2, color, thickness ) ;
740  }
741  }
742  catch (...)
743  {
744  vpERROR_TRACE("Error caught") ;
745  throw ;
746  }
747  }
748  else
749  {
750  vpERROR_TRACE("GTK not initialized " ) ;
752  "GTK not initialized")) ;
753  }
754 }
755 
756 
769  const char *text,
770  const vpColor &color )
771 {
773  {
774  if (color.id < vpColor::id_unknown)
775  gdk_gc_set_foreground(gc, col[color.id]);
776  else {
777  gdkcolor.red = 256 * color.R;
778  gdkcolor.green = 256 * color.G;
779  gdkcolor.blue = 256 * color.B;
780  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
781  gdk_gc_set_foreground(gc, &gdkcolor);
782  }
783 
784  gdk_draw_string(background, Police2, gc,
785  vpMath::round( ip.get_u() ),
786  vpMath::round( ip.get_v() ),
787  (const gchar *)text);
788 
789  }
790  else
791  {
792  vpERROR_TRACE("GTK not initialized " ) ;
794  "GTK not initialized")) ;
795  }
796 }
807  unsigned int radius,
808  const vpColor &color,
809  bool fill,
810  unsigned int thickness )
811 {
813  {
814  if ( thickness == 1 ) thickness = 0;
815 
816  if (color.id < vpColor::id_unknown)
817  gdk_gc_set_foreground(gc, col[color.id]);
818  else {
819  gdkcolor.red = 256 * color.R;
820  gdkcolor.green = 256 * color.G;
821  gdkcolor.blue = 256 * color.B;
822  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
823  gdk_gc_set_foreground(gc, &gdkcolor);
824  }
825 
826  gdk_gc_set_line_attributes(gc, (gint)thickness,
827  GDK_LINE_SOLID, GDK_CAP_BUTT,
828  GDK_JOIN_BEVEL) ;
829 
830  if (fill == false)
831  gdk_draw_arc(background, gc, FALSE,
832  vpMath::round( center.get_u()-radius ),
833  vpMath::round( center.get_v()-radius ),
834  (gint)(2*radius), (gint)(2*radius), 23040, 23040) ; /* 23040 = 360*64 */
835  else
836  gdk_draw_arc(background, gc, TRUE,
837  vpMath::round( center.get_u()-radius ),
838  vpMath::round( center.get_v()-radius ),
839  (gint)(2*radius), (gint)(2*radius), 23040, 23040) ; /* 23040 = 360*64 */
840  }
841  else
842  {
843  vpERROR_TRACE("GTK not initialized " ) ;
845  "GTK not initialized")) ;
846  }
847 }
856  unsigned int size,
857  const vpColor &color,
858  unsigned int thickness)
859 {
861  {
862  try{
863  double i = ip.get_i();
864  double j = ip.get_j();
865  vpImagePoint ip1, ip2;
866 
867  ip1.set_i( i-size/2 );
868  ip1.set_j( j );
869  ip2.set_i( i+size/2 );
870  ip2.set_j( j );
871  displayLine ( ip1, ip2, color, thickness ) ;
872 
873  ip1.set_i( i );
874  ip1.set_j( j-size/2 );
875  ip2.set_i( i );
876  ip2.set_j( j+size/2 );
877 
878  displayLine ( ip1, ip2, color, thickness ) ;
879  }
880  catch (...)
881  {
882  vpERROR_TRACE("Error caught") ;
883  throw ;
884  }
885  }
886 
887  else
888  {
889  vpERROR_TRACE("GTK not initialized " ) ;
891  "GTK not initialized")) ;
892  }
893 }
901  const vpImagePoint &ip2,
902  const vpColor &color,
903  unsigned int thickness )
904 {
905 
907  {
908  if ( thickness == 1 ) thickness = 0;
909 
910  if (color.id < vpColor::id_unknown)
911  gdk_gc_set_foreground(gc, col[color.id]);
912  else {
913  gdkcolor.red = 256 * color.R;
914  gdkcolor.green = 256 * color.G;
915  gdkcolor.blue = 256 * color.B;
916  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
917  gdk_gc_set_foreground(gc, &gdkcolor);
918  }
919 
920  gdk_gc_set_line_attributes(gc, (gint)thickness,
921  GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT,
922  GDK_JOIN_BEVEL) ;
923  gdk_draw_line(background, gc,
924  vpMath::round( ip1.get_u() ),
925  vpMath::round( ip1.get_v() ),
926  vpMath::round( ip2.get_u() ),
927  vpMath::round( ip2.get_v() ) );
928  gdk_gc_set_line_attributes(gc, 0,
929  GDK_LINE_SOLID, GDK_CAP_BUTT,
930  GDK_JOIN_BEVEL) ;
931  }
932  else
933  {
934  vpERROR_TRACE("GTK not initialized " ) ;
936  "GTK not initialized")) ;
937  }
938 }
939 
947  const vpImagePoint &ip2,
948  const vpColor &color,
949  unsigned int thickness )
950 {
952  {
953  if ( thickness == 1 ) thickness = 0;
954 
955  if (color.id < vpColor::id_unknown)
956  gdk_gc_set_foreground(gc, col[color.id]);
957  else {
958  gdkcolor.red = 256 * color.R;
959  gdkcolor.green = 256 * color.G;
960  gdkcolor.blue = 256 * color.B;
961  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
962  gdk_gc_set_foreground(gc, &gdkcolor);
963  }
964 
965  gdk_gc_set_line_attributes(gc, (gint)thickness,
966  GDK_LINE_SOLID, GDK_CAP_BUTT,
967  GDK_JOIN_BEVEL) ;
968  gdk_draw_line(background, gc,
969  vpMath::round( ip1.get_u() ),
970  vpMath::round( ip1.get_v() ),
971  vpMath::round( ip2.get_u() ),
972  vpMath::round( ip2.get_v() ) );
973  }
974  else
975  {
976  vpERROR_TRACE("GTK not initialized " ) ;
978  "GTK not initialized")) ;
979  }
980 }
981 
988  const vpColor &color )
989 {
991  {
992  if (color.id < vpColor::id_unknown)
993  gdk_gc_set_foreground(gc, col[color.id]);
994  else {
995  gdkcolor.red = 256 * color.R;
996  gdkcolor.green = 256 * color.G;
997  gdkcolor.blue = 256 * color.B;
998  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
999  gdk_gc_set_foreground(gc, &gdkcolor);
1000  }
1001 
1002  gdk_draw_point(background,gc,
1003  vpMath::round( ip.get_u() ),
1004  vpMath::round( ip.get_v() ) );
1005  }
1006  else
1007  {
1008  vpERROR_TRACE("GTK not initialized " ) ;
1010  "GTK not initialized")) ;
1011  }
1012 }
1013 
1014 
1028 void
1030  unsigned int width, unsigned int height,
1031  const vpColor &color, bool fill,
1032  unsigned int thickness )
1033 {
1035  {
1036  if ( thickness == 1 ) thickness = 0;
1037 
1038  if (color.id < vpColor::id_unknown)
1039  gdk_gc_set_foreground(gc, col[color.id]);
1040  else {
1041  gdkcolor.red = 256 * color.R;
1042  gdkcolor.green = 256 * color.G;
1043  gdkcolor.blue = 256 * color.B;
1044  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1045  gdk_gc_set_foreground(gc, &gdkcolor);
1046  }
1047  gdk_gc_set_line_attributes(gc, (gint)thickness,
1048  GDK_LINE_SOLID, GDK_CAP_BUTT,
1049  GDK_JOIN_BEVEL) ;
1050 
1051  if (fill == false)
1052  gdk_draw_rectangle(background, gc, FALSE,
1053  vpMath::round( topLeft.get_u() ),
1054  vpMath::round( topLeft.get_v() ),
1055  (gint)width-1, (gint)height-1);
1056  else
1057  gdk_draw_rectangle(background, gc, TRUE,
1058  vpMath::round( topLeft.get_u() ),
1059  vpMath::round( topLeft.get_v() ),
1060  (gint)width, (gint)height);
1061 
1062  if (thickness > 1)
1063  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1064  GDK_JOIN_BEVEL) ;
1065  }
1066  else
1067  {
1068  vpERROR_TRACE("GTK not initialized " ) ;
1070  "GTK not initialized")) ;
1071  }
1072 }
1073 
1086 void
1088  const vpImagePoint &bottomRight,
1089  const vpColor &color, bool fill,
1090  unsigned int thickness )
1091 {
1093  {
1094  if ( thickness == 1 ) thickness = 0;
1095 
1096  if (color.id < vpColor::id_unknown)
1097  gdk_gc_set_foreground(gc, col[color.id]);
1098  else {
1099  gdkcolor.red = 256 * color.R;
1100  gdkcolor.green = 256 * color.G;
1101  gdkcolor.blue = 256 * color.B;
1102  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1103  gdk_gc_set_foreground(gc, &gdkcolor);
1104  }
1105 
1106  gdk_gc_set_line_attributes(gc, (gint)thickness,
1107  GDK_LINE_SOLID, GDK_CAP_BUTT,
1108  GDK_JOIN_BEVEL) ;
1109 
1110  int width = vpMath::round( bottomRight.get_u() - topLeft.get_u() );
1111  int height = vpMath::round( bottomRight.get_v() - topLeft.get_v() );
1112 
1113  if (fill == false)
1114  gdk_draw_rectangle(background, gc, FALSE,
1115  vpMath::round( topLeft.get_u() ),
1116  vpMath::round( topLeft.get_v() ),
1117  width-1,height-1);
1118  else
1119  gdk_draw_rectangle(background, gc, TRUE,
1120  vpMath::round( topLeft.get_u() ),
1121  vpMath::round( topLeft.get_v() ),
1122  width, height);
1123 
1124  if (thickness > 1)
1125  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1126  GDK_JOIN_BEVEL) ;
1127  }
1128  else
1129  {
1130  vpERROR_TRACE("GTK not initialized " ) ;
1132  "GTK not initialized")) ;
1133  }
1134 }
1135 
1148 void
1150  const vpColor &color, bool fill,
1151  unsigned int thickness )
1152 {
1154  {
1155  if (color.id < vpColor::id_unknown)
1156  gdk_gc_set_foreground(gc, col[color.id]);
1157  else {
1158  gdkcolor.red = 256 * color.R;
1159  gdkcolor.green = 256 * color.G;
1160  gdkcolor.blue = 256 * color.B;
1161  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1162  gdk_gc_set_foreground(gc, &gdkcolor);
1163  }
1164 
1165  if ( thickness == 1 ) thickness = 0;
1166 
1167  gdk_gc_set_line_attributes(gc, (gint)thickness, GDK_LINE_SOLID, GDK_CAP_BUTT,
1168  GDK_JOIN_BEVEL) ;
1169 
1170  if (fill == false)
1171  gdk_draw_rectangle(background, gc, FALSE,
1172  vpMath::round( rectangle.getLeft() ),
1173  vpMath::round( rectangle.getTop() ),
1174  vpMath::round( rectangle.getWidth()-1 ),
1175  vpMath::round( rectangle.getHeight()-1 ) );
1176 
1177  else
1178  gdk_draw_rectangle(background, gc, TRUE,
1179  vpMath::round( rectangle.getLeft() ),
1180  vpMath::round( rectangle.getTop() ),
1181  vpMath::round( rectangle.getWidth()-1 ),
1182  vpMath::round( rectangle.getHeight()-1 ) );
1183 
1184  if (thickness > 1)
1185  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1186  GDK_JOIN_BEVEL) ;
1187  }
1188  else
1189  {
1190  vpERROR_TRACE("GTK not initialized " ) ;
1192  "GTK not initialized")) ;
1193  }
1194 }
1195 
1196 
1212 bool
1214 {
1215  bool ret = false;
1216 
1217  int cpt =0;
1219 
1220 // flushDisplay() ;
1221  GdkEvent *ev = NULL;
1222  do {
1223  while ((ev = gdk_event_get())!=NULL){
1224  cpt++;
1225  // printf("event %d type %d on window %p My window %p\n",
1226  //cpt, ev->type, ev->any.window, widget->window);
1227 
1228  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1229  ret = true ;
1230  //printf("Click detection\n");
1231  }
1232  gdk_event_free(ev) ;
1233  }
1234  if (blocking){
1235  flushDisplay();
1236  vpTime::wait(100);
1237  }
1238  } while ( ret == false && blocking == true);
1239  }
1240  else {
1241  vpERROR_TRACE("GTK not initialized " ) ;
1243  "GTK not initialized")) ;
1244  }
1245  return ret;
1246 }
1247 
1264 bool
1266 {
1267  bool ret = false;
1268 
1270 
1271  GdkEvent *ev = NULL;
1272  double u, v ;
1273  do {
1274  while ((ev = gdk_event_get())!=NULL){
1275  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS) {
1276  u = ((GdkEventButton *)ev)->x ;
1277  v = ((GdkEventButton *)ev)->y ;
1278  ip.set_u( u );
1279  ip.set_v( v );
1280  ret = true ;
1281  }
1282  gdk_event_free(ev) ;
1283  }
1284  if (blocking){
1285  flushDisplay();
1286  vpTime::wait(100);
1287  }
1288  } while ( ret == false && blocking == true);
1289  }
1290  else {
1291  vpERROR_TRACE("GTK not initialized " ) ;
1293  "GTK not initialized")) ;
1294  }
1295  return ret ;
1296 }
1297 
1298 
1318 bool
1321  bool blocking)
1322 {
1323  bool ret = false;
1324 
1326  GdkEvent *ev = NULL;
1327  double u, v ;
1328  do {
1329  while ((ev = gdk_event_get())){
1330  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1331  u = ((GdkEventButton *)ev)->x ;
1332  v = ((GdkEventButton *)ev)->y ;
1333  ip.set_u( u );
1334  ip.set_v( v );
1335 
1336  switch ((int)((GdkEventButton *)ev)->button) {
1337  case 1:
1338  button = vpMouseButton::button1; break;
1339  case 2:
1340  button = vpMouseButton::button2; break;
1341  case 3:
1342  button = vpMouseButton::button3; break;
1343  }
1344  ret = true ;
1345  }
1346  gdk_event_free(ev) ;
1347  }
1348  if (blocking){
1349  flushDisplay();
1350  vpTime::wait(100);
1351  }
1352 
1353  } while ( ret == false && blocking == true);
1354  }
1355  else {
1356  vpERROR_TRACE("GTK not initialized " ) ;
1358  "GTK not initialized")) ;
1359  }
1360  return ret;
1361 }
1362 
1386 bool
1389  bool blocking)
1390 {
1391  bool ret = false;
1392 
1393  if ( displayHasBeenInitialized ) {
1394 
1395  //flushDisplay() ;
1396  GdkEvent *ev = NULL;
1397  double u, v ;
1398  do {
1399  while ((ev = gdk_event_get())!=NULL){
1400  if ( ev->any.window == widget->window
1401  && ev->type == GDK_BUTTON_RELEASE) {
1402  u = ((GdkEventButton *)ev)->x ;
1403  v = ((GdkEventButton *)ev)->y ;
1404  ip.set_u( u );
1405  ip.set_v( v );
1406 
1407  switch ( ( int ) ( ( GdkEventButton * ) ev )->button ) {
1408  case 1:
1409  button = vpMouseButton::button1; break;
1410  case 2:
1411  button = vpMouseButton::button2; break;
1412  case 3:
1413  button = vpMouseButton::button3; break;
1414  }
1415  ret = true ;
1416  }
1417  gdk_event_free(ev) ;
1418  }
1419  if (blocking){
1420  flushDisplay();
1421  vpTime::wait(100);
1422  }
1423 
1424  } while ( ret == false && blocking == true);
1425  }
1426  else {
1427  vpERROR_TRACE ( "GTK not initialized " ) ;
1429  "GTK not initialized" ) ) ;
1430  }
1431  return ret;
1432 }
1433 
1434 /*
1435  \brief gets the displayed image (including the overlay plane)
1436  and returns an RGBa image
1437 */
1439 {
1440 
1441 
1442  // shoudl certainly be optimized.
1443  // doesn't work
1445  {
1446 
1447  GdkImage *ImageGtk;
1448  /*
1449  */
1450 
1451  ImageGtk = gdk_image_get(background, 0, 0, (gint)width, (gint)height);
1452 
1453 
1454  I.resize(height,width) ;
1455  guchar *pos;
1456  guint32 pixel;
1457  gint x,y;
1458  guchar OctetRouge,OctetVert,OctetBleu,mask;
1459  mask = 0x000000FF;
1460 
1461  pos = (unsigned char *)I.bitmap;
1462  for (y=0;y<(gint)height;y++)
1463  {
1464  for (x=0;x<(gint)width;x++)
1465  {
1466  pixel = gdk_image_get_pixel(ImageGtk,x,y);
1467  OctetBleu = (guchar)pixel & mask;
1468  OctetVert = (guchar)(pixel>>8) & mask;
1469  OctetRouge = (guchar)(pixel>>16) & mask;
1470  *pos++ = OctetRouge;
1471  *pos++ = OctetVert;
1472  *pos++ = OctetBleu;
1473  *pos++ = 0;
1474  }
1475  }
1476 
1477 
1478  }
1479  else
1480  {
1481  vpERROR_TRACE("GTK not initialized " ) ;
1483  "GTK not initialized")) ;
1484  }
1485 
1486 }
1487 
1494 {
1495 
1496  unsigned int depth;
1497 
1498  depth = (unsigned int)gdk_window_get_visual(widget->window)->depth ;
1499 
1500  return (depth);
1501 }
1502 
1508 void vpDisplayGTK::getScreenSize(unsigned int &width, unsigned int &height)
1509 {
1510  vpTRACE("Not implemented") ;
1511  width = 0;
1512  height = 0;
1513 }
1514 
1530 bool
1532 {
1533  bool ret = false;
1534 
1535  int cpt =0;
1537 
1538  GdkEvent *ev = NULL;
1539  do {
1540  while ((ev = gdk_event_get())!=NULL){
1541  cpt++;
1542  // printf("event %d type %d on window %p My window %p\n",
1543  //cpt, ev->type, ev->any.window, widget->window);
1544 
1545  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1546  ret = true ;
1547  //printf("Key press detection\n");
1548  }
1549  gdk_event_free(ev) ;
1550  }
1551  if (blocking){
1552  flushDisplay();
1553  vpTime::wait(100);
1554  }
1555  } while ( ret == false && blocking == true);
1556  }
1557  else {
1558  vpERROR_TRACE("GTK not initialized " ) ;
1560  "GTK not initialized")) ;
1561  }
1562  return ret;
1563 }
1564 
1584 bool
1585 vpDisplayGTK::getKeyboardEvent(char *string, bool blocking)
1586 {
1587  bool ret = false;
1588 
1589  int cpt =0;
1591 
1592  GdkEvent *ev = NULL;
1593  do {
1594  while ((ev = gdk_event_get())!=NULL){
1595  cpt++;
1596  // printf("event %d type %d on window %p My window %p\n",
1597  //cpt, ev->type, ev->any.window, widget->window);
1598 
1599  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1600  //std::cout << "Key val: \"" << gdk_keyval_name (ev->key.keyval) /*ev->key.string*/ << "\"" << std::endl;
1601  sprintf(string, "%s", gdk_keyval_name (ev->key.keyval));
1602  ret = true ;
1603  //printf("Key press detection\n");
1604  }
1605  gdk_event_free(ev) ;
1606  }
1607  if (blocking){
1608  flushDisplay();
1609  vpTime::wait(100);
1610  }
1611  } while ( ret == false && blocking == true);
1612  }
1613  else {
1614  vpERROR_TRACE("GTK not initialized " ) ;
1616  "GTK not initialized")) ;
1617  }
1618  return ret;
1619 }
1620 
1633 bool
1635 {
1636  bool ret = false;
1637 
1639  GdkEvent *ev = NULL;
1640  double u, v ;
1641  if ((ev = gdk_event_get())){
1642  if (ev->any.window == widget->window && ev->type == GDK_MOTION_NOTIFY){
1643  u = ((GdkEventMotion *)ev)->x ;
1644  v = ((GdkEventMotion *)ev)->y ;
1645  ip.set_u( u );
1646  ip.set_v( v );
1647 
1648  ret = true ;
1649  }
1650  gdk_event_free(ev) ;
1651  }
1652  }
1653  else {
1654  vpERROR_TRACE("GTK not initialized " ) ;
1656  "GTK not initialized")) ;
1657  }
1658  return ret;
1659 }
1660 
1671 bool
1673 {
1675  {
1676  int u,v;
1677  gdk_window_get_pointer(widget->window, &u, &v, NULL);
1678  ip.set_u( u );
1679  ip.set_v( v );
1680  }
1681  else {
1682  vpERROR_TRACE("GTK not initialized " ) ;
1684  "GTK not initialized")) ;
1685  }
1686 
1687  return true;
1688 }
1689 
1690 #endif
1691 
1692 /*
1693  * Local variables:
1694  * c-basic-offset: 2
1695  * End:
1696  */
void set_j(const double j)
Definition: vpImagePoint.h:156
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: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:175
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:187
double get_i() const
Definition: vpImagePoint.h:181
unsigned int getWidth() const
Definition: vpImage.h:159
#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:120
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:239
void resize(const unsigned int height, const unsigned int width)
set the size of the image
Definition: vpImage.h:535
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:185
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:179
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:156
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:188
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:133
void setWindowPosition(int winx, int winy)
int windowXPosition
display position
Definition: vpDisplay.h:181
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:150
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)
int windowYPosition
display position
Definition: vpDisplay.h:183
double getLeft() const
Definition: vpRect.h:156