Visual Servoing Platform  version 3.0.0
vpDisplayX.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Image display.
32  *
33  * Authors:
34  * Fabien Spindler
35  * Anthony Saunier
36  *
37  *****************************************************************************/
38 
45 #include <visp3/core/vpConfig.h>
46 #ifdef VISP_HAVE_X11
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <iostream>
51 #include <cmath> // std::fabs
52 #include <limits> // numeric_limits
53 
54 // Display stuff
55 #include <visp3/core/vpDisplay.h>
56 #include <visp3/gui/vpDisplayX.h>
57 
58 //debug / exception
59 #include <visp3/core/vpDebug.h>
60 #include <visp3/core/vpDisplayException.h>
61 
62 // math
63 #include <visp3/core/vpMath.h>
64 
76  int x,
77  int y,
78  const char *title )
79  : display(NULL), window(), Ximage(NULL), lut(), context(),
80  screen(0), event(), pixmap(), x_color(NULL),
81  screen_depth(8), xcolor(), values(), ximage_data_init(false),
82  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
83 {
84  init ( I, x, y, title ) ;
85 }
86 
87 
88 
98  int x,
99  int y,
100  const char *title )
101  : display(NULL), window(), Ximage(NULL), lut(), context(),
102  screen(0), event(), pixmap(), x_color(NULL),
103  screen_depth(8), xcolor(), values(), ximage_data_init(false),
104  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
105 {
106  init ( I, x, y, title ) ;
107 }
108 
131 vpDisplayX::vpDisplayX ( int x, int y, const char *title )
132  : display(NULL), window(), Ximage(NULL), lut(), context(),
133  screen(0), event(), pixmap(), x_color(NULL),
134  screen_depth(8), xcolor(), values(), ximage_data_init(false),
135  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
136 {
137  windowXPosition = x ;
138  windowYPosition = y ;
139 
140  if (title != NULL)
141  title_ = std::string(title);
142  else
143  title_ = std::string(" ");
144 }
145 
166  : display(NULL), window(), Ximage(NULL), lut(), context(),
167  screen(0), event(), pixmap(), x_color(NULL),
168  screen_depth(8), xcolor(), values(), ximage_data_init(false),
169  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
170 {
171 }
172 
177 {
178  closeDisplay() ;
179 }
180 
189 void
190 vpDisplayX::init ( vpImage<unsigned char> &I, int x, int y, const char *title )
191 {
192 
193  if (x_color == NULL) {
194  // id_unknown = number of predefined colors
195  x_color= new unsigned long [vpColor::id_unknown];
196  }
197 
198  XSizeHints hints;
199  if (x != -1)
200  windowXPosition = x ;
201  if (y != -1)
202  windowYPosition = y ;
203 
204  if (title != NULL)
205  title_ = std::string(title);
206  else
207  title_ = std::string(" ");
208 
209  // Positionnement de la fenetre dans l'ecran.
210  if ( ( windowXPosition < 0 ) || ( windowYPosition < 0 ) )
211  {
212  hints.flags = 0;
213  }
214  else
215  {
216  hints.flags = USPosition;
217  hints.x = windowXPosition;
218  hints.y = windowYPosition;
219  }
220 
221  // setup X11 --------------------------------------------------
222  width = I.getWidth();
223  height = I.getHeight();
224  display = XOpenDisplay ( NULL );
225  if ( display == NULL )
226  {
227  vpERROR_TRACE ( "Can't connect display on server %s.\n", XDisplayName ( NULL ) );
229  "Can't connect display on server." ) ) ;
230  }
231 
232  screen = DefaultScreen ( display );
233  lut = DefaultColormap ( display, screen );
234  screen_depth = (unsigned int)DefaultDepth ( display, screen );
235 
236  if ( ( window = XCreateSimpleWindow ( display, RootWindow ( display, screen ),
238  BlackPixel ( display, screen ),
239  WhitePixel ( display, screen ) ) ) == 0 )
240  {
241  vpERROR_TRACE ( "Can't create window." );
243  "Can't create window." ) ) ;
244  }
245 
246  //
247  // Create color table for 8 and 16 bits screen
248  //
249  if ( screen_depth == 8 )
250  {
251  lut = XCreateColormap ( display, window,
252  DefaultVisual ( display, screen ), AllocAll ) ;
253  xcolor.flags = DoRed | DoGreen | DoBlue ;
254 
255  for ( unsigned int i = 0 ; i < 256 ; i++ )
256  {
257  xcolor.pixel = i ;
258  xcolor.red = 256 * i;
259  xcolor.green = 256 * i;
260  xcolor.blue = 256 * i;
261  XStoreColor ( display, lut, &xcolor );
262  }
263 
264  XSetWindowColormap ( display, window, lut ) ;
265  XInstallColormap ( display, lut ) ;
266  }
267 
268  else if ( screen_depth == 16 )
269  {
270  for ( unsigned int i = 0; i < 256; i ++ )
271  {
272  xcolor.pad = 0;
273  xcolor.red = xcolor.green = xcolor.blue = 256 * i;
274  if ( XAllocColor ( display, lut, &xcolor ) == 0 )
275  {
276  vpERROR_TRACE ( "Can't allocate 256 colors. Only %d allocated.", i );
278  "Can't allocate 256 colors." ) ) ;
279  }
280  colortable[i] = xcolor.pixel;
281  }
282 
283  XSetWindowColormap ( display, window, lut ) ;
284  XInstallColormap ( display, lut ) ;
285 
286  Visual *visual = DefaultVisual (display, screen);
287  RMask = visual->red_mask;
288  GMask = visual->green_mask;
289  BMask = visual->blue_mask;
290 
291  RShift = 15 - getMsb(RMask); /* these are right-shifts */
292  GShift = 15 - getMsb(GMask);
293  BShift = 15 - getMsb(BMask);
294  }
295 
296  //
297  // Create colors for overlay
298  //
299  switch ( screen_depth )
300  {
301  case 8:
302  // Color BLACK and WHITE are set properly by default.
303 
304  // Color LIGHT GRAY.
305  x_color[vpColor::id_lightGray] = 254;
306  xcolor.pixel = x_color[vpColor::id_lightGray] ;
307  xcolor.red = 256 * 192;
308  xcolor.green = 256 * 192;
309  xcolor.blue = 256 * 192;
310  XStoreColor ( display, lut, &xcolor );
311 
312  // Color GRAY.
313  x_color[vpColor::id_gray] = 253;
314  xcolor.pixel = x_color[vpColor::id_gray] ;
315  xcolor.red = 256 * 128;
316  xcolor.green = 256 * 128;
317  xcolor.blue = 256 * 128;
318  XStoreColor ( display, lut, &xcolor );
319 
320  // Color DARK GRAY.
321  x_color[vpColor::id_darkGray] = 252;
322  xcolor.pixel = x_color[vpColor::id_darkGray] ;
323  xcolor.red = 256 * 64;
324  xcolor.green = 256 * 64;
325  xcolor.blue = 256 * 64;
326  XStoreColor ( display, lut, &xcolor );
327 
328  // Color LIGHT RED.
329  x_color[vpColor::id_lightRed] = 251;
330  xcolor.pixel = x_color[vpColor::id_lightRed] ;
331  xcolor.red = 256 * 255;
332  xcolor.green = 256 * 140;
333  xcolor.blue = 256 * 140;
334  XStoreColor ( display, lut, &xcolor );
335 
336  // Color RED.
337  x_color[vpColor::id_red] = 250;
338  xcolor.pixel = x_color[vpColor::id_red] ;
339  xcolor.red = 256 * 255;
340  xcolor.green = 0;
341  xcolor.blue = 0;
342  XStoreColor ( display, lut, &xcolor );
343 
344  // Color DARK RED.
345  x_color[vpColor::id_darkRed] = 249;
346  xcolor.pixel = x_color[vpColor::id_darkRed] ;
347  xcolor.red = 256 * 128;
348  xcolor.green = 0;
349  xcolor.blue = 0;
350  XStoreColor ( display, lut, &xcolor );
351 
352  // Color LIGHT GREEN.
353  x_color[vpColor::id_lightGreen] = 248;
354  xcolor.pixel = x_color[vpColor::id_lightGreen] ;
355  xcolor.red = 256 * 140;
356  xcolor.green = 256 * 255;
357  xcolor.blue = 256 * 140;
358  XStoreColor ( display, lut, &xcolor );
359 
360  // Color GREEN.
361  x_color[vpColor::id_green] = 247;
362  xcolor.pixel = x_color[vpColor::id_green];
363  xcolor.red = 0;
364  xcolor.green = 256 * 255;
365  xcolor.blue = 0;
366  XStoreColor ( display, lut, &xcolor );
367 
368  // Color DARK GREEN.
369  x_color[vpColor::id_darkGreen] = 246;
370  xcolor.pixel = x_color[vpColor::id_darkGreen] ;
371  xcolor.red = 0;
372  xcolor.green = 256 * 128;
373  xcolor.blue = 0;
374  XStoreColor ( display, lut, &xcolor );
375 
376  // Color LIGHT BLUE.
377  x_color[vpColor::id_lightBlue] = 245;
378  xcolor.pixel = x_color[vpColor::id_lightBlue] ;
379  xcolor.red = 256 * 140;
380  xcolor.green = 256 * 140;
381  xcolor.blue = 256 * 255;
382  XStoreColor ( display, lut, &xcolor );
383 
384  // Color BLUE.
385  x_color[vpColor::id_blue] = 244;
386  xcolor.pixel = x_color[vpColor::id_blue];
387  xcolor.red = 0;
388  xcolor.green = 0;
389  xcolor.blue = 256 * 255;
390  XStoreColor ( display, lut, &xcolor );
391 
392  // Color DARK BLUE.
393  x_color[vpColor::id_darkBlue] = 243;
394  xcolor.pixel = x_color[vpColor::id_darkBlue] ;
395  xcolor.red = 0;
396  xcolor.green = 0;
397  xcolor.blue = 256 * 128;
398  XStoreColor ( display, lut, &xcolor );
399 
400  // Color YELLOW.
401  x_color[vpColor::id_yellow] = 242;
402  xcolor.pixel = x_color[vpColor::id_yellow];
403  xcolor.red = 256 * 255;
404  xcolor.green = 256 * 255;
405  xcolor.blue = 0;
406  XStoreColor ( display, lut, &xcolor );
407 
408  // Color ORANGE.
409  x_color[vpColor::id_orange] = 241;
410  xcolor.pixel = x_color[vpColor::id_orange];
411  xcolor.red = 256 * 255;
412  xcolor.green = 256 * 165;
413  xcolor.blue = 0;
414  XStoreColor ( display, lut, &xcolor );
415 
416  // Color CYAN.
417  x_color[vpColor::id_cyan] = 240;
418  xcolor.pixel = x_color[vpColor::id_cyan];
419  xcolor.red = 0;
420  xcolor.green = 256 * 255;
421  xcolor.blue = 256 * 255;
422  XStoreColor ( display, lut, &xcolor );
423 
424  // Color PURPLE.
425  x_color[vpColor::id_purple] = 239;
426  xcolor.pixel = x_color[vpColor::id_purple];
427  xcolor.red = 256 * 128;
428  xcolor.green = 0;
429  xcolor.blue = 256 * 128;
430  XStoreColor ( display, lut, &xcolor );
431 
432  break;
433 
434  case 16:
435  case 24:
436  {
437  xcolor.flags = DoRed | DoGreen | DoBlue ;
438 
439  // Couleur BLACK.
440  xcolor.pad = 0;
441  xcolor.red = 0;
442  xcolor.green = 0;
443  xcolor.blue = 0;
444  XAllocColor ( display, lut, &xcolor );
445  x_color[vpColor::id_black] = xcolor.pixel;
446 
447  // Couleur WHITE.
448  xcolor.pad = 0;
449  xcolor.red = 256* 255;
450  xcolor.green = 256* 255;
451  xcolor.blue = 256* 255;
452  XAllocColor ( display, lut, &xcolor );
453  x_color[vpColor::id_white] = xcolor.pixel;
454 
455  // Couleur LIGHT GRAY.
456  xcolor.pad = 0;
457  xcolor.red = 256 * 192;
458  xcolor.green = 256 * 192;
459  xcolor.blue = 256 * 192;
460  XAllocColor ( display, lut, &xcolor );
461  x_color[vpColor::id_lightGray] = xcolor.pixel;
462 
463  // Couleur GRAY.
464  xcolor.pad = 0;
465  xcolor.red = 256 * 128;
466  xcolor.green = 256 * 128;
467  xcolor.blue = 256 * 128;
468  XAllocColor ( display, lut, &xcolor );
469  x_color[vpColor::id_gray] = xcolor.pixel;
470 
471  // Couleur DARK GRAY.
472  xcolor.pad = 0;
473  xcolor.red = 256 * 64;
474  xcolor.green = 256 * 64;
475  xcolor.blue = 256 * 64;
476  XAllocColor ( display, lut, &xcolor );
477  x_color[vpColor::id_darkGray] = xcolor.pixel;
478 
479  // Couleur LIGHT RED.
480  xcolor.pad = 0;
481  xcolor.red = 256 * 255;
482  xcolor.green = 256 * 140;
483  xcolor.blue = 256 * 140;
484  XAllocColor ( display, lut, &xcolor );
485  x_color[vpColor::id_lightRed] = xcolor.pixel;
486 
487  // Couleur RED.
488  xcolor.pad = 0;
489  xcolor.red = 256* 255;
490  xcolor.green = 0;
491  xcolor.blue = 0;
492  XAllocColor ( display, lut, &xcolor );
493  x_color[vpColor::id_red] = xcolor.pixel;
494 
495  // Couleur DARK RED.
496  xcolor.pad = 0;
497  xcolor.red = 256* 128;
498  xcolor.green = 0;
499  xcolor.blue = 0;
500  XAllocColor ( display, lut, &xcolor );
501  x_color[vpColor::id_darkRed] = xcolor.pixel;
502 
503  // Couleur LIGHT GREEN.
504  xcolor.pad = 0;
505  xcolor.red = 256 * 140;
506  xcolor.green = 256 * 255;
507  xcolor.blue = 256 * 140;
508  XAllocColor ( display, lut, &xcolor );
509  x_color[vpColor::id_lightGreen] = xcolor.pixel;
510 
511  // Couleur GREEN.
512  xcolor.pad = 0;
513  xcolor.red = 0;
514  xcolor.green = 256*255;
515  xcolor.blue = 0;
516  XAllocColor ( display, lut, &xcolor );
517  x_color[vpColor::id_green] = xcolor.pixel;
518 
519  // Couleur DARK GREEN.
520  xcolor.pad = 0;
521  xcolor.red = 0;
522  xcolor.green = 256* 128;
523  xcolor.blue = 0;
524  XAllocColor ( display, lut, &xcolor );
525  x_color[vpColor::id_darkGreen] = xcolor.pixel;
526 
527  // Couleur LIGHT Blue.
528  xcolor.pad = 0;
529  xcolor.red = 256 * 140;
530  xcolor.green = 256 * 140;
531  xcolor.blue = 256 * 255;
532  XAllocColor ( display, lut, &xcolor );
533  x_color[vpColor::id_lightBlue] = xcolor.pixel;
534 
535  // Couleur BLUE.
536  xcolor.pad = 0;
537  xcolor.red = 0;
538  xcolor.green = 0;
539  xcolor.blue = 256* 255;
540  XAllocColor ( display, lut, &xcolor );
541  x_color[vpColor::id_blue] = xcolor.pixel;
542 
543  // Couleur DARK BLUE.
544  xcolor.pad = 0;
545  xcolor.red = 0;
546  xcolor.green = 0;
547  xcolor.blue = 256* 128;
548  XAllocColor ( display, lut, &xcolor );
549  x_color[vpColor::id_darkBlue] = xcolor.pixel;
550 
551  // Couleur YELLOW.
552  xcolor.pad = 0;
553  xcolor.red = 256 * 255;
554  xcolor.green = 256 * 255;
555  xcolor.blue = 0;
556  XAllocColor ( display, lut, &xcolor );
557  x_color[vpColor::id_yellow] = xcolor.pixel;
558 
559  // Couleur ORANGE.
560  xcolor.pad = 0;
561  xcolor.red = 256 * 255;
562  xcolor.green = 256 * 165;
563  xcolor.blue = 0;
564  XAllocColor ( display, lut, &xcolor );
565  x_color[vpColor::id_orange] = xcolor.pixel;
566 
567  // Couleur CYAN.
568  xcolor.pad = 0;
569  xcolor.red = 0;
570  xcolor.green = 256 * 255;
571  xcolor.blue = 256 * 255;
572  XAllocColor ( display, lut, &xcolor );
573  x_color[vpColor::id_cyan] = xcolor.pixel;
574 
575  // Couleur PURPLE.
576  xcolor.pad = 0;
577  xcolor.red = 256 * 128;
578  xcolor.green = 0;
579  xcolor.blue = 256 * 128;
580  XAllocColor ( display, lut, &xcolor );
581  x_color[vpColor::id_purple] = xcolor.pixel;
582  break;
583  }
584  }
585 
586  XSetStandardProperties ( display, window, this->title_.c_str(), this->title_.c_str(), None, 0, 0, &hints );
587  XMapWindow ( display, window ) ;
588  // Selection des evenements.
589  XSelectInput ( display, window,
590  ExposureMask |
591  ButtonPressMask | ButtonReleaseMask |
592  KeyPressMask | KeyReleaseMask |
593  StructureNotifyMask |
594  PointerMotionMask);
595 
596  // graphic context creation
597  values.plane_mask = AllPlanes;
598  values.fill_style = FillSolid;
599  values.foreground = WhitePixel ( display, screen );
600  values.background = BlackPixel ( display, screen );
601  context = XCreateGC ( display, window,
602  GCPlaneMask | GCFillStyle | GCForeground | GCBackground,
603  &values );
604 
605  if ( context == NULL )
606  {
607  vpERROR_TRACE ( "Can't create graphics context." );
609  "Can't create graphics context" ) ) ;
610 
611  }
612 
613  // Pixmap creation.
614  pixmap = XCreatePixmap ( display, window, width, height, screen_depth );
615 
616  // Hangs when forward X11 is used to send the display to an other computer
617 // do
618 // XNextEvent ( display, &event );
619 // while ( event.xany.type != Expose );
620 
621  {
622  Ximage = XCreateImage ( display, DefaultVisual ( display, screen ),
623  screen_depth, ZPixmap, 0, NULL,
624  I.getWidth() , I.getHeight(), XBitmapPad ( display ), 0 );
625 
626  Ximage->data = ( char * ) malloc ( I.getHeight() * (unsigned int)Ximage->bytes_per_line );
627  ximage_data_init = true;
628 
629  }
631 
632  XStoreName ( display, window, title_.c_str() );
633 
634  XSync ( display, 1 );
635 
636  I.display = this ;
637 }
638 
648 void
649 vpDisplayX::init ( vpImage<vpRGBa> &I, int x, int y, const char *title )
650 {
651 
652  XSizeHints hints;
653  if (x != -1)
654  windowXPosition = x ;
655  if (y != -1)
656  windowYPosition = y ;
657 
658  if (x_color == NULL) {
659  // id_unknown = number of predefined colors
660  x_color= new unsigned long [vpColor::id_unknown];
661  }
662 
663  if (title != NULL)
664  title_ = std::string(title);
665  else
666  title_ = std::string(" ");
667 
668  // Positionnement de la fenetre dans l'ecran.
669  if ( ( windowXPosition < 0 ) || ( windowYPosition < 0 ) )
670  {
671  hints.flags = 0;
672  }
673  else
674  {
675  hints.flags = USPosition;
676  hints.x = windowXPosition;
677  hints.y = windowYPosition;
678  }
679 
680  // setup X11 --------------------------------------------------
681  width = I.getWidth();
682  height = I.getHeight();
683 
684  if ( ( display = XOpenDisplay ( NULL ) ) == NULL )
685  {
686  vpERROR_TRACE ( "Can't connect display on server %s.\n", XDisplayName ( NULL ) );
688  "Can't connect display on server." ) ) ;
689  }
690 
691  screen = DefaultScreen ( display );
692  lut = DefaultColormap ( display, screen );
693  screen_depth = (unsigned int)DefaultDepth ( display, screen );
694 
695  vpDEBUG_TRACE ( 1, "Screen depth: %d\n", screen_depth );
696 
697  if ( ( window = XCreateSimpleWindow ( display, RootWindow ( display, screen ),
699  width, height, 1,
700  BlackPixel ( display, screen ),
701  WhitePixel ( display, screen ) ) ) == 0 )
702  {
703  vpERROR_TRACE ( "Can't create window." );
705  "Can't create window." ) ) ;
706  }
707 
708  //
709  // Create color table for 8 and 16 bits screen
710  //
711  if ( screen_depth == 8 )
712  {
713  lut = XCreateColormap ( display, window,
714  DefaultVisual ( display, screen ), AllocAll ) ;
715  xcolor.flags = DoRed | DoGreen | DoBlue ;
716 
717  for ( unsigned int i = 0 ; i < 256 ; i++ )
718  {
719  xcolor.pixel = i ;
720  xcolor.red = 256 * i;
721  xcolor.green = 256 * i;
722  xcolor.blue = 256 * i;
723  XStoreColor ( display, lut, &xcolor );
724  }
725 
726  XSetWindowColormap ( display, window, lut ) ;
727  XInstallColormap ( display, lut ) ;
728  }
729 
730  else if ( screen_depth == 16 )
731  {
732  for ( unsigned int i = 0; i < 256; i ++ )
733  {
734  xcolor.pad = 0;
735  xcolor.red = xcolor.green = xcolor.blue = 256 * i;
736  if ( XAllocColor ( display, lut, &xcolor ) == 0 )
737  {
738  vpERROR_TRACE ( "Can't allocate 256 colors. Only %d allocated.", i );
740  "Can't allocate 256 colors." ) ) ;
741  }
742  colortable[i] = xcolor.pixel;
743  }
744 
745  Visual *visual = DefaultVisual (display, screen);
746  RMask = visual->red_mask;
747  GMask = visual->green_mask;
748  BMask = visual->blue_mask;
749 
750  RShift = 15 - getMsb(RMask); /* these are right-shifts */
751  GShift = 15 - getMsb(GMask);
752  BShift = 15 - getMsb(BMask);
753 
754  XSetWindowColormap ( display, window, lut ) ;
755  XInstallColormap ( display, lut ) ;
756  }
757 
758 
759  //
760  // Create colors for overlay
761  //
762  switch ( screen_depth )
763  {
764 
765  case 8:
766  // Color BLACK and WHITE are set properly.
767 
768  // Color LIGHT GRAY.
769  x_color[vpColor::id_lightGray] = 254;
770  xcolor.pixel = x_color[vpColor::id_lightGray] ;
771  xcolor.red = 256 * 192;
772  xcolor.green = 256 * 192;
773  xcolor.blue = 256 * 192;
774  XStoreColor ( display, lut, &xcolor );
775 
776  // Color GRAY.
777  x_color[vpColor::id_gray] = 253;
778  xcolor.pixel = x_color[vpColor::id_gray] ;
779  xcolor.red = 256 * 128;
780  xcolor.green = 256 * 128;
781  xcolor.blue = 256 * 128;
782  XStoreColor ( display, lut, &xcolor );
783 
784  // Color DARK GRAY.
785  x_color[vpColor::id_darkGray] = 252;
786  xcolor.pixel = x_color[vpColor::id_darkGray] ;
787  xcolor.red = 256 * 64;
788  xcolor.green = 256 * 64;
789  xcolor.blue = 256 * 64;
790  XStoreColor ( display, lut, &xcolor );
791 
792  // Color LIGHT RED.
793  x_color[vpColor::id_lightRed] = 251;
794  xcolor.pixel = x_color[vpColor::id_lightRed] ;
795  xcolor.red = 256 * 255;
796  xcolor.green = 256 * 140;
797  xcolor.blue = 256 * 140;
798  XStoreColor ( display, lut, &xcolor );
799 
800  // Color RED.
801  x_color[vpColor::id_red] = 250;
802  xcolor.pixel = x_color[vpColor::id_red] ;
803  xcolor.red = 256 * 255;
804  xcolor.green = 0;
805  xcolor.blue = 0;
806  XStoreColor ( display, lut, &xcolor );
807 
808  // Color DARK RED.
809  x_color[vpColor::id_darkRed] = 249;
810  xcolor.pixel = x_color[vpColor::id_darkRed] ;
811  xcolor.red = 256 * 128;
812  xcolor.green = 0;
813  xcolor.blue = 0;
814  XStoreColor ( display, lut, &xcolor );
815 
816  // Color LIGHT GREEN.
817  x_color[vpColor::id_lightGreen] = 248;
818  xcolor.pixel = x_color[vpColor::id_lightGreen] ;
819  xcolor.red = 256 * 140;
820  xcolor.green = 256 * 255;
821  xcolor.blue = 256 * 140;
822  XStoreColor ( display, lut, &xcolor );
823 
824  // Color GREEN.
825  x_color[vpColor::id_green] = 247;
826  xcolor.pixel = x_color[vpColor::id_green];
827  xcolor.red = 0;
828  xcolor.green = 256 * 255;
829  xcolor.blue = 0;
830  XStoreColor ( display, lut, &xcolor );
831 
832  // Color DARK GREEN.
833  x_color[vpColor::id_darkGreen] = 246;
834  xcolor.pixel = x_color[vpColor::id_darkGreen] ;
835  xcolor.red = 0;
836  xcolor.green = 256 * 128;
837  xcolor.blue = 0;
838  XStoreColor ( display, lut, &xcolor );
839 
840  // Color LIGHT BLUE.
841  x_color[vpColor::id_lightBlue] = 245;
842  xcolor.pixel = x_color[vpColor::id_lightBlue] ;
843  xcolor.red = 256 * 140;
844  xcolor.green = 256 * 140;
845  xcolor.blue = 256 * 255;
846  XStoreColor ( display, lut, &xcolor );
847 
848  // Color BLUE.
849  x_color[vpColor::id_blue] = 244;
850  xcolor.pixel = x_color[vpColor::id_blue];
851  xcolor.red = 0;
852  xcolor.green = 0;
853  xcolor.blue = 256 * 255;
854  XStoreColor ( display, lut, &xcolor );
855 
856  // Color DARK BLUE.
857  x_color[vpColor::id_darkBlue] = 243;
858  xcolor.pixel = x_color[vpColor::id_darkBlue] ;
859  xcolor.red = 0;
860  xcolor.green = 0;
861  xcolor.blue = 256 * 128;
862  XStoreColor ( display, lut, &xcolor );
863 
864  // Color YELLOW.
865  x_color[vpColor::id_yellow] = 242;
866  xcolor.pixel = x_color[vpColor::id_yellow];
867  xcolor.red = 256 * 255;
868  xcolor.green = 256 * 255;
869  xcolor.blue = 0;
870  XStoreColor ( display, lut, &xcolor );
871 
872  // Color ORANGE.
873  x_color[vpColor::id_orange] = 241;
874  xcolor.pixel = x_color[vpColor::id_orange];
875  xcolor.red = 256 * 255;
876  xcolor.green = 256 * 165;
877  xcolor.blue = 0;
878  XStoreColor ( display, lut, &xcolor );
879 
880  // Color CYAN.
881  x_color[vpColor::id_cyan] = 240;
882  xcolor.pixel = x_color[vpColor::id_cyan];
883  xcolor.red = 0;
884  xcolor.green = 256 * 255;
885  xcolor.blue = 256 * 255;
886  XStoreColor ( display, lut, &xcolor );
887 
888  // Color PURPLE.
889  x_color[vpColor::id_purple] = 239;
890  xcolor.pixel = x_color[vpColor::id_purple];
891  xcolor.red = 256 * 128;
892  xcolor.green = 0;
893  xcolor.blue = 256 * 128;
894  XStoreColor ( display, lut, &xcolor );
895 
896  break;
897 
898  case 16:
899  case 24:
900  {
901  xcolor.flags = DoRed | DoGreen | DoBlue ;
902 
903  // Couleur BLACK.
904  xcolor.pad = 0;
905  xcolor.red = 0;
906  xcolor.green = 0;
907  xcolor.blue = 0;
908  XAllocColor ( display, lut, &xcolor );
909  x_color[vpColor::id_black] = xcolor.pixel;
910 
911  // Couleur WHITE.
912  xcolor.pad = 0;
913  xcolor.red = 256* 255;
914  xcolor.green = 256* 255;
915  xcolor.blue = 256* 255;
916  XAllocColor ( display, lut, &xcolor );
917  x_color[vpColor::id_white] = xcolor.pixel;
918 
919  // Couleur LIGHT GRAY.
920  xcolor.pad = 0;
921  xcolor.red = 256 * 192;
922  xcolor.green = 256 * 192;
923  xcolor.blue = 256 * 192;
924  XAllocColor ( display, lut, &xcolor );
925  x_color[vpColor::id_lightGray] = xcolor.pixel;
926 
927  // Couleur GRAY.
928  xcolor.pad = 0;
929  xcolor.red = 256 * 128;
930  xcolor.green = 256 * 128;
931  xcolor.blue = 256 * 128;
932  XAllocColor ( display, lut, &xcolor );
933  x_color[vpColor::id_gray] = xcolor.pixel;
934 
935  // Couleur DARK GRAY.
936  xcolor.pad = 0;
937  xcolor.red = 256 * 64;
938  xcolor.green = 256 * 64;
939  xcolor.blue = 256 * 64;
940  XAllocColor ( display, lut, &xcolor );
941  x_color[vpColor::id_darkGray] = xcolor.pixel;
942 
943  // Couleur LIGHT RED.
944  xcolor.pad = 0;
945  xcolor.red = 256 * 255;
946  xcolor.green = 256 * 140;
947  xcolor.blue = 256 * 140;
948  XAllocColor ( display, lut, &xcolor );
949  x_color[vpColor::id_lightRed] = xcolor.pixel;
950 
951  // Couleur RED.
952  xcolor.pad = 0;
953  xcolor.red = 256* 255;
954  xcolor.green = 0;
955  xcolor.blue = 0;
956  XAllocColor ( display, lut, &xcolor );
957  x_color[vpColor::id_red] = xcolor.pixel;
958 
959  // Couleur DARK RED.
960  xcolor.pad = 0;
961  xcolor.red = 256* 128;
962  xcolor.green = 0;
963  xcolor.blue = 0;
964  XAllocColor ( display, lut, &xcolor );
965  x_color[vpColor::id_darkRed] = xcolor.pixel;
966 
967  // Couleur LIGHT GREEN.
968  xcolor.pad = 0;
969  xcolor.red = 256 * 140;
970  xcolor.green = 256 * 255;
971  xcolor.blue = 256 * 140;
972  XAllocColor ( display, lut, &xcolor );
973  x_color[vpColor::id_lightGreen] = xcolor.pixel;
974 
975  // Couleur GREEN.
976  xcolor.pad = 0;
977  xcolor.red = 0;
978  xcolor.green = 256*255;
979  xcolor.blue = 0;
980  XAllocColor ( display, lut, &xcolor );
981  x_color[vpColor::id_green] = xcolor.pixel;
982 
983  // Couleur DARK GREEN.
984  xcolor.pad = 0;
985  xcolor.red = 0;
986  xcolor.green = 256* 128;
987  xcolor.blue = 0;
988  XAllocColor ( display, lut, &xcolor );
989  x_color[vpColor::id_darkGreen] = xcolor.pixel;
990 
991  // Couleur LIGHT Blue.
992  xcolor.pad = 0;
993  xcolor.red = 256 * 140;
994  xcolor.green = 256 * 140;
995  xcolor.blue = 256 * 255;
996  XAllocColor ( display, lut, &xcolor );
997  x_color[vpColor::id_lightBlue] = xcolor.pixel;
998 
999  // Couleur BLUE.
1000  xcolor.pad = 0;
1001  xcolor.red = 0;
1002  xcolor.green = 0;
1003  xcolor.blue = 256* 255;
1004  XAllocColor ( display, lut, &xcolor );
1005  x_color[vpColor::id_blue] = xcolor.pixel;
1006 
1007  // Couleur DARK BLUE.
1008  xcolor.pad = 0;
1009  xcolor.red = 0;
1010  xcolor.green = 0;
1011  xcolor.blue = 256* 128;
1012  XAllocColor ( display, lut, &xcolor );
1013  x_color[vpColor::id_darkBlue] = xcolor.pixel;
1014 
1015  // Couleur YELLOW.
1016  xcolor.pad = 0;
1017  xcolor.red = 256 * 255;
1018  xcolor.green = 256 * 255;
1019  xcolor.blue = 0;
1020  XAllocColor ( display, lut, &xcolor );
1021  x_color[vpColor::id_yellow] = xcolor.pixel;
1022 
1023  // Couleur ORANGE.
1024  xcolor.pad = 0;
1025  xcolor.red = 256 * 255;
1026  xcolor.green = 256 * 165;
1027  xcolor.blue = 0;
1028  XAllocColor ( display, lut, &xcolor );
1029  x_color[vpColor::id_orange] = xcolor.pixel;
1030 
1031  // Couleur CYAN.
1032  xcolor.pad = 0;
1033  xcolor.red = 0;
1034  xcolor.green = 256 * 255;
1035  xcolor.blue = 256 * 255;
1036  XAllocColor ( display, lut, &xcolor );
1037  x_color[vpColor::id_cyan] = xcolor.pixel;
1038 
1039  // Couleur PURPLE.
1040  xcolor.pad = 0;
1041  xcolor.red = 256 * 128;
1042  xcolor.green = 0;
1043  xcolor.blue = 256 * 128;
1044  XAllocColor ( display, lut, &xcolor );
1045  x_color[vpColor::id_purple] = xcolor.pixel;
1046  break;
1047  }
1048  }
1049 
1050  XSetStandardProperties ( display, window, this->title_.c_str(), this->title_.c_str(), None, 0, 0, &hints );
1051  XMapWindow ( display, window ) ;
1052  // Selection des evenements.
1053  XSelectInput ( display, window,
1054  ExposureMask |
1055  ButtonPressMask | ButtonReleaseMask |
1056  KeyPressMask | KeyReleaseMask |
1057  StructureNotifyMask |
1058  PointerMotionMask);
1059 
1060  // Creation du contexte graphique
1061  values.plane_mask = AllPlanes;
1062  values.fill_style = FillSolid;
1063  values.foreground = WhitePixel ( display, screen );
1064  values.background = BlackPixel ( display, screen );
1065  context = XCreateGC ( display, window,
1066  GCPlaneMask | GCFillStyle | GCForeground | GCBackground,
1067  &values );
1068 
1069  if ( context == NULL )
1070  {
1071  vpERROR_TRACE ( "Can't create graphics context." );
1073  "Can't create graphics context" ) ) ;
1074  }
1075 
1076  // Pixmap creation.
1077  pixmap = XCreatePixmap ( display, window, width, height, screen_depth );
1078 
1079  // Hangs when forward X11 is used to send the display to an other computer
1080 // do
1081 // XNextEvent ( display, &event );
1082 // while ( event.xany.type != Expose );
1083 
1084  {
1085  Ximage = XCreateImage ( display, DefaultVisual ( display, screen ),
1086  screen_depth, ZPixmap, 0, NULL,
1087  I.getWidth() , I.getHeight(), XBitmapPad ( display ), 0 );
1088 
1089 
1090  Ximage->data = ( char * ) malloc ( I.getHeight() * (unsigned int)Ximage->bytes_per_line );
1091  ximage_data_init = true;
1092 
1093  }
1094  displayHasBeenInitialized = true ;
1095 
1096  XSync ( display, true );
1097 
1098  XStoreName ( display, window, title_.c_str() );
1099 
1100  I.display = this ;
1101 }
1102 
1103 
1111 void vpDisplayX::init ( unsigned int w, unsigned int h, int x, int y, const char *title )
1112 {
1113  if (x_color == NULL) {
1114  // id_unknown = number of predefined colors
1115  x_color= new unsigned long [vpColor::id_unknown];
1116  }
1117  /* setup X11 ------------------------------------------------------------- */
1118  this->width = w;
1119  this->height = h;
1120 
1121  XSizeHints hints;
1122 
1123  if (x != -1)
1124  windowXPosition = x ;
1125  if (y != -1)
1126  windowYPosition = y ;
1127  // Positionnement de la fenetre dans l'ecran.
1128  if ( ( windowXPosition < 0 ) || ( windowYPosition < 0 ) )
1129  {
1130  hints.flags = 0;
1131  }
1132  else
1133  {
1134  hints.flags = USPosition;
1135  hints.x = windowXPosition;
1136  hints.y = windowYPosition;
1137  }
1138 
1139  if (title != NULL)
1140  title_ = std::string(title);
1141  else
1142  title_ = std::string(" ");
1143 
1144  if ( ( display = XOpenDisplay ( NULL ) ) == NULL )
1145  {
1146  vpERROR_TRACE ( "Can't connect display on server %s.\n", XDisplayName ( NULL ) );
1148  "Can't connect display on server." ) ) ;
1149  }
1150 
1151  screen = DefaultScreen ( display );
1152  lut = DefaultColormap ( display, screen );
1153  screen_depth = (unsigned int)DefaultDepth ( display, screen );
1154 
1155  vpTRACE ( "Screen depth: %d\n", screen_depth );
1156 
1157  if ( ( window = XCreateSimpleWindow ( display, RootWindow ( display, screen ),
1159  width, height, 1,
1160  BlackPixel ( display, screen ),
1161  WhitePixel ( display, screen ) ) ) == 0 )
1162  {
1163  vpERROR_TRACE ( "Can't create window." );
1165  "Can't create window." ) ) ;
1166  }
1167 
1168 
1169  //
1170  // Create color table for 8 and 16 bits screen
1171  //
1172  if ( screen_depth == 8 )
1173  {
1174  lut = XCreateColormap ( display, window,
1175  DefaultVisual ( display, screen ), AllocAll ) ;
1176  xcolor.flags = DoRed | DoGreen | DoBlue ;
1177 
1178  for ( unsigned int i = 0 ; i < 256 ; i++ )
1179  {
1180  xcolor.pixel = i ;
1181  xcolor.red = 256 * i;
1182  xcolor.green = 256 * i;
1183  xcolor.blue = 256 * i;
1184  XStoreColor ( display, lut, &xcolor );
1185  }
1186 
1187  XSetWindowColormap ( display, window, lut ) ;
1188  XInstallColormap ( display, lut ) ;
1189  }
1190 
1191  else if ( screen_depth == 16 )
1192  {
1193  for ( unsigned int i = 0; i < 256; i ++ )
1194  {
1195  xcolor.pad = 0;
1196  xcolor.red = xcolor.green = xcolor.blue = 256 * i;
1197  if ( XAllocColor ( display, lut, &xcolor ) == 0 )
1198  {
1199  vpERROR_TRACE ( "Can't allocate 256 colors. Only %d allocated.", i );
1201  "Can't allocate 256 colors." ) ) ;
1202  }
1203  colortable[i] = xcolor.pixel;
1204  }
1205 
1206  XSetWindowColormap ( display, window, lut ) ;
1207  XInstallColormap ( display, lut ) ;
1208 
1209  Visual *visual = DefaultVisual (display, screen);
1210  RMask = visual->red_mask;
1211  GMask = visual->green_mask;
1212  BMask = visual->blue_mask;
1213 
1214  RShift = 15 - getMsb(RMask); /* these are right-shifts */
1215  GShift = 15 - getMsb(GMask);
1216  BShift = 15 - getMsb(BMask);
1217  }
1218 
1219  vpColor pcolor; // predefined colors
1220 
1221  //
1222  // Create colors for overlay
1223  //
1224  switch ( screen_depth )
1225  {
1226 
1227  case 8:
1228  // Color BLACK: default set to 0
1229 
1230  // Color WHITE: default set to 255
1231 
1232  // Color LIGHT GRAY.
1233  pcolor = vpColor::lightGray;
1234  xcolor.pixel = 254 ; // affected to 254
1235  xcolor.red = 256 * pcolor.R;
1236  xcolor.green = 256 * pcolor.G;
1237  xcolor.blue = 256 * pcolor.B;
1238  XStoreColor ( display, lut, &xcolor );
1239 
1240  // Color GRAY.
1241  pcolor = vpColor::gray;
1242  xcolor.pixel = 253 ; // affected to 253
1243  xcolor.red = 256 * pcolor.R;
1244  xcolor.green = 256 * pcolor.G;
1245  xcolor.blue = 256 * pcolor.B;
1246  XStoreColor ( display, lut, &xcolor );
1247 
1248  // Color DARK GRAY.
1249  pcolor = vpColor::darkGray;
1250  xcolor.pixel = 252 ; // affected to 252
1251  xcolor.red = 256 * pcolor.R;
1252  xcolor.green = 256 * pcolor.G;
1253  xcolor.blue = 256 * pcolor.B;
1254  XStoreColor ( display, lut, &xcolor );
1255 
1256  // Color LIGHT RED.
1257  pcolor = vpColor::lightRed;
1258  xcolor.pixel = 251 ; // affected to 251
1259  xcolor.red = 256 * pcolor.R;
1260  xcolor.green = 256 * pcolor.G;
1261  xcolor.blue = 256 * pcolor.B;
1262  XStoreColor ( display, lut, &xcolor );
1263 
1264  // Color RED.
1265  pcolor = vpColor::red;
1266  xcolor.pixel = 250 ; // affected to 250
1267  xcolor.red = 256 * pcolor.R;
1268  xcolor.green = 256 * pcolor.G;
1269  xcolor.blue = 256 * pcolor.B;
1270  XStoreColor ( display, lut, &xcolor );
1271 
1272  // Color DARK RED.
1273  pcolor = vpColor::darkRed;
1274  xcolor.pixel = 249 ; // affected to 249
1275  xcolor.red = 256 * pcolor.R;
1276  xcolor.green = 256 * pcolor.G;
1277  xcolor.blue = 256 * pcolor.B;
1278  XStoreColor ( display, lut, &xcolor );
1279 
1280  // Color LIGHT GREEN.
1281  pcolor = vpColor::lightGreen;
1282  xcolor.pixel = 248 ; // affected to 248
1283  xcolor.red = 256 * pcolor.R;
1284  xcolor.green = 256 * pcolor.G;
1285  xcolor.blue = 256 * pcolor.B;
1286  XStoreColor ( display, lut, &xcolor );
1287 
1288  // Color GREEN.
1289  pcolor = vpColor::green;
1290  xcolor.pixel = 247; // affected to 247
1291  xcolor.red = 256 * pcolor.R;
1292  xcolor.green = 256 * pcolor.G;
1293  xcolor.blue = 256 * pcolor.B;
1294  XStoreColor ( display, lut, &xcolor );
1295 
1296  // Color DARK GREEN.
1297  pcolor = vpColor::darkGreen;
1298  xcolor.pixel = 246 ; // affected to 246
1299  xcolor.red = 256 * pcolor.R;
1300  xcolor.green = 256 * pcolor.G;
1301  xcolor.blue = 256 * pcolor.B;
1302  XStoreColor ( display, lut, &xcolor );
1303 
1304  // Color LIGHT BLUE.
1305  pcolor = vpColor::lightBlue;
1306  xcolor.pixel = 245 ; // affected to 245
1307  xcolor.red = 256 * pcolor.R;
1308  xcolor.green = 256 * pcolor.G;
1309  xcolor.blue = 256 * pcolor.B;
1310  XStoreColor ( display, lut, &xcolor );
1311 
1312  // Color BLUE.
1313  pcolor = vpColor::blue;
1314  xcolor.pixel = 244; // affected to 244
1315  xcolor.red = 256 * pcolor.R;
1316  xcolor.green = 256 * pcolor.G;
1317  xcolor.blue = 256 * pcolor.B;
1318  XStoreColor ( display, lut, &xcolor );
1319 
1320  // Color DARK BLUE.
1321  pcolor = vpColor::darkBlue;
1322  xcolor.pixel = 243 ; // affected to 243
1323  xcolor.red = 256 * pcolor.R;
1324  xcolor.green = 256 * pcolor.G;
1325  xcolor.blue = 256 * pcolor.B;
1326  XStoreColor ( display, lut, &xcolor );
1327 
1328  // Color YELLOW.
1329  pcolor = vpColor::yellow;
1330  xcolor.pixel = 242; // affected to 242
1331  xcolor.red = 256 * pcolor.R;
1332  xcolor.green = 256 * pcolor.G;
1333  xcolor.blue = 256 * pcolor.B;
1334  XStoreColor ( display, lut, &xcolor );
1335 
1336  // Color ORANGE.
1337  pcolor = vpColor::orange;
1338  xcolor.pixel = 241; // affected to 241
1339  xcolor.red = 256 * pcolor.R;
1340  xcolor.green = 256 * pcolor.G;
1341  xcolor.blue = 256 * pcolor.B;
1342  XStoreColor ( display, lut, &xcolor );
1343 
1344  // Color CYAN.
1345  pcolor = vpColor::cyan;
1346  xcolor.pixel = 240; // affected to 240
1347  xcolor.red = 256 * pcolor.R;
1348  xcolor.green = 256 * pcolor.G;
1349  xcolor.blue = 256 * pcolor.B;
1350  XStoreColor ( display, lut, &xcolor );
1351 
1352  // Color PURPLE.
1353  pcolor = vpColor::purple;
1354  xcolor.pixel = 239; // affected to 239
1355  xcolor.red = 256 * pcolor.R;
1356  xcolor.green = 256 * pcolor.G;
1357  xcolor.blue = 256 * pcolor.B;
1358  XStoreColor ( display, lut, &xcolor );
1359 
1360  break;
1361 
1362  case 16:
1363  case 24:
1364  {
1365  xcolor.flags = DoRed | DoGreen | DoBlue ;
1366 
1367  // Couleur BLACK.
1368  pcolor = vpColor::black;
1369  xcolor.pad = 0;
1370  xcolor.red = 256 * pcolor.R;
1371  xcolor.green = 256 * pcolor.G;
1372  xcolor.blue = 256 * pcolor.B;
1373  XAllocColor ( display, lut, &xcolor );
1374  x_color[vpColor::id_black] = xcolor.pixel;
1375 
1376  // Color WHITE.
1377  pcolor = vpColor::white;
1378  xcolor.pad = 0;
1379  xcolor.red = 256 * pcolor.R;
1380  xcolor.green = 256 * pcolor.G;
1381  xcolor.blue = 256 * pcolor.B;
1382  XAllocColor ( display, lut, &xcolor );
1383  x_color[vpColor::id_white] = xcolor.pixel;
1384 
1385  // Color LIGHT GRAY.
1386  pcolor = vpColor::lightGray;
1387  xcolor.pad = 0;
1388  xcolor.red = 256 * pcolor.R;
1389  xcolor.green = 256 * pcolor.G;
1390  xcolor.blue = 256 * pcolor.B;
1391  XAllocColor ( display, lut, &xcolor );
1392  x_color[vpColor::id_lightGray] = xcolor.pixel;
1393 
1394  // Color GRAY.
1395  pcolor = vpColor::gray;
1396  xcolor.pad = 0;
1397  xcolor.red = 256 * pcolor.R;
1398  xcolor.green = 256 * pcolor.G;
1399  xcolor.blue = 256 * pcolor.B;
1400  XAllocColor ( display, lut, &xcolor );
1401  x_color[vpColor::id_gray] = xcolor.pixel;
1402 
1403  // Color DARK GRAY.
1404  pcolor = vpColor::darkGray;
1405  xcolor.pad = 0;
1406  xcolor.red = 256 * pcolor.R;
1407  xcolor.green = 256 * pcolor.G;
1408  xcolor.blue = 256 * pcolor.B;
1409  XAllocColor ( display, lut, &xcolor );
1410  x_color[vpColor::id_darkGray] = xcolor.pixel;
1411 
1412  // Color LIGHT RED.
1413  pcolor = vpColor::lightRed;
1414  xcolor.pad = 0;
1415  xcolor.red = 256 * pcolor.R;
1416  xcolor.green = 256 * pcolor.G;
1417  xcolor.blue = 256 * pcolor.B;
1418  XAllocColor ( display, lut, &xcolor );
1419  x_color[vpColor::id_lightRed] = xcolor.pixel;
1420 
1421  // Color RED.
1422  pcolor = vpColor::red;
1423  xcolor.pad = 0;
1424  xcolor.red = 256 * pcolor.R;
1425  xcolor.green = 256 * pcolor.G;
1426  xcolor.blue = 256 * pcolor.B;
1427  XAllocColor ( display, lut, &xcolor );
1428  x_color[vpColor::id_red] = xcolor.pixel;
1429 
1430  // Color DARK RED.
1431  pcolor = vpColor::darkRed;
1432  xcolor.pad = 0;
1433  xcolor.red = 256 * pcolor.R;
1434  xcolor.green = 256 * pcolor.G;
1435  xcolor.blue = 256 * pcolor.B;
1436  XAllocColor ( display, lut, &xcolor );
1437  x_color[vpColor::id_darkRed] = xcolor.pixel;
1438 
1439  // Color LIGHT GREEN.
1440  pcolor = vpColor::lightGreen;
1441  xcolor.pad = 0;
1442  xcolor.red = 256 * pcolor.R;
1443  xcolor.green = 256 * pcolor.G;
1444  xcolor.blue = 256 * pcolor.B;
1445  XAllocColor ( display, lut, &xcolor );
1446  x_color[vpColor::id_lightGreen] = xcolor.pixel;
1447 
1448  // Color GREEN.
1449  pcolor = vpColor::green;
1450  xcolor.pad = 0;
1451  xcolor.red = 256 * pcolor.R;
1452  xcolor.green = 256 * pcolor.G;
1453  xcolor.blue = 256 * pcolor.B;
1454  XAllocColor ( display, lut, &xcolor );
1455  x_color[vpColor::id_green] = xcolor.pixel;
1456 
1457  // Color DARK GREEN.
1458  pcolor = vpColor::darkGreen;
1459  xcolor.pad = 0;
1460  xcolor.red = 256 * pcolor.R;
1461  xcolor.green = 256 * pcolor.G;
1462  xcolor.blue = 256 * pcolor.B;
1463  XAllocColor ( display, lut, &xcolor );
1464  x_color[vpColor::id_darkGreen] = xcolor.pixel;
1465 
1466  // Color LIGHT BLUE.
1467  pcolor = vpColor::lightBlue;
1468  xcolor.pad = 0;
1469  xcolor.red = 256 * pcolor.R;
1470  xcolor.green = 256 * pcolor.G;
1471  xcolor.blue = 256 * pcolor.B;
1472  XAllocColor ( display, lut, &xcolor );
1473  x_color[vpColor::id_lightBlue] = xcolor.pixel;
1474 
1475  // Color BLUE.
1476  pcolor = vpColor::blue;
1477  xcolor.pad = 0;
1478  xcolor.red = 256 * pcolor.R;
1479  xcolor.green = 256 * pcolor.G;
1480  xcolor.blue = 256 * pcolor.B;
1481  XAllocColor ( display, lut, &xcolor );
1482  x_color[vpColor::id_blue] = xcolor.pixel;
1483 
1484  // Color DARK BLUE.
1485  pcolor = vpColor::darkBlue;
1486  xcolor.pad = 0;
1487  xcolor.red = 256 * pcolor.R;
1488  xcolor.green = 256 * pcolor.G;
1489  xcolor.blue = 256 * pcolor.B;
1490  XAllocColor ( display, lut, &xcolor );
1491  x_color[vpColor::id_darkBlue] = xcolor.pixel;
1492 
1493  // Color YELLOW.
1494  pcolor = vpColor::yellow;
1495  xcolor.pad = 0;
1496  xcolor.red = 256 * pcolor.R;
1497  xcolor.green = 256 * pcolor.G;
1498  xcolor.blue = 256 * pcolor.B;
1499  XAllocColor ( display, lut, &xcolor );
1500  x_color[vpColor::id_yellow] = xcolor.pixel;
1501 
1502  // Color ORANGE.
1503  pcolor = vpColor::orange;
1504  xcolor.pad = 0;
1505  xcolor.red = 256 * pcolor.R;
1506  xcolor.green = 256 * pcolor.G;
1507  xcolor.blue = 256 * pcolor.B;
1508  XAllocColor ( display, lut, &xcolor );
1509  x_color[vpColor::id_orange] = xcolor.pixel;
1510 
1511  // Color CYAN.
1512  pcolor = vpColor::cyan;
1513  xcolor.pad = 0;
1514  xcolor.red = 256 * pcolor.R;
1515  xcolor.green = 256 * pcolor.G;
1516  xcolor.blue = 256 * pcolor.B;
1517  XAllocColor ( display, lut, &xcolor );
1518  x_color[vpColor::id_cyan] = xcolor.pixel;
1519 
1520  // Color PURPLE.
1521  pcolor = vpColor::purple;
1522  xcolor.pad = 0;
1523  xcolor.red = 256 * pcolor.R;
1524  xcolor.green = 256 * pcolor.G;
1525  xcolor.blue = 256 * pcolor.B;
1526  XAllocColor ( display, lut, &xcolor );
1527  x_color[vpColor::id_purple] = xcolor.pixel;
1528  break;
1529  }
1530  }
1531 
1532  XSetStandardProperties ( display, window, this->title_.c_str(), this->title_.c_str(), None, 0, 0, &hints );
1533  XMapWindow ( display, window ) ;
1534  // Selection des evenements.
1535  XSelectInput ( display, window,
1536  ExposureMask |
1537  ButtonPressMask | ButtonReleaseMask |
1538  KeyPressMask | KeyReleaseMask |
1539  StructureNotifyMask |
1540  PointerMotionMask);
1541 
1542  /* Creation du contexte graphique */
1543  values.plane_mask = AllPlanes;
1544  values.fill_style = FillSolid;
1545  values.foreground = WhitePixel ( display, screen );
1546  values.background = BlackPixel ( display, screen );
1547  context = XCreateGC ( display, window,
1548  GCPlaneMask | GCFillStyle | GCForeground | GCBackground,
1549  &values );
1550 
1551  if ( context == NULL )
1552  {
1553  vpERROR_TRACE ( "Can't create graphics context." );
1555  "Can't create graphics context" ) ) ;
1556  }
1557 
1558  // Pixmap creation.
1559  pixmap = XCreatePixmap ( display, window, width, height, screen_depth );
1560 
1561  // Hangs when forward X11 is used to send the display to an other computer
1562 // do
1563 // XNextEvent ( display, &event );
1564 // while ( event.xany.type != Expose );
1565 
1566  {
1567  Ximage = XCreateImage ( display, DefaultVisual ( display, screen ),
1568  screen_depth, ZPixmap, 0, NULL,
1569  width, height, XBitmapPad ( display ), 0 );
1570 
1571  Ximage->data = ( char * ) malloc ( height * (unsigned int)Ximage->bytes_per_line );
1572  ximage_data_init = true;
1573  }
1574  displayHasBeenInitialized = true ;
1575 
1576  XSync ( display, true );
1577 
1578  XStoreName ( display, window, title_.c_str() );
1579 }
1580 
1595 void vpDisplayX::setFont( const char* font )
1596 {
1598  {
1599  if (font!=NULL)
1600  {
1601  try
1602  {
1603  Font stringfont;
1604  stringfont = XLoadFont (display, font) ; //"-adobe-times-bold-r-normal--18*");
1605  XSetFont (display, context, stringfont);
1606  }
1607  catch(...)
1608  {
1609  vpERROR_TRACE ( "Bad font " ) ;
1611  }
1612  }
1613  }
1614  else
1615  {
1616  vpERROR_TRACE ( "X not initialized " ) ;
1618  "X not initialized" ) ) ;
1619  }
1620 }
1621 
1626 void
1627 vpDisplayX::setTitle ( const char *title )
1628 {
1630  {
1631  if(title != NULL)
1632  title_ = std::string(title);
1633  else
1634  title_ = std::string(" ");
1635  XStoreName ( display, window, title_.c_str() );
1636  }
1637  else
1638  {
1639  vpERROR_TRACE ( "X not initialized " ) ;
1641  "X not initialized" ) ) ;
1642  }
1643 }
1644 
1653 void vpDisplayX::setWindowPosition(int winx, int winy)
1654 {
1655  if ( displayHasBeenInitialized ) {
1656  XMoveWindow(display, window, winx, winy);
1657  }
1658  else
1659  {
1660  vpERROR_TRACE ( "X not initialized " ) ;
1662  "X not initialized" ) ) ;
1663  }
1664 }
1665 
1678 {
1679 
1681  {
1682  switch ( screen_depth )
1683  {
1684  case 8:
1685  {
1686  unsigned char *src_8 = NULL;
1687  unsigned char *dst_8 = NULL;
1688  src_8 = ( unsigned char * ) I.bitmap;
1689  dst_8 = ( unsigned char * ) Ximage->data;
1690  // Correction de l'image de facon a liberer les niveaux de gris
1691  // ROUGE, VERT, BLEU, JAUNE
1692  {
1693  unsigned int i = 0;
1694  unsigned int size_ = width * height;
1695  unsigned char nivGris;
1696  unsigned char nivGrisMax = 255 - vpColor::id_unknown;
1697 
1698  while ( i < size_ )
1699  {
1700  nivGris = src_8[i] ;
1701  if ( nivGris > nivGrisMax )
1702  dst_8[i] = 255;
1703  else
1704  dst_8[i] = nivGris;
1705  i++ ;
1706  }
1707  }
1708 
1709  // Affichage de l'image dans la Pixmap.
1710  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, width, height );
1711  XSetWindowBackgroundPixmap ( display, window, pixmap );
1712 // XClearWindow ( display, window );
1713 // XSync ( display,1 );
1714  break;
1715  }
1716  case 16:
1717  {
1718  unsigned short *dst_16 = ( unsigned short* ) Ximage->data;
1719  unsigned char *dst_8 = NULL;
1720  unsigned int bytes_per_line = (unsigned int)Ximage->bytes_per_line;
1721  for ( unsigned int i = 0; i < height ; i++ ) {
1722  dst_8 = (unsigned char*) Ximage->data + i * bytes_per_line;
1723  dst_16 = (unsigned short *) dst_8;
1724  for ( unsigned int j=0 ; j < width; j++ )
1725  {
1726  * ( dst_16 + j ) = ( unsigned short ) colortable[I[i][j]] ;
1727  }
1728  }
1729 
1730  // Affichage de l'image dans la Pixmap.
1731  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, width, height );
1732  XSetWindowBackgroundPixmap ( display, window, pixmap );
1733 // XClearWindow ( display, window );
1734 // XSync ( display,1 );
1735  break;
1736  }
1737 
1738  case 24:
1739  default:
1740  {
1741  unsigned char *dst_32 = NULL;
1742  unsigned int size_ = width * height ;
1743  dst_32 = ( unsigned char* ) Ximage->data;
1744  unsigned char *bitmap = I.bitmap ;
1745  unsigned char *n = I.bitmap + size_;
1746  //for (unsigned int i = 0; i < size; i++) // suppression de l'iterateur i
1747  while ( bitmap < n )
1748  {
1749  unsigned char val = * ( bitmap++ );
1750  * ( dst_32 ++ ) = val; // Composante Rouge.
1751  * ( dst_32 ++ ) = val; // Composante Verte.
1752  * ( dst_32 ++ ) = val; // Composante Bleue.
1753  * ( dst_32 ++ ) = val;
1754  }
1755 
1756  // Affichage de l'image dans la Pixmap.
1757  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, width, height );
1758  XSetWindowBackgroundPixmap ( display, window, pixmap );
1759 // XClearWindow ( display, window );
1760 // XSync ( display,1 );
1761  break;
1762  }
1763  }
1764  }
1765  else
1766  {
1767  vpERROR_TRACE ( "X not initialized " ) ;
1769  "X not initialized" ) ) ;
1770  }
1771 }
1784 {
1786  {
1787  switch ( screen_depth )
1788  {
1789  case 16: {
1790  unsigned short *dst_16 = NULL;
1791  unsigned char *dst_8 = NULL;
1792  vpRGBa* bitmap = I.bitmap;
1793  unsigned int r, g, b;
1794  unsigned int bytes_per_line = (unsigned int)Ximage->bytes_per_line;
1795 
1796  for ( unsigned int i = 0; i < height ; i++ ) {
1797  dst_8 = (unsigned char*) Ximage->data + i * bytes_per_line;
1798  dst_16 = (unsigned short *) dst_8;
1799  for ( unsigned int j=0 ; j < width; j++ )
1800  {
1801  r = bitmap->R;
1802  g = bitmap->G;
1803  b = bitmap->B;
1804  * ( dst_16 + j ) = (((r << 8) >> RShift) & RMask) |
1805  (((g << 8) >> GShift) & GMask) |
1806  (((b << 8) >> BShift) & BMask);
1807  bitmap++;
1808  }
1809  }
1810 
1811  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, width, height );
1812  XSetWindowBackgroundPixmap ( display, window, pixmap );
1813 
1814  break;
1815  }
1816  case 24:
1817  case 32:
1818  {
1819  /*
1820  * 32-bit source, 24/32-bit destination
1821  */
1822  unsigned char *dst_32 = NULL;
1823  dst_32 = ( unsigned char* ) Ximage->data;
1824  vpRGBa* bitmap = I.bitmap;
1825  unsigned int sizeI = I.getWidth() * I.getHeight();
1826  if (XImageByteOrder(display) == 1) {
1827  // big endian
1828  for ( unsigned int i = 0; i < sizeI ; i++ ) {
1829  *(dst_32++) = bitmap->A;
1830  *(dst_32++) = bitmap->R;
1831  *(dst_32++) = bitmap->G;
1832  *(dst_32++) = bitmap->B;
1833  bitmap++;
1834  }
1835  }
1836  else {
1837  // little endian
1838  for ( unsigned int i = 0; i < sizeI; i++ ) {
1839  *(dst_32++) = bitmap->B;
1840  *(dst_32++) = bitmap->G;
1841  *(dst_32++) = bitmap->R;
1842  *(dst_32++) = bitmap->A;
1843  bitmap++;
1844  }
1845  }
1846  // Affichage de l'image dans la Pixmap.
1847  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, width, height );
1848  XSetWindowBackgroundPixmap ( display, window, pixmap );
1849  // XClearWindow ( display, window );
1850  // XSync ( display,1 );
1851  break;
1852 
1853  }
1854  default:
1855  vpERROR_TRACE ( "Unsupported depth (%d bpp) for color display",
1856  screen_depth ) ;
1858  "Unsupported depth for color display" ) ) ;
1859  }
1860  }
1861  else
1862  {
1863  vpERROR_TRACE ( "X not initialized " ) ;
1865  "X not initialized" ) ) ;
1866  }
1867 }
1868 
1880 void vpDisplayX::displayImage ( const unsigned char *I )
1881 {
1882  unsigned char *dst_32 = NULL;
1883 
1885  {
1886 
1887  dst_32 = ( unsigned char* ) Ximage->data;
1888 
1889  for ( unsigned int i = 0; i < width * height; i++ )
1890  {
1891  * ( dst_32 ++ ) = *I; // red component.
1892  * ( dst_32 ++ ) = *I; // green component.
1893  * ( dst_32 ++ ) = *I; // blue component.
1894  * ( dst_32 ++ ) = *I; // luminance component.
1895  I++;
1896  }
1897 
1898  // Affichage de l'image dans la Pixmap.
1899  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, width, height );
1900  XSetWindowBackgroundPixmap ( display, window, pixmap );
1901 // XClearWindow ( display, window );
1902 // XSync ( display,1 );
1903  }
1904  else
1905  {
1906  vpERROR_TRACE ( "X not initialized " ) ;
1908  "X not initialized" ) ) ;
1909  }
1910 }
1911 
1912 
1929  const unsigned int w, const unsigned int h )
1930 {
1932  {
1933  switch ( screen_depth )
1934  {
1935  case 8:
1936  {
1937  unsigned char *src_8 = NULL;
1938  unsigned char *dst_8 = NULL;
1939  src_8 = ( unsigned char * ) I.bitmap;
1940  dst_8 = ( unsigned char * ) Ximage->data;
1941  // Correction de l'image de facon a liberer les niveaux de gris
1942  // ROUGE, VERT, BLEU, JAUNE
1943  {
1944  //int size = width * height;
1945  unsigned char nivGris;
1946  unsigned char nivGrisMax = 255 - vpColor::id_unknown;
1947 
1948  //unsigned int iwidth = I.getWidth();
1949  unsigned int iwidth = I.getWidth();
1950 
1951  src_8 = src_8 + (int)(iP.get_i()*iwidth+ iP.get_j());
1952  dst_8 = dst_8 + (int)(iP.get_i()*this->width+ iP.get_j());
1953 
1954  unsigned int i = 0;
1955  while (i < h)
1956  {
1957  unsigned int j = 0;
1958  while (j < w)
1959  {
1960  nivGris = *(src_8+j);
1961  if ( nivGris > nivGrisMax )
1962  *(dst_8+j) = 255;
1963  else
1964  *(dst_8+j) = nivGris;
1965  j++;
1966  }
1967  src_8 = src_8 + iwidth;
1968  dst_8 = dst_8 + this->width;
1969  i++;
1970  }
1971  }
1972 
1973  // Affichage de l'image dans la Pixmap.
1974  XPutImage ( display, pixmap, context, Ximage, (int)iP.get_u(), (int)iP.get_v(), (int)iP.get_u(), (int)iP.get_v(), w, h );
1975  XSetWindowBackgroundPixmap ( display, window, pixmap );
1976  // XClearWindow ( display, window );
1977  // XSync ( display,1 );
1978  break;
1979  }
1980  case 16:
1981  {
1982  unsigned short *dst_16 = NULL;
1983  unsigned char *dst_8 = NULL;
1984  unsigned int bytes_per_line = (unsigned int)Ximage->bytes_per_line;
1985  for ( unsigned int i = (unsigned int)iP.get_i(); i < (unsigned int)(iP.get_i()+h) ; i++ ) {
1986  dst_8 = (unsigned char *) Ximage->data + i * bytes_per_line;
1987  dst_16 = (unsigned short *) dst_8;
1988  for ( unsigned int j=(unsigned int)iP.get_j() ; j < (unsigned int)(iP.get_j()+w); j++ )
1989  {
1990  * ( dst_16 + j ) = ( unsigned short ) colortable[I[i][j]] ;
1991  }
1992  }
1993 
1994 // unsigned char *src_8 = (unsigned char *) I.bitmap;
1995 // unsigned char *dst_8 = (unsigned char *) Ximage->data;
1996 // unsigned short *dst_16 = NULL;
1997 
1998 // unsigned int iwidth = I.getWidth();
1999 
2000 // src_8 += (int)(iP.get_i()*iwidth + iP.get_j());
2001 // dst_8 += (int)(iP.get_i()*Ximage->bytes_per_line + iP.get_j()*Ximage->bits_per_pixel/8);
2002 // dst_16 = (unsigned short *) dst_8;
2003 
2004 // unsigned int i = 0;
2005 // while (i < h) {
2006 // unsigned int j = 0;
2007 
2008 // while (j < w) {
2009 // *(dst_16 + j) = ( unsigned short ) colortable[*(src_8+j)];
2010 // j++;
2011 // }
2012 // src_8 = src_8 + iwidth;
2013 // dst_16 = dst_16 + Ximage->bytes_per_line;
2014 // i++;
2015 // }
2016 
2017  // Affichage de l'image dans la Pixmap.
2018  XPutImage ( display, pixmap, context, Ximage, (int)iP.get_u(), (int)iP.get_v(), (int)iP.get_u(), (int)iP.get_v(), w, h );
2019  XSetWindowBackgroundPixmap ( display, window, pixmap );
2020  // XClearWindow ( display, window );
2021  // XSync ( display,1 );
2022  break;
2023  }
2024 
2025  case 24:
2026  default:
2027  {
2028  unsigned char *dst_32 = NULL;
2029  //unsigned int size = width * height ;
2030  dst_32 = ( unsigned char* ) Ximage->data;
2031  unsigned char *src_8 = I.bitmap ;
2032  //unsigned char *n = I.bitmap + size;
2033 
2034  unsigned int iwidth = I.getWidth();
2035 
2036  src_8 = src_8 + (int)(iP.get_i()*iwidth+ iP.get_j());
2037  dst_32 = dst_32 + (int)(iP.get_i()*4*this->width+ iP.get_j()*4);
2038 
2039  unsigned int i = 0;
2040  while (i < h)
2041  {
2042  unsigned int j = 0;
2043  while (j < w)
2044  {
2045  unsigned char val = *(src_8+j);
2046  *(dst_32+4*j) = val;
2047  *(dst_32+4*j+1) = val;
2048  *(dst_32+4*j+2) = val;
2049  *(dst_32+4*j+3) = val;
2050  j++;
2051  }
2052  src_8 = src_8 + iwidth;
2053  dst_32 = dst_32 + 4*this->width;
2054  i++;
2055  }
2056 
2057  // Affichage de l'image dans la Pixmap.
2058  XPutImage ( display, pixmap, context, Ximage, (int)iP.get_u(), (int)iP.get_v(), (int)iP.get_u(), (int)iP.get_v(), w, h );
2059  XSetWindowBackgroundPixmap ( display, window, pixmap );
2060  // XClearWindow ( display, window );
2061  // XSync ( display,1 );
2062  break;
2063  }
2064  }
2065  }
2066  else
2067  {
2068  vpERROR_TRACE ( "X not initialized " ) ;
2070  "X not initialized" ) ) ;
2071  }
2072 }
2073 
2074 
2091  const unsigned int w, const unsigned int h )
2092 {
2094  {
2095  switch ( screen_depth )
2096  {
2097  case 16: {
2098  unsigned short *dst_16 = NULL;
2099  unsigned char *dst_8 = NULL;
2100  unsigned int r, g, b;
2101  unsigned int bytes_per_line = (unsigned int)Ximage->bytes_per_line;
2102  for ( unsigned int i = (unsigned int)iP.get_i(); i < (unsigned int)(iP.get_i()+h) ; i++ ) {
2103  dst_8 = (unsigned char *) Ximage->data + i * bytes_per_line;
2104  dst_16 = (unsigned short *) dst_8;
2105  for ( unsigned int j=(unsigned int)iP.get_j() ; j < (unsigned int)(iP.get_j()+w); j++ )
2106  {
2107  r = I[i][j].R;
2108  g = I[i][j].G;
2109  b = I[i][j].B;
2110  * ( dst_16 + j ) = (((r << 8) >> RShift) & RMask) |
2111  (((g << 8) >> GShift) & GMask) |
2112  (((b << 8) >> BShift) & BMask);
2113  }
2114  }
2115 
2116  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, width, height );
2117  XSetWindowBackgroundPixmap ( display, window, pixmap );
2118 
2119  break;
2120  }
2121  case 24:
2122  case 32:
2123  {
2124  /*
2125  * 32-bit source, 24/32-bit destination
2126  */
2127 
2128  unsigned char *dst_32 = NULL;
2129  dst_32 = ( unsigned char* ) Ximage->data;
2130  vpRGBa* src_32 = I.bitmap;
2131  //unsigned int sizeI = I.getWidth() * I.getHeight();
2132 
2133  unsigned int iwidth = I.getWidth();
2134 
2135  src_32 = src_32 + (int)(iP.get_i()*iwidth+ iP.get_j());
2136  dst_32 = dst_32 + (int)(iP.get_i()*4*this->width+ iP.get_j()*4);
2137 
2138  unsigned int i = 0;
2139 
2140  if (XImageByteOrder(display) == 1) {
2141  // big endian
2142  while (i < h) {
2143  unsigned int j = 0;
2144  while (j < w) {
2145  *(dst_32+4*j) = (src_32+j)->A;
2146  *(dst_32+4*j+1) = (src_32+j)->R;
2147  *(dst_32+4*j+2) = (src_32+j)->G;
2148  *(dst_32+4*j+3) = (src_32+j)->B;
2149 
2150  j++;
2151  }
2152  src_32 = src_32 + iwidth;
2153  dst_32 = dst_32 + 4*this->width;
2154  i++;
2155  }
2156 
2157  }
2158  else {
2159  // little endian
2160  while (i < h) {
2161  unsigned int j = 0;
2162  while (j < w) {
2163  *(dst_32+4*j) = (src_32+j)->B;
2164  *(dst_32+4*j+1) = (src_32+j)->G;
2165  *(dst_32+4*j+2) = (src_32+j)->R;
2166  *(dst_32+4*j+3) = (src_32+j)->A;
2167 
2168  j++;
2169  }
2170  src_32 = src_32 + iwidth;
2171  dst_32 = dst_32 + 4*this->width;
2172  i++;
2173  }
2174  }
2175 
2176  // Affichage de l'image dans la Pixmap.
2177  XPutImage ( display, pixmap, context, Ximage, (int)iP.get_u(), (int)iP.get_v(), (int)iP.get_u(), (int)iP.get_v(), w, h );
2178  XSetWindowBackgroundPixmap ( display, window, pixmap );
2179  // XClearWindow ( display, window );
2180  // XSync ( display,1 );
2181  break;
2182 
2183  }
2184  default:
2185  vpERROR_TRACE ( "Unsupported depth (%d bpp) for color display",
2186  screen_depth ) ;
2188  "Unsupported depth for color display" ) ) ;
2189  }
2190  }
2191  else
2192  {
2193  vpERROR_TRACE ( "X not initialized " ) ;
2195  "X not initialized" ) ) ;
2196  }
2197 }
2198 
2207 {
2209  {
2210  if ( ximage_data_init == true )
2211  free ( Ximage->data );
2212 
2213  Ximage->data = NULL;
2214  XDestroyImage ( Ximage );
2215 
2216  XFreePixmap ( display, pixmap );
2217 
2218  XFreeGC ( display, context );
2219  XDestroyWindow ( display, window );
2220  XCloseDisplay ( display );
2221 
2222  displayHasBeenInitialized = false;
2223 
2224  if (x_color != NULL) {
2225  delete [] x_color;
2226  x_color = NULL;
2227  }
2228  }
2229 }
2230 
2231 
2238 {
2240  {
2241  XClearWindow ( display, window );
2242  //XClearArea ( display, window,0,0,100,100,0 );
2243  XFlush ( display );
2244  }
2245  else
2246  {
2247  vpERROR_TRACE ( "X not initialized " ) ;
2249  "X not initialized" ) ) ;
2250  }
2251 }
2252 
2260 void vpDisplayX::flushDisplayROI(const vpImagePoint &iP, const unsigned int w, const unsigned int h)
2261 {
2263  {
2264  //XClearWindow ( display, window );
2265  XClearArea ( display, window,(int)iP.get_u(),(int)iP.get_v(),w,h,0 );
2266  XFlush ( display );
2267  }
2268  else
2269  {
2270  vpERROR_TRACE ( "X not initialized " ) ;
2272  "X not initialized" ) ) ;
2273  }
2274 }
2275 
2276 
2281 void vpDisplayX::clearDisplay ( const vpColor &color )
2282 {
2284  {
2285 
2286  if (color.id < vpColor::id_unknown)
2287  XSetWindowBackground ( display, window, x_color[color.id] );
2288  else {
2289  xcolor.pad = 0;
2290  xcolor.red = 256 * color.R;
2291  xcolor.green = 256 * color.G;
2292  xcolor.blue = 256 * color.B;
2293  XAllocColor ( display, lut, &xcolor );
2294  XSetForeground ( display, context, xcolor.pixel );
2295  }
2296 
2297  XClearWindow ( display, window );
2298 
2299  XFreePixmap ( display, pixmap );
2300  // Pixmap creation.
2301  pixmap = XCreatePixmap ( display, window, width, height, screen_depth );
2302  }
2303  else
2304  {
2305  vpERROR_TRACE ( "X not initialized " ) ;
2307  "X not initialized" ) ) ;
2308  }
2309 }
2310 
2319  const vpImagePoint &ip2,
2320  const vpColor &color,
2321  unsigned int w, unsigned int h,
2322  unsigned int thickness)
2323 {
2325  {
2326  try
2327  {
2328  double a = ip2.get_i() - ip1.get_i() ;
2329  double b = ip2.get_j() - ip1.get_j() ;
2330  double lg = sqrt ( vpMath::sqr ( a ) + vpMath::sqr ( b ) ) ;
2331 
2332  //if ( ( a==0 ) && ( b==0 ) )
2333  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon() )&&(std::fabs(b) <= std::numeric_limits<double>::epsilon()) )
2334  {
2335  // DisplayCrossLarge(i1,j1,3,col) ;
2336  }
2337  else
2338  {
2339  a /= lg ;
2340  b /= lg ;
2341 
2342  vpImagePoint ip3;
2343  ip3.set_i(ip2.get_i() - w*a);
2344  ip3.set_j(ip2.get_j() - w*b);
2345 
2346  vpImagePoint ip4;
2347  ip4.set_i( ip3.get_i() - b*h );
2348  ip4.set_j( ip3.get_j() + a*h );
2349 
2350  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
2351  displayLine ( ip2, ip4, color, thickness ) ;
2352 
2353  ip4.set_i( ip3.get_i() + b*h );
2354  ip4.set_j( ip3.get_j() - a*h );
2355 
2356  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
2357  displayLine ( ip2, ip4, color, thickness ) ;
2358 
2359  displayLine ( ip1, ip2, color, thickness ) ;
2360  }
2361  }
2362  catch ( ... )
2363  {
2364  vpERROR_TRACE ( "Error caught" ) ;
2365  throw ;
2366  }
2367  }
2368  else
2369  {
2370  vpERROR_TRACE ( "X not initialized " ) ;
2372  "X not initialized" ) ) ;
2373  }
2374 }
2375 
2388  const char *text,
2389  const vpColor &color )
2390 {
2392  {
2393  if (color.id < vpColor::id_unknown)
2394  XSetForeground ( display, context, x_color[color.id] );
2395  else {
2396  xcolor.pad = 0;
2397  xcolor.red = 256 * color.R;
2398  xcolor.green = 256 * color.G;
2399  xcolor.blue = 256 * color.B;
2400  XAllocColor ( display, lut, &xcolor );
2401  XSetForeground ( display, context, xcolor.pixel );
2402  }
2403  XDrawString ( display, pixmap, context,
2404  (int)ip.get_u(), (int)ip.get_v(),
2405  text, (int)strlen ( text ) );
2406  }
2407  else
2408  {
2409  vpERROR_TRACE ( "X not initialized " ) ;
2411  "X not initialized" ) ) ;
2412  }
2413 }
2414 
2425  unsigned int radius,
2426  const vpColor &color,
2427  bool fill,
2428  unsigned int thickness )
2429 {
2431  {
2432  if ( thickness == 1 ) thickness = 0;
2433  if (color.id < vpColor::id_unknown)
2434  XSetForeground ( display, context, x_color[color.id] );
2435  else {
2436  xcolor.pad = 0;
2437  xcolor.red = 256 * color.R;
2438  xcolor.green = 256 * color.G;
2439  xcolor.blue = 256 * color.B;
2440  XAllocColor ( display, lut, &xcolor );
2441  XSetForeground ( display, context, xcolor.pixel );
2442  }
2443 
2444  XSetLineAttributes ( display, context, thickness,
2445  LineSolid, CapButt, JoinBevel );
2446 
2447  if ( fill == false )
2448  {
2449  XDrawArc ( display, pixmap, context,
2450  vpMath::round( center.get_u()-radius ),
2451  vpMath::round( center.get_v()-radius ),
2452  radius*2, radius*2, 0, 23040 ); /* 23040 = 360*64 */
2453  }
2454  else
2455  {
2456  XFillArc ( display, pixmap, context,
2457  vpMath::round( center.get_u()-radius ),
2458  vpMath::round( center.get_v()-radius ),
2459  radius*2, radius*2, 0, 23040 ); /* 23040 = 360*64 */
2460  }
2461  }
2462  else
2463  {
2464  vpERROR_TRACE ( "X not initialized " ) ;
2466  "X not initialized" ) ) ;
2467  }
2468 }
2469 
2478  unsigned int cross_size,
2479  const vpColor &color,
2480  unsigned int thickness)
2481 {
2483  {
2484  try
2485  {
2486  double i = ip.get_i();
2487  double j = ip.get_j();
2488  vpImagePoint ip1, ip2;
2489 
2490  ip1.set_i( i-cross_size/2 );
2491  ip1.set_j( j );
2492  ip2.set_i( i+cross_size/2 );
2493  ip2.set_j( j );
2494  displayLine ( ip1, ip2, color, thickness ) ;
2495 
2496  ip1.set_i( i );
2497  ip1.set_j( j-cross_size/2 );
2498  ip2.set_i( i );
2499  ip2.set_j( j+cross_size/2 );
2500 
2501  displayLine ( ip1, ip2, color, thickness ) ;
2502  }
2503  catch ( ... )
2504  {
2505  vpERROR_TRACE ( "Error caught" ) ;
2506  throw ;
2507  }
2508  }
2509 
2510  else
2511  {
2512  vpERROR_TRACE ( "X not initialized " ) ;
2514  "X not initialized" ) ) ;
2515  }
2516 
2517 }
2525  const vpImagePoint &ip2,
2526  const vpColor &color,
2527  unsigned int thickness )
2528 {
2529 
2531  {
2532  if ( thickness == 1 ) thickness = 0;
2533 
2534  if (color.id < vpColor::id_unknown)
2535  XSetForeground ( display, context, x_color[color.id] );
2536  else {
2537  xcolor.pad = 0;
2538  xcolor.red = 256 * color.R;
2539  xcolor.green = 256 * color.G;
2540  xcolor.blue = 256 * color.B;
2541  XAllocColor ( display, lut, &xcolor );
2542  XSetForeground ( display, context, xcolor.pixel );
2543  }
2544 
2545  XSetLineAttributes ( display, context, thickness,
2546  LineOnOffDash, CapButt, JoinBevel );
2547 
2548  XDrawLine ( display, pixmap, context,
2549  vpMath::round( ip1.get_u() ),
2550  vpMath::round( ip1.get_v() ),
2551  vpMath::round( ip2.get_u() ),
2552  vpMath::round( ip2.get_v() ) );
2553  }
2554  else
2555  {
2556  vpERROR_TRACE ( "X not initialized " ) ;
2558  "X not initialized" ) ) ;
2559  }
2560 }
2561 
2569  const vpImagePoint &ip2,
2570  const vpColor &color,
2571  unsigned int thickness )
2572 {
2574  {
2575  if ( thickness == 1 ) thickness = 0;
2576 
2577  if (color.id < vpColor::id_unknown)
2578  XSetForeground ( display, context, x_color[color.id] );
2579  else {
2580  xcolor.pad = 0;
2581  xcolor.red = 256 * color.R;
2582  xcolor.green = 256 * color.G;
2583  xcolor.blue = 256 * color.B;
2584  XAllocColor ( display, lut, &xcolor );
2585  XSetForeground ( display, context, xcolor.pixel );
2586  }
2587 
2588  XSetLineAttributes ( display, context, thickness,
2589  LineSolid, CapButt, JoinBevel );
2590 
2591  XDrawLine ( display, pixmap, context,
2592  vpMath::round( ip1.get_u() ),
2593  vpMath::round( ip1.get_v() ),
2594  vpMath::round( ip2.get_u() ),
2595  vpMath::round( ip2.get_v() ) );
2596  }
2597  else
2598  {
2599  vpERROR_TRACE ( "X not initialized " ) ;
2601  "X not initialized" ) ) ;
2602  }
2603 }
2604 
2611  const vpColor &color )
2612 {
2614  {
2615  if (color.id < vpColor::id_unknown)
2616  XSetForeground ( display, context, x_color[color.id] );
2617  else {
2618  xcolor.pad = 0;
2619  xcolor.red = 256 * color.R;
2620  xcolor.green = 256 * color.G;
2621  xcolor.blue = 256 * color.B;
2622  XAllocColor ( display, lut, &xcolor );
2623  XSetForeground ( display, context, xcolor.pixel );
2624  }
2625 
2626  XDrawPoint ( display, pixmap, context,
2627  vpMath::round( ip.get_u() ),
2628  vpMath::round( ip.get_v() ) );
2629  }
2630  else
2631  {
2632  vpERROR_TRACE ( "X not initialized " ) ;
2634  "X not initialized" ) ) ;
2635  }
2636 }
2637 
2651 void
2653  unsigned int w, unsigned int h,
2654  const vpColor &color, bool fill,
2655  unsigned int thickness )
2656 {
2658  {
2659  if ( thickness == 1 ) thickness = 0;
2660  if (color.id < vpColor::id_unknown)
2661  XSetForeground ( display, context, x_color[color.id] );
2662  else {
2663  xcolor.pad = 0;
2664  xcolor.red = 256 * color.R;
2665  xcolor.green = 256 * color.G;
2666  xcolor.blue = 256 * color.B;
2667  XAllocColor ( display, lut, &xcolor );
2668  XSetForeground ( display, context, xcolor.pixel );
2669  }
2670  XSetLineAttributes ( display, context, thickness,
2671  LineSolid, CapButt, JoinBevel );
2672  if ( fill == false )
2673  {
2674  XDrawRectangle ( display, pixmap, context,
2675  vpMath::round( topLeft.get_u() ),
2676  vpMath::round( topLeft.get_v() ),
2677  w-1, h-1 );
2678  }
2679  else
2680  {
2681  XFillRectangle ( display, pixmap, context,
2682  vpMath::round( topLeft.get_u() ),
2683  vpMath::round( topLeft.get_v() ),
2684  w, h );
2685  }
2686  }
2687  else
2688  {
2689  vpERROR_TRACE ( "X not initialized " ) ;
2691  "X not initialized" ) ) ;
2692  }
2693 }
2694 
2707 void
2709  const vpImagePoint &bottomRight,
2710  const vpColor &color, bool fill,
2711  unsigned int thickness )
2712 {
2714  {
2715  if ( thickness == 1 ) thickness = 0;
2716  if (color.id < vpColor::id_unknown)
2717  XSetForeground ( display, context, x_color[color.id] );
2718  else {
2719  xcolor.pad = 0;
2720  xcolor.red = 256 * color.R;
2721  xcolor.green = 256 * color.G;
2722  xcolor.blue = 256 * color.B;
2723  XAllocColor ( display, lut, &xcolor );
2724  XSetForeground ( display, context, xcolor.pixel );
2725  }
2726 
2727  XSetLineAttributes ( display, context, thickness,
2728  LineSolid, CapButt, JoinBevel );
2729 
2730  unsigned int w = (unsigned int)vpMath::round( std::fabs(bottomRight.get_u() - topLeft.get_u()) );
2731  unsigned int h = (unsigned int)vpMath::round( std::fabs(bottomRight.get_v() - topLeft.get_v()) );
2732  if ( fill == false )
2733  {
2734 
2735  XDrawRectangle ( display, pixmap, context,
2736  vpMath::round( topLeft.get_u() < bottomRight.get_u() ? topLeft.get_u() : bottomRight.get_u() ),
2737  vpMath::round( topLeft.get_v() < bottomRight.get_v() ? topLeft.get_v() : bottomRight.get_v() ),
2738  w > 0 ? w-1 : 1, h > 0 ? h : 1 );
2739  }
2740  else
2741  {
2742  XFillRectangle ( display, pixmap, context,
2743  vpMath::round( topLeft.get_u() < bottomRight.get_u() ? topLeft.get_u() : bottomRight.get_u() ),
2744  vpMath::round( topLeft.get_v() < bottomRight.get_v() ? topLeft.get_v() : bottomRight.get_v() ),
2745  w, h );
2746  }
2747  }
2748  else
2749  {
2750  vpERROR_TRACE ( "X not initialized " ) ;
2752  "X not initialized" ) ) ;
2753  }
2754 }
2755 
2768 void
2770  const vpColor &color, bool fill,
2771  unsigned int thickness )
2772 {
2774  {
2775  if ( thickness == 1 ) thickness = 0;
2776  if (color.id < vpColor::id_unknown)
2777  XSetForeground ( display, context, x_color[color.id] );
2778  else {
2779  xcolor.pad = 0;
2780  xcolor.red = 256 * color.R;
2781  xcolor.green = 256 * color.G;
2782  xcolor.blue = 256 * color.B;
2783  XAllocColor ( display, lut, &xcolor );
2784  XSetForeground ( display, context, xcolor.pixel );
2785  }
2786 
2787  XSetLineAttributes ( display, context, thickness,
2788  LineSolid, CapButt, JoinBevel );
2789 
2790  if ( fill == false )
2791  {
2792  XDrawRectangle ( display, pixmap, context,
2793  vpMath::round( rectangle.getLeft() ),
2794  vpMath::round( rectangle.getTop() ),
2795  (unsigned int)vpMath::round( rectangle.getWidth()-1 ),
2796  (unsigned int)vpMath::round( rectangle.getHeight()-1 ) );
2797  }
2798  else
2799  {
2800  XFillRectangle ( display, pixmap, context,
2801  vpMath::round( rectangle.getLeft() ),
2802  vpMath::round( rectangle.getTop() ),
2803  (unsigned int)vpMath::round( rectangle.getWidth() ),
2804  (unsigned int)vpMath::round( rectangle.getHeight() ) );
2805  }
2806 
2807  }
2808  else
2809  {
2810  vpERROR_TRACE ( "X not initialized " ) ;
2812  "X not initialized" ) ) ;
2813  }
2814 }
2815 
2832 bool
2833 vpDisplayX::getClick(bool blocking)
2834 {
2835 
2836  bool ret = false;
2837 
2838  if ( displayHasBeenInitialized ) {
2839  Window rootwin, childwin ;
2840  int root_x, root_y, win_x, win_y ;
2841  unsigned int modifier ;
2842 
2843  // Event testing
2844  if(blocking){
2845  XCheckMaskEvent(display , ButtonPressMask, &event);
2846  XCheckMaskEvent(display , ButtonReleaseMask, &event);
2847  XMaskEvent ( display, ButtonPressMask ,&event );
2848  ret = true;
2849  }
2850  else{
2851  ret = XCheckMaskEvent(display , ButtonPressMask, &event);
2852  }
2853 
2854  if(ret){
2855  /* Recuperation de la coordonnee du pixel clique. */
2856  if ( XQueryPointer ( display,
2857  window,
2858  &rootwin, &childwin,
2859  &root_x, &root_y,
2860  &win_x, &win_y,
2861  &modifier ) ) {}
2862  }
2863  }
2864  else {
2865  vpERROR_TRACE ( "X not initialized " ) ;
2867  "X not initialized" ) ) ;
2868  }
2869  return ret;
2870 }
2871 
2888 bool
2889 vpDisplayX::getClick ( vpImagePoint &ip, bool blocking )
2890 {
2891 
2892  bool ret = false;
2893  if ( displayHasBeenInitialized ) {
2894 
2895  Window rootwin, childwin ;
2896  int root_x, root_y, win_x, win_y ;
2897  unsigned int modifier ;
2898  // Event testing
2899  if(blocking){
2900  XCheckMaskEvent(display , ButtonPressMask, &event);
2901  XCheckMaskEvent(display , ButtonReleaseMask, &event);
2902  XMaskEvent ( display, ButtonPressMask ,&event );
2903  ret = true;
2904  }
2905  else{
2906  ret = XCheckMaskEvent(display , ButtonPressMask, &event);
2907  }
2908 
2909  if(ret){
2910  // Get mouse position
2911  if ( XQueryPointer ( display,
2912  window,
2913  &rootwin, &childwin,
2914  &root_x, &root_y,
2915  &win_x, &win_y,
2916  &modifier ) ) {
2917  ip.set_u( (double)event.xbutton.x );
2918  ip.set_v( (double)event.xbutton.y );
2919  }
2920  }
2921  }
2922  else {
2923  vpERROR_TRACE ( "X not initialized " ) ;
2925  "X not initialized" ) ) ;
2926  }
2927  return ret ;
2928 }
2929 
2949 bool
2952  bool blocking )
2953 {
2954 
2955  bool ret = false;
2956  if ( displayHasBeenInitialized ) {
2957 
2958  Window rootwin, childwin ;
2959  int root_x, root_y, win_x, win_y ;
2960  unsigned int modifier ;
2961 
2962  // Event testing
2963  if(blocking){
2964  XCheckMaskEvent(display , ButtonPressMask, &event);
2965  XCheckMaskEvent(display , ButtonReleaseMask, &event);
2966  XMaskEvent ( display, ButtonPressMask ,&event );
2967  ret = true;
2968  }
2969  else{
2970  ret = XCheckMaskEvent(display , ButtonPressMask, &event);
2971  }
2972 
2973  if(ret){
2974  // Get mouse position
2975  if ( XQueryPointer ( display,
2976  window,
2977  &rootwin, &childwin,
2978  &root_x, &root_y,
2979  &win_x, &win_y,
2980  &modifier ) ) {
2981  ip.set_u( (double)event.xbutton.x );
2982  ip.set_v( (double)event.xbutton.y );
2983  switch ( event.xbutton.button ) {
2984  case Button1: button = vpMouseButton::button1; break;
2985  case Button2: button = vpMouseButton::button2; break;
2986  case Button3: button = vpMouseButton::button3; break;
2987  }
2988  }
2989  }
2990  }
2991  else {
2992  vpERROR_TRACE ( "X not initialized " ) ;
2994  "X not initialized" ) ) ;
2995  }
2996  return ret ;
2997 }
2998 
3022 bool
3025  bool blocking )
3026 {
3027 
3028  bool ret = false;
3029  if ( displayHasBeenInitialized ) {
3030  Window rootwin, childwin ;
3031  int root_x, root_y, win_x, win_y ;
3032  unsigned int modifier ;
3033 
3034  // Event testing
3035  if(blocking){
3036  XCheckMaskEvent(display , ButtonPressMask, &event);
3037  XCheckMaskEvent(display , ButtonReleaseMask, &event);
3038  XMaskEvent ( display, ButtonReleaseMask ,&event );
3039  ret = true;
3040  }
3041  else{
3042  ret = XCheckMaskEvent(display , ButtonReleaseMask, &event);
3043  }
3044 
3045  if(ret){
3046  /* Recuperation de la coordonnee du pixel clique. */
3047  if ( XQueryPointer ( display,
3048  window,
3049  &rootwin, &childwin,
3050  &root_x, &root_y,
3051  &win_x, &win_y,
3052  &modifier ) ) {
3053  ip.set_u( (double)event.xbutton.x );
3054  ip.set_v( (double)event.xbutton.y );
3055  switch ( event.xbutton.button ) {
3056  case Button1: button = vpMouseButton::button1; break;
3057  case Button2: button = vpMouseButton::button2; break;
3058  case Button3: button = vpMouseButton::button3; break;
3059  }
3060  }
3061  }
3062  }
3063  else {
3064  vpERROR_TRACE ( "X not initialized " ) ;
3066  "X not initialized" ) ) ;
3067  }
3068  return ret ;
3069 }
3070 
3071 /*
3072  Gets the displayed image (including the overlay plane)
3073  and returns an RGBa image.
3074 
3075  \param I : Image to get.
3076 */
3078 {
3080  {
3081  XImage *xi ;
3082 
3083  XCopyArea (display,window, pixmap, context,
3084  0,0, getWidth(), getHeight(), 0, 0);
3085 
3086  xi= XGetImage ( display,pixmap, 0,0, getWidth(), getHeight(),
3087  AllPlanes, ZPixmap ) ;
3088 
3089  try
3090  {
3091  I.resize ( getHeight(), getWidth() ) ;
3092  }
3093  catch ( ... )
3094  {
3095  vpERROR_TRACE ( "Error caught" ) ;
3096  throw ;
3097  }
3098 
3099  unsigned char *src_32 = NULL;
3100  src_32 = ( unsigned char* ) xi->data;
3101 
3102  if (screen_depth == 16) {
3103  for ( unsigned int i = 0; i < I.getHeight() ; i++ ) {
3104  size_t i_ = i*I.getWidth();
3105  for ( unsigned int j = 0; j < I.getWidth() ; j++ ) {
3106  size_t ij_ = i_+j;
3107  unsigned long pixel = XGetPixel(xi, (int)j, (int)i);
3108  I.bitmap[ij_].R = (((pixel & RMask) << RShift) >> 8);
3109  I.bitmap[ij_].G = (((pixel & GMask) << GShift) >> 8);
3110  I.bitmap[ij_].B = (((pixel & BMask) << BShift) >> 8);
3111  I.bitmap[ij_].A = 0;
3112  }
3113  }
3114 
3115  }
3116  else {
3117  if (XImageByteOrder(display) == 1) {
3118  // big endian
3119  for ( unsigned int i = 0; i < I.getWidth() * I.getHeight() ; i++ ) {
3120  I.bitmap[i].A = src_32[i*4] ;
3121  I.bitmap[i].R = src_32[i*4 + 1] ;
3122  I.bitmap[i].G = src_32[i*4 + 2] ;
3123  I.bitmap[i].B = src_32[i*4 + 3] ;
3124  }
3125  }
3126  else {
3127  // little endian
3128  for ( unsigned int i = 0; i < I.getWidth() * I.getHeight() ; i++ ) {
3129  I.bitmap[i].B = src_32[i*4] ;
3130  I.bitmap[i].G = src_32[i*4 + 1] ;
3131  I.bitmap[i].R = src_32[i*4 + 2] ;
3132  I.bitmap[i].A = src_32[i*4 + 3] ;
3133  }
3134  }
3135  }
3136  XDestroyImage ( xi ) ;
3137  }
3138  else
3139  {
3140  vpERROR_TRACE ( "X not initialized " ) ;
3142  "X not initialized" ) ) ;
3143  }
3144 }
3145 
3150 {
3151  Display *display_;
3152  int screen_;
3153  unsigned int depth;
3154 
3155  if ( ( display_ = XOpenDisplay ( NULL ) ) == NULL )
3156  {
3157  vpERROR_TRACE ( "Can't connect display on server %s.",
3158  XDisplayName ( NULL ) );
3160  "Can't connect display on server." ) ) ;
3161  }
3162  screen_ = DefaultScreen ( display_ );
3163  depth = (unsigned int)DefaultDepth ( display_, screen_ );
3164 
3165  XCloseDisplay ( display_ );
3166 
3167  return ( depth );
3168 }
3169 
3174 void vpDisplayX::getScreenSize ( unsigned int &w, unsigned int &h )
3175 {
3176  Display *display_;
3177  int screen_;
3178 
3179  if ( ( display_ = XOpenDisplay ( NULL ) ) == NULL )
3180  {
3181  vpERROR_TRACE ( "Can't connect display on server %s.",
3182  XDisplayName ( NULL ) );
3184  "Can't connect display on server." ) ) ;
3185  }
3186  screen_ = DefaultScreen ( display_ );
3187  w = (unsigned int)DisplayWidth ( display_, screen_ );
3188  h = (unsigned int)DisplayHeight ( display_, screen_ );
3189 
3190  XCloseDisplay ( display_ );
3191 }
3212 bool
3214 {
3215 
3216  bool ret = false;
3217 
3218  if ( displayHasBeenInitialized ) {
3219  // Event testing
3220  if(blocking){
3221  XMaskEvent ( display, KeyPressMask ,&event );
3222  ret = true;
3223  }
3224  else{
3225  ret = XCheckMaskEvent(display , KeyPressMask, &event);
3226  }
3227  }
3228  else {
3229  vpERROR_TRACE ( "X not initialized " ) ;
3231  "X not initialized" ) ) ;
3232  }
3233  return ret;
3234 }
3258 bool
3259 vpDisplayX::getKeyboardEvent(char *string, bool blocking)
3260 {
3261 
3262  bool ret = false;
3263  KeySym keysym;
3264 // int count;
3265  XComposeStatus compose_status;
3266  char buffer;
3267 
3268  if ( displayHasBeenInitialized ) {
3269  // Event testing
3270  if(blocking){
3271  XMaskEvent ( display, KeyPressMask ,&event );
3272  /* count = */ XLookupString ((XKeyEvent *)&event, &buffer, 1,
3273  &keysym, &compose_status);
3274  //std::cout <<"count: " << count << " get \"" << buffer << "\"" << std::endl;
3275  sprintf(string, "%c", buffer);
3276  ret = true;
3277  }
3278  else{
3279  ret = XCheckMaskEvent(display , KeyPressMask, &event);
3280  if (ret) {
3281  /* count = */ XLookupString ((XKeyEvent *)&event, &buffer, 1,
3282  &keysym, &compose_status);
3283  sprintf(string, "%c", buffer);
3284  }
3285  }
3286  }
3287  else {
3288  vpERROR_TRACE ( "X not initialized " ) ;
3290  "X not initialized" ) ) ;
3291  }
3292  return ret;
3293 }
3306 bool
3308 {
3309 
3310  bool ret = false;
3311  if ( displayHasBeenInitialized ) {
3312 
3313  Window rootwin, childwin ;
3314  int root_x, root_y, win_x, win_y ;
3315  unsigned int modifier ;
3316  // Event testing
3317  ret = XCheckMaskEvent(display , PointerMotionMask, &event);
3318 
3319  if(ret){
3320  // Get mouse position
3321  if ( XQueryPointer ( display,
3322  window,
3323  &rootwin, &childwin,
3324  &root_x, &root_y,
3325  &win_x, &win_y,
3326  &modifier ) ) {
3327  ip.set_u( (double)event.xbutton.x );
3328  ip.set_v( (double)event.xbutton.y );
3329  }
3330  }
3331  }
3332  else {
3333  vpERROR_TRACE ( "X not initialized " ) ;
3335  "X not initialized" ) ) ;
3336  }
3337  return ret ;
3338 }
3339 
3350 bool
3352 {
3353 
3354  bool ret = false;
3355  if ( displayHasBeenInitialized ) {
3356 
3357  Window rootwin, childwin ;
3358  int root_x, root_y, win_x, win_y ;
3359  unsigned int modifier ;
3360  // Event testing
3361  ret = true;
3362 
3363  if(ret){
3364  // Get mouse position
3365  if ( XQueryPointer ( display,
3366  window,
3367  &rootwin, &childwin,
3368  &root_x, &root_y,
3369  &win_x, &win_y,
3370  &modifier ) ) {
3371  ip.set_u( (double)win_x );
3372  ip.set_v( (double)win_y );
3373  }
3374  }
3375  }
3376  else {
3377  vpERROR_TRACE ( "X not initialized " ) ;
3379  "X not initialized" ) ) ;
3380  }
3381  return ret ;
3382 }
3383 
3387 int vpDisplayX::getMsb(unsigned int u32val)
3388 {
3389  int i;
3390 
3391  for (i = 31; i >= 0; --i) {
3392  if (u32val & 0x80000000L)
3393  break;
3394  u32val <<= 1;
3395  }
3396  return i;
3397 }
3398 #elif !defined(VISP_BUILD_SHARED_LIBS)
3399 // Work arround to avoid warning: libvisp_core.a(vpDisplayX.cpp.o) has no symbols
3400 void dummy_vpDisplayX() {};
3401 #endif
3402 
void closeDisplay()
void clearDisplay(const vpColor &color=vpColor::white)
vpDisplay * display
Definition: vpImage.h:117
double getTop() const
Definition: vpRect.h:176
double get_v() const
Definition: vpImagePoint.h:259
unsigned int getScreenDepth()
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void setFont(const char *font)
void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
unsigned int width
Definition: vpDisplay.h:179
double get_i() const
Definition: vpImagePoint.h:190
unsigned int getWidth() const
Definition: vpImage.h:161
void displayImage(const vpImage< vpRGBa > &I)
unsigned char B
Blue component.
Definition: vpRGBa.h:144
Type * bitmap
points toward the bitmap
Definition: vpImage.h:116
static const vpColor black
Definition: vpColor.h:157
static const vpColor darkRed
Definition: vpColor.h:164
#define vpERROR_TRACE
Definition: vpDebug.h:391
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
double get_u() const
Definition: vpImagePoint.h:248
static const vpColor lightGray
Definition: vpColor.h:159
double getHeight() const
Definition: vpRect.h:151
void flushDisplay()
static const vpColor darkBlue
Definition: vpColor.h:170
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:174
unsigned char G
Green component.
Definition: vpRGBa.h:143
static const vpColor green
Definition: vpColor.h:166
unsigned int getHeight() const
Definition: vpDisplayX.h:281
static int round(const double x)
Definition: vpMath.h:248
double get_j() const
Definition: vpImagePoint.h:201
static const vpColor lightRed
Definition: vpColor.h:162
Class that defines a RGB 32 bits structure.
Definition: vpRGBa.h:64
static const vpColor red
Definition: vpColor.h:163
bool getPointerPosition(vpImagePoint &ip)
vpWin32Window window
The window.
static const vpColor orange
Definition: vpColor.h:173
std::string title_
Definition: vpDisplay.h:181
bool getClick(bool blocking=true)
vpColorIdentifier id
Definition: vpColor.h:152
void set_i(const double ii)
Definition: vpImagePoint.h:154
double getWidth() const
Definition: vpRect.h:195
void displayImageROI(const vpImage< unsigned char > &I, const vpImagePoint &iP, const unsigned int width, const unsigned int height)
static const vpColor cyan
Definition: vpColor.h:172
static const vpColor lightGreen
Definition: vpColor.h:165
void set_u(const double u)
Definition: vpImagePoint.h:212
#define vpTRACE
Definition: vpDebug.h:414
static double sqr(double x)
Definition: vpMath.h:110
void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
unsigned int height
Definition: vpDisplay.h:180
void getScreenSize(unsigned int &width, unsigned int &height)
void set_v(const double v)
Definition: vpImagePoint.h:223
unsigned char A
Additionnal component.
Definition: vpRGBa.h:145
int getMsb(unsigned int u32val)
bool getKeyboardEvent(bool blocking=true)
void resize(const unsigned int h, const unsigned int w)
set the size of the image without initializing it.
Definition: vpImage.h:616
static const vpColor gray
Definition: vpColor.h:160
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
void setWindowPosition(int winx, int winy)
virtual ~vpDisplayX()
Definition: vpDisplayX.cpp:176
static const vpColor darkGray
Definition: vpColor.h:161
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
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 setTitle(const char *title)
void set_j(const double jj)
Definition: vpImagePoint.h:165
Error that can be emited by the vpDisplay class and its derivates.
void displayPoint(const vpImagePoint &ip, const vpColor &color)
int windowXPosition
display position
Definition: vpDisplay.h:176
unsigned char R
Red component.
Definition: vpRGBa.h:142
unsigned int getWidth() const
Definition: vpDisplayX.h:280
#define vpDEBUG_TRACE
Definition: vpDebug.h:478
unsigned int getHeight() const
Definition: vpImage.h:152
Defines a rectangle in the plane.
Definition: vpRect.h:81
static const vpColor darkGreen
Definition: vpColor.h:167
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
Definition: vpDisplayX.cpp:190
static const vpColor yellow
Definition: vpColor.h:171
static const vpColor lightBlue
Definition: vpColor.h:168
int windowYPosition
display position
Definition: vpDisplay.h:178
static const vpColor purple
Definition: vpColor.h:174
static const vpColor white
Definition: vpColor.h:158
double getLeft() const
Definition: vpRect.h:157
void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
bool getPointerMotionEvent(vpImagePoint &ip)
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
Definition: vpImagePoint.h:270
void getImage(vpImage< vpRGBa > &I)
get the window pixmap and put it in vpRGBa image
static const vpColor blue
Definition: vpColor.h:169
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)