ViSP  2.10.0
vpDisplayX.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayX.cpp 5214 2015-01-27 18:33:01Z 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  * Fabien Spindler
39  * Anthony Saunier
40  *
41  *****************************************************************************/
42 
49 #include <visp/vpConfig.h>
50 #ifdef VISP_HAVE_X11
51 
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <iostream>
55 #include <cmath> // std::fabs
56 #include <limits> // numeric_limits
57 
58 // Display stuff
59 #include <visp/vpDisplay.h>
60 #include <visp/vpDisplayX.h>
61 
62 //debug / exception
63 #include <visp/vpDebug.h>
64 #include <visp/vpDisplayException.h>
65 
66 // math
67 #include <visp/vpMath.h>
68 
80  int x,
81  int y,
82  const char *title )
83  : display(NULL), window(), Ximage(NULL), lut(), context(),
84  screen(0), planes(0), event(), pixmap(), x_color(NULL),
85  screen_depth(8), xcolor(), values(), size(0), ximage_data_init(false),
86  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
87 {
88  init ( I, x, y, title ) ;
89 }
90 
91 
92 
102  int x,
103  int y,
104  const char *title )
105  : display(NULL), window(), Ximage(NULL), lut(), context(),
106  screen(0), planes(0), event(), pixmap(), x_color(NULL),
107  screen_depth(8), xcolor(), values(), size(0), ximage_data_init(false),
108  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
109 {
110  init ( I, x, y, title ) ;
111 }
112 
135 vpDisplayX::vpDisplayX ( int x, int y, const char *title )
136  : display(NULL), window(), Ximage(NULL), lut(), context(),
137  screen(0), planes(0), event(), pixmap(), x_color(NULL),
138  screen_depth(8), xcolor(), values(), size(0), ximage_data_init(false),
139  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
140 {
141  windowXPosition = x ;
142  windowYPosition = y ;
143 
144  if (title != NULL)
145  title_ = std::string(title);
146  else
147  title_ = std::string(" ");
148 }
149 
170  : display(NULL), window(), Ximage(NULL), lut(), context(),
171  screen(0), planes(0), event(), pixmap(), x_color(NULL),
172  screen_depth(8), xcolor(), values(), size(0), ximage_data_init(false),
173  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
174 {
175 }
176 
181 {
182  closeDisplay() ;
183 }
184 
193 void
194 vpDisplayX::init ( vpImage<unsigned char> &I, int x, int y, const char *title )
195 {
196 
197  if (x_color == NULL) {
198  // id_unknown = number of predefined colors
199  x_color= new unsigned long [vpColor::id_unknown];
200  }
201 
202  XSizeHints hints;
203  if (x != -1)
204  windowXPosition = x ;
205  if (y != -1)
206  windowYPosition = y ;
207 
208  if (title != NULL)
209  title_ = std::string(title);
210  else
211  title_ = std::string(" ");
212 
213  // Positionnement de la fenetre dans l'ecran.
214  if ( ( windowXPosition < 0 ) || ( windowYPosition < 0 ) )
215  {
216  hints.flags = 0;
217  }
218  else
219  {
220  hints.flags = USPosition;
221  hints.x = windowXPosition;
222  hints.y = windowYPosition;
223  }
224 
225  // setup X11 --------------------------------------------------
226  width = I.getWidth();
227  height = I.getHeight();
228  display = XOpenDisplay ( NULL );
229  if ( display == NULL )
230  {
231  vpERROR_TRACE ( "Can't connect display on server %s.\n", XDisplayName ( NULL ) );
233  "Can't connect display on server." ) ) ;
234  }
235 
236  screen = DefaultScreen ( display );
237  lut = DefaultColormap ( display, screen );
238  screen_depth = (unsigned int)DefaultDepth ( display, screen );
239 
240  if ( ( window = XCreateSimpleWindow ( display, RootWindow ( display, screen ),
242  BlackPixel ( display, screen ),
243  WhitePixel ( display, screen ) ) ) == 0 )
244  {
245  vpERROR_TRACE ( "Can't create window." );
247  "Can't create window." ) ) ;
248  }
249 
250  //
251  // Create color table for 8 and 16 bits screen
252  //
253  if ( screen_depth == 8 )
254  {
255  lut = XCreateColormap ( display, window,
256  DefaultVisual ( display, screen ), AllocAll ) ;
257  xcolor.flags = DoRed | DoGreen | DoBlue ;
258 
259  for ( unsigned int i = 0 ; i < 256 ; i++ )
260  {
261  xcolor.pixel = i ;
262  xcolor.red = 256 * i;
263  xcolor.green = 256 * i;
264  xcolor.blue = 256 * i;
265  XStoreColor ( display, lut, &xcolor );
266  }
267 
268  XSetWindowColormap ( display, window, lut ) ;
269  XInstallColormap ( display, lut ) ;
270  }
271 
272  else if ( screen_depth == 16 )
273  {
274  for ( unsigned int i = 0; i < 256; i ++ )
275  {
276  xcolor.pad = 0;
277  xcolor.red = xcolor.green = xcolor.blue = 256 * i;
278  if ( XAllocColor ( display, lut, &xcolor ) == 0 )
279  {
280  vpERROR_TRACE ( "Can't allocate 256 colors. Only %d allocated.", i );
282  "Can't allocate 256 colors." ) ) ;
283  }
284  colortable[i] = xcolor.pixel;
285  }
286 
287  XSetWindowColormap ( display, window, lut ) ;
288  XInstallColormap ( display, lut ) ;
289 
290  Visual *visual = DefaultVisual (display, screen);
291  RMask = visual->red_mask;
292  GMask = visual->green_mask;
293  BMask = visual->blue_mask;
294 
295  RShift = 15 - getMsb(RMask); /* these are right-shifts */
296  GShift = 15 - getMsb(GMask);
297  BShift = 15 - getMsb(BMask);
298  }
299 
300  //
301  // Create colors for overlay
302  //
303  switch ( screen_depth )
304  {
305  case 8:
306  // Color BLACK and WHITE are set properly by default.
307 
308  // Color LIGHT GRAY.
309  x_color[vpColor::id_lightGray] = 254;
310  xcolor.pixel = x_color[vpColor::id_lightGray] ;
311  xcolor.red = 256 * 192;
312  xcolor.green = 256 * 192;
313  xcolor.blue = 256 * 192;
314  XStoreColor ( display, lut, &xcolor );
315 
316  // Color GRAY.
317  x_color[vpColor::id_gray] = 253;
318  xcolor.pixel = x_color[vpColor::id_gray] ;
319  xcolor.red = 256 * 128;
320  xcolor.green = 256 * 128;
321  xcolor.blue = 256 * 128;
322  XStoreColor ( display, lut, &xcolor );
323 
324  // Color DARK GRAY.
325  x_color[vpColor::id_darkGray] = 252;
326  xcolor.pixel = x_color[vpColor::id_darkGray] ;
327  xcolor.red = 256 * 64;
328  xcolor.green = 256 * 64;
329  xcolor.blue = 256 * 64;
330  XStoreColor ( display, lut, &xcolor );
331 
332  // Color LIGHT RED.
333  x_color[vpColor::id_lightRed] = 251;
334  xcolor.pixel = x_color[vpColor::id_lightRed] ;
335  xcolor.red = 256 * 255;
336  xcolor.green = 256 * 140;
337  xcolor.blue = 256 * 140;
338  XStoreColor ( display, lut, &xcolor );
339 
340  // Color RED.
341  x_color[vpColor::id_red] = 250;
342  xcolor.pixel = x_color[vpColor::id_red] ;
343  xcolor.red = 256 * 255;
344  xcolor.green = 0;
345  xcolor.blue = 0;
346  XStoreColor ( display, lut, &xcolor );
347 
348  // Color DARK RED.
349  x_color[vpColor::id_darkRed] = 249;
350  xcolor.pixel = x_color[vpColor::id_darkRed] ;
351  xcolor.red = 256 * 128;
352  xcolor.green = 0;
353  xcolor.blue = 0;
354  XStoreColor ( display, lut, &xcolor );
355 
356  // Color LIGHT GREEN.
357  x_color[vpColor::id_lightGreen] = 248;
358  xcolor.pixel = x_color[vpColor::id_lightGreen] ;
359  xcolor.red = 256 * 140;
360  xcolor.green = 256 * 255;
361  xcolor.blue = 256 * 140;
362  XStoreColor ( display, lut, &xcolor );
363 
364  // Color GREEN.
365  x_color[vpColor::id_green] = 247;
366  xcolor.pixel = x_color[vpColor::id_green];
367  xcolor.red = 0;
368  xcolor.green = 256 * 255;
369  xcolor.blue = 0;
370  XStoreColor ( display, lut, &xcolor );
371 
372  // Color DARK GREEN.
373  x_color[vpColor::id_darkGreen] = 246;
374  xcolor.pixel = x_color[vpColor::id_darkGreen] ;
375  xcolor.red = 0;
376  xcolor.green = 256 * 128;
377  xcolor.blue = 0;
378  XStoreColor ( display, lut, &xcolor );
379 
380  // Color LIGHT BLUE.
381  x_color[vpColor::id_lightBlue] = 245;
382  xcolor.pixel = x_color[vpColor::id_lightBlue] ;
383  xcolor.red = 256 * 140;
384  xcolor.green = 256 * 140;
385  xcolor.blue = 256 * 255;
386  XStoreColor ( display, lut, &xcolor );
387 
388  // Color BLUE.
389  x_color[vpColor::id_blue] = 244;
390  xcolor.pixel = x_color[vpColor::id_blue];
391  xcolor.red = 0;
392  xcolor.green = 0;
393  xcolor.blue = 256 * 255;
394  XStoreColor ( display, lut, &xcolor );
395 
396  // Color DARK BLUE.
397  x_color[vpColor::id_darkBlue] = 243;
398  xcolor.pixel = x_color[vpColor::id_darkBlue] ;
399  xcolor.red = 0;
400  xcolor.green = 0;
401  xcolor.blue = 256 * 128;
402  XStoreColor ( display, lut, &xcolor );
403 
404  // Color YELLOW.
405  x_color[vpColor::id_yellow] = 242;
406  xcolor.pixel = x_color[vpColor::id_yellow];
407  xcolor.red = 256 * 255;
408  xcolor.green = 256 * 255;
409  xcolor.blue = 0;
410  XStoreColor ( display, lut, &xcolor );
411 
412  // Color ORANGE.
413  x_color[vpColor::id_orange] = 241;
414  xcolor.pixel = x_color[vpColor::id_orange];
415  xcolor.red = 256 * 255;
416  xcolor.green = 256 * 165;
417  xcolor.blue = 0;
418  XStoreColor ( display, lut, &xcolor );
419 
420  // Color CYAN.
421  x_color[vpColor::id_cyan] = 240;
422  xcolor.pixel = x_color[vpColor::id_cyan];
423  xcolor.red = 0;
424  xcolor.green = 256 * 255;
425  xcolor.blue = 256 * 255;
426  XStoreColor ( display, lut, &xcolor );
427 
428  // Color PURPLE.
429  x_color[vpColor::id_purple] = 239;
430  xcolor.pixel = x_color[vpColor::id_purple];
431  xcolor.red = 256 * 128;
432  xcolor.green = 0;
433  xcolor.blue = 256 * 128;
434  XStoreColor ( display, lut, &xcolor );
435 
436  break;
437 
438  case 16:
439  case 24:
440  {
441  xcolor.flags = DoRed | DoGreen | DoBlue ;
442 
443  // Couleur BLACK.
444  xcolor.pad = 0;
445  xcolor.red = 0;
446  xcolor.green = 0;
447  xcolor.blue = 0;
448  XAllocColor ( display, lut, &xcolor );
449  x_color[vpColor::id_black] = xcolor.pixel;
450 
451  // Couleur WHITE.
452  xcolor.pad = 0;
453  xcolor.red = 256* 255;
454  xcolor.green = 256* 255;
455  xcolor.blue = 256* 255;
456  XAllocColor ( display, lut, &xcolor );
457  x_color[vpColor::id_white] = xcolor.pixel;
458 
459  // Couleur LIGHT GRAY.
460  xcolor.pad = 0;
461  xcolor.red = 256 * 192;
462  xcolor.green = 256 * 192;
463  xcolor.blue = 256 * 192;
464  XAllocColor ( display, lut, &xcolor );
465  x_color[vpColor::id_lightGray] = xcolor.pixel;
466 
467  // Couleur GRAY.
468  xcolor.pad = 0;
469  xcolor.red = 256 * 128;
470  xcolor.green = 256 * 128;
471  xcolor.blue = 256 * 128;
472  XAllocColor ( display, lut, &xcolor );
473  x_color[vpColor::id_gray] = xcolor.pixel;
474 
475  // Couleur DARK GRAY.
476  xcolor.pad = 0;
477  xcolor.red = 256 * 64;
478  xcolor.green = 256 * 64;
479  xcolor.blue = 256 * 64;
480  XAllocColor ( display, lut, &xcolor );
481  x_color[vpColor::id_darkGray] = xcolor.pixel;
482 
483  // Couleur LIGHT RED.
484  xcolor.pad = 0;
485  xcolor.red = 256 * 255;
486  xcolor.green = 256 * 140;
487  xcolor.blue = 256 * 140;
488  XAllocColor ( display, lut, &xcolor );
489  x_color[vpColor::id_lightRed] = xcolor.pixel;
490 
491  // Couleur RED.
492  xcolor.pad = 0;
493  xcolor.red = 256* 255;
494  xcolor.green = 0;
495  xcolor.blue = 0;
496  XAllocColor ( display, lut, &xcolor );
497  x_color[vpColor::id_red] = xcolor.pixel;
498 
499  // Couleur DARK RED.
500  xcolor.pad = 0;
501  xcolor.red = 256* 128;
502  xcolor.green = 0;
503  xcolor.blue = 0;
504  XAllocColor ( display, lut, &xcolor );
505  x_color[vpColor::id_darkRed] = xcolor.pixel;
506 
507  // Couleur LIGHT GREEN.
508  xcolor.pad = 0;
509  xcolor.red = 256 * 140;
510  xcolor.green = 256 * 255;
511  xcolor.blue = 256 * 140;
512  XAllocColor ( display, lut, &xcolor );
513  x_color[vpColor::id_lightGreen] = xcolor.pixel;
514 
515  // Couleur GREEN.
516  xcolor.pad = 0;
517  xcolor.red = 0;
518  xcolor.green = 256*255;
519  xcolor.blue = 0;
520  XAllocColor ( display, lut, &xcolor );
521  x_color[vpColor::id_green] = xcolor.pixel;
522 
523  // Couleur DARK GREEN.
524  xcolor.pad = 0;
525  xcolor.red = 0;
526  xcolor.green = 256* 128;
527  xcolor.blue = 0;
528  XAllocColor ( display, lut, &xcolor );
529  x_color[vpColor::id_darkGreen] = xcolor.pixel;
530 
531  // Couleur LIGHT Blue.
532  xcolor.pad = 0;
533  xcolor.red = 256 * 140;
534  xcolor.green = 256 * 140;
535  xcolor.blue = 256 * 255;
536  XAllocColor ( display, lut, &xcolor );
537  x_color[vpColor::id_lightBlue] = xcolor.pixel;
538 
539  // Couleur BLUE.
540  xcolor.pad = 0;
541  xcolor.red = 0;
542  xcolor.green = 0;
543  xcolor.blue = 256* 255;
544  XAllocColor ( display, lut, &xcolor );
545  x_color[vpColor::id_blue] = xcolor.pixel;
546 
547  // Couleur DARK BLUE.
548  xcolor.pad = 0;
549  xcolor.red = 0;
550  xcolor.green = 0;
551  xcolor.blue = 256* 128;
552  XAllocColor ( display, lut, &xcolor );
553  x_color[vpColor::id_darkBlue] = xcolor.pixel;
554 
555  // Couleur YELLOW.
556  xcolor.pad = 0;
557  xcolor.red = 256 * 255;
558  xcolor.green = 256 * 255;
559  xcolor.blue = 0;
560  XAllocColor ( display, lut, &xcolor );
561  x_color[vpColor::id_yellow] = xcolor.pixel;
562 
563  // Couleur ORANGE.
564  xcolor.pad = 0;
565  xcolor.red = 256 * 255;
566  xcolor.green = 256 * 165;
567  xcolor.blue = 0;
568  XAllocColor ( display, lut, &xcolor );
569  x_color[vpColor::id_orange] = xcolor.pixel;
570 
571  // Couleur CYAN.
572  xcolor.pad = 0;
573  xcolor.red = 0;
574  xcolor.green = 256 * 255;
575  xcolor.blue = 256 * 255;
576  XAllocColor ( display, lut, &xcolor );
577  x_color[vpColor::id_cyan] = xcolor.pixel;
578 
579  // Couleur PURPLE.
580  xcolor.pad = 0;
581  xcolor.red = 256 * 128;
582  xcolor.green = 0;
583  xcolor.blue = 256 * 128;
584  XAllocColor ( display, lut, &xcolor );
585  x_color[vpColor::id_purple] = xcolor.pixel;
586  break;
587  }
588  }
589 
590  XSetStandardProperties ( display, window, this->title_.c_str(), this->title_.c_str(), None, 0, 0, &hints );
591  XMapWindow ( display, window ) ;
592  // Selection des evenements.
593  XSelectInput ( display, window,
594  ExposureMask |
595  ButtonPressMask | ButtonReleaseMask |
596  KeyPressMask | KeyReleaseMask |
597  StructureNotifyMask |
598  PointerMotionMask);
599 
600  // graphic context creation
601  values.plane_mask = AllPlanes;
602  values.fill_style = FillSolid;
603  values.foreground = WhitePixel ( display, screen );
604  values.background = BlackPixel ( display, screen );
605  context = XCreateGC ( display, window,
606  GCPlaneMask | GCFillStyle | GCForeground | GCBackground,
607  &values );
608 
609  if ( context == NULL )
610  {
611  vpERROR_TRACE ( "Can't create graphics context." );
613  "Can't create graphics context" ) ) ;
614 
615  }
616 
617  // Pixmap creation.
618  pixmap = XCreatePixmap ( display, window, width, height, screen_depth );
619 
620  // Hangs when forward X11 is used to send the display to an other computer
621 // do
622 // XNextEvent ( display, &event );
623 // while ( event.xany.type != Expose );
624 
625  {
626  Ximage = XCreateImage ( display, DefaultVisual ( display, screen ),
627  screen_depth, ZPixmap, 0, NULL,
628  I.getWidth() , I.getHeight(), XBitmapPad ( display ), 0 );
629 
630  Ximage->data = ( char * ) malloc ( I.getHeight() * (unsigned int)Ximage->bytes_per_line );
631  ximage_data_init = true;
632 
633  }
635 
636  XStoreName ( display, window, title_.c_str() );
637 
638  XSync ( display, 1 );
639 
640  I.display = this ;
641 }
642 
652 void
653 vpDisplayX::init ( vpImage<vpRGBa> &I, int x, int y, const char *title )
654 {
655 
656  XSizeHints hints;
657  if (x != -1)
658  windowXPosition = x ;
659  if (y != -1)
660  windowYPosition = y ;
661 
662  if (x_color == NULL) {
663  // id_unknown = number of predefined colors
664  x_color= new unsigned long [vpColor::id_unknown];
665  }
666 
667  if (title != NULL)
668  title_ = std::string(title);
669  else
670  title_ = std::string(" ");
671 
672  // Positionnement de la fenetre dans l'ecran.
673  if ( ( windowXPosition < 0 ) || ( windowYPosition < 0 ) )
674  {
675  hints.flags = 0;
676  }
677  else
678  {
679  hints.flags = USPosition;
680  hints.x = windowXPosition;
681  hints.y = windowYPosition;
682  }
683 
684  // setup X11 --------------------------------------------------
685  width = I.getWidth();
686  height = I.getHeight();
687 
688  if ( ( display = XOpenDisplay ( NULL ) ) == NULL )
689  {
690  vpERROR_TRACE ( "Can't connect display on server %s.\n", XDisplayName ( NULL ) );
692  "Can't connect display on server." ) ) ;
693  }
694 
695  screen = DefaultScreen ( display );
696  lut = DefaultColormap ( display, screen );
697  screen_depth = (unsigned int)DefaultDepth ( display, screen );
698 
699  vpDEBUG_TRACE ( 1, "Screen depth: %d\n", screen_depth );
700 
701  if ( ( window = XCreateSimpleWindow ( display, RootWindow ( display, screen ),
703  width, height, 1,
704  BlackPixel ( display, screen ),
705  WhitePixel ( display, screen ) ) ) == 0 )
706  {
707  vpERROR_TRACE ( "Can't create window." );
709  "Can't create window." ) ) ;
710  }
711 
712  //
713  // Create color table for 8 and 16 bits screen
714  //
715  if ( screen_depth == 8 )
716  {
717  lut = XCreateColormap ( display, window,
718  DefaultVisual ( display, screen ), AllocAll ) ;
719  xcolor.flags = DoRed | DoGreen | DoBlue ;
720 
721  for ( unsigned int i = 0 ; i < 256 ; i++ )
722  {
723  xcolor.pixel = i ;
724  xcolor.red = 256 * i;
725  xcolor.green = 256 * i;
726  xcolor.blue = 256 * i;
727  XStoreColor ( display, lut, &xcolor );
728  }
729 
730  XSetWindowColormap ( display, window, lut ) ;
731  XInstallColormap ( display, lut ) ;
732  }
733 
734  else if ( screen_depth == 16 )
735  {
736  for ( unsigned int i = 0; i < 256; i ++ )
737  {
738  xcolor.pad = 0;
739  xcolor.red = xcolor.green = xcolor.blue = 256 * i;
740  if ( XAllocColor ( display, lut, &xcolor ) == 0 )
741  {
742  vpERROR_TRACE ( "Can't allocate 256 colors. Only %d allocated.", i );
744  "Can't allocate 256 colors." ) ) ;
745  }
746  colortable[i] = xcolor.pixel;
747  }
748 
749  Visual *visual = DefaultVisual (display, screen);
750  RMask = visual->red_mask;
751  GMask = visual->green_mask;
752  BMask = visual->blue_mask;
753 
754  RShift = 15 - getMsb(RMask); /* these are right-shifts */
755  GShift = 15 - getMsb(GMask);
756  BShift = 15 - getMsb(BMask);
757 
758  XSetWindowColormap ( display, window, lut ) ;
759  XInstallColormap ( display, lut ) ;
760  }
761 
762 
763  //
764  // Create colors for overlay
765  //
766  switch ( screen_depth )
767  {
768 
769  case 8:
770  // Color BLACK and WHITE are set properly.
771 
772  // Color LIGHT GRAY.
773  x_color[vpColor::id_lightGray] = 254;
774  xcolor.pixel = x_color[vpColor::id_lightGray] ;
775  xcolor.red = 256 * 192;
776  xcolor.green = 256 * 192;
777  xcolor.blue = 256 * 192;
778  XStoreColor ( display, lut, &xcolor );
779 
780  // Color GRAY.
781  x_color[vpColor::id_gray] = 253;
782  xcolor.pixel = x_color[vpColor::id_gray] ;
783  xcolor.red = 256 * 128;
784  xcolor.green = 256 * 128;
785  xcolor.blue = 256 * 128;
786  XStoreColor ( display, lut, &xcolor );
787 
788  // Color DARK GRAY.
789  x_color[vpColor::id_darkGray] = 252;
790  xcolor.pixel = x_color[vpColor::id_darkGray] ;
791  xcolor.red = 256 * 64;
792  xcolor.green = 256 * 64;
793  xcolor.blue = 256 * 64;
794  XStoreColor ( display, lut, &xcolor );
795 
796  // Color LIGHT RED.
797  x_color[vpColor::id_lightRed] = 251;
798  xcolor.pixel = x_color[vpColor::id_lightRed] ;
799  xcolor.red = 256 * 255;
800  xcolor.green = 256 * 140;
801  xcolor.blue = 256 * 140;
802  XStoreColor ( display, lut, &xcolor );
803 
804  // Color RED.
805  x_color[vpColor::id_red] = 250;
806  xcolor.pixel = x_color[vpColor::id_red] ;
807  xcolor.red = 256 * 255;
808  xcolor.green = 0;
809  xcolor.blue = 0;
810  XStoreColor ( display, lut, &xcolor );
811 
812  // Color DARK RED.
813  x_color[vpColor::id_darkRed] = 249;
814  xcolor.pixel = x_color[vpColor::id_darkRed] ;
815  xcolor.red = 256 * 128;
816  xcolor.green = 0;
817  xcolor.blue = 0;
818  XStoreColor ( display, lut, &xcolor );
819 
820  // Color LIGHT GREEN.
821  x_color[vpColor::id_lightGreen] = 248;
822  xcolor.pixel = x_color[vpColor::id_lightGreen] ;
823  xcolor.red = 256 * 140;
824  xcolor.green = 256 * 255;
825  xcolor.blue = 256 * 140;
826  XStoreColor ( display, lut, &xcolor );
827 
828  // Color GREEN.
829  x_color[vpColor::id_green] = 247;
830  xcolor.pixel = x_color[vpColor::id_green];
831  xcolor.red = 0;
832  xcolor.green = 256 * 255;
833  xcolor.blue = 0;
834  XStoreColor ( display, lut, &xcolor );
835 
836  // Color DARK GREEN.
837  x_color[vpColor::id_darkGreen] = 246;
838  xcolor.pixel = x_color[vpColor::id_darkGreen] ;
839  xcolor.red = 0;
840  xcolor.green = 256 * 128;
841  xcolor.blue = 0;
842  XStoreColor ( display, lut, &xcolor );
843 
844  // Color LIGHT BLUE.
845  x_color[vpColor::id_lightBlue] = 245;
846  xcolor.pixel = x_color[vpColor::id_lightBlue] ;
847  xcolor.red = 256 * 140;
848  xcolor.green = 256 * 140;
849  xcolor.blue = 256 * 255;
850  XStoreColor ( display, lut, &xcolor );
851 
852  // Color BLUE.
853  x_color[vpColor::id_blue] = 244;
854  xcolor.pixel = x_color[vpColor::id_blue];
855  xcolor.red = 0;
856  xcolor.green = 0;
857  xcolor.blue = 256 * 255;
858  XStoreColor ( display, lut, &xcolor );
859 
860  // Color DARK BLUE.
861  x_color[vpColor::id_darkBlue] = 243;
862  xcolor.pixel = x_color[vpColor::id_darkBlue] ;
863  xcolor.red = 0;
864  xcolor.green = 0;
865  xcolor.blue = 256 * 128;
866  XStoreColor ( display, lut, &xcolor );
867 
868  // Color YELLOW.
869  x_color[vpColor::id_yellow] = 242;
870  xcolor.pixel = x_color[vpColor::id_yellow];
871  xcolor.red = 256 * 255;
872  xcolor.green = 256 * 255;
873  xcolor.blue = 0;
874  XStoreColor ( display, lut, &xcolor );
875 
876  // Color ORANGE.
877  x_color[vpColor::id_orange] = 241;
878  xcolor.pixel = x_color[vpColor::id_orange];
879  xcolor.red = 256 * 255;
880  xcolor.green = 256 * 165;
881  xcolor.blue = 0;
882  XStoreColor ( display, lut, &xcolor );
883 
884  // Color CYAN.
885  x_color[vpColor::id_cyan] = 240;
886  xcolor.pixel = x_color[vpColor::id_cyan];
887  xcolor.red = 0;
888  xcolor.green = 256 * 255;
889  xcolor.blue = 256 * 255;
890  XStoreColor ( display, lut, &xcolor );
891 
892  // Color PURPLE.
893  x_color[vpColor::id_purple] = 239;
894  xcolor.pixel = x_color[vpColor::id_purple];
895  xcolor.red = 256 * 128;
896  xcolor.green = 0;
897  xcolor.blue = 256 * 128;
898  XStoreColor ( display, lut, &xcolor );
899 
900  break;
901 
902  case 16:
903  case 24:
904  {
905  xcolor.flags = DoRed | DoGreen | DoBlue ;
906 
907  // Couleur BLACK.
908  xcolor.pad = 0;
909  xcolor.red = 0;
910  xcolor.green = 0;
911  xcolor.blue = 0;
912  XAllocColor ( display, lut, &xcolor );
913  x_color[vpColor::id_black] = xcolor.pixel;
914 
915  // Couleur WHITE.
916  xcolor.pad = 0;
917  xcolor.red = 256* 255;
918  xcolor.green = 256* 255;
919  xcolor.blue = 256* 255;
920  XAllocColor ( display, lut, &xcolor );
921  x_color[vpColor::id_white] = xcolor.pixel;
922 
923  // Couleur LIGHT GRAY.
924  xcolor.pad = 0;
925  xcolor.red = 256 * 192;
926  xcolor.green = 256 * 192;
927  xcolor.blue = 256 * 192;
928  XAllocColor ( display, lut, &xcolor );
929  x_color[vpColor::id_lightGray] = xcolor.pixel;
930 
931  // Couleur GRAY.
932  xcolor.pad = 0;
933  xcolor.red = 256 * 128;
934  xcolor.green = 256 * 128;
935  xcolor.blue = 256 * 128;
936  XAllocColor ( display, lut, &xcolor );
937  x_color[vpColor::id_gray] = xcolor.pixel;
938 
939  // Couleur DARK GRAY.
940  xcolor.pad = 0;
941  xcolor.red = 256 * 64;
942  xcolor.green = 256 * 64;
943  xcolor.blue = 256 * 64;
944  XAllocColor ( display, lut, &xcolor );
945  x_color[vpColor::id_darkGray] = xcolor.pixel;
946 
947  // Couleur LIGHT RED.
948  xcolor.pad = 0;
949  xcolor.red = 256 * 255;
950  xcolor.green = 256 * 140;
951  xcolor.blue = 256 * 140;
952  XAllocColor ( display, lut, &xcolor );
953  x_color[vpColor::id_lightRed] = xcolor.pixel;
954 
955  // Couleur RED.
956  xcolor.pad = 0;
957  xcolor.red = 256* 255;
958  xcolor.green = 0;
959  xcolor.blue = 0;
960  XAllocColor ( display, lut, &xcolor );
961  x_color[vpColor::id_red] = xcolor.pixel;
962 
963  // Couleur DARK RED.
964  xcolor.pad = 0;
965  xcolor.red = 256* 128;
966  xcolor.green = 0;
967  xcolor.blue = 0;
968  XAllocColor ( display, lut, &xcolor );
969  x_color[vpColor::id_darkRed] = xcolor.pixel;
970 
971  // Couleur LIGHT GREEN.
972  xcolor.pad = 0;
973  xcolor.red = 256 * 140;
974  xcolor.green = 256 * 255;
975  xcolor.blue = 256 * 140;
976  XAllocColor ( display, lut, &xcolor );
977  x_color[vpColor::id_lightGreen] = xcolor.pixel;
978 
979  // Couleur GREEN.
980  xcolor.pad = 0;
981  xcolor.red = 0;
982  xcolor.green = 256*255;
983  xcolor.blue = 0;
984  XAllocColor ( display, lut, &xcolor );
985  x_color[vpColor::id_green] = xcolor.pixel;
986 
987  // Couleur DARK GREEN.
988  xcolor.pad = 0;
989  xcolor.red = 0;
990  xcolor.green = 256* 128;
991  xcolor.blue = 0;
992  XAllocColor ( display, lut, &xcolor );
993  x_color[vpColor::id_darkGreen] = xcolor.pixel;
994 
995  // Couleur LIGHT Blue.
996  xcolor.pad = 0;
997  xcolor.red = 256 * 140;
998  xcolor.green = 256 * 140;
999  xcolor.blue = 256 * 255;
1000  XAllocColor ( display, lut, &xcolor );
1001  x_color[vpColor::id_lightBlue] = xcolor.pixel;
1002 
1003  // Couleur BLUE.
1004  xcolor.pad = 0;
1005  xcolor.red = 0;
1006  xcolor.green = 0;
1007  xcolor.blue = 256* 255;
1008  XAllocColor ( display, lut, &xcolor );
1009  x_color[vpColor::id_blue] = xcolor.pixel;
1010 
1011  // Couleur DARK BLUE.
1012  xcolor.pad = 0;
1013  xcolor.red = 0;
1014  xcolor.green = 0;
1015  xcolor.blue = 256* 128;
1016  XAllocColor ( display, lut, &xcolor );
1017  x_color[vpColor::id_darkBlue] = xcolor.pixel;
1018 
1019  // Couleur YELLOW.
1020  xcolor.pad = 0;
1021  xcolor.red = 256 * 255;
1022  xcolor.green = 256 * 255;
1023  xcolor.blue = 0;
1024  XAllocColor ( display, lut, &xcolor );
1025  x_color[vpColor::id_yellow] = xcolor.pixel;
1026 
1027  // Couleur ORANGE.
1028  xcolor.pad = 0;
1029  xcolor.red = 256 * 255;
1030  xcolor.green = 256 * 165;
1031  xcolor.blue = 0;
1032  XAllocColor ( display, lut, &xcolor );
1033  x_color[vpColor::id_orange] = xcolor.pixel;
1034 
1035  // Couleur CYAN.
1036  xcolor.pad = 0;
1037  xcolor.red = 0;
1038  xcolor.green = 256 * 255;
1039  xcolor.blue = 256 * 255;
1040  XAllocColor ( display, lut, &xcolor );
1041  x_color[vpColor::id_cyan] = xcolor.pixel;
1042 
1043  // Couleur PURPLE.
1044  xcolor.pad = 0;
1045  xcolor.red = 256 * 128;
1046  xcolor.green = 0;
1047  xcolor.blue = 256 * 128;
1048  XAllocColor ( display, lut, &xcolor );
1049  x_color[vpColor::id_purple] = xcolor.pixel;
1050  break;
1051  }
1052  }
1053 
1054  XSetStandardProperties ( display, window, this->title_.c_str(), this->title_.c_str(), None, 0, 0, &hints );
1055  XMapWindow ( display, window ) ;
1056  // Selection des evenements.
1057  XSelectInput ( display, window,
1058  ExposureMask |
1059  ButtonPressMask | ButtonReleaseMask |
1060  KeyPressMask | KeyReleaseMask |
1061  StructureNotifyMask |
1062  PointerMotionMask);
1063 
1064  // Creation du contexte graphique
1065  values.plane_mask = AllPlanes;
1066  values.fill_style = FillSolid;
1067  values.foreground = WhitePixel ( display, screen );
1068  values.background = BlackPixel ( display, screen );
1069  context = XCreateGC ( display, window,
1070  GCPlaneMask | GCFillStyle | GCForeground | GCBackground,
1071  &values );
1072 
1073  if ( context == NULL )
1074  {
1075  vpERROR_TRACE ( "Can't create graphics context." );
1077  "Can't create graphics context" ) ) ;
1078  }
1079 
1080  // Pixmap creation.
1081  pixmap = XCreatePixmap ( display, window, width, height, screen_depth );
1082 
1083  // Hangs when forward X11 is used to send the display to an other computer
1084 // do
1085 // XNextEvent ( display, &event );
1086 // while ( event.xany.type != Expose );
1087 
1088  {
1089  Ximage = XCreateImage ( display, DefaultVisual ( display, screen ),
1090  screen_depth, ZPixmap, 0, NULL,
1091  I.getWidth() , I.getHeight(), XBitmapPad ( display ), 0 );
1092 
1093 
1094  Ximage->data = ( char * ) malloc ( I.getHeight() * (unsigned int)Ximage->bytes_per_line );
1095  ximage_data_init = true;
1096 
1097  }
1098  displayHasBeenInitialized = true ;
1099 
1100  XSync ( display, true );
1101 
1102  XStoreName ( display, window, title_.c_str() );
1103 
1104  I.display = this ;
1105 }
1106 
1107 
1115 void vpDisplayX::init ( unsigned int w, unsigned int h, int x, int y, const char *title )
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 
3399 #endif
3400 
void closeDisplay()
void clearDisplay(const vpColor &color=vpColor::white)
vpDisplay * display
Definition: vpImage.h:121
#define vpDEBUG_TRACE
Definition: vpDebug.h:482
double getTop() const
Definition: vpRect.h:180
double get_v() const
Definition: vpImagePoint.h:264
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:183
double get_i() const
Definition: vpImagePoint.h:195
unsigned int getWidth() const
Definition: vpImage.h:161
void displayImage(const vpImage< vpRGBa > &I)
#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
static const vpColor black
Definition: vpColor.h:161
static const vpColor darkRed
Definition: vpColor.h:168
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
double get_u() const
Definition: vpImagePoint.h:253
static const vpColor lightGray
Definition: vpColor.h:163
double getHeight() const
Definition: vpRect.h:155
void flushDisplay()
static const vpColor darkBlue
Definition: vpColor.h:174
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:178
unsigned char G
Green component.
Definition: vpRGBa.h:147
static const vpColor green
Definition: vpColor.h:170
unsigned int getHeight() const
Definition: vpDisplayX.h:272
static int round(const double x)
Definition: vpMath.h:228
double get_j() const
Definition: vpImagePoint.h:206
static const vpColor lightRed
Definition: vpColor.h:166
Class that defines a RGB 32 bits structure.
Definition: vpRGBa.h:68
static const vpColor red
Definition: vpColor.h:167
bool getPointerPosition(vpImagePoint &ip)
static const vpColor orange
Definition: vpColor.h:177
std::string title_
Definition: vpDisplay.h:185
bool getClick(bool blocking=true)
vpColorIdentifier id
Definition: vpColor.h:156
void set_i(const double ii)
Definition: vpImagePoint.h:159
double getWidth() const
Definition: vpRect.h:199
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:176
static const vpColor lightGreen
Definition: vpColor.h:169
void set_u(const double u)
Definition: vpImagePoint.h:217
static double sqr(double x)
Definition: vpMath.h:106
void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)
unsigned int height
Definition: vpDisplay.h:184
void getScreenSize(unsigned int &width, unsigned int &height)
void set_v(const double v)
Definition: vpImagePoint.h:228
unsigned char A
Additionnal component.
Definition: vpRGBa.h:149
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:536
static const vpColor gray
Definition: vpColor.h:164
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
void setWindowPosition(int winx, int winy)
virtual ~vpDisplayX()
Definition: vpDisplayX.cpp:180
static const vpColor darkGray
Definition: vpColor.h:165
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:170
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:180
unsigned char R
Red component.
Definition: vpRGBa.h:146
unsigned int getWidth() const
Definition: vpDisplayX.h:271
unsigned int getHeight() const
Definition: vpImage.h:152
Defines a rectangle in the plane.
Definition: vpRect.h:85
static const vpColor darkGreen
Definition: vpColor.h:171
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
Definition: vpDisplayX.cpp:194
static const vpColor yellow
Definition: vpColor.h:175
static const vpColor lightBlue
Definition: vpColor.h:172
int windowYPosition
display position
Definition: vpDisplay.h:182
static const vpColor purple
Definition: vpColor.h:178
static const vpColor white
Definition: vpColor.h:162
double getLeft() const
Definition: vpRect.h:161
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:275
void getImage(vpImage< vpRGBa > &I)
get the window pixmap and put it in vpRGBa image
static const vpColor blue
Definition: vpColor.h:173
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)