ViSP  2.9.0
vpDisplayGTK.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayGTK.cpp 4632 2014-02-03 17:06:40Z 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 {
85  widget = NULL;
86  vectgtk = NULL;
87  font = NULL;
88  colormap = NULL;
89  background = NULL;
90  gc = NULL;
91  nrow = ncol = 0;
92  col = NULL;
93 
94  init(I, x, y, title) ;
95 }
96 
97 
107  int x,
108  int y,
109  const char *title)
110 {
111  widget = NULL;
112  vectgtk = NULL;
113  font = NULL;
114  colormap = NULL;
115  background = NULL;
116  gc = NULL;
117  nrow = ncol = 0;
118  col = NULL;
119 
120  init(I, x, y, title) ;
121 }
122 
123 
124 
147 vpDisplayGTK::vpDisplayGTK(int x, int y, const char *title)
148 {
149  widget = NULL;
150  vectgtk = NULL;
151  font = NULL;
152  colormap = NULL;
153  background = NULL;
154  gc = NULL;
155  nrow = ncol = 0;
156  col = NULL;
157 
158  windowXPosition = x ;
159  windowYPosition = y ;
160 
161  if(title != NULL)
162  title_ = std::string(title);
163  else
164  title_ = std::string(" ");
165 }
166 
187 {
188  widget = NULL;
189  vectgtk = NULL;
190  font = NULL;
191  colormap = NULL;
192  background = NULL;
193  gc = NULL;
194  nrow = ncol = 0;
195  col = NULL;
196 }
197 
202 {
203  closeDisplay() ;
204 }
205 
214 void
216  int x,
217  int y,
218  const char *title)
219 {
220  if ((I.getHeight() == 0) || (I.getWidth()==0))
221  {
222  vpERROR_TRACE("Image not initialized " ) ;
224  "Image not initialized")) ;
225  }
226 
227  if (x != -1)
228  windowXPosition = x ;
229  if (y != -1)
230  windowYPosition = y ;
231 
233  I.display = this ;
235 }
236 
246 void
248  int x,
249  int y,
250  const char *title)
251 {
252  if ((I.getHeight() == 0) || (I.getWidth()==0))
253  {
254  vpERROR_TRACE("Image not initialized " ) ;
256  "Image not initialized")) ;
257  }
258 
259  if (x != -1)
260  windowXPosition = x ;
261  if (y != -1)
262  windowYPosition = y ;
263 
265  I.display = this ;
267 }
276 void
277 vpDisplayGTK::init(unsigned int width, unsigned int height,
278  int x, int y,
279  const char *title)
280 {
281  /* Initialisation of thegdk et gdk_rgb library */
282  int *argc=NULL ;
283  char **argv ;
284  gtk_init(argc,&argv);
285 
286  this->width = width;
287  this->height = height;
288 
289  /* Create the window*/
290  widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
291 
292  gtk_widget_add_events(widget, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
293  | GDK_POINTER_MOTION_MASK);
294 
295  gtk_window_set_default_size(GTK_WINDOW(widget), (gint)width, (gint)height);
296 
297  if (x != -1)
298  windowXPosition = x ;
299  if (y != -1)
300  windowYPosition = y ;
301 
302  gtk_window_move(GTK_WINDOW(widget), windowXPosition, windowYPosition);
303 
304  gtk_widget_show(widget);
305 
306  gdk_rgb_init();
307 
308  /* Create background pixmap */
309  background = gdk_pixmap_new(widget->window, (gint)width, (gint)height, -1);
310 
311  /* Create graphic context */
312  gc = gdk_gc_new(widget->window);
313 
314  /* get the colormap */
315  colormap = gdk_window_get_colormap(widget->window);
316 
317  col = new GdkColor *[vpColor::id_unknown] ; // id_unknown = number of predefined colors
318 
319  /* Create color */
320  gdk_color_parse("light blue",&lightBlue);
321  gdk_colormap_alloc_color(colormap,&lightBlue,FALSE,TRUE);
322  col[vpColor::id_lightBlue] = &lightBlue ;
323 
324  gdk_color_parse("blue",&blue);
325  gdk_colormap_alloc_color(colormap,&blue,FALSE,TRUE);
326  col[vpColor::id_blue] = &blue ;
327 
328  gdk_color_parse("dark blue",&darkBlue);
329  gdk_colormap_alloc_color(colormap,&darkBlue,FALSE,TRUE);
330  col[vpColor::id_darkBlue] = &darkBlue ;
331 
332  gdk_color_parse("#FF8C8C",&lightRed);
333  gdk_colormap_alloc_color(colormap,&lightRed,FALSE,TRUE);
334  col[vpColor::id_lightRed] = &lightRed ;
335 
336  gdk_color_parse("red",&red);
337  gdk_colormap_alloc_color(colormap,&red,FALSE,TRUE);
338  col[vpColor::id_red] = &red ;
339 
340  gdk_color_parse("dark red",&darkRed);
341  gdk_colormap_alloc_color(colormap,&darkRed,FALSE,TRUE);
342  col[vpColor::id_darkRed] = &darkRed ;
343 
344  gdk_color_parse("light green",&lightGreen);
345  gdk_colormap_alloc_color(colormap,&lightGreen,FALSE,TRUE);
346  col[vpColor::id_lightGreen] = &lightGreen ;
347 
348  gdk_color_parse("green",&green);
349  gdk_colormap_alloc_color(colormap,&green,FALSE,TRUE);
350  col[vpColor::id_green] = &green ;
351 
352  gdk_color_parse("dark green",&darkGreen);
353  gdk_colormap_alloc_color(colormap,&darkGreen,FALSE,TRUE);
354  col[vpColor::id_darkGreen] = &darkGreen ;
355 
356  gdk_color_parse("yellow",&yellow);
357  gdk_colormap_alloc_color(colormap,&yellow,FALSE,TRUE);
358  col[vpColor::id_yellow] = &yellow ;
359 
360  gdk_color_parse("cyan",&cyan);
361  gdk_colormap_alloc_color(colormap,&cyan,FALSE,TRUE);
362  col[vpColor::id_cyan] = &cyan ;
363 
364  gdk_color_parse("orange",&orange);
365  gdk_colormap_alloc_color(colormap,&orange,FALSE,TRUE);
366  col[vpColor::id_orange] = &orange ;
367 
368  gdk_color_parse("purple",&purple);
369  gdk_colormap_alloc_color(colormap,&purple,FALSE,TRUE);
370  col[vpColor::id_purple] = &purple ;
371 
372  gdk_color_parse("white",&white);
373  gdk_colormap_alloc_color(colormap,&white,FALSE,TRUE);
374  col[vpColor::id_white] = &white ;
375 
376  gdk_color_parse("black",&black);
377  gdk_colormap_alloc_color(colormap,&black,FALSE,TRUE);
378  col[vpColor::id_black] = &black ;
379 
380  gdk_color_parse("#C0C0C0",&lightGray);
381  gdk_colormap_alloc_color(colormap,&lightGray,FALSE,TRUE);
382  col[vpColor::id_lightGray] = &lightGray ;
383 
384  gdk_color_parse("#808080",&gray);
385  gdk_colormap_alloc_color(colormap,&gray,FALSE,TRUE);
386  col[vpColor::id_gray] = &gray ;
387 
388  gdk_color_parse("#404040",&darkGray);
389  gdk_colormap_alloc_color(colormap,&darkGray,FALSE,TRUE);
390  col[vpColor::id_darkGray] = &darkGray ;
391 
392  // Try to load a default font
393  font = gdk_font_load("-*-times-medium-r-normal-*-16-*-*-*-*-*-*-*");
394  if (font == NULL)
395  font = gdk_font_load("-*-courier-bold-r-normal-*-*-140-*-*-*-*-*-*");
396  if (font == NULL)
397  font = gdk_font_load("-*-courier 10 pitch-medium-r-normal-*-16-*-*-*-*-*-*-*");
398 
399  if(title != NULL)
400  title_ = std::string(title);
401  else
402  title_ = std::string(" ");
403 
405  gdk_window_set_title(widget->window, title_.c_str());
406 }
407 
408 
423 void
424 vpDisplayGTK::setFont(const char *fontname)
425 {
426  font = gdk_font_load((const gchar*)fontname);
427 }
428 
433 void
434 vpDisplayGTK::setTitle(const char *title)
435 {
437  {
438  if(title != NULL)
439  title_ = std::string(title);
440  else
441  title_ = std::string(" ");
442  gdk_window_set_title(widget->window, title_.c_str());
443  }
444  else
445  {
446  vpERROR_TRACE("GTK not initialized " ) ;
448  "GTK not initialized")) ;
449  }
450 }
451 
460 void vpDisplayGTK::setWindowPosition(int winx, int winy)
461 {
462 
464  gtk_window_move(GTK_WINDOW(widget), winx, winy);
465  }
466  else
467  {
468  vpERROR_TRACE("GTK not initialized " ) ;
470  "GTK not initialized")) ;
471  }
472 }
473 
474 
487 {
488 
490  {
491  /* Copie de l'image dans le pixmap fond */
492  gdk_draw_gray_image(background,
493  gc, 0, 0, (gint)width, (gint)height,
494  GDK_RGB_DITHER_NONE,
495  I.bitmap,
496  (gint)width);
497 
498  /* Le pixmap background devient le fond de la zone de dessin */
499  gdk_window_set_back_pixmap(widget->window, background, FALSE);
500 
501  /* Affichage */
502  //gdk_window_clear(GTK_WINDOW(widget));
503  //gdk_flush();
504  }
505  else
506  {
507  vpERROR_TRACE("GTK not initialized " ) ;
509  "GTK not initialized")) ;
510  }
511 }
512 
513 
531 void vpDisplayGTK::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
532 {
534  {
536  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Itemp);
537  /* Copie de l'image dans le pixmap fond */
538  gdk_draw_gray_image(background,
539  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)width, (gint)height,
540  GDK_RGB_DITHER_NONE,
541  I.bitmap,
542  (gint)width);
543 
544  /* Le pixmap background devient le fond de la zone de dessin */
545  gdk_window_set_back_pixmap(widget->window, background, FALSE);
546 
547  /* Affichage */
548  //gdk_window_clear(GTK_WINDOW(widget));
549  //gdk_flush();
550  }
551  else
552  {
553  vpERROR_TRACE("GTK not initialized " ) ;
555  "GTK not initialized")) ;
556  }
557 }
558 
559 
572 {
573 
575  {
576 
577  /* Copie de l'image dans le pixmap fond */
578  gdk_draw_rgb_32_image(background,
579  gc, 0, 0, (gint)width, (gint)height,
580  GDK_RGB_DITHER_NONE,
581  (unsigned char *)I.bitmap,
582  (gint)(4*width));
583 
584  /* Permet de fermer la fen�tre si besoin (cas des s�quences d'images) */
585  //while (g_main_iteration(FALSE));
586 
587  /* Le pixmap background devient le fond de la zone de dessin */
588  gdk_window_set_back_pixmap(widget->window, background, FALSE);
589 
590  /* Affichage */
591  //gdk_window_clear(GTK_WINDOW(widget));
592  //flushDisplay() ;
593 
594  }
595  else
596  {
597  vpERROR_TRACE("GTK not initialized " ) ;
599  "GTK not initialized")) ;
600  }
601 }
602 
620 void vpDisplayGTK::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
621 {
623  {
624  vpImage<vpRGBa> Itemp;
625  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),height,width,Itemp);
626  /* Copie de l'image dans le pixmap fond */
627  gdk_draw_rgb_32_image(background,
628  gc, (gint)iP.get_u(), (gint)iP.get_v(), (gint)width, (gint)height,
629  GDK_RGB_DITHER_NONE,
630  (unsigned char *)Itemp.bitmap,
631  (gint)(4*width));
632 
633  /* Permet de fermer la fen�tre si besoin (cas des s�quences d'images) */
634  //while (g_main_iteration(FALSE));
635 
636  /* Le pixmap background devient le fond de la zone de dessin */
637  gdk_window_set_back_pixmap(widget->window, background, FALSE);
638 
639  /* Affichage */
640  //gdk_window_clear(GTK_WINDOW(widget));
641  //flushDisplay() ;
642  }
643  else
644  {
645  vpERROR_TRACE("GTK not initialized " ) ;
647  "GTK not initialized")) ;
648  }
649 }
650 
656 void vpDisplayGTK::displayImage(const unsigned char * /* I */)
657 {
658  vpTRACE(" not implemented ") ;
659 }
660 
667 {
668  if (col != NULL)
669  {
670  delete [] col ; col = NULL ;
671  }
672 
673  if (widget != NULL)
674  {
675  gdk_window_hide (widget->window);
676  gdk_window_destroy(widget->window);
677  widget = NULL;
678  }
680 }
681 
682 
688 {
690  {
691  gdk_window_clear(widget->window);
692  gdk_flush();
693  }
694  else
695  {
696  vpERROR_TRACE("GTK not initialized " ) ;
698  "GTK not initialized")) ;
699  }
700 }
701 
702 
707 void vpDisplayGTK::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
708 {
710  {
711  gdk_window_clear(widget->window);
712  gdk_flush();
713  }
714  else
715  {
716  vpERROR_TRACE("GTK not initialized " ) ;
718  "GTK not initialized")) ;
719  }
720 }
721 
722 
726 void vpDisplayGTK::clearDisplay(const vpColor & /* color */)
727 {
728  vpTRACE("Not implemented") ;
729 }
730 
739  const vpImagePoint &ip2,
740  const vpColor &color,
741  unsigned int w, unsigned int h,
742  unsigned int thickness)
743 {
745  {
746  try{
747  double a = ip2.get_i() - ip1.get_i() ;
748  double b = ip2.get_j() - ip1.get_j() ;
749  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
750 
751  //if ((a==0)&&(b==0))
752  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon() )&&(std::fabs(b) <= std::numeric_limits<double>::epsilon()) )
753  {
754  // DisplayCrossLarge(i1,j1,3,col) ;
755  }
756  else
757  {
758  a /= lg ;
759  b /= lg ;
760 
761  vpImagePoint ip3;
762  ip3.set_i(ip2.get_i() - w*a);
763  ip3.set_j(ip2.get_j() - w*b);
764 
765  vpImagePoint ip4;
766  ip4.set_i( ip3.get_i() - b*h );
767  ip4.set_j( ip3.get_j() + a*h );
768 
769  displayLine ( ip2, ip4, color, thickness ) ;
770 
771  ip4.set_i( ip3.get_i() + b*h );
772  ip4.set_j( ip3.get_j() - a*h );
773 
774  displayLine ( ip2, ip4, color, thickness ) ;
775  displayLine ( ip1, ip2, color, thickness ) ;
776  }
777  }
778  catch (...)
779  {
780  vpERROR_TRACE("Error caught") ;
781  throw ;
782  }
783  }
784  else
785  {
786  vpERROR_TRACE("GTK not initialized " ) ;
788  "GTK not initialized")) ;
789  }
790 }
791 
792 
805  const char *text,
806  const vpColor &color )
807 {
809  {
810  if (color.id < vpColor::id_unknown)
811  gdk_gc_set_foreground(gc, col[color.id]);
812  else {
813  gdkcolor.red = 256 * color.R;
814  gdkcolor.green = 256 * color.G;
815  gdkcolor.blue = 256 * color.B;
816  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
817  gdk_gc_set_foreground(gc, &gdkcolor);
818  }
819  if (font != NULL)
820  gdk_draw_string(background, font, gc,
821  vpMath::round( ip.get_u() ),
822  vpMath::round( ip.get_v() ),
823  (const gchar *)text);
824  else
825  std::cout << "Cannot draw string: no font is selected" << std::endl;
826  }
827  else
828  {
829  vpERROR_TRACE("GTK not initialized " ) ;
831  "GTK not initialized")) ;
832  }
833 }
844  unsigned int radius,
845  const vpColor &color,
846  bool fill,
847  unsigned int thickness )
848 {
850  {
851  if ( thickness == 1 ) thickness = 0;
852 
853  if (color.id < vpColor::id_unknown)
854  gdk_gc_set_foreground(gc, col[color.id]);
855  else {
856  gdkcolor.red = 256 * color.R;
857  gdkcolor.green = 256 * color.G;
858  gdkcolor.blue = 256 * color.B;
859  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
860  gdk_gc_set_foreground(gc, &gdkcolor);
861  }
862 
863  gdk_gc_set_line_attributes(gc, (gint)thickness,
864  GDK_LINE_SOLID, GDK_CAP_BUTT,
865  GDK_JOIN_BEVEL) ;
866 
867  if (fill == false)
868  gdk_draw_arc(background, gc, FALSE,
869  vpMath::round( center.get_u()-radius ),
870  vpMath::round( center.get_v()-radius ),
871  (gint)(2*radius), (gint)(2*radius), 23040, 23040) ; /* 23040 = 360*64 */
872  else
873  gdk_draw_arc(background, gc, TRUE,
874  vpMath::round( center.get_u()-radius ),
875  vpMath::round( center.get_v()-radius ),
876  (gint)(2*radius), (gint)(2*radius), 23040, 23040) ; /* 23040 = 360*64 */
877  }
878  else
879  {
880  vpERROR_TRACE("GTK not initialized " ) ;
882  "GTK not initialized")) ;
883  }
884 }
893  unsigned int size,
894  const vpColor &color,
895  unsigned int thickness)
896 {
898  {
899  try{
900  double i = ip.get_i();
901  double j = ip.get_j();
902  vpImagePoint ip1, ip2;
903 
904  ip1.set_i( i-size/2 );
905  ip1.set_j( j );
906  ip2.set_i( i+size/2 );
907  ip2.set_j( j );
908  displayLine ( ip1, ip2, color, thickness ) ;
909 
910  ip1.set_i( i );
911  ip1.set_j( j-size/2 );
912  ip2.set_i( i );
913  ip2.set_j( j+size/2 );
914 
915  displayLine ( ip1, ip2, color, thickness ) ;
916  }
917  catch (...)
918  {
919  vpERROR_TRACE("Error caught") ;
920  throw ;
921  }
922  }
923 
924  else
925  {
926  vpERROR_TRACE("GTK not initialized " ) ;
928  "GTK not initialized")) ;
929  }
930 }
938  const vpImagePoint &ip2,
939  const vpColor &color,
940  unsigned int thickness )
941 {
942 
944  {
945  if ( thickness == 1 ) thickness = 0;
946 
947  if (color.id < vpColor::id_unknown)
948  gdk_gc_set_foreground(gc, col[color.id]);
949  else {
950  gdkcolor.red = 256 * color.R;
951  gdkcolor.green = 256 * color.G;
952  gdkcolor.blue = 256 * color.B;
953  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
954  gdk_gc_set_foreground(gc, &gdkcolor);
955  }
956 
957  gdk_gc_set_line_attributes(gc, (gint)thickness,
958  GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT,
959  GDK_JOIN_BEVEL) ;
960  gdk_draw_line(background, gc,
961  vpMath::round( ip1.get_u() ),
962  vpMath::round( ip1.get_v() ),
963  vpMath::round( ip2.get_u() ),
964  vpMath::round( ip2.get_v() ) );
965  gdk_gc_set_line_attributes(gc, 0,
966  GDK_LINE_SOLID, GDK_CAP_BUTT,
967  GDK_JOIN_BEVEL) ;
968  }
969  else
970  {
971  vpERROR_TRACE("GTK not initialized " ) ;
973  "GTK not initialized")) ;
974  }
975 }
976 
984  const vpImagePoint &ip2,
985  const vpColor &color,
986  unsigned int thickness )
987 {
989  {
990  if ( thickness == 1 ) thickness = 0;
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_gc_set_line_attributes(gc, (gint)thickness,
1003  GDK_LINE_SOLID, GDK_CAP_BUTT,
1004  GDK_JOIN_BEVEL) ;
1005  gdk_draw_line(background, gc,
1006  vpMath::round( ip1.get_u() ),
1007  vpMath::round( ip1.get_v() ),
1008  vpMath::round( ip2.get_u() ),
1009  vpMath::round( ip2.get_v() ) );
1010  }
1011  else
1012  {
1013  vpERROR_TRACE("GTK not initialized " ) ;
1015  "GTK not initialized")) ;
1016  }
1017 }
1018 
1025  const vpColor &color )
1026 {
1028  {
1029  if (color.id < vpColor::id_unknown)
1030  gdk_gc_set_foreground(gc, col[color.id]);
1031  else {
1032  gdkcolor.red = 256 * color.R;
1033  gdkcolor.green = 256 * color.G;
1034  gdkcolor.blue = 256 * color.B;
1035  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1036  gdk_gc_set_foreground(gc, &gdkcolor);
1037  }
1038 
1039  gdk_draw_point(background,gc,
1040  vpMath::round( ip.get_u() ),
1041  vpMath::round( ip.get_v() ) );
1042  }
1043  else
1044  {
1045  vpERROR_TRACE("GTK not initialized " ) ;
1047  "GTK not initialized")) ;
1048  }
1049 }
1050 
1051 
1065 void
1067  unsigned int width, unsigned int height,
1068  const vpColor &color, bool fill,
1069  unsigned int thickness )
1070 {
1072  {
1073  if ( thickness == 1 ) thickness = 0;
1074 
1075  if (color.id < vpColor::id_unknown)
1076  gdk_gc_set_foreground(gc, col[color.id]);
1077  else {
1078  gdkcolor.red = 256 * color.R;
1079  gdkcolor.green = 256 * color.G;
1080  gdkcolor.blue = 256 * color.B;
1081  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1082  gdk_gc_set_foreground(gc, &gdkcolor);
1083  }
1084  gdk_gc_set_line_attributes(gc, (gint)thickness,
1085  GDK_LINE_SOLID, GDK_CAP_BUTT,
1086  GDK_JOIN_BEVEL) ;
1087 
1088  if (fill == false)
1089  gdk_draw_rectangle(background, gc, FALSE,
1090  vpMath::round( topLeft.get_u() ),
1091  vpMath::round( topLeft.get_v() ),
1092  (gint)width-1, (gint)height-1);
1093  else
1094  gdk_draw_rectangle(background, gc, TRUE,
1095  vpMath::round( topLeft.get_u() ),
1096  vpMath::round( topLeft.get_v() ),
1097  (gint)width, (gint)height);
1098 
1099  if (thickness > 1)
1100  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1101  GDK_JOIN_BEVEL) ;
1102  }
1103  else
1104  {
1105  vpERROR_TRACE("GTK not initialized " ) ;
1107  "GTK not initialized")) ;
1108  }
1109 }
1110 
1123 void
1125  const vpImagePoint &bottomRight,
1126  const vpColor &color, bool fill,
1127  unsigned int thickness )
1128 {
1130  {
1131  if ( thickness == 1 ) thickness = 0;
1132 
1133  if (color.id < vpColor::id_unknown)
1134  gdk_gc_set_foreground(gc, col[color.id]);
1135  else {
1136  gdkcolor.red = 256 * color.R;
1137  gdkcolor.green = 256 * color.G;
1138  gdkcolor.blue = 256 * color.B;
1139  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1140  gdk_gc_set_foreground(gc, &gdkcolor);
1141  }
1142 
1143  gdk_gc_set_line_attributes(gc, (gint)thickness,
1144  GDK_LINE_SOLID, GDK_CAP_BUTT,
1145  GDK_JOIN_BEVEL) ;
1146 
1147  int width = vpMath::round( bottomRight.get_u() - topLeft.get_u() );
1148  int height = vpMath::round( bottomRight.get_v() - topLeft.get_v() );
1149 
1150  if (fill == false)
1151  gdk_draw_rectangle(background, gc, FALSE,
1152  vpMath::round( topLeft.get_u() ),
1153  vpMath::round( topLeft.get_v() ),
1154  width-1,height-1);
1155  else
1156  gdk_draw_rectangle(background, gc, TRUE,
1157  vpMath::round( topLeft.get_u() ),
1158  vpMath::round( topLeft.get_v() ),
1159  width, height);
1160 
1161  if (thickness > 1)
1162  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1163  GDK_JOIN_BEVEL) ;
1164  }
1165  else
1166  {
1167  vpERROR_TRACE("GTK not initialized " ) ;
1169  "GTK not initialized")) ;
1170  }
1171 }
1172 
1185 void
1187  const vpColor &color, bool fill,
1188  unsigned int thickness )
1189 {
1191  {
1192  if (color.id < vpColor::id_unknown)
1193  gdk_gc_set_foreground(gc, col[color.id]);
1194  else {
1195  gdkcolor.red = 256 * color.R;
1196  gdkcolor.green = 256 * color.G;
1197  gdkcolor.blue = 256 * color.B;
1198  gdk_colormap_alloc_color(colormap,&gdkcolor,FALSE,TRUE);
1199  gdk_gc_set_foreground(gc, &gdkcolor);
1200  }
1201 
1202  if ( thickness == 1 ) thickness = 0;
1203 
1204  gdk_gc_set_line_attributes(gc, (gint)thickness, GDK_LINE_SOLID, GDK_CAP_BUTT,
1205  GDK_JOIN_BEVEL) ;
1206 
1207  if (fill == false)
1208  gdk_draw_rectangle(background, gc, FALSE,
1209  vpMath::round( rectangle.getLeft() ),
1210  vpMath::round( rectangle.getTop() ),
1211  vpMath::round( rectangle.getWidth()-1 ),
1212  vpMath::round( rectangle.getHeight()-1 ) );
1213 
1214  else
1215  gdk_draw_rectangle(background, gc, TRUE,
1216  vpMath::round( rectangle.getLeft() ),
1217  vpMath::round( rectangle.getTop() ),
1218  vpMath::round( rectangle.getWidth()-1 ),
1219  vpMath::round( rectangle.getHeight()-1 ) );
1220 
1221  if (thickness > 1)
1222  gdk_gc_set_line_attributes(gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT,
1223  GDK_JOIN_BEVEL) ;
1224  }
1225  else
1226  {
1227  vpERROR_TRACE("GTK not initialized " ) ;
1229  "GTK not initialized")) ;
1230  }
1231 }
1232 
1233 
1249 bool
1251 {
1252  bool ret = false;
1253 
1254  int cpt =0;
1256 
1257 // flushDisplay() ;
1258  GdkEvent *ev = NULL;
1259  do {
1260  while ((ev = gdk_event_get())!=NULL){
1261  cpt++;
1262  // printf("event %d type %d on window %p My window %p\n",
1263  //cpt, ev->type, ev->any.window, widget->window);
1264 
1265  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1266  ret = true ;
1267  //printf("Click detection\n");
1268  }
1269  gdk_event_free(ev) ;
1270  }
1271  if (blocking){
1272  flushDisplay();
1273  vpTime::wait(100);
1274  }
1275  } while ( ret == false && blocking == true);
1276  }
1277  else {
1278  vpERROR_TRACE("GTK not initialized " ) ;
1280  "GTK not initialized")) ;
1281  }
1282  return ret;
1283 }
1284 
1301 bool
1303 {
1304  bool ret = false;
1305 
1307 
1308  GdkEvent *ev = NULL;
1309  double u, v ;
1310  do {
1311  while ((ev = gdk_event_get())!=NULL){
1312  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS) {
1313  u = ((GdkEventButton *)ev)->x ;
1314  v = ((GdkEventButton *)ev)->y ;
1315  ip.set_u( u );
1316  ip.set_v( v );
1317  ret = true ;
1318  }
1319  gdk_event_free(ev) ;
1320  }
1321  if (blocking){
1322  flushDisplay();
1323  vpTime::wait(100);
1324  }
1325  } while ( ret == false && blocking == true);
1326  }
1327  else {
1328  vpERROR_TRACE("GTK not initialized " ) ;
1330  "GTK not initialized")) ;
1331  }
1332  return ret ;
1333 }
1334 
1335 
1355 bool
1358  bool blocking)
1359 {
1360  bool ret = false;
1361 
1363  GdkEvent *ev = NULL;
1364  double u, v ;
1365  do {
1366  while ((ev = gdk_event_get())){
1367  if (ev->any.window == widget->window && ev->type == GDK_BUTTON_PRESS){
1368  u = ((GdkEventButton *)ev)->x ;
1369  v = ((GdkEventButton *)ev)->y ;
1370  ip.set_u( u );
1371  ip.set_v( v );
1372 
1373  switch ((int)((GdkEventButton *)ev)->button) {
1374  case 1:
1375  button = vpMouseButton::button1; break;
1376  case 2:
1377  button = vpMouseButton::button2; break;
1378  case 3:
1379  button = vpMouseButton::button3; break;
1380  }
1381  ret = true ;
1382  }
1383  gdk_event_free(ev) ;
1384  }
1385  if (blocking){
1386  flushDisplay();
1387  vpTime::wait(100);
1388  }
1389 
1390  } while ( ret == false && blocking == true);
1391  }
1392  else {
1393  vpERROR_TRACE("GTK not initialized " ) ;
1395  "GTK not initialized")) ;
1396  }
1397  return ret;
1398 }
1399 
1423 bool
1426  bool blocking)
1427 {
1428  bool ret = false;
1429 
1430  if ( displayHasBeenInitialized ) {
1431 
1432  //flushDisplay() ;
1433  GdkEvent *ev = NULL;
1434  double u, v ;
1435  do {
1436  while ((ev = gdk_event_get())!=NULL){
1437  if ( ev->any.window == widget->window
1438  && ev->type == GDK_BUTTON_RELEASE) {
1439  u = ((GdkEventButton *)ev)->x ;
1440  v = ((GdkEventButton *)ev)->y ;
1441  ip.set_u( u );
1442  ip.set_v( v );
1443 
1444  switch ( ( int ) ( ( GdkEventButton * ) ev )->button ) {
1445  case 1:
1446  button = vpMouseButton::button1; break;
1447  case 2:
1448  button = vpMouseButton::button2; break;
1449  case 3:
1450  button = vpMouseButton::button3; break;
1451  }
1452  ret = true ;
1453  }
1454  gdk_event_free(ev) ;
1455  }
1456  if (blocking){
1457  flushDisplay();
1458  vpTime::wait(100);
1459  }
1460 
1461  } while ( ret == false && blocking == true);
1462  }
1463  else {
1464  vpERROR_TRACE ( "GTK not initialized " ) ;
1466  "GTK not initialized" ) ) ;
1467  }
1468  return ret;
1469 }
1470 
1471 /*
1472  \brief gets the displayed image (including the overlay plane)
1473  and returns an RGBa image
1474 */
1476 {
1477 
1478 
1479  // shoudl certainly be optimized.
1480  // doesn't work
1482  {
1483 
1484  GdkImage *ImageGtk;
1485  /*
1486  */
1487 
1488  ImageGtk = gdk_image_get(background, 0, 0, (gint)width, (gint)height);
1489 
1490 
1491  I.resize(height,width) ;
1492  guchar *pos;
1493  guint32 pixel;
1494  gint x,y;
1495  guchar OctetRouge,OctetVert,OctetBleu,mask;
1496  mask = 0x000000FF;
1497 
1498  pos = (unsigned char *)I.bitmap;
1499  for (y=0;y<(gint)height;y++)
1500  {
1501  for (x=0;x<(gint)width;x++)
1502  {
1503  pixel = gdk_image_get_pixel(ImageGtk,x,y);
1504  OctetBleu = (guchar)pixel & mask;
1505  OctetVert = (guchar)(pixel>>8) & mask;
1506  OctetRouge = (guchar)(pixel>>16) & mask;
1507  *pos++ = OctetRouge;
1508  *pos++ = OctetVert;
1509  *pos++ = OctetBleu;
1510  *pos++ = 0;
1511  }
1512  }
1513 
1514 
1515  }
1516  else
1517  {
1518  vpERROR_TRACE("GTK not initialized " ) ;
1520  "GTK not initialized")) ;
1521  }
1522 
1523 }
1524 
1531 {
1532 
1533  unsigned int depth;
1534 
1535  depth = (unsigned int)gdk_window_get_visual(widget->window)->depth ;
1536 
1537  return (depth);
1538 }
1539 
1545 void vpDisplayGTK::getScreenSize(unsigned int &width, unsigned int &height)
1546 {
1547  vpTRACE("Not implemented") ;
1548  width = 0;
1549  height = 0;
1550 }
1551 
1567 bool
1569 {
1570  bool ret = false;
1571 
1572  int cpt =0;
1574 
1575  GdkEvent *ev = NULL;
1576  do {
1577  while ((ev = gdk_event_get())!=NULL){
1578  cpt++;
1579  // printf("event %d type %d on window %p My window %p\n",
1580  //cpt, ev->type, ev->any.window, widget->window);
1581 
1582  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1583  ret = true ;
1584  //printf("Key press detection\n");
1585  }
1586  gdk_event_free(ev) ;
1587  }
1588  if (blocking){
1589  flushDisplay();
1590  vpTime::wait(100);
1591  }
1592  } while ( ret == false && blocking == true);
1593  }
1594  else {
1595  vpERROR_TRACE("GTK not initialized " ) ;
1597  "GTK not initialized")) ;
1598  }
1599  return ret;
1600 }
1601 
1621 bool
1622 vpDisplayGTK::getKeyboardEvent(char *string, bool blocking)
1623 {
1624  bool ret = false;
1625 
1626  int cpt =0;
1628 
1629  GdkEvent *ev = NULL;
1630  do {
1631  while ((ev = gdk_event_get())!=NULL){
1632  cpt++;
1633  // printf("event %d type %d on window %p My window %p\n",
1634  //cpt, ev->type, ev->any.window, widget->window);
1635 
1636  if (ev->any.window == widget->window && ev->type == GDK_KEY_PRESS){
1637  //std::cout << "Key val: \"" << gdk_keyval_name (ev->key.keyval) /*ev->key.string*/ << "\"" << std::endl;
1638  sprintf(string, "%s", gdk_keyval_name (ev->key.keyval));
1639  ret = true ;
1640  //printf("Key press detection\n");
1641  }
1642  gdk_event_free(ev) ;
1643  }
1644  if (blocking){
1645  flushDisplay();
1646  vpTime::wait(100);
1647  }
1648  } while ( ret == false && blocking == true);
1649  }
1650  else {
1651  vpERROR_TRACE("GTK not initialized " ) ;
1653  "GTK not initialized")) ;
1654  }
1655  return ret;
1656 }
1657 
1670 bool
1672 {
1673  bool ret = false;
1674 
1676  GdkEvent *ev = NULL;
1677  double u, v ;
1678  if ((ev = gdk_event_get())){
1679  if (ev->any.window == widget->window && ev->type == GDK_MOTION_NOTIFY){
1680  u = ((GdkEventMotion *)ev)->x ;
1681  v = ((GdkEventMotion *)ev)->y ;
1682  ip.set_u( u );
1683  ip.set_v( v );
1684 
1685  ret = true ;
1686  }
1687  gdk_event_free(ev) ;
1688  }
1689  }
1690  else {
1691  vpERROR_TRACE("GTK not initialized " ) ;
1693  "GTK not initialized")) ;
1694  }
1695  return ret;
1696 }
1697 
1708 bool
1710 {
1712  {
1713  int u,v;
1714  gdk_window_get_pointer(widget->window, &u, &v, NULL);
1715  ip.set_u( u );
1716  ip.set_v( v );
1717  }
1718  else {
1719  vpERROR_TRACE("GTK not initialized " ) ;
1721  "GTK not initialized")) ;
1722  }
1723 
1724  return true;
1725 }
1726 
1727 #endif
1728 
1729 /*
1730  * Local variables:
1731  * c-basic-offset: 2
1732  * End:
1733  */
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:174
double get_v() const
Definition: vpImagePoint.h:263
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:176
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:185
double get_i() const
Definition: vpImagePoint.h:194
unsigned int getWidth() const
Definition: vpImage.h:159
#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:252
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:180
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:205
std::string title_
Definition: vpDisplay.h:187
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:158
double getWidth() const
Definition: vpRect.h:193
void flushDisplay()
void set_u(const double u)
Definition: vpImagePoint.h:216
static double sqr(double x)
Definition: vpMath.h:106
virtual ~vpDisplayGTK()
unsigned int height
Definition: vpDisplay.h:186
void set_v(const double v)
Definition: vpImagePoint.h:227
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
Definition: vpImage.h:532
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:169
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:182
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:150
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:92
void setTitle(const char *title)
bool getKeyboardEvent(bool blocking=true)
int windowYPosition
display position
Definition: vpDisplay.h:184
double getLeft() const
Definition: vpRect.h:161