Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpDisplayX.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
82  : display(NULL), window(), Ximage(NULL), lut(), context(),
83  screen(0), event(), pixmap(), x_color(NULL),
84  screen_depth(8), xcolor(), values(), ximage_data_init(false),
85  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
86 {
87  setScale(scaleType, I.getWidth(), I.getHeight());
88 
89  init ( I ) ;
90 }
91 
111  int x,
112  int y,
113  const std::string &title, vpScaleType scaleType )
114  : display(NULL), window(), Ximage(NULL), lut(), context(),
115  screen(0), event(), pixmap(), x_color(NULL),
116  screen_depth(8), xcolor(), values(), ximage_data_init(false),
117  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
118 {
119  setScale(scaleType, I.getWidth(), I.getHeight());
120  init ( I, x, y, title ) ;
121 }
122 
138  : display(NULL), window(), Ximage(NULL), lut(), context(),
139  screen(0), event(), pixmap(), x_color(NULL),
140  screen_depth(8), xcolor(), values(), ximage_data_init(false),
141  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
142 {
143  setScale(scaleType, I.getWidth(), I.getHeight());
144  init ( I ) ;
145 }
146 
164  int x,
165  int y,
166  const std::string &title, vpScaleType scaleType)
167  : display(NULL), window(), Ximage(NULL), lut(), context(),
168  screen(0), event(), pixmap(), x_color(NULL),
169  screen_depth(8), xcolor(), values(), ximage_data_init(false),
170  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
171 {
172  setScale(scaleType, I.getWidth(), I.getHeight());
173  init ( I, x, y, title ) ;
174 }
175 
198 vpDisplayX::vpDisplayX ( int x, int y, const std::string &title )
199  : display(NULL), window(), Ximage(NULL), lut(), context(),
200  screen(0), event(), pixmap(), x_color(NULL),
201  screen_depth(8), xcolor(), values(), ximage_data_init(false),
202  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
203 {
204  m_windowXPosition = x ;
205  m_windowYPosition = y ;
206 
207  m_title = title;
208 }
209 
230  : display(NULL), window(), Ximage(NULL), lut(), context(),
231  screen(0), event(), pixmap(), x_color(NULL),
232  screen_depth(8), xcolor(), values(), ximage_data_init(false),
233  RMask(0), GMask(0), BMask(0), RShift(0), GShift(0), BShift(0)
234 {
235 }
236 
241 {
242  closeDisplay() ;
243 }
244 
253 void
254 vpDisplayX::init ( vpImage<unsigned char> &I, int x, int y, const std::string &title )
255 {
257 
258  if (x_color == NULL) {
259  // id_unknown = number of predefined colors
260  x_color= new unsigned long [vpColor::id_unknown];
261  }
262 
263  XSizeHints hints;
264  if (x != -1)
265  m_windowXPosition = x ;
266  if (y != -1)
267  m_windowYPosition = y ;
268 
269  if (! title.empty())
270  m_title = title;
271 
272  // Positionnement de la fenetre dans l'ecran.
273  if ( ( m_windowXPosition < 0 ) || ( m_windowYPosition < 0 ) )
274  {
275  hints.flags = 0;
276  }
277  else
278  {
279  hints.flags = USPosition;
280  hints.x = m_windowXPosition;
281  hints.y = m_windowYPosition;
282  }
283 
284  // setup X11 --------------------------------------------------
285  m_width = I.getWidth() / m_scale;
286  m_height = I.getHeight() / m_scale;
287  display = XOpenDisplay ( NULL );
288  if ( display == NULL )
289  {
290  vpERROR_TRACE ( "Can't connect display on server %s.\n", XDisplayName ( NULL ) );
292  "Can't connect display on server." ) ) ;
293  }
294 
295  screen = DefaultScreen ( display );
296  lut = DefaultColormap ( display, screen );
297  screen_depth = (unsigned int)DefaultDepth ( display, screen );
298 
299  if ( ( window = XCreateSimpleWindow ( display, RootWindow ( display, screen ),
301  BlackPixel ( display, screen ),
302  WhitePixel ( display, screen ) ) ) == 0 )
303  {
304  vpERROR_TRACE ( "Can't create window." );
306  "Can't create window." ) ) ;
307  }
308 
309  //
310  // Create color table for 8 and 16 bits screen
311  //
312  if ( screen_depth == 8 )
313  {
314  lut = XCreateColormap ( display, window,
315  DefaultVisual ( display, screen ), AllocAll ) ;
316  xcolor.flags = DoRed | DoGreen | DoBlue ;
317 
318  for ( unsigned int i = 0 ; i < 256 ; i++ )
319  {
320  xcolor.pixel = i ;
321  xcolor.red = 256 * i;
322  xcolor.green = 256 * i;
323  xcolor.blue = 256 * i;
324  XStoreColor ( display, lut, &xcolor );
325  }
326 
327  XSetWindowColormap ( display, window, lut ) ;
328  XInstallColormap ( display, lut ) ;
329  }
330 
331  else if ( screen_depth == 16 )
332  {
333  for ( unsigned int i = 0; i < 256; i ++ )
334  {
335  xcolor.pad = 0;
336  xcolor.red = xcolor.green = xcolor.blue = 256 * i;
337  if ( XAllocColor ( display, lut, &xcolor ) == 0 )
338  {
339  vpERROR_TRACE ( "Can't allocate 256 colors. Only %d allocated.", i );
341  "Can't allocate 256 colors." ) ) ;
342  }
343  colortable[i] = xcolor.pixel;
344  }
345 
346  XSetWindowColormap ( display, window, lut ) ;
347  XInstallColormap ( display, lut ) ;
348 
349  Visual *visual = DefaultVisual (display, screen);
350  RMask = visual->red_mask;
351  GMask = visual->green_mask;
352  BMask = visual->blue_mask;
353 
354  RShift = 15 - getMsb(RMask); /* these are right-shifts */
355  GShift = 15 - getMsb(GMask);
356  BShift = 15 - getMsb(BMask);
357  }
358 
359  //
360  // Create colors for overlay
361  //
362  switch ( screen_depth )
363  {
364  case 8:
365  // Color BLACK and WHITE are set properly by default.
366 
367  // Color LIGHT GRAY.
368  x_color[vpColor::id_lightGray] = 254;
369  xcolor.pixel = x_color[vpColor::id_lightGray] ;
370  xcolor.red = 256 * 192;
371  xcolor.green = 256 * 192;
372  xcolor.blue = 256 * 192;
373  XStoreColor ( display, lut, &xcolor );
374 
375  // Color GRAY.
376  x_color[vpColor::id_gray] = 253;
377  xcolor.pixel = x_color[vpColor::id_gray] ;
378  xcolor.red = 256 * 128;
379  xcolor.green = 256 * 128;
380  xcolor.blue = 256 * 128;
381  XStoreColor ( display, lut, &xcolor );
382 
383  // Color DARK GRAY.
384  x_color[vpColor::id_darkGray] = 252;
385  xcolor.pixel = x_color[vpColor::id_darkGray] ;
386  xcolor.red = 256 * 64;
387  xcolor.green = 256 * 64;
388  xcolor.blue = 256 * 64;
389  XStoreColor ( display, lut, &xcolor );
390 
391  // Color LIGHT RED.
392  x_color[vpColor::id_lightRed] = 251;
393  xcolor.pixel = x_color[vpColor::id_lightRed] ;
394  xcolor.red = 256 * 255;
395  xcolor.green = 256 * 140;
396  xcolor.blue = 256 * 140;
397  XStoreColor ( display, lut, &xcolor );
398 
399  // Color RED.
400  x_color[vpColor::id_red] = 250;
401  xcolor.pixel = x_color[vpColor::id_red] ;
402  xcolor.red = 256 * 255;
403  xcolor.green = 0;
404  xcolor.blue = 0;
405  XStoreColor ( display, lut, &xcolor );
406 
407  // Color DARK RED.
408  x_color[vpColor::id_darkRed] = 249;
409  xcolor.pixel = x_color[vpColor::id_darkRed] ;
410  xcolor.red = 256 * 128;
411  xcolor.green = 0;
412  xcolor.blue = 0;
413  XStoreColor ( display, lut, &xcolor );
414 
415  // Color LIGHT GREEN.
416  x_color[vpColor::id_lightGreen] = 248;
417  xcolor.pixel = x_color[vpColor::id_lightGreen] ;
418  xcolor.red = 256 * 140;
419  xcolor.green = 256 * 255;
420  xcolor.blue = 256 * 140;
421  XStoreColor ( display, lut, &xcolor );
422 
423  // Color GREEN.
424  x_color[vpColor::id_green] = 247;
425  xcolor.pixel = x_color[vpColor::id_green];
426  xcolor.red = 0;
427  xcolor.green = 256 * 255;
428  xcolor.blue = 0;
429  XStoreColor ( display, lut, &xcolor );
430 
431  // Color DARK GREEN.
432  x_color[vpColor::id_darkGreen] = 246;
433  xcolor.pixel = x_color[vpColor::id_darkGreen] ;
434  xcolor.red = 0;
435  xcolor.green = 256 * 128;
436  xcolor.blue = 0;
437  XStoreColor ( display, lut, &xcolor );
438 
439  // Color LIGHT BLUE.
440  x_color[vpColor::id_lightBlue] = 245;
441  xcolor.pixel = x_color[vpColor::id_lightBlue] ;
442  xcolor.red = 256 * 140;
443  xcolor.green = 256 * 140;
444  xcolor.blue = 256 * 255;
445  XStoreColor ( display, lut, &xcolor );
446 
447  // Color BLUE.
448  x_color[vpColor::id_blue] = 244;
449  xcolor.pixel = x_color[vpColor::id_blue];
450  xcolor.red = 0;
451  xcolor.green = 0;
452  xcolor.blue = 256 * 255;
453  XStoreColor ( display, lut, &xcolor );
454 
455  // Color DARK BLUE.
456  x_color[vpColor::id_darkBlue] = 243;
457  xcolor.pixel = x_color[vpColor::id_darkBlue] ;
458  xcolor.red = 0;
459  xcolor.green = 0;
460  xcolor.blue = 256 * 128;
461  XStoreColor ( display, lut, &xcolor );
462 
463  // Color YELLOW.
464  x_color[vpColor::id_yellow] = 242;
465  xcolor.pixel = x_color[vpColor::id_yellow];
466  xcolor.red = 256 * 255;
467  xcolor.green = 256 * 255;
468  xcolor.blue = 0;
469  XStoreColor ( display, lut, &xcolor );
470 
471  // Color ORANGE.
472  x_color[vpColor::id_orange] = 241;
473  xcolor.pixel = x_color[vpColor::id_orange];
474  xcolor.red = 256 * 255;
475  xcolor.green = 256 * 165;
476  xcolor.blue = 0;
477  XStoreColor ( display, lut, &xcolor );
478 
479  // Color CYAN.
480  x_color[vpColor::id_cyan] = 240;
481  xcolor.pixel = x_color[vpColor::id_cyan];
482  xcolor.red = 0;
483  xcolor.green = 256 * 255;
484  xcolor.blue = 256 * 255;
485  XStoreColor ( display, lut, &xcolor );
486 
487  // Color PURPLE.
488  x_color[vpColor::id_purple] = 239;
489  xcolor.pixel = x_color[vpColor::id_purple];
490  xcolor.red = 256 * 128;
491  xcolor.green = 0;
492  xcolor.blue = 256 * 128;
493  XStoreColor ( display, lut, &xcolor );
494 
495  break;
496 
497  case 16:
498  case 24:
499  {
500  xcolor.flags = DoRed | DoGreen | DoBlue ;
501 
502  // Couleur BLACK.
503  xcolor.pad = 0;
504  xcolor.red = 0;
505  xcolor.green = 0;
506  xcolor.blue = 0;
507  XAllocColor ( display, lut, &xcolor );
508  x_color[vpColor::id_black] = xcolor.pixel;
509 
510  // Couleur WHITE.
511  xcolor.pad = 0;
512  xcolor.red = 256* 255;
513  xcolor.green = 256* 255;
514  xcolor.blue = 256* 255;
515  XAllocColor ( display, lut, &xcolor );
516  x_color[vpColor::id_white] = xcolor.pixel;
517 
518  // Couleur LIGHT GRAY.
519  xcolor.pad = 0;
520  xcolor.red = 256 * 192;
521  xcolor.green = 256 * 192;
522  xcolor.blue = 256 * 192;
523  XAllocColor ( display, lut, &xcolor );
524  x_color[vpColor::id_lightGray] = xcolor.pixel;
525 
526  // Couleur GRAY.
527  xcolor.pad = 0;
528  xcolor.red = 256 * 128;
529  xcolor.green = 256 * 128;
530  xcolor.blue = 256 * 128;
531  XAllocColor ( display, lut, &xcolor );
532  x_color[vpColor::id_gray] = xcolor.pixel;
533 
534  // Couleur DARK GRAY.
535  xcolor.pad = 0;
536  xcolor.red = 256 * 64;
537  xcolor.green = 256 * 64;
538  xcolor.blue = 256 * 64;
539  XAllocColor ( display, lut, &xcolor );
540  x_color[vpColor::id_darkGray] = xcolor.pixel;
541 
542  // Couleur LIGHT RED.
543  xcolor.pad = 0;
544  xcolor.red = 256 * 255;
545  xcolor.green = 256 * 140;
546  xcolor.blue = 256 * 140;
547  XAllocColor ( display, lut, &xcolor );
548  x_color[vpColor::id_lightRed] = xcolor.pixel;
549 
550  // Couleur RED.
551  xcolor.pad = 0;
552  xcolor.red = 256* 255;
553  xcolor.green = 0;
554  xcolor.blue = 0;
555  XAllocColor ( display, lut, &xcolor );
556  x_color[vpColor::id_red] = xcolor.pixel;
557 
558  // Couleur DARK RED.
559  xcolor.pad = 0;
560  xcolor.red = 256* 128;
561  xcolor.green = 0;
562  xcolor.blue = 0;
563  XAllocColor ( display, lut, &xcolor );
564  x_color[vpColor::id_darkRed] = xcolor.pixel;
565 
566  // Couleur LIGHT GREEN.
567  xcolor.pad = 0;
568  xcolor.red = 256 * 140;
569  xcolor.green = 256 * 255;
570  xcolor.blue = 256 * 140;
571  XAllocColor ( display, lut, &xcolor );
572  x_color[vpColor::id_lightGreen] = xcolor.pixel;
573 
574  // Couleur GREEN.
575  xcolor.pad = 0;
576  xcolor.red = 0;
577  xcolor.green = 256*255;
578  xcolor.blue = 0;
579  XAllocColor ( display, lut, &xcolor );
580  x_color[vpColor::id_green] = xcolor.pixel;
581 
582  // Couleur DARK GREEN.
583  xcolor.pad = 0;
584  xcolor.red = 0;
585  xcolor.green = 256* 128;
586  xcolor.blue = 0;
587  XAllocColor ( display, lut, &xcolor );
588  x_color[vpColor::id_darkGreen] = xcolor.pixel;
589 
590  // Couleur LIGHT Blue.
591  xcolor.pad = 0;
592  xcolor.red = 256 * 140;
593  xcolor.green = 256 * 140;
594  xcolor.blue = 256 * 255;
595  XAllocColor ( display, lut, &xcolor );
596  x_color[vpColor::id_lightBlue] = xcolor.pixel;
597 
598  // Couleur BLUE.
599  xcolor.pad = 0;
600  xcolor.red = 0;
601  xcolor.green = 0;
602  xcolor.blue = 256* 255;
603  XAllocColor ( display, lut, &xcolor );
604  x_color[vpColor::id_blue] = xcolor.pixel;
605 
606  // Couleur DARK BLUE.
607  xcolor.pad = 0;
608  xcolor.red = 0;
609  xcolor.green = 0;
610  xcolor.blue = 256* 128;
611  XAllocColor ( display, lut, &xcolor );
612  x_color[vpColor::id_darkBlue] = xcolor.pixel;
613 
614  // Couleur YELLOW.
615  xcolor.pad = 0;
616  xcolor.red = 256 * 255;
617  xcolor.green = 256 * 255;
618  xcolor.blue = 0;
619  XAllocColor ( display, lut, &xcolor );
620  x_color[vpColor::id_yellow] = xcolor.pixel;
621 
622  // Couleur ORANGE.
623  xcolor.pad = 0;
624  xcolor.red = 256 * 255;
625  xcolor.green = 256 * 165;
626  xcolor.blue = 0;
627  XAllocColor ( display, lut, &xcolor );
628  x_color[vpColor::id_orange] = xcolor.pixel;
629 
630  // Couleur CYAN.
631  xcolor.pad = 0;
632  xcolor.red = 0;
633  xcolor.green = 256 * 255;
634  xcolor.blue = 256 * 255;
635  XAllocColor ( display, lut, &xcolor );
636  x_color[vpColor::id_cyan] = xcolor.pixel;
637 
638  // Couleur PURPLE.
639  xcolor.pad = 0;
640  xcolor.red = 256 * 128;
641  xcolor.green = 0;
642  xcolor.blue = 256 * 128;
643  XAllocColor ( display, lut, &xcolor );
644  x_color[vpColor::id_purple] = xcolor.pixel;
645  break;
646  }
647  }
648 
649  XSetStandardProperties ( display, window, this->m_title.c_str(), this->m_title.c_str(), None, 0, 0, &hints );
650  XMapWindow ( display, window ) ;
651  // Selection des evenements.
652  XSelectInput ( display, window,
653  ExposureMask |
654  ButtonPressMask | ButtonReleaseMask |
655  KeyPressMask | KeyReleaseMask |
656  StructureNotifyMask |
657  PointerMotionMask);
658 
659  // graphic context creation
660  values.plane_mask = AllPlanes;
661  values.fill_style = FillSolid;
662  values.foreground = WhitePixel ( display, screen );
663  values.background = BlackPixel ( display, screen );
664  context = XCreateGC ( display, window,
665  GCPlaneMask | GCFillStyle | GCForeground | GCBackground,
666  &values );
667 
668  if ( context == NULL )
669  {
670  vpERROR_TRACE ( "Can't create graphics context." );
672  "Can't create graphics context" ) ) ;
673 
674  }
675 
676  // Pixmap creation.
677  pixmap = XCreatePixmap ( display, window, m_width, m_height, screen_depth );
678 
679  // Hangs when forward X11 is used to send the display to an other computer
680  // do
681  // XNextEvent ( display, &event );
682  // while ( event.xany.type != Expose );
683 
684  {
685  Ximage = XCreateImage ( display, DefaultVisual ( display, screen ),
686  screen_depth, ZPixmap, 0, NULL,
687  m_width, m_height, XBitmapPad ( display ), 0 );
688 
689  Ximage->data = ( char * ) malloc ( m_height * (unsigned int)Ximage->bytes_per_line );
690  ximage_data_init = true;
691 
692  }
694 
695  XStoreName ( display, window, m_title.c_str() );
696 
697  XSync ( display, 1 );
698 
699  I.display = this ;
700 }
701 
711 void
712 vpDisplayX::init ( vpImage<vpRGBa> &I, int x, int y, const std::string &title)
713 {
715 
716  XSizeHints hints;
717  if (x != -1)
718  m_windowXPosition = x ;
719  if (y != -1)
720  m_windowYPosition = y ;
721 
722  if (x_color == NULL) {
723  // id_unknown = number of predefined colors
724  x_color= new unsigned long [vpColor::id_unknown];
725  }
726 
727  if (! title.empty())
728  m_title = title;
729 
730  // Positionnement de la fenetre dans l'ecran.
731  if ( ( m_windowXPosition < 0 ) || ( m_windowYPosition < 0 ) )
732  {
733  hints.flags = 0;
734  }
735  else
736  {
737  hints.flags = USPosition;
738  hints.x = m_windowXPosition;
739  hints.y = m_windowYPosition;
740  }
741 
742  // setup X11 --------------------------------------------------
743  m_width = I.getWidth() / m_scale;
744  m_height = I.getHeight() / m_scale;
745 
746  if ( ( display = XOpenDisplay ( NULL ) ) == NULL )
747  {
748  vpERROR_TRACE ( "Can't connect display on server %s.\n", XDisplayName ( NULL ) );
750  "Can't connect display on server." ) ) ;
751  }
752 
753  screen = DefaultScreen ( display );
754  lut = DefaultColormap ( display, screen );
755  screen_depth = (unsigned int)DefaultDepth ( display, screen );
756 
757  vpDEBUG_TRACE ( 1, "Screen depth: %d\n", screen_depth );
758 
759  if ( ( window = XCreateSimpleWindow ( display, RootWindow ( display, screen ),
761  m_width, m_height, 1,
762  BlackPixel ( display, screen ),
763  WhitePixel ( display, screen ) ) ) == 0 )
764  {
765  vpERROR_TRACE ( "Can't create window." );
767  "Can't create window." ) ) ;
768  }
769 
770  //
771  // Create color table for 8 and 16 bits screen
772  //
773  if ( screen_depth == 8 )
774  {
775  lut = XCreateColormap ( display, window,
776  DefaultVisual ( display, screen ), AllocAll ) ;
777  xcolor.flags = DoRed | DoGreen | DoBlue ;
778 
779  for ( unsigned int i = 0 ; i < 256 ; i++ )
780  {
781  xcolor.pixel = i ;
782  xcolor.red = 256 * i;
783  xcolor.green = 256 * i;
784  xcolor.blue = 256 * i;
785  XStoreColor ( display, lut, &xcolor );
786  }
787 
788  XSetWindowColormap ( display, window, lut ) ;
789  XInstallColormap ( display, lut ) ;
790  }
791 
792  else if ( screen_depth == 16 )
793  {
794  for ( unsigned int i = 0; i < 256; i ++ )
795  {
796  xcolor.pad = 0;
797  xcolor.red = xcolor.green = xcolor.blue = 256 * i;
798  if ( XAllocColor ( display, lut, &xcolor ) == 0 )
799  {
800  vpERROR_TRACE ( "Can't allocate 256 colors. Only %d allocated.", i );
802  "Can't allocate 256 colors." ) ) ;
803  }
804  colortable[i] = xcolor.pixel;
805  }
806 
807  Visual *visual = DefaultVisual (display, screen);
808  RMask = visual->red_mask;
809  GMask = visual->green_mask;
810  BMask = visual->blue_mask;
811 
812  RShift = 15 - getMsb(RMask); /* these are right-shifts */
813  GShift = 15 - getMsb(GMask);
814  BShift = 15 - getMsb(BMask);
815 
816  XSetWindowColormap ( display, window, lut ) ;
817  XInstallColormap ( display, lut ) ;
818  }
819 
820 
821  //
822  // Create colors for overlay
823  //
824  switch ( screen_depth )
825  {
826 
827  case 8:
828  // Color BLACK and WHITE are set properly.
829 
830  // Color LIGHT GRAY.
831  x_color[vpColor::id_lightGray] = 254;
832  xcolor.pixel = x_color[vpColor::id_lightGray] ;
833  xcolor.red = 256 * 192;
834  xcolor.green = 256 * 192;
835  xcolor.blue = 256 * 192;
836  XStoreColor ( display, lut, &xcolor );
837 
838  // Color GRAY.
839  x_color[vpColor::id_gray] = 253;
840  xcolor.pixel = x_color[vpColor::id_gray] ;
841  xcolor.red = 256 * 128;
842  xcolor.green = 256 * 128;
843  xcolor.blue = 256 * 128;
844  XStoreColor ( display, lut, &xcolor );
845 
846  // Color DARK GRAY.
847  x_color[vpColor::id_darkGray] = 252;
848  xcolor.pixel = x_color[vpColor::id_darkGray] ;
849  xcolor.red = 256 * 64;
850  xcolor.green = 256 * 64;
851  xcolor.blue = 256 * 64;
852  XStoreColor ( display, lut, &xcolor );
853 
854  // Color LIGHT RED.
855  x_color[vpColor::id_lightRed] = 251;
856  xcolor.pixel = x_color[vpColor::id_lightRed] ;
857  xcolor.red = 256 * 255;
858  xcolor.green = 256 * 140;
859  xcolor.blue = 256 * 140;
860  XStoreColor ( display, lut, &xcolor );
861 
862  // Color RED.
863  x_color[vpColor::id_red] = 250;
864  xcolor.pixel = x_color[vpColor::id_red] ;
865  xcolor.red = 256 * 255;
866  xcolor.green = 0;
867  xcolor.blue = 0;
868  XStoreColor ( display, lut, &xcolor );
869 
870  // Color DARK RED.
871  x_color[vpColor::id_darkRed] = 249;
872  xcolor.pixel = x_color[vpColor::id_darkRed] ;
873  xcolor.red = 256 * 128;
874  xcolor.green = 0;
875  xcolor.blue = 0;
876  XStoreColor ( display, lut, &xcolor );
877 
878  // Color LIGHT GREEN.
879  x_color[vpColor::id_lightGreen] = 248;
880  xcolor.pixel = x_color[vpColor::id_lightGreen] ;
881  xcolor.red = 256 * 140;
882  xcolor.green = 256 * 255;
883  xcolor.blue = 256 * 140;
884  XStoreColor ( display, lut, &xcolor );
885 
886  // Color GREEN.
887  x_color[vpColor::id_green] = 247;
888  xcolor.pixel = x_color[vpColor::id_green];
889  xcolor.red = 0;
890  xcolor.green = 256 * 255;
891  xcolor.blue = 0;
892  XStoreColor ( display, lut, &xcolor );
893 
894  // Color DARK GREEN.
895  x_color[vpColor::id_darkGreen] = 246;
896  xcolor.pixel = x_color[vpColor::id_darkGreen] ;
897  xcolor.red = 0;
898  xcolor.green = 256 * 128;
899  xcolor.blue = 0;
900  XStoreColor ( display, lut, &xcolor );
901 
902  // Color LIGHT BLUE.
903  x_color[vpColor::id_lightBlue] = 245;
904  xcolor.pixel = x_color[vpColor::id_lightBlue] ;
905  xcolor.red = 256 * 140;
906  xcolor.green = 256 * 140;
907  xcolor.blue = 256 * 255;
908  XStoreColor ( display, lut, &xcolor );
909 
910  // Color BLUE.
911  x_color[vpColor::id_blue] = 244;
912  xcolor.pixel = x_color[vpColor::id_blue];
913  xcolor.red = 0;
914  xcolor.green = 0;
915  xcolor.blue = 256 * 255;
916  XStoreColor ( display, lut, &xcolor );
917 
918  // Color DARK BLUE.
919  x_color[vpColor::id_darkBlue] = 243;
920  xcolor.pixel = x_color[vpColor::id_darkBlue] ;
921  xcolor.red = 0;
922  xcolor.green = 0;
923  xcolor.blue = 256 * 128;
924  XStoreColor ( display, lut, &xcolor );
925 
926  // Color YELLOW.
927  x_color[vpColor::id_yellow] = 242;
928  xcolor.pixel = x_color[vpColor::id_yellow];
929  xcolor.red = 256 * 255;
930  xcolor.green = 256 * 255;
931  xcolor.blue = 0;
932  XStoreColor ( display, lut, &xcolor );
933 
934  // Color ORANGE.
935  x_color[vpColor::id_orange] = 241;
936  xcolor.pixel = x_color[vpColor::id_orange];
937  xcolor.red = 256 * 255;
938  xcolor.green = 256 * 165;
939  xcolor.blue = 0;
940  XStoreColor ( display, lut, &xcolor );
941 
942  // Color CYAN.
943  x_color[vpColor::id_cyan] = 240;
944  xcolor.pixel = x_color[vpColor::id_cyan];
945  xcolor.red = 0;
946  xcolor.green = 256 * 255;
947  xcolor.blue = 256 * 255;
948  XStoreColor ( display, lut, &xcolor );
949 
950  // Color PURPLE.
951  x_color[vpColor::id_purple] = 239;
952  xcolor.pixel = x_color[vpColor::id_purple];
953  xcolor.red = 256 * 128;
954  xcolor.green = 0;
955  xcolor.blue = 256 * 128;
956  XStoreColor ( display, lut, &xcolor );
957 
958  break;
959 
960  case 16:
961  case 24:
962  {
963  xcolor.flags = DoRed | DoGreen | DoBlue ;
964 
965  // Couleur BLACK.
966  xcolor.pad = 0;
967  xcolor.red = 0;
968  xcolor.green = 0;
969  xcolor.blue = 0;
970  XAllocColor ( display, lut, &xcolor );
971  x_color[vpColor::id_black] = xcolor.pixel;
972 
973  // Couleur WHITE.
974  xcolor.pad = 0;
975  xcolor.red = 256* 255;
976  xcolor.green = 256* 255;
977  xcolor.blue = 256* 255;
978  XAllocColor ( display, lut, &xcolor );
979  x_color[vpColor::id_white] = xcolor.pixel;
980 
981  // Couleur LIGHT GRAY.
982  xcolor.pad = 0;
983  xcolor.red = 256 * 192;
984  xcolor.green = 256 * 192;
985  xcolor.blue = 256 * 192;
986  XAllocColor ( display, lut, &xcolor );
987  x_color[vpColor::id_lightGray] = xcolor.pixel;
988 
989  // Couleur GRAY.
990  xcolor.pad = 0;
991  xcolor.red = 256 * 128;
992  xcolor.green = 256 * 128;
993  xcolor.blue = 256 * 128;
994  XAllocColor ( display, lut, &xcolor );
995  x_color[vpColor::id_gray] = xcolor.pixel;
996 
997  // Couleur DARK GRAY.
998  xcolor.pad = 0;
999  xcolor.red = 256 * 64;
1000  xcolor.green = 256 * 64;
1001  xcolor.blue = 256 * 64;
1002  XAllocColor ( display, lut, &xcolor );
1003  x_color[vpColor::id_darkGray] = xcolor.pixel;
1004 
1005  // Couleur LIGHT RED.
1006  xcolor.pad = 0;
1007  xcolor.red = 256 * 255;
1008  xcolor.green = 256 * 140;
1009  xcolor.blue = 256 * 140;
1010  XAllocColor ( display, lut, &xcolor );
1011  x_color[vpColor::id_lightRed] = xcolor.pixel;
1012 
1013  // Couleur RED.
1014  xcolor.pad = 0;
1015  xcolor.red = 256* 255;
1016  xcolor.green = 0;
1017  xcolor.blue = 0;
1018  XAllocColor ( display, lut, &xcolor );
1019  x_color[vpColor::id_red] = xcolor.pixel;
1020 
1021  // Couleur DARK RED.
1022  xcolor.pad = 0;
1023  xcolor.red = 256* 128;
1024  xcolor.green = 0;
1025  xcolor.blue = 0;
1026  XAllocColor ( display, lut, &xcolor );
1027  x_color[vpColor::id_darkRed] = xcolor.pixel;
1028 
1029  // Couleur LIGHT GREEN.
1030  xcolor.pad = 0;
1031  xcolor.red = 256 * 140;
1032  xcolor.green = 256 * 255;
1033  xcolor.blue = 256 * 140;
1034  XAllocColor ( display, lut, &xcolor );
1035  x_color[vpColor::id_lightGreen] = xcolor.pixel;
1036 
1037  // Couleur GREEN.
1038  xcolor.pad = 0;
1039  xcolor.red = 0;
1040  xcolor.green = 256*255;
1041  xcolor.blue = 0;
1042  XAllocColor ( display, lut, &xcolor );
1043  x_color[vpColor::id_green] = xcolor.pixel;
1044 
1045  // Couleur DARK GREEN.
1046  xcolor.pad = 0;
1047  xcolor.red = 0;
1048  xcolor.green = 256* 128;
1049  xcolor.blue = 0;
1050  XAllocColor ( display, lut, &xcolor );
1051  x_color[vpColor::id_darkGreen] = xcolor.pixel;
1052 
1053  // Couleur LIGHT Blue.
1054  xcolor.pad = 0;
1055  xcolor.red = 256 * 140;
1056  xcolor.green = 256 * 140;
1057  xcolor.blue = 256 * 255;
1058  XAllocColor ( display, lut, &xcolor );
1059  x_color[vpColor::id_lightBlue] = xcolor.pixel;
1060 
1061  // Couleur BLUE.
1062  xcolor.pad = 0;
1063  xcolor.red = 0;
1064  xcolor.green = 0;
1065  xcolor.blue = 256* 255;
1066  XAllocColor ( display, lut, &xcolor );
1067  x_color[vpColor::id_blue] = xcolor.pixel;
1068 
1069  // Couleur DARK BLUE.
1070  xcolor.pad = 0;
1071  xcolor.red = 0;
1072  xcolor.green = 0;
1073  xcolor.blue = 256* 128;
1074  XAllocColor ( display, lut, &xcolor );
1075  x_color[vpColor::id_darkBlue] = xcolor.pixel;
1076 
1077  // Couleur YELLOW.
1078  xcolor.pad = 0;
1079  xcolor.red = 256 * 255;
1080  xcolor.green = 256 * 255;
1081  xcolor.blue = 0;
1082  XAllocColor ( display, lut, &xcolor );
1083  x_color[vpColor::id_yellow] = xcolor.pixel;
1084 
1085  // Couleur ORANGE.
1086  xcolor.pad = 0;
1087  xcolor.red = 256 * 255;
1088  xcolor.green = 256 * 165;
1089  xcolor.blue = 0;
1090  XAllocColor ( display, lut, &xcolor );
1091  x_color[vpColor::id_orange] = xcolor.pixel;
1092 
1093  // Couleur CYAN.
1094  xcolor.pad = 0;
1095  xcolor.red = 0;
1096  xcolor.green = 256 * 255;
1097  xcolor.blue = 256 * 255;
1098  XAllocColor ( display, lut, &xcolor );
1099  x_color[vpColor::id_cyan] = xcolor.pixel;
1100 
1101  // Couleur PURPLE.
1102  xcolor.pad = 0;
1103  xcolor.red = 256 * 128;
1104  xcolor.green = 0;
1105  xcolor.blue = 256 * 128;
1106  XAllocColor ( display, lut, &xcolor );
1107  x_color[vpColor::id_purple] = xcolor.pixel;
1108  break;
1109  }
1110  }
1111 
1112  XSetStandardProperties ( display, window, this->m_title.c_str(), this->m_title.c_str(), None, 0, 0, &hints );
1113  XMapWindow ( display, window ) ;
1114  // Selection des evenements.
1115  XSelectInput ( display, window,
1116  ExposureMask |
1117  ButtonPressMask | ButtonReleaseMask |
1118  KeyPressMask | KeyReleaseMask |
1119  StructureNotifyMask |
1120  PointerMotionMask);
1121 
1122  // Creation du contexte graphique
1123  values.plane_mask = AllPlanes;
1124  values.fill_style = FillSolid;
1125  values.foreground = WhitePixel ( display, screen );
1126  values.background = BlackPixel ( display, screen );
1127  context = XCreateGC ( display, window,
1128  GCPlaneMask | GCFillStyle | GCForeground | GCBackground,
1129  &values );
1130 
1131  if ( context == NULL )
1132  {
1133  vpERROR_TRACE ( "Can't create graphics context." );
1135  "Can't create graphics context" ) ) ;
1136  }
1137 
1138  // Pixmap creation.
1139  pixmap = XCreatePixmap ( display, window, m_width, m_height, screen_depth );
1140 
1141  // Hangs when forward X11 is used to send the display to an other computer
1142  // do
1143  // XNextEvent ( display, &event );
1144  // while ( event.xany.type != Expose );
1145 
1146  {
1147  Ximage = XCreateImage ( display, DefaultVisual ( display, screen ),
1148  screen_depth, ZPixmap, 0, NULL,
1149  m_width, m_height, XBitmapPad ( display ), 0 );
1150 
1151 
1152  Ximage->data = ( char * ) malloc ( m_height * (unsigned int)Ximage->bytes_per_line );
1153  ximage_data_init = true;
1154 
1155  }
1157 
1158  XSync ( display, true );
1159 
1160  XStoreName ( display, window, m_title.c_str() );
1161 
1162  I.display = this ;
1163 }
1164 
1172 void vpDisplayX::init ( unsigned int w, unsigned int h, int x, int y, const std::string &title)
1173 {
1174  setScale(m_scaleType, w, h);
1175 
1176  if (x_color == NULL) {
1177  // id_unknown = number of predefined colors
1178  x_color= new unsigned long [vpColor::id_unknown];
1179  }
1180  /* setup X11 ------------------------------------------------------------- */
1181  this->m_width = w / m_scale;
1182  this->m_height = h / m_scale;
1183 
1184  XSizeHints hints;
1185 
1186  if (x != -1)
1187  m_windowXPosition = x ;
1188  if (y != -1)
1189  m_windowYPosition = y ;
1190  // Positionnement de la fenetre dans l'ecran.
1191  if ( ( m_windowXPosition < 0 ) || ( m_windowYPosition < 0 ) )
1192  {
1193  hints.flags = 0;
1194  }
1195  else
1196  {
1197  hints.flags = USPosition;
1198  hints.x = m_windowXPosition;
1199  hints.y = m_windowYPosition;
1200  }
1201 
1202  m_title = title;
1203 
1204  if ( ( display = XOpenDisplay ( NULL ) ) == NULL )
1205  {
1206  vpERROR_TRACE ( "Can't connect display on server %s.\n", XDisplayName ( NULL ) );
1208  "Can't connect display on server." ) ) ;
1209  }
1210 
1211  screen = DefaultScreen ( display );
1212  lut = DefaultColormap ( display, screen );
1213  screen_depth = (unsigned int)DefaultDepth ( display, screen );
1214 
1215  vpTRACE ( "Screen depth: %d\n", screen_depth );
1216 
1217  if ( ( window = XCreateSimpleWindow ( display, RootWindow ( display, screen ),
1219  m_width, m_height, 1,
1220  BlackPixel ( display, screen ),
1221  WhitePixel ( display, screen ) ) ) == 0 )
1222  {
1223  vpERROR_TRACE ( "Can't create window." );
1225  "Can't create window." ) ) ;
1226  }
1227 
1228 
1229  //
1230  // Create color table for 8 and 16 bits screen
1231  //
1232  if ( screen_depth == 8 )
1233  {
1234  lut = XCreateColormap ( display, window,
1235  DefaultVisual ( display, screen ), AllocAll ) ;
1236  xcolor.flags = DoRed | DoGreen | DoBlue ;
1237 
1238  for ( unsigned int i = 0 ; i < 256 ; i++ )
1239  {
1240  xcolor.pixel = i ;
1241  xcolor.red = 256 * i;
1242  xcolor.green = 256 * i;
1243  xcolor.blue = 256 * i;
1244  XStoreColor ( display, lut, &xcolor );
1245  }
1246 
1247  XSetWindowColormap ( display, window, lut ) ;
1248  XInstallColormap ( display, lut ) ;
1249  }
1250 
1251  else if ( screen_depth == 16 )
1252  {
1253  for ( unsigned int i = 0; i < 256; i ++ )
1254  {
1255  xcolor.pad = 0;
1256  xcolor.red = xcolor.green = xcolor.blue = 256 * i;
1257  if ( XAllocColor ( display, lut, &xcolor ) == 0 )
1258  {
1259  vpERROR_TRACE ( "Can't allocate 256 colors. Only %d allocated.", i );
1261  "Can't allocate 256 colors." ) ) ;
1262  }
1263  colortable[i] = xcolor.pixel;
1264  }
1265 
1266  XSetWindowColormap ( display, window, lut ) ;
1267  XInstallColormap ( display, lut ) ;
1268 
1269  Visual *visual = DefaultVisual (display, screen);
1270  RMask = visual->red_mask;
1271  GMask = visual->green_mask;
1272  BMask = visual->blue_mask;
1273 
1274  RShift = 15 - getMsb(RMask); /* these are right-shifts */
1275  GShift = 15 - getMsb(GMask);
1276  BShift = 15 - getMsb(BMask);
1277  }
1278 
1279  vpColor pcolor; // predefined colors
1280 
1281  //
1282  // Create colors for overlay
1283  //
1284  switch ( screen_depth )
1285  {
1286 
1287  case 8:
1288  // Color BLACK: default set to 0
1289 
1290  // Color WHITE: default set to 255
1291 
1292  // Color LIGHT GRAY.
1293  pcolor = vpColor::lightGray;
1294  xcolor.pixel = 254 ; // affected to 254
1295  xcolor.red = 256 * pcolor.R;
1296  xcolor.green = 256 * pcolor.G;
1297  xcolor.blue = 256 * pcolor.B;
1298  XStoreColor ( display, lut, &xcolor );
1299 
1300  // Color GRAY.
1301  pcolor = vpColor::gray;
1302  xcolor.pixel = 253 ; // affected to 253
1303  xcolor.red = 256 * pcolor.R;
1304  xcolor.green = 256 * pcolor.G;
1305  xcolor.blue = 256 * pcolor.B;
1306  XStoreColor ( display, lut, &xcolor );
1307 
1308  // Color DARK GRAY.
1309  pcolor = vpColor::darkGray;
1310  xcolor.pixel = 252 ; // affected to 252
1311  xcolor.red = 256 * pcolor.R;
1312  xcolor.green = 256 * pcolor.G;
1313  xcolor.blue = 256 * pcolor.B;
1314  XStoreColor ( display, lut, &xcolor );
1315 
1316  // Color LIGHT RED.
1317  pcolor = vpColor::lightRed;
1318  xcolor.pixel = 251 ; // affected to 251
1319  xcolor.red = 256 * pcolor.R;
1320  xcolor.green = 256 * pcolor.G;
1321  xcolor.blue = 256 * pcolor.B;
1322  XStoreColor ( display, lut, &xcolor );
1323 
1324  // Color RED.
1325  pcolor = vpColor::red;
1326  xcolor.pixel = 250 ; // affected to 250
1327  xcolor.red = 256 * pcolor.R;
1328  xcolor.green = 256 * pcolor.G;
1329  xcolor.blue = 256 * pcolor.B;
1330  XStoreColor ( display, lut, &xcolor );
1331 
1332  // Color DARK RED.
1333  pcolor = vpColor::darkRed;
1334  xcolor.pixel = 249 ; // affected to 249
1335  xcolor.red = 256 * pcolor.R;
1336  xcolor.green = 256 * pcolor.G;
1337  xcolor.blue = 256 * pcolor.B;
1338  XStoreColor ( display, lut, &xcolor );
1339 
1340  // Color LIGHT GREEN.
1341  pcolor = vpColor::lightGreen;
1342  xcolor.pixel = 248 ; // affected to 248
1343  xcolor.red = 256 * pcolor.R;
1344  xcolor.green = 256 * pcolor.G;
1345  xcolor.blue = 256 * pcolor.B;
1346  XStoreColor ( display, lut, &xcolor );
1347 
1348  // Color GREEN.
1349  pcolor = vpColor::green;
1350  xcolor.pixel = 247; // affected to 247
1351  xcolor.red = 256 * pcolor.R;
1352  xcolor.green = 256 * pcolor.G;
1353  xcolor.blue = 256 * pcolor.B;
1354  XStoreColor ( display, lut, &xcolor );
1355 
1356  // Color DARK GREEN.
1357  pcolor = vpColor::darkGreen;
1358  xcolor.pixel = 246 ; // affected to 246
1359  xcolor.red = 256 * pcolor.R;
1360  xcolor.green = 256 * pcolor.G;
1361  xcolor.blue = 256 * pcolor.B;
1362  XStoreColor ( display, lut, &xcolor );
1363 
1364  // Color LIGHT BLUE.
1365  pcolor = vpColor::lightBlue;
1366  xcolor.pixel = 245 ; // affected to 245
1367  xcolor.red = 256 * pcolor.R;
1368  xcolor.green = 256 * pcolor.G;
1369  xcolor.blue = 256 * pcolor.B;
1370  XStoreColor ( display, lut, &xcolor );
1371 
1372  // Color BLUE.
1373  pcolor = vpColor::blue;
1374  xcolor.pixel = 244; // affected to 244
1375  xcolor.red = 256 * pcolor.R;
1376  xcolor.green = 256 * pcolor.G;
1377  xcolor.blue = 256 * pcolor.B;
1378  XStoreColor ( display, lut, &xcolor );
1379 
1380  // Color DARK BLUE.
1381  pcolor = vpColor::darkBlue;
1382  xcolor.pixel = 243 ; // affected to 243
1383  xcolor.red = 256 * pcolor.R;
1384  xcolor.green = 256 * pcolor.G;
1385  xcolor.blue = 256 * pcolor.B;
1386  XStoreColor ( display, lut, &xcolor );
1387 
1388  // Color YELLOW.
1389  pcolor = vpColor::yellow;
1390  xcolor.pixel = 242; // affected to 242
1391  xcolor.red = 256 * pcolor.R;
1392  xcolor.green = 256 * pcolor.G;
1393  xcolor.blue = 256 * pcolor.B;
1394  XStoreColor ( display, lut, &xcolor );
1395 
1396  // Color ORANGE.
1397  pcolor = vpColor::orange;
1398  xcolor.pixel = 241; // affected to 241
1399  xcolor.red = 256 * pcolor.R;
1400  xcolor.green = 256 * pcolor.G;
1401  xcolor.blue = 256 * pcolor.B;
1402  XStoreColor ( display, lut, &xcolor );
1403 
1404  // Color CYAN.
1405  pcolor = vpColor::cyan;
1406  xcolor.pixel = 240; // affected to 240
1407  xcolor.red = 256 * pcolor.R;
1408  xcolor.green = 256 * pcolor.G;
1409  xcolor.blue = 256 * pcolor.B;
1410  XStoreColor ( display, lut, &xcolor );
1411 
1412  // Color PURPLE.
1413  pcolor = vpColor::purple;
1414  xcolor.pixel = 239; // affected to 239
1415  xcolor.red = 256 * pcolor.R;
1416  xcolor.green = 256 * pcolor.G;
1417  xcolor.blue = 256 * pcolor.B;
1418  XStoreColor ( display, lut, &xcolor );
1419 
1420  break;
1421 
1422  case 16:
1423  case 24:
1424  {
1425  xcolor.flags = DoRed | DoGreen | DoBlue ;
1426 
1427  // Couleur BLACK.
1428  pcolor = vpColor::black;
1429  xcolor.pad = 0;
1430  xcolor.red = 256 * pcolor.R;
1431  xcolor.green = 256 * pcolor.G;
1432  xcolor.blue = 256 * pcolor.B;
1433  XAllocColor ( display, lut, &xcolor );
1434  x_color[vpColor::id_black] = xcolor.pixel;
1435 
1436  // Color WHITE.
1437  pcolor = vpColor::white;
1438  xcolor.pad = 0;
1439  xcolor.red = 256 * pcolor.R;
1440  xcolor.green = 256 * pcolor.G;
1441  xcolor.blue = 256 * pcolor.B;
1442  XAllocColor ( display, lut, &xcolor );
1443  x_color[vpColor::id_white] = xcolor.pixel;
1444 
1445  // Color LIGHT GRAY.
1446  pcolor = vpColor::lightGray;
1447  xcolor.pad = 0;
1448  xcolor.red = 256 * pcolor.R;
1449  xcolor.green = 256 * pcolor.G;
1450  xcolor.blue = 256 * pcolor.B;
1451  XAllocColor ( display, lut, &xcolor );
1452  x_color[vpColor::id_lightGray] = xcolor.pixel;
1453 
1454  // Color GRAY.
1455  pcolor = vpColor::gray;
1456  xcolor.pad = 0;
1457  xcolor.red = 256 * pcolor.R;
1458  xcolor.green = 256 * pcolor.G;
1459  xcolor.blue = 256 * pcolor.B;
1460  XAllocColor ( display, lut, &xcolor );
1461  x_color[vpColor::id_gray] = xcolor.pixel;
1462 
1463  // Color DARK GRAY.
1464  pcolor = vpColor::darkGray;
1465  xcolor.pad = 0;
1466  xcolor.red = 256 * pcolor.R;
1467  xcolor.green = 256 * pcolor.G;
1468  xcolor.blue = 256 * pcolor.B;
1469  XAllocColor ( display, lut, &xcolor );
1470  x_color[vpColor::id_darkGray] = xcolor.pixel;
1471 
1472  // Color LIGHT RED.
1473  pcolor = vpColor::lightRed;
1474  xcolor.pad = 0;
1475  xcolor.red = 256 * pcolor.R;
1476  xcolor.green = 256 * pcolor.G;
1477  xcolor.blue = 256 * pcolor.B;
1478  XAllocColor ( display, lut, &xcolor );
1479  x_color[vpColor::id_lightRed] = xcolor.pixel;
1480 
1481  // Color RED.
1482  pcolor = vpColor::red;
1483  xcolor.pad = 0;
1484  xcolor.red = 256 * pcolor.R;
1485  xcolor.green = 256 * pcolor.G;
1486  xcolor.blue = 256 * pcolor.B;
1487  XAllocColor ( display, lut, &xcolor );
1488  x_color[vpColor::id_red] = xcolor.pixel;
1489 
1490  // Color DARK RED.
1491  pcolor = vpColor::darkRed;
1492  xcolor.pad = 0;
1493  xcolor.red = 256 * pcolor.R;
1494  xcolor.green = 256 * pcolor.G;
1495  xcolor.blue = 256 * pcolor.B;
1496  XAllocColor ( display, lut, &xcolor );
1497  x_color[vpColor::id_darkRed] = xcolor.pixel;
1498 
1499  // Color LIGHT GREEN.
1500  pcolor = vpColor::lightGreen;
1501  xcolor.pad = 0;
1502  xcolor.red = 256 * pcolor.R;
1503  xcolor.green = 256 * pcolor.G;
1504  xcolor.blue = 256 * pcolor.B;
1505  XAllocColor ( display, lut, &xcolor );
1506  x_color[vpColor::id_lightGreen] = xcolor.pixel;
1507 
1508  // Color GREEN.
1509  pcolor = vpColor::green;
1510  xcolor.pad = 0;
1511  xcolor.red = 256 * pcolor.R;
1512  xcolor.green = 256 * pcolor.G;
1513  xcolor.blue = 256 * pcolor.B;
1514  XAllocColor ( display, lut, &xcolor );
1515  x_color[vpColor::id_green] = xcolor.pixel;
1516 
1517  // Color DARK GREEN.
1518  pcolor = vpColor::darkGreen;
1519  xcolor.pad = 0;
1520  xcolor.red = 256 * pcolor.R;
1521  xcolor.green = 256 * pcolor.G;
1522  xcolor.blue = 256 * pcolor.B;
1523  XAllocColor ( display, lut, &xcolor );
1524  x_color[vpColor::id_darkGreen] = xcolor.pixel;
1525 
1526  // Color LIGHT BLUE.
1527  pcolor = vpColor::lightBlue;
1528  xcolor.pad = 0;
1529  xcolor.red = 256 * pcolor.R;
1530  xcolor.green = 256 * pcolor.G;
1531  xcolor.blue = 256 * pcolor.B;
1532  XAllocColor ( display, lut, &xcolor );
1533  x_color[vpColor::id_lightBlue] = xcolor.pixel;
1534 
1535  // Color BLUE.
1536  pcolor = vpColor::blue;
1537  xcolor.pad = 0;
1538  xcolor.red = 256 * pcolor.R;
1539  xcolor.green = 256 * pcolor.G;
1540  xcolor.blue = 256 * pcolor.B;
1541  XAllocColor ( display, lut, &xcolor );
1542  x_color[vpColor::id_blue] = xcolor.pixel;
1543 
1544  // Color DARK BLUE.
1545  pcolor = vpColor::darkBlue;
1546  xcolor.pad = 0;
1547  xcolor.red = 256 * pcolor.R;
1548  xcolor.green = 256 * pcolor.G;
1549  xcolor.blue = 256 * pcolor.B;
1550  XAllocColor ( display, lut, &xcolor );
1551  x_color[vpColor::id_darkBlue] = xcolor.pixel;
1552 
1553  // Color YELLOW.
1554  pcolor = vpColor::yellow;
1555  xcolor.pad = 0;
1556  xcolor.red = 256 * pcolor.R;
1557  xcolor.green = 256 * pcolor.G;
1558  xcolor.blue = 256 * pcolor.B;
1559  XAllocColor ( display, lut, &xcolor );
1560  x_color[vpColor::id_yellow] = xcolor.pixel;
1561 
1562  // Color ORANGE.
1563  pcolor = vpColor::orange;
1564  xcolor.pad = 0;
1565  xcolor.red = 256 * pcolor.R;
1566  xcolor.green = 256 * pcolor.G;
1567  xcolor.blue = 256 * pcolor.B;
1568  XAllocColor ( display, lut, &xcolor );
1569  x_color[vpColor::id_orange] = xcolor.pixel;
1570 
1571  // Color CYAN.
1572  pcolor = vpColor::cyan;
1573  xcolor.pad = 0;
1574  xcolor.red = 256 * pcolor.R;
1575  xcolor.green = 256 * pcolor.G;
1576  xcolor.blue = 256 * pcolor.B;
1577  XAllocColor ( display, lut, &xcolor );
1578  x_color[vpColor::id_cyan] = xcolor.pixel;
1579 
1580  // Color PURPLE.
1581  pcolor = vpColor::purple;
1582  xcolor.pad = 0;
1583  xcolor.red = 256 * pcolor.R;
1584  xcolor.green = 256 * pcolor.G;
1585  xcolor.blue = 256 * pcolor.B;
1586  XAllocColor ( display, lut, &xcolor );
1587  x_color[vpColor::id_purple] = xcolor.pixel;
1588  break;
1589  }
1590  }
1591 
1592  XSetStandardProperties ( display, window, this->m_title.c_str(), this->m_title.c_str(), None, 0, 0, &hints );
1593  XMapWindow ( display, window ) ;
1594  // Selection des evenements.
1595  XSelectInput ( display, window,
1596  ExposureMask |
1597  ButtonPressMask | ButtonReleaseMask |
1598  KeyPressMask | KeyReleaseMask |
1599  StructureNotifyMask |
1600  PointerMotionMask);
1601 
1602  /* Creation du contexte graphique */
1603  values.plane_mask = AllPlanes;
1604  values.fill_style = FillSolid;
1605  values.foreground = WhitePixel ( display, screen );
1606  values.background = BlackPixel ( display, screen );
1607  context = XCreateGC ( display, window,
1608  GCPlaneMask | GCFillStyle | GCForeground | GCBackground,
1609  &values );
1610 
1611  if ( context == NULL )
1612  {
1613  vpERROR_TRACE ( "Can't create graphics context." );
1615  "Can't create graphics context" ) ) ;
1616  }
1617 
1618  // Pixmap creation.
1619  pixmap = XCreatePixmap ( display, window, m_width, m_height, screen_depth );
1620 
1621  // Hangs when forward X11 is used to send the display to an other computer
1622  // do
1623  // XNextEvent ( display, &event );
1624  // while ( event.xany.type != Expose );
1625 
1626  {
1627  Ximage = XCreateImage ( display, DefaultVisual ( display, screen ),
1628  screen_depth, ZPixmap, 0, NULL,
1629  m_width, m_height, XBitmapPad ( display ), 0 );
1630 
1631  Ximage->data = ( char * ) malloc ( m_height * (unsigned int)Ximage->bytes_per_line );
1632  ximage_data_init = true;
1633  }
1635 
1636  XSync ( display, true );
1637 
1638  XStoreName ( display, window, m_title.c_str() );
1639 }
1640 
1655 void vpDisplayX::setFont( const std::string &font )
1656 {
1658  {
1659  if (!font.empty())
1660  {
1661  try
1662  {
1663  Font stringfont;
1664  stringfont = XLoadFont (display, font.c_str()) ; //"-adobe-times-bold-r-normal--18*");
1665  XSetFont (display, context, stringfont);
1666  }
1667  catch(...)
1668  {
1670  }
1671  }
1672  }
1673  else
1674  {
1676  "X not initialized" ) ) ;
1677  }
1678 }
1679 
1684 void
1685 vpDisplayX::setTitle(const std::string &title)
1686 {
1688  {
1689  m_title = title;
1690  if(! title.empty())
1691  XStoreName ( display, window, m_title.c_str() );
1692  }
1693  else
1694  {
1696  "X not initialized" ) ) ;
1697  }
1698 }
1699 
1708 void vpDisplayX::setWindowPosition(int winx, int winy)
1709 {
1711  XMoveWindow(display, window, winx, winy);
1712  }
1713  else
1714  {
1716  "X not initialized" ) ) ;
1717  }
1718 }
1719 
1732 {
1734  {
1735  switch ( screen_depth )
1736  {
1737  case 8:
1738  {
1739  // Correction de l'image de facon a liberer les niveaux de gris
1740  // ROUGE, VERT, BLEU, JAUNE
1741  unsigned char nivGrisMax = 255 - vpColor::id_unknown;
1742  if (m_scale == 1) {
1743  unsigned char *src_8 = ( unsigned char * ) I.bitmap;
1744  unsigned char *dst_8 = ( unsigned char * ) Ximage->data;
1745  unsigned int i = 0;
1746  unsigned int size = m_width * m_height;
1747 
1748  while ( i < size )
1749  {
1750  unsigned char nivGris = src_8[i] ;
1751  if ( nivGris > nivGrisMax )
1752  dst_8[i] = 255;
1753  else
1754  dst_8[i] = nivGris;
1755  i++ ;
1756  }
1757  }
1758  else {
1759  // Correction de l'image de facon a liberer les niveaux de gris
1760  // ROUGE, VERT, BLEU, JAUNE
1761  unsigned char *dst_8 = ( unsigned char * ) Ximage->data;
1762  unsigned int k = 0;
1763  for (unsigned int i=0; i<m_height; i++) {
1764  for (unsigned int j=0; j<m_width; j++) {
1765  unsigned char nivGris = I[i*m_scale][j*m_scale];
1766  if ( nivGris > nivGrisMax )
1767  dst_8[k++] = 255;
1768  else
1769  dst_8[k++] = nivGris;
1770  }
1771  }
1772  }
1773 
1774  // Affichage de l'image dans la Pixmap.
1775  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, m_width, m_height );
1776  XSetWindowBackgroundPixmap ( display, window, pixmap );
1777  break;
1778  }
1779  case 16:
1780  {
1781  unsigned int bytes_per_line = (unsigned int)Ximage->bytes_per_line;
1782  if(m_scale == 1) {
1783  for ( unsigned int i = 0; i < m_height ; i++ ) {
1784  unsigned char *dst_8 = (unsigned char*) Ximage->data + i * bytes_per_line;
1785  unsigned short *dst_16 = (unsigned short *) dst_8;
1786  for ( unsigned int j=0 ; j < m_width; j++ )
1787  {
1788  * ( dst_16 + j ) = ( unsigned short ) colortable[I[i][j]] ;
1789  }
1790  }
1791  }
1792  else {
1793  for ( unsigned int i = 0; i < m_height ; i++ ) {
1794  unsigned char *dst_8 = (unsigned char*) Ximage->data + i * bytes_per_line;
1795  unsigned short *dst_16 = (unsigned short *) dst_8;
1796  for ( unsigned int j=0 ; j < m_width; j++ )
1797  {
1798  * ( dst_16 + j ) = ( unsigned short ) colortable[I[i*m_scale][j*m_scale]] ;
1799  }
1800  }
1801  }
1802 
1803  // Affichage de l'image dans la Pixmap.
1804  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, m_width, m_height );
1805  XSetWindowBackgroundPixmap ( display, window, pixmap );
1806  break;
1807  }
1808 
1809  case 24:
1810  default:
1811  {
1812  unsigned char *dst_32 = ( unsigned char* ) Ximage->data;
1813  if (m_scale == 1) {
1814  unsigned int size_ = m_width * m_height ;
1815  unsigned char *bitmap = I.bitmap ;
1816  unsigned char *n = I.bitmap + size_;
1817  //for (unsigned int i = 0; i < size; i++) // suppression de l'iterateur i
1818  if (XImageByteOrder(display) == 1) {
1819  // big endian
1820  while ( bitmap < n )
1821  {
1822  unsigned char val = * ( bitmap++ );
1823  * ( dst_32 ++ ) = vpRGBa::alpha_default;
1824  * ( dst_32 ++ ) = val; // Red
1825  * ( dst_32 ++ ) = val; // Green
1826  * ( dst_32 ++ ) = val; // Blue
1827  }
1828  }
1829  else {
1830  // little endian
1831  while ( bitmap < n )
1832  {
1833  unsigned char val = * ( bitmap++ );
1834  * ( dst_32 ++ ) = val; // Blue
1835  * ( dst_32 ++ ) = val; // Green
1836  * ( dst_32 ++ ) = val; // Red
1837  * ( dst_32 ++ ) = vpRGBa::alpha_default;
1838  }
1839  }
1840  }
1841  else {
1842  if (XImageByteOrder(display) == 1) {
1843  // big endian
1844  for (unsigned int i=0; i<m_height; i++) {
1845  for (unsigned int j=0; j<m_width; j++) {
1846  unsigned char val = I[i*m_scale][j*m_scale];
1847  * ( dst_32 ++ ) = vpRGBa::alpha_default;
1848  * ( dst_32 ++ ) = val; // Red
1849  * ( dst_32 ++ ) = val; // Green
1850  * ( dst_32 ++ ) = val; // Blue
1851  }
1852  }
1853  }
1854  else {
1855  // little endian
1856  for (unsigned int i=0; i<m_height; i++) {
1857  for (unsigned int j=0; j<m_width; j++) {
1858  unsigned char val = I[i*m_scale][j*m_scale];
1859  * ( dst_32 ++ ) = val; // Blue
1860  * ( dst_32 ++ ) = val; // Green
1861  * ( dst_32 ++ ) = val; // Red
1862  * ( dst_32 ++ ) = vpRGBa::alpha_default;
1863  }
1864  }
1865  }
1866  }
1867 
1868  // Affichage de l'image dans la Pixmap.
1869  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, m_width, m_height );
1870  XSetWindowBackgroundPixmap ( display, window, pixmap );
1871  break;
1872  }
1873  }
1874  }
1875  else
1876  {
1878  "X not initialized" ) ) ;
1879  }
1880 }
1893 {
1895  {
1896  switch ( screen_depth )
1897  {
1898  case 16: {
1899  vpRGBa* bitmap = I.bitmap;
1900  unsigned int r, g, b;
1901  unsigned int bytes_per_line = (unsigned int)Ximage->bytes_per_line;
1902 
1903  if (m_scale == 1) {
1904  for ( unsigned int i = 0; i < m_height ; i++ ) {
1905  unsigned char *dst_8 = (unsigned char*) Ximage->data + i * bytes_per_line;
1906  unsigned short *dst_16 = (unsigned short *) dst_8;
1907  for ( unsigned int j=0 ; j < m_width; j++ )
1908  {
1909  r = bitmap->R;
1910  g = bitmap->G;
1911  b = bitmap->B;
1912  * ( dst_16 + j ) = (((r << 8) >> RShift) & RMask) |
1913  (((g << 8) >> GShift) & GMask) |
1914  (((b << 8) >> BShift) & BMask);
1915  bitmap++;
1916  }
1917  }
1918  }
1919  else {
1920  for ( unsigned int i = 0; i < m_height ; i++ ) {
1921  unsigned char *dst_8 = (unsigned char*) Ximage->data + i * bytes_per_line;
1922  unsigned short *dst_16 = (unsigned short *) dst_8;
1923  for ( unsigned int j=0 ; j < m_width; j++ )
1924  {
1925  vpRGBa val = I[i*m_scale][j*m_scale];
1926  r = val.R;
1927  g = val.G;
1928  b = val.B;
1929  * ( dst_16 + j ) = (((r << 8) >> RShift) & RMask) |
1930  (((g << 8) >> GShift) & GMask) |
1931  (((b << 8) >> BShift) & BMask);
1932  bitmap++;
1933  }
1934  }
1935  }
1936 
1937  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, m_width, m_height );
1938  XSetWindowBackgroundPixmap ( display, window, pixmap );
1939 
1940  break;
1941  }
1942  case 24:
1943  case 32:
1944  {
1945  /*
1946  * 32-bit source, 24/32-bit destination
1947  */
1948  unsigned char *dst_32 = NULL;
1949  dst_32 = ( unsigned char* ) Ximage->data;
1950  if (m_scale == 1) {
1951  vpRGBa* bitmap = I.bitmap;
1952  unsigned int sizeI = m_width * m_height;
1953  if (XImageByteOrder(display) == 1) {
1954  // big endian
1955  for ( unsigned int i = 0; i < sizeI ; i++ ) {
1956  *(dst_32++) = bitmap->A;
1957  *(dst_32++) = bitmap->R;
1958  *(dst_32++) = bitmap->G;
1959  *(dst_32++) = bitmap->B;
1960  bitmap++;
1961  }
1962  }
1963  else {
1964  // little endian
1965  for ( unsigned int i = 0; i < sizeI; i++ ) {
1966  *(dst_32++) = bitmap->B;
1967  *(dst_32++) = bitmap->G;
1968  *(dst_32++) = bitmap->R;
1969  *(dst_32++) = bitmap->A;
1970  bitmap++;
1971  }
1972  }
1973  }
1974  else {
1975  if (XImageByteOrder(display) == 1) {
1976  // big endian
1977  for (unsigned int i=0; i<m_height; i++) {
1978  for (unsigned int j=0; j<m_width; j++) {
1979  vpRGBa val = I[i*m_scale][j*m_scale];
1980  *(dst_32++) = val.A;
1981  *(dst_32++) = val.R;
1982  *(dst_32++) = val.G;
1983  *(dst_32++) = val.B;
1984  }
1985  }
1986  }
1987  else {
1988  // little endian
1989  for (unsigned int i=0; i<m_height; i++) {
1990  for (unsigned int j=0; j<m_width; j++) {
1991  vpRGBa val = I[i*m_scale][j*m_scale];
1992  *(dst_32++) = val.B;
1993  *(dst_32++) = val.G;
1994  *(dst_32++) = val.R;
1995  *(dst_32++) = val.A;
1996  }
1997  }
1998  }
1999  }
2000 
2001  // Affichage de l'image dans la Pixmap.
2002  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, m_width, m_height );
2003  XSetWindowBackgroundPixmap ( display, window, pixmap );
2004  break;
2005  }
2006  default:
2008  "Unsupported depth (%d bpp) for color display", screen_depth ) ) ;
2009  }
2010  }
2011  else
2012  {
2014  "X not initialized" ) ) ;
2015  }
2016 }
2017 
2029 void vpDisplayX::displayImage ( const unsigned char *bitmap )
2030 {
2031 
2033  {
2034  unsigned char *dst_32 = ( unsigned char* ) Ximage->data;
2035  for ( unsigned int i = 0; i < m_width * m_height; i++ )
2036  {
2037  * ( dst_32 ++ ) = *bitmap; // red component.
2038  * ( dst_32 ++ ) = *bitmap; // green component.
2039  * ( dst_32 ++ ) = *bitmap; // blue component.
2040  * ( dst_32 ++ ) = *bitmap; // luminance component.
2041  bitmap ++;
2042  }
2043 
2044  // Affichage de l'image dans la Pixmap.
2045  XPutImage ( display, pixmap, context, Ximage, 0, 0, 0, 0, m_width, m_height );
2046  XSetWindowBackgroundPixmap ( display, window, pixmap );
2047  }
2048  else
2049  {
2051  "X not initialized" ) ) ;
2052  }
2053 }
2054 
2055 
2072  const unsigned int w, const unsigned int h )
2073 {
2075  {
2076  switch ( screen_depth )
2077  {
2078  case 8:
2079  {
2080  // Correction de l'image de facon a liberer les niveaux de gris
2081  // ROUGE, VERT, BLEU, JAUNE
2082  unsigned char nivGrisMax = 255 - vpColor::id_unknown;
2083  if (m_scale == 1) {
2084  unsigned char *src_8 = ( unsigned char * ) I.bitmap;
2085  unsigned char *dst_8 = ( unsigned char * ) Ximage->data;
2086  unsigned int iwidth = I.getWidth();
2087 
2088  src_8 = src_8 + (int)(iP.get_i()*iwidth+ iP.get_j());
2089  dst_8 = dst_8 + (int)(iP.get_i()*m_width+ iP.get_j());
2090 
2091  unsigned int i = 0;
2092  while (i < h)
2093  {
2094  unsigned int j = 0;
2095  while (j < w)
2096  {
2097  unsigned char nivGris = *(src_8+j);
2098  if ( nivGris > nivGrisMax )
2099  *(dst_8+j) = 255;
2100  else
2101  *(dst_8+j) = nivGris;
2102  j++;
2103  }
2104  src_8 = src_8 + iwidth;
2105  dst_8 = dst_8 + m_width;
2106  i++;
2107  }
2108 
2109  XPutImage ( display, pixmap, context, Ximage, (int)iP.get_u(), (int)iP.get_v(), (int)iP.get_u(), (int)iP.get_v(), w, h );
2110  }
2111  else {
2112  // Correction de l'image de facon a liberer les niveaux de gris
2113  // ROUGE, VERT, BLEU, JAUNE
2114  int i_min = std::max((int)ceil(iP.get_i()/m_scale), 0);
2115  int j_min = std::max((int)ceil(iP.get_j()/m_scale), 0);
2116  int i_max = std::min((int)ceil((iP.get_i() + h)/m_scale), (int)m_height);
2117  int j_max = std::min((int)ceil((iP.get_j() + w)/m_scale), (int)m_width);
2118 
2119  unsigned int i_min_ = (unsigned int)i_min;
2120  unsigned int i_max_ = (unsigned int)i_max;
2121  unsigned int j_min_ = (unsigned int)j_min;
2122  unsigned int j_max_ = (unsigned int)j_max;
2123 
2124  for (unsigned int i=i_min_; i<i_max_; i++) {
2125  unsigned char *dst_8 = ( unsigned char * ) Ximage->data + i*m_width;
2126  for (unsigned int j=j_min_; j<j_max_; j++) {
2127  unsigned char nivGris = I[i*m_scale][j*m_scale];
2128  if ( nivGris > nivGrisMax )
2129  dst_8[j] = 255;
2130  else
2131  dst_8[j] = nivGris;
2132  }
2133  }
2134  XPutImage(display, pixmap, context, Ximage, j_min, i_min, j_min, i_min, j_max_-j_min_, i_max_-i_min_);
2135  }
2136 
2137  // Affichage de l'image dans la Pixmap.
2138  XSetWindowBackgroundPixmap ( display, window, pixmap );
2139  break;
2140  }
2141  case 16:
2142  {
2143  unsigned int bytes_per_line = (unsigned int)Ximage->bytes_per_line;
2144  if(m_scale == 1) {
2145  for ( unsigned int i = (unsigned int)iP.get_i(); i < (unsigned int)(iP.get_i()+h) ; i++ ) {
2146  unsigned char *dst_8 = (unsigned char *) Ximage->data + i * bytes_per_line;
2147  unsigned short *dst_16 = (unsigned short *) dst_8;
2148  for ( unsigned int j=(unsigned int)iP.get_j() ; j < (unsigned int)(iP.get_j()+w); j++ )
2149  {
2150  * ( dst_16 + j ) = ( unsigned short ) colortable[I[i][j]] ;
2151  }
2152  }
2153 
2154  XPutImage ( display, pixmap, context, Ximage, (int)iP.get_u(), (int)iP.get_v(), (int)iP.get_u(), (int)iP.get_v(), w, h );
2155  }
2156  else {
2157  int i_min = std::max((int)ceil(iP.get_i()/m_scale), 0);
2158  int j_min = std::max((int)ceil(iP.get_j()/m_scale), 0);
2159  int i_max = std::min((int)ceil((iP.get_i() + h)/m_scale), (int)m_height);
2160  int j_max = std::min((int)ceil((iP.get_j() + w)/m_scale), (int)m_width);
2161 
2162  unsigned int i_min_ = (unsigned int)i_min;
2163  unsigned int i_max_ = (unsigned int)i_max;
2164  unsigned int j_min_ = (unsigned int)j_min;
2165  unsigned int j_max_ = (unsigned int)j_max;
2166 
2167  for (unsigned int i=i_min_; i<i_max_; i++) {
2168  unsigned char *dst_8 = (unsigned char*) Ximage->data + i * bytes_per_line;
2169  unsigned short *dst_16 = (unsigned short *) dst_8;
2170  for (unsigned int j=j_min_; j<j_max_; j++) {
2171  * ( dst_16 + j ) = ( unsigned short ) colortable[I[i*m_scale][j*m_scale]] ;
2172  }
2173  }
2174 
2175  XPutImage(display, pixmap, context, Ximage, j_min, i_min, j_min, i_min, j_max_-j_min_, i_max_-i_min_);
2176  }
2177 
2178  XSetWindowBackgroundPixmap ( display, window, pixmap );
2179  break;
2180  }
2181 
2182  case 24:
2183  default:
2184  {
2185  if (m_scale == 1) {
2186  unsigned int iwidth = I.getWidth();
2187  unsigned char *src_8 = I.bitmap + (int)(iP.get_i()*iwidth+ iP.get_j());
2188  unsigned char *dst_32 = ( unsigned char* ) Ximage->data + (int)(iP.get_i()*4*m_width+ iP.get_j()*4);
2189 
2190  if (XImageByteOrder(display) == 1) {
2191  // big endian
2192  unsigned int i = 0;
2193  while (i < h)
2194  {
2195  unsigned int j = 0;
2196  while (j < w)
2197  {
2198  unsigned char val = *(src_8+j);
2199  *(dst_32+4*j) = vpRGBa::alpha_default;
2200  *(dst_32+4*j+1) = val;
2201  *(dst_32+4*j+2) = val;
2202  *(dst_32+4*j+3) = val;
2203  j++;
2204  }
2205  src_8 = src_8 + iwidth;
2206  dst_32 = dst_32 + 4*m_width;
2207  i++;
2208  }
2209  }
2210  else {
2211  // little endian
2212  unsigned int i = 0;
2213  while (i < h)
2214  {
2215  unsigned int j = 0;
2216  while (j < w)
2217  {
2218  unsigned char val = *(src_8+j);
2219  *(dst_32+4*j) = val;
2220  *(dst_32+4*j+1) = val;
2221  *(dst_32+4*j+2) = val;
2222  *(dst_32+4*j+3) = vpRGBa::alpha_default;
2223  j++;
2224  }
2225  src_8 = src_8 + iwidth;
2226  dst_32 = dst_32 + 4*m_width;
2227  i++;
2228  }
2229  }
2230 
2231  XPutImage ( display, pixmap, context, Ximage, (int)iP.get_u(), (int)iP.get_v(), (int)iP.get_u(), (int)iP.get_v(), w, h );
2232  }
2233  else {
2234  int i_min = std::max((int)ceil(iP.get_i()/m_scale), 0);
2235  int j_min = std::max((int)ceil(iP.get_j()/m_scale), 0);
2236  int i_max = std::min((int)ceil((iP.get_i() + h)/m_scale), (int)m_height);
2237  int j_max = std::min((int)ceil((iP.get_j() + w)/m_scale), (int)m_width);
2238 
2239  unsigned int i_min_ = (unsigned int)i_min;
2240  unsigned int i_max_ = (unsigned int)i_max;
2241  unsigned int j_min_ = (unsigned int)j_min;
2242  unsigned int j_max_ = (unsigned int)j_max;
2243 
2244  if (XImageByteOrder(display) == 1) {
2245  // big endian
2246  for (unsigned int i=i_min_; i<i_max_; i++) {
2247  unsigned char *dst_32 = ( unsigned char* ) Ximage->data + (int)(i*4*m_width + j_min_*4);
2248  for (unsigned int j=j_min_; j<j_max_; j++) {
2249  unsigned char val = I[i*m_scale][j*m_scale];
2250  * ( dst_32 ++ ) = vpRGBa::alpha_default;
2251  * ( dst_32 ++ ) = val;
2252  * ( dst_32 ++ ) = val;
2253  * ( dst_32 ++ ) = val;
2254  }
2255  }
2256  }
2257  else {
2258  // little endian
2259  for (unsigned int i=i_min_; i<i_max_; i++) {
2260  unsigned char *dst_32 = ( unsigned char* ) Ximage->data + (int)(i*4*m_width + j_min_*4);
2261  for (unsigned int j=j_min_; j<j_max_; j++) {
2262  unsigned char val = I[i*m_scale][j*m_scale];
2263  * ( dst_32 ++ ) = val;
2264  * ( dst_32 ++ ) = val;
2265  * ( dst_32 ++ ) = val;
2266  * ( dst_32 ++ ) = vpRGBa::alpha_default;
2267  }
2268  }
2269  }
2270 
2271  XPutImage(display, pixmap, context, Ximage, j_min, i_min, j_min, i_min, j_max_-j_min_, i_max_-i_min_);
2272  }
2273 
2274  XSetWindowBackgroundPixmap ( display, window, pixmap );
2275  break;
2276  }
2277  }
2278  }
2279  else
2280  {
2282  "X not initialized" ) ) ;
2283  }
2284 }
2285 
2286 
2303  const unsigned int w, const unsigned int h )
2304 {
2306  {
2307  switch ( screen_depth )
2308  {
2309  case 16: {
2310  if (m_scale == 1) {
2311  unsigned int bytes_per_line = (unsigned int)Ximage->bytes_per_line;
2312  for ( unsigned int i = (unsigned int)iP.get_i(); i < (unsigned int)(iP.get_i()+h) ; i++ ) {
2313  unsigned char *dst_8 = (unsigned char *) Ximage->data + i * bytes_per_line;
2314  unsigned short *dst_16 = (unsigned short *) dst_8;
2315  for ( unsigned int j=(unsigned int)iP.get_j() ; j < (unsigned int)(iP.get_j()+w); j++ )
2316  {
2317  vpRGBa val = I[i][j];
2318  unsigned int r = val.R;
2319  unsigned int g = val.G;
2320  unsigned int b = val.B;
2321  * ( dst_16 + j ) = (((r << 8) >> RShift) & RMask) |
2322  (((g << 8) >> GShift) & GMask) |
2323  (((b << 8) >> BShift) & BMask);
2324  }
2325  }
2326  XPutImage ( display, pixmap, context, Ximage, (int)iP.get_u(), (int)iP.get_v(), (int)iP.get_u(), (int)iP.get_v(), w, h );
2327  }
2328  else {
2329  unsigned int bytes_per_line = (unsigned int)Ximage->bytes_per_line;
2330  int i_min = std::max((int)ceil(iP.get_i()/m_scale), 0);
2331  int j_min = std::max((int)ceil(iP.get_j()/m_scale), 0);
2332  int i_max = std::min((int)ceil((iP.get_i() + h)/m_scale), (int)m_height);
2333  int j_max = std::min((int)ceil((iP.get_j() + w)/m_scale), (int)m_width);
2334 
2335  unsigned int i_min_ = (unsigned int)i_min;
2336  unsigned int i_max_ = (unsigned int)i_max;
2337  unsigned int j_min_ = (unsigned int)j_min;
2338  unsigned int j_max_ = (unsigned int)j_max;
2339 
2340  for (unsigned int i=i_min_; i<i_max_; i++) {
2341  unsigned char *dst_8 = ( unsigned char* ) Ximage->data + i * bytes_per_line;
2342  unsigned short *dst_16 = (unsigned short *) dst_8;
2343  for (unsigned int j=j_min_; j < j_max_; j++)
2344  {
2345  vpRGBa val = I[i*m_scale][j*m_scale];
2346  unsigned int r = val.R;
2347  unsigned int g = val.G;
2348  unsigned int b = val.B;
2349  * ( dst_16 + j ) = (((r << 8) >> RShift) & RMask) |
2350  (((g << 8) >> GShift) & GMask) |
2351  (((b << 8) >> BShift) & BMask);
2352  }
2353  }
2354  XPutImage(display, pixmap, context, Ximage, j_min, i_min, j_min, i_min, j_max_-j_min_, i_max_-i_min_);
2355  }
2356 
2357  XSetWindowBackgroundPixmap ( display, window, pixmap );
2358 
2359  break;
2360  }
2361  case 24:
2362  case 32:
2363  {
2364  /*
2365  * 32-bit source, 24/32-bit destination
2366  */
2367 
2368  if (m_scale == 1) {
2369  unsigned char *dst_32 = ( unsigned char* ) Ximage->data;
2370  vpRGBa* src_32 = I.bitmap;
2371 
2372  unsigned int iwidth = I.getWidth();
2373 
2374  src_32 = src_32 + (int)(iP.get_i()*iwidth+ iP.get_j());
2375  dst_32 = dst_32 + (int)(iP.get_i()*4*m_width+ iP.get_j()*4);
2376 
2377  unsigned int i = 0;
2378 
2379  if (XImageByteOrder(display) == 1) {
2380  // big endian
2381  while (i < h) {
2382  unsigned int j = 0;
2383  while (j < w) {
2384  *(dst_32+4*j) = (src_32+j)->A;
2385  *(dst_32+4*j+1) = (src_32+j)->R;
2386  *(dst_32+4*j+2) = (src_32+j)->G;
2387  *(dst_32+4*j+3) = (src_32+j)->B;
2388 
2389  j++;
2390  }
2391  src_32 = src_32 + iwidth;
2392  dst_32 = dst_32 + 4*m_width;
2393  i++;
2394  }
2395 
2396  }
2397  else {
2398  // little endian
2399  while (i < h) {
2400  unsigned int j = 0;
2401  while (j < w) {
2402  *(dst_32+4*j) = (src_32+j)->B;
2403  *(dst_32+4*j+1) = (src_32+j)->G;
2404  *(dst_32+4*j+2) = (src_32+j)->R;
2405  *(dst_32+4*j+3) = (src_32+j)->A;
2406 
2407  j++;
2408  }
2409  src_32 = src_32 + iwidth;
2410  dst_32 = dst_32 + 4*m_width;
2411  i++;
2412  }
2413  }
2414 
2415  XPutImage ( display, pixmap, context, Ximage, (int)iP.get_u(), (int)iP.get_v(), (int)iP.get_u(), (int)iP.get_v(), w, h );
2416  }
2417  else {
2418  int i_min = std::max((int)ceil(iP.get_i()/m_scale), 0);
2419  int j_min = std::max((int)ceil(iP.get_j()/m_scale), 0);
2420  int i_max = std::min((int)ceil((iP.get_i() + h)/m_scale), (int)m_height);
2421  int j_max = std::min((int)ceil((iP.get_j() + w)/m_scale), (int)m_width);
2422 
2423  unsigned int i_min_ = (unsigned int)i_min;
2424  unsigned int i_max_ = (unsigned int)i_max;
2425  unsigned int j_min_ = (unsigned int)j_min;
2426  unsigned int j_max_ = (unsigned int)j_max;
2427 
2428  if (XImageByteOrder(display) == 1) {
2429  // big endian
2430  for (unsigned int i=i_min_; i<i_max_; i++) {
2431  unsigned char *dst_32 = ( unsigned char* ) Ximage->data + (int)(i*4*m_width + j_min_*4);
2432  for (unsigned int j=j_min_; j<j_max_; j++) {
2433  vpRGBa val = I[i*m_scale][j*m_scale];
2434  *(dst_32++) = val.A;
2435  *(dst_32++) = val.R;
2436  *(dst_32++) = val.G;
2437  *(dst_32++) = val.B;
2438  }
2439  }
2440  }
2441  else {
2442  // little endian
2443  for (unsigned int i=i_min_; i<i_max_; i++) {
2444  unsigned char *dst_32 = ( unsigned char* ) Ximage->data + (int)(i*4*m_width + j_min_*4);
2445  for (unsigned int j=j_min_; j<j_max_; j++) {
2446  vpRGBa val = I[i*m_scale][j*m_scale];
2447  *(dst_32++) = val.B;
2448  *(dst_32++) = val.G;
2449  *(dst_32++) = val.R;
2450  *(dst_32++) = val.A;
2451  }
2452  }
2453  }
2454  XPutImage(display, pixmap, context, Ximage, j_min, i_min, j_min, i_min, j_max_-j_min_, i_max_-i_min_);
2455  }
2456 
2457  XSetWindowBackgroundPixmap ( display, window, pixmap );
2458  break;
2459 
2460  }
2461  default:
2463  "Unsupported depth (%d bpp) for color display", screen_depth ) ) ;
2464  }
2465  }
2466  else
2467  {
2469  "X not initialized" ) ) ;
2470  }
2471 }
2472 
2481 {
2483  {
2484  if ( ximage_data_init == true )
2485  free ( Ximage->data );
2486 
2487  Ximage->data = NULL;
2488  XDestroyImage ( Ximage );
2489 
2490  XFreePixmap ( display, pixmap );
2491 
2492  XFreeGC ( display, context );
2493  XDestroyWindow ( display, window );
2494  XCloseDisplay ( display );
2495 
2497 
2498  if (x_color != NULL) {
2499  delete [] x_color;
2500  x_color = NULL;
2501  }
2502  }
2503 }
2504 
2505 
2512 {
2514  {
2515  XClearWindow ( display, window );
2516  XFlush ( display );
2517  }
2518  else
2519  {
2521  "X not initialized" ) ) ;
2522  }
2523 }
2524 
2532 void vpDisplayX::flushDisplayROI(const vpImagePoint &iP, const unsigned int w, const unsigned int h)
2533 {
2535  {
2536  XClearArea ( display, window, (int)(iP.get_u()/m_scale),(int)(iP.get_v()/m_scale), w/m_scale, h/m_scale, 0 );
2537  XFlush ( display );
2538  }
2539  else
2540  {
2542  "X not initialized" ) ) ;
2543  }
2544 }
2545 
2546 
2551 void vpDisplayX::clearDisplay ( const vpColor &color )
2552 {
2554  {
2555 
2556  if (color.id < vpColor::id_unknown)
2557  XSetWindowBackground ( display, window, x_color[color.id] );
2558  else {
2559  xcolor.pad = 0;
2560  xcolor.red = 256 * color.R;
2561  xcolor.green = 256 * color.G;
2562  xcolor.blue = 256 * color.B;
2563  XAllocColor ( display, lut, &xcolor );
2564  XSetForeground ( display, context, xcolor.pixel );
2565  }
2566 
2567  XClearWindow ( display, window );
2568 
2569  XFreePixmap ( display, pixmap );
2570  // Pixmap creation.
2571  pixmap = XCreatePixmap ( display, window, m_width, m_height, screen_depth );
2572  }
2573  else
2574  {
2576  "X not initialized" ) ) ;
2577  }
2578 }
2579 
2588  const vpImagePoint &ip2,
2589  const vpColor &color,
2590  unsigned int w, unsigned int h,
2591  unsigned int thickness)
2592 {
2594  {
2595  double a = ip2.get_i() - ip1.get_i() ;
2596  double b = ip2.get_j() - ip1.get_j() ;
2597  double lg = sqrt ( vpMath::sqr ( a ) + vpMath::sqr ( b ) ) ;
2598 
2599  //if ( ( a==0 ) && ( b==0 ) )
2600  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon() )&&(std::fabs(b) <= std::numeric_limits<double>::epsilon()) )
2601  {
2602  // DisplayCrossLarge(i1,j1,3,col) ;
2603  }
2604  else
2605  {
2606  a /= lg ;
2607  b /= lg ;
2608 
2609  vpImagePoint ip3;
2610  ip3.set_i(ip2.get_i() - w*a);
2611  ip3.set_j(ip2.get_j() - w*b);
2612 
2613  vpImagePoint ip4;
2614  ip4.set_i( ip3.get_i() - b*h );
2615  ip4.set_j( ip3.get_j() + a*h );
2616 
2617  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
2618  displayLine ( ip2, ip4, color, thickness ) ;
2619 
2620  ip4.set_i( ip3.get_i() + b*h );
2621  ip4.set_j( ip3.get_j() - a*h );
2622 
2623  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
2624  displayLine ( ip2, ip4, color, thickness ) ;
2625 
2626  displayLine ( ip1, ip2, color, thickness ) ;
2627  }
2628  }
2629  else
2630  {
2632  "X not initialized" ) ) ;
2633  }
2634 }
2635 
2648  const char *text,
2649  const vpColor &color )
2650 {
2652  {
2653  if (color.id < vpColor::id_unknown)
2654  XSetForeground ( display, context, x_color[color.id] );
2655  else {
2656  xcolor.pad = 0;
2657  xcolor.red = 256 * color.R;
2658  xcolor.green = 256 * color.G;
2659  xcolor.blue = 256 * color.B;
2660  XAllocColor ( display, lut, &xcolor );
2661  XSetForeground ( display, context, xcolor.pixel );
2662  }
2663  XDrawString ( display, pixmap, context,
2664  (int)(ip.get_u()/m_scale), (int)(ip.get_v()/m_scale),
2665  text, (int)strlen ( text ) );
2666  }
2667  else
2668  {
2670  "X not initialized" ) ) ;
2671  }
2672 }
2673 
2684  unsigned int radius,
2685  const vpColor &color,
2686  bool fill,
2687  unsigned int thickness )
2688 {
2690  {
2691  if ( thickness == 1 ) thickness = 0;
2692  if (color.id < vpColor::id_unknown)
2693  XSetForeground ( display, context, x_color[color.id] );
2694  else {
2695  xcolor.pad = 0;
2696  xcolor.red = 256 * color.R;
2697  xcolor.green = 256 * color.G;
2698  xcolor.blue = 256 * color.B;
2699  XAllocColor ( display, lut, &xcolor );
2700  XSetForeground ( display, context, xcolor.pixel );
2701  }
2702 
2703  XSetLineAttributes ( display, context, thickness,
2704  LineSolid, CapButt, JoinBevel );
2705 
2706  if ( fill == false )
2707  {
2708  XDrawArc ( display, pixmap, context,
2709  vpMath::round( (center.get_u()-radius)/m_scale ),
2710  vpMath::round( (center.get_v()-radius)/m_scale ),
2711  radius*2/m_scale, radius*2/m_scale, 0, 23040 ); /* 23040 = 360*64 */
2712  }
2713  else
2714  {
2715  XFillArc ( display, pixmap, context,
2716  vpMath::round( (center.get_u()-radius)/m_scale ),
2717  vpMath::round( (center.get_v()-radius)/m_scale ),
2718  radius*2/m_scale, radius*2/m_scale, 0, 23040 ); /* 23040 = 360*64 */
2719  }
2720  }
2721  else
2722  {
2724  "X not initialized" ) ) ;
2725  }
2726 }
2727 
2736  unsigned int cross_size,
2737  const vpColor &color,
2738  unsigned int thickness)
2739 {
2741  {
2742  double i = ip.get_i();
2743  double j = ip.get_j();
2744  vpImagePoint ip1, ip2;
2745 
2746  ip1.set_i( i-cross_size/2 );
2747  ip1.set_j( j );
2748  ip2.set_i( i+cross_size/2 );
2749  ip2.set_j( j );
2750  displayLine ( ip1, ip2, color, thickness ) ;
2751 
2752  ip1.set_i( i );
2753  ip1.set_j( j-cross_size/2 );
2754  ip2.set_i( i );
2755  ip2.set_j( j+cross_size/2 );
2756 
2757  displayLine ( ip1, ip2, color, thickness ) ;
2758  }
2759  else
2760  {
2762  "X not initialized" ) ) ;
2763  }
2764 }
2772  const vpImagePoint &ip2,
2773  const vpColor &color,
2774  unsigned int thickness )
2775 {
2777  {
2778  if ( thickness == 1 ) thickness = 0;
2779 
2780  if (color.id < vpColor::id_unknown)
2781  XSetForeground ( display, context, x_color[color.id] );
2782  else {
2783  xcolor.pad = 0;
2784  xcolor.red = 256 * color.R;
2785  xcolor.green = 256 * color.G;
2786  xcolor.blue = 256 * color.B;
2787  XAllocColor ( display, lut, &xcolor );
2788  XSetForeground ( display, context, xcolor.pixel );
2789  }
2790 
2791  XSetLineAttributes ( display, context, thickness,
2792  LineOnOffDash, CapButt, JoinBevel );
2793 
2794  XDrawLine ( display, pixmap, context,
2795  vpMath::round( ip1.get_u()/m_scale ),
2796  vpMath::round( ip1.get_v()/m_scale ),
2797  vpMath::round( ip2.get_u()/m_scale ),
2798  vpMath::round( ip2.get_v()/m_scale ) );
2799  }
2800  else
2801  {
2803  "X not initialized" ) ) ;
2804  }
2805 }
2806 
2814  const vpImagePoint &ip2,
2815  const vpColor &color,
2816  unsigned int thickness )
2817 {
2819  {
2820  if ( thickness == 1 ) thickness = 0;
2821 
2822  if (color.id < vpColor::id_unknown)
2823  XSetForeground ( display, context, x_color[color.id] );
2824  else {
2825  xcolor.pad = 0;
2826  xcolor.red = 256 * color.R;
2827  xcolor.green = 256 * color.G;
2828  xcolor.blue = 256 * color.B;
2829  XAllocColor ( display, lut, &xcolor );
2830  XSetForeground ( display, context, xcolor.pixel );
2831  }
2832 
2833  XSetLineAttributes ( display, context, thickness,
2834  LineSolid, CapButt, JoinBevel );
2835 
2836  XDrawLine ( display, pixmap, context,
2837  vpMath::round( ip1.get_u()/m_scale ),
2838  vpMath::round( ip1.get_v()/m_scale ),
2839  vpMath::round( ip2.get_u()/m_scale ),
2840  vpMath::round( ip2.get_v()/m_scale ) );
2841  }
2842  else
2843  {
2845  "X not initialized" ) ) ;
2846  }
2847 }
2848 
2855 void vpDisplayX::displayPoint ( const vpImagePoint &ip, const vpColor &color, unsigned int thickness )
2856 {
2858  {
2859  if (color.id < vpColor::id_unknown)
2860  XSetForeground ( display, context, x_color[color.id] );
2861  else {
2862  xcolor.pad = 0;
2863  xcolor.red = 256 * color.R;
2864  xcolor.green = 256 * color.G;
2865  xcolor.blue = 256 * color.B;
2866  XAllocColor ( display, lut, &xcolor );
2867  XSetForeground ( display, context, xcolor.pixel );
2868  }
2869 
2870  if (thickness == 1) {
2871  XDrawPoint ( display, pixmap, context,
2872  vpMath::round( ip.get_u()/m_scale ),
2873  vpMath::round( ip.get_v()/m_scale ) );
2874  }
2875  else {
2876  XFillRectangle ( display, pixmap, context,
2877  vpMath::round( ip.get_u()/m_scale ),
2878  vpMath::round( ip.get_v()/m_scale ),
2879  thickness, thickness );
2880  }
2881 
2882  }
2883  else
2884  {
2886  "X not initialized" ) ) ;
2887  }
2888 }
2889 
2903 void
2905  unsigned int w, unsigned int h,
2906  const vpColor &color, bool fill,
2907  unsigned int thickness )
2908 {
2910  {
2911  if ( thickness == 1 ) thickness = 0;
2912  if (color.id < vpColor::id_unknown)
2913  XSetForeground ( display, context, x_color[color.id] );
2914  else {
2915  xcolor.pad = 0;
2916  xcolor.red = 256 * color.R;
2917  xcolor.green = 256 * color.G;
2918  xcolor.blue = 256 * color.B;
2919  XAllocColor ( display, lut, &xcolor );
2920  XSetForeground ( display, context, xcolor.pixel );
2921  }
2922  XSetLineAttributes ( display, context, thickness,
2923  LineSolid, CapButt, JoinBevel );
2924  if ( fill == false )
2925  {
2926  XDrawRectangle ( display, pixmap, context,
2927  vpMath::round( topLeft.get_u()/m_scale ),
2928  vpMath::round( topLeft.get_v()/m_scale ),
2929  w/m_scale, h/m_scale );
2930  }
2931  else
2932  {
2933  XFillRectangle ( display, pixmap, context,
2934  vpMath::round( topLeft.get_u()/m_scale ),
2935  vpMath::round( topLeft.get_v()/m_scale ),
2936  w/m_scale, h/m_scale );
2937  }
2938  }
2939  else
2940  {
2942  "X not initialized" ) ) ;
2943  }
2944 }
2945 
2958 void
2960  const vpImagePoint &bottomRight,
2961  const vpColor &color, bool fill,
2962  unsigned int thickness )
2963 {
2965  {
2966  if ( thickness == 1 ) thickness = 0;
2967  if (color.id < vpColor::id_unknown)
2968  XSetForeground ( display, context, x_color[color.id] );
2969  else {
2970  xcolor.pad = 0;
2971  xcolor.red = 256 * color.R;
2972  xcolor.green = 256 * color.G;
2973  xcolor.blue = 256 * color.B;
2974  XAllocColor ( display, lut, &xcolor );
2975  XSetForeground ( display, context, xcolor.pixel );
2976  }
2977 
2978  XSetLineAttributes ( display, context, thickness,
2979  LineSolid, CapButt, JoinBevel );
2980 
2981  vpImagePoint topLeft_ = topLeft/m_scale;
2982  vpImagePoint bottomRight_ = bottomRight/m_scale;
2983  unsigned int w = (unsigned int)vpMath::round( std::fabs(bottomRight_.get_u() - topLeft_.get_u()) );
2984  unsigned int h = (unsigned int)vpMath::round( std::fabs(bottomRight_.get_v() - topLeft_.get_v()) );
2985  if ( fill == false )
2986  {
2987 
2988  XDrawRectangle ( display, pixmap, context,
2989  vpMath::round( topLeft_.get_u() < bottomRight_.get_u() ? topLeft_.get_u() : bottomRight_.get_u() ),
2990  vpMath::round( topLeft_.get_v() < bottomRight_.get_v() ? topLeft_.get_v() : bottomRight_.get_v() ),
2991  w > 0 ? w : 1, h > 0 ? h : 1 );
2992  }
2993  else
2994  {
2995  XFillRectangle ( display, pixmap, context,
2996  vpMath::round( topLeft_.get_u() < bottomRight_.get_u() ? topLeft_.get_u() : bottomRight_.get_u() ),
2997  vpMath::round( topLeft_.get_v() < bottomRight_.get_v() ? topLeft_.get_v() : bottomRight_.get_v() ),
2998  w, h );
2999  }
3000  }
3001  else
3002  {
3004  "X not initialized" ) ) ;
3005  }
3006 }
3007 
3020 void
3022  const vpColor &color, bool fill,
3023  unsigned int thickness )
3024 {
3026  {
3027  if ( thickness == 1 ) thickness = 0;
3028  if (color.id < vpColor::id_unknown)
3029  XSetForeground ( display, context, x_color[color.id] );
3030  else {
3031  xcolor.pad = 0;
3032  xcolor.red = 256 * color.R;
3033  xcolor.green = 256 * color.G;
3034  xcolor.blue = 256 * color.B;
3035  XAllocColor ( display, lut, &xcolor );
3036  XSetForeground ( display, context, xcolor.pixel );
3037  }
3038 
3039  XSetLineAttributes ( display, context, thickness,
3040  LineSolid, CapButt, JoinBevel );
3041 
3042  if ( fill == false )
3043  {
3044  XDrawRectangle ( display, pixmap, context,
3045  vpMath::round( rectangle.getLeft()/m_scale ),
3046  vpMath::round( rectangle.getTop()/m_scale ),
3047  (unsigned int)vpMath::round( rectangle.getWidth()/m_scale-1 ),
3048  (unsigned int)vpMath::round( rectangle.getHeight()/m_scale-1 ) );
3049  }
3050  else
3051  {
3052  XFillRectangle ( display, pixmap, context,
3053  vpMath::round( rectangle.getLeft()/m_scale ),
3054  vpMath::round( rectangle.getTop()/m_scale ),
3055  (unsigned int)vpMath::round( rectangle.getWidth()/m_scale ),
3056  (unsigned int)vpMath::round( rectangle.getHeight()/m_scale ) );
3057  }
3058 
3059  }
3060  else
3061  {
3063  "X not initialized" ) ) ;
3064  }
3065 }
3066 
3083 bool
3084 vpDisplayX::getClick(bool blocking)
3085 {
3086 
3087  bool ret = false;
3088 
3090  Window rootwin, childwin ;
3091  int root_x, root_y, win_x, win_y ;
3092  unsigned int modifier ;
3093 
3094  // Event testing
3095  if(blocking){
3096  XCheckMaskEvent(display , ButtonPressMask, &event);
3097  XCheckMaskEvent(display , ButtonReleaseMask, &event);
3098  XMaskEvent ( display, ButtonPressMask ,&event );
3099  ret = true;
3100  }
3101  else{
3102  ret = XCheckMaskEvent(display , ButtonPressMask, &event);
3103  }
3104 
3105  if(ret){
3106  /* Recuperation de la coordonnee du pixel clique. */
3107  if ( XQueryPointer ( display,
3108  window,
3109  &rootwin, &childwin,
3110  &root_x, &root_y,
3111  &win_x, &win_y,
3112  &modifier ) ) {}
3113  }
3114  }
3115  else {
3117  "X not initialized" ) ) ;
3118  }
3119  return ret;
3120 }
3121 
3138 bool
3139 vpDisplayX::getClick ( vpImagePoint &ip, bool blocking )
3140 {
3141 
3142  bool ret = false;
3144 
3145  Window rootwin, childwin ;
3146  int root_x, root_y, win_x, win_y ;
3147  unsigned int modifier ;
3148  // Event testing
3149  if(blocking){
3150  XCheckMaskEvent(display , ButtonPressMask, &event);
3151  XCheckMaskEvent(display , ButtonReleaseMask, &event);
3152  XMaskEvent ( display, ButtonPressMask ,&event );
3153  ret = true;
3154  }
3155  else{
3156  ret = XCheckMaskEvent(display , ButtonPressMask, &event);
3157  }
3158 
3159  if(ret){
3160  // Get mouse position
3161  if ( XQueryPointer ( display,
3162  window,
3163  &rootwin, &childwin,
3164  &root_x, &root_y,
3165  &win_x, &win_y,
3166  &modifier ) ) {
3167  ip.set_u( (double)event.xbutton.x * m_scale);
3168  ip.set_v( (double)event.xbutton.y * m_scale);
3169  }
3170  }
3171  }
3172  else {
3174  "X not initialized" ) ) ;
3175  }
3176  return ret ;
3177 }
3178 
3198 bool
3201  bool blocking )
3202 {
3203 
3204  bool ret = false;
3206 
3207  Window rootwin, childwin ;
3208  int root_x, root_y, win_x, win_y ;
3209  unsigned int modifier ;
3210 
3211  // Event testing
3212  if(blocking){
3213  XCheckMaskEvent(display , ButtonPressMask, &event);
3214  XCheckMaskEvent(display , ButtonReleaseMask, &event);
3215  XMaskEvent ( display, ButtonPressMask ,&event );
3216  ret = true;
3217  }
3218  else{
3219  ret = XCheckMaskEvent(display , ButtonPressMask, &event);
3220  }
3221 
3222  if(ret){
3223  // Get mouse position
3224  if ( XQueryPointer ( display,
3225  window,
3226  &rootwin, &childwin,
3227  &root_x, &root_y,
3228  &win_x, &win_y,
3229  &modifier ) ) {
3230  ip.set_u( (double)event.xbutton.x * m_scale);
3231  ip.set_v( (double)event.xbutton.y * m_scale);
3232  switch ( event.xbutton.button ) {
3233  case Button1: button = vpMouseButton::button1; break;
3234  case Button2: button = vpMouseButton::button2; break;
3235  case Button3: button = vpMouseButton::button3; break;
3236  }
3237  }
3238  }
3239  }
3240  else {
3242  "X not initialized" ) ) ;
3243  }
3244  return ret ;
3245 }
3246 
3270 bool
3273  bool blocking )
3274 {
3275 
3276  bool ret = false;
3278  Window rootwin, childwin ;
3279  int root_x, root_y, win_x, win_y ;
3280  unsigned int modifier ;
3281 
3282  // Event testing
3283  if(blocking){
3284  XCheckMaskEvent(display , ButtonPressMask, &event);
3285  XCheckMaskEvent(display , ButtonReleaseMask, &event);
3286  XMaskEvent ( display, ButtonReleaseMask ,&event );
3287  ret = true;
3288  }
3289  else{
3290  ret = XCheckMaskEvent(display , ButtonReleaseMask, &event);
3291  }
3292 
3293  if(ret){
3294  /* Recuperation de la coordonnee du pixel clique. */
3295  if ( XQueryPointer ( display,
3296  window,
3297  &rootwin, &childwin,
3298  &root_x, &root_y,
3299  &win_x, &win_y,
3300  &modifier ) ) {
3301  ip.set_u( (double)event.xbutton.x * m_scale);
3302  ip.set_v( (double)event.xbutton.y * m_scale);
3303  switch ( event.xbutton.button ) {
3304  case Button1: button = vpMouseButton::button1; break;
3305  case Button2: button = vpMouseButton::button2; break;
3306  case Button3: button = vpMouseButton::button3; break;
3307  }
3308  }
3309  }
3310  }
3311  else {
3313  "X not initialized" ) ) ;
3314  }
3315  return ret ;
3316 }
3317 
3318 /*
3319  Gets the displayed image (including the overlay plane)
3320  and returns an RGBa image. If a scale factor is set using setScale(), the size of the image is the
3321  size of the downscaled image.
3322 
3323  \param I : Image to get.
3324 */
3326 {
3328  {
3329  XImage *xi ;
3330 
3331  XCopyArea (display,window, pixmap, context,
3332  0,0, m_width, m_height, 0, 0);
3333 
3334  xi= XGetImage ( display,pixmap, 0,0, m_width, m_height,
3335  AllPlanes, ZPixmap ) ;
3336 
3337  I.resize ( m_height, m_width ) ;
3338 
3339  unsigned char *src_32 = NULL;
3340  src_32 = ( unsigned char* ) xi->data;
3341 
3342  if (screen_depth == 16) {
3343  for ( unsigned int i = 0; i < I.getHeight() ; i++ ) {
3344  size_t i_ = i*m_width;
3345  for ( unsigned int j = 0; j < m_height ; j++ ) {
3346  size_t ij_ = i_+j;
3347  unsigned long pixel = XGetPixel(xi, (int)j, (int)i);
3348  I.bitmap[ij_].R = (((pixel & RMask) << RShift) >> 8);
3349  I.bitmap[ij_].G = (((pixel & GMask) << GShift) >> 8);
3350  I.bitmap[ij_].B = (((pixel & BMask) << BShift) >> 8);
3351  // On OSX the bottom/right corner (arround the resizing icon) has alpha component
3352  // with different values than 255. That's why we force alpha to vpRGBa::alpha_default
3353  I.bitmap[ij_].A = vpRGBa::alpha_default;
3354  }
3355  }
3356 
3357  }
3358  else {
3359  if (XImageByteOrder(display) == 1) {
3360  // big endian
3361  for ( unsigned int i = 0; i < m_width * m_height ; i++ ) {
3362  // On OSX the bottom/right corner (arround the resizing icon) has alpha component
3363  // with different values than 255. That's why we force alpha to vpRGBa::alpha_default
3364  I.bitmap[i].A = vpRGBa::alpha_default; //src_32[i*4] ;
3365  I.bitmap[i].R = src_32[i*4 + 1] ;
3366  I.bitmap[i].G = src_32[i*4 + 2] ;
3367  I.bitmap[i].B = src_32[i*4 + 3] ;
3368  }
3369  }
3370  else {
3371  // little endian
3372  for ( unsigned int i = 0; i < m_width * m_height ; i++ ) {
3373  I.bitmap[i].B = src_32[i*4] ;
3374  I.bitmap[i].G = src_32[i*4 + 1] ;
3375  I.bitmap[i].R = src_32[i*4 + 2] ;
3376  // On OSX the bottom/right corner (arround the resizing icon) has alpha component
3377  // with different values than 255. That's why we force alpha to vpRGBa::alpha_default
3378  I.bitmap[i].A = vpRGBa::alpha_default; //src_32[i*4 + 3];
3379  }
3380  }
3381  }
3382  XDestroyImage ( xi ) ;
3383  }
3384  else
3385  {
3387  "X not initialized" ) ) ;
3388  }
3389 }
3390 
3395 {
3396  Display *display_;
3397  int screen_;
3398  unsigned int depth;
3399 
3400  if ( ( display_ = XOpenDisplay ( NULL ) ) == NULL )
3401  {
3403  "Can't connect display on server %s.", XDisplayName ( NULL ) ) ) ;
3404  }
3405  screen_ = DefaultScreen ( display_ );
3406  depth = (unsigned int)DefaultDepth ( display_, screen_ );
3407 
3408  XCloseDisplay ( display_ );
3409 
3410  return ( depth );
3411 }
3412 
3417 void vpDisplayX::getScreenSize ( unsigned int &w, unsigned int &h )
3418 {
3419  Display *display_;
3420  int screen_;
3421 
3422  if ( ( display_ = XOpenDisplay ( NULL ) ) == NULL )
3423  {
3425  "Can't connect display on server %s.", XDisplayName ( NULL ) ) ) ;
3426  }
3427  screen_ = DefaultScreen ( display_ );
3428  w = (unsigned int)DisplayWidth ( display_, screen_ );
3429  h = (unsigned int)DisplayHeight ( display_, screen_ );
3430 
3431  XCloseDisplay ( display_ );
3432 }
3433 
3438 {
3439  unsigned int width, height;
3440  getScreenSize(width, height);
3441  return width;
3442 }
3443 
3448 {
3449  unsigned int width, height;
3450  getScreenSize(width, height);
3451  return height;
3452 }
3453 
3474 bool
3476 {
3477 
3478  bool ret = false;
3479 
3481  // Event testing
3482  if(blocking){
3483  XMaskEvent ( display, KeyPressMask ,&event );
3484  ret = true;
3485  }
3486  else{
3487  ret = XCheckMaskEvent(display , KeyPressMask, &event);
3488  }
3489  }
3490  else {
3491  vpERROR_TRACE ( "X not initialized " ) ;
3493  "X not initialized" ) ) ;
3494  }
3495  return ret;
3496 }
3520 bool
3521 vpDisplayX::getKeyboardEvent(std::string &key, bool blocking)
3522 {
3523  bool ret = false;
3524  KeySym keysym;
3525  // int count;
3526  XComposeStatus compose_status;
3527  char buffer;
3528 
3530  // Event testing
3531  if(blocking){
3532  XMaskEvent ( display, KeyPressMask ,&event );
3533  /* count = */ XLookupString ((XKeyEvent *)&event, &buffer, 1,
3534  &keysym, &compose_status);
3535  key = buffer;
3536  ret = true;
3537  }
3538  else{
3539  ret = XCheckMaskEvent(display , KeyPressMask, &event);
3540  if (ret) {
3541  /* count = */ XLookupString ((XKeyEvent *)&event, &buffer, 1,
3542  &keysym, &compose_status);
3543  key = buffer;
3544  }
3545  }
3546  }
3547  else {
3549  "X not initialized" ) ) ;
3550  }
3551  return ret;
3552 }
3565 bool
3567 {
3568 
3569  bool ret = false;
3571 
3572  Window rootwin, childwin ;
3573  int root_x, root_y, win_x, win_y ;
3574  unsigned int modifier ;
3575  // Event testing
3576  ret = XCheckMaskEvent(display , PointerMotionMask, &event);
3577 
3578  if(ret){
3579  // Get mouse position
3580  if ( XQueryPointer ( display,
3581  window,
3582  &rootwin, &childwin,
3583  &root_x, &root_y,
3584  &win_x, &win_y,
3585  &modifier ) ) {
3586  ip.set_u( (double)event.xbutton.x * m_scale);
3587  ip.set_v( (double)event.xbutton.y * m_scale);
3588  }
3589  }
3590  }
3591  else {
3593  "X not initialized" ) ) ;
3594  }
3595  return ret ;
3596 }
3597 
3608 bool
3610 {
3611 
3612  bool ret = false;
3614 
3615  Window rootwin, childwin ;
3616  int root_x, root_y, win_x, win_y ;
3617  unsigned int modifier ;
3618  // Event testing
3619  ret = true;
3620 
3621  if(ret){
3622  // Get mouse position
3623  if ( XQueryPointer ( display,
3624  window,
3625  &rootwin, &childwin,
3626  &root_x, &root_y,
3627  &win_x, &win_y,
3628  &modifier ) ) {
3629  ip.set_u( (double)win_x * m_scale);
3630  ip.set_v( (double)win_y * m_scale);
3631  }
3632  }
3633  }
3634  else {
3636  "X not initialized" ) ) ;
3637  }
3638  return ret ;
3639 }
3640 
3644 int vpDisplayX::getMsb(unsigned int u32val)
3645 {
3646  int i;
3647 
3648  for (i = 31; i >= 0; --i) {
3649  if (u32val & 0x80000000L)
3650  break;
3651  u32val <<= 1;
3652  }
3653  return i;
3654 }
3655 
3656 #elif !defined(VISP_BUILD_SHARED_LIBS)
3657 // Work arround to avoid warning: libvisp_core.a(vpDisplayX.cpp.o) has no symbols
3658 void dummy_vpDisplayX() {};
3659 #endif
3660 
unsigned int m_height
Definition: vpDisplay.h:196
void closeDisplay()
void clearDisplay(const vpColor &color=vpColor::white)
vpDisplay * display
Definition: vpImage.h:135
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayX.cpp:254
double getTop() const
Definition: vpRect.h:178
int m_windowYPosition
display position
Definition: vpDisplay.h:194
double get_v() const
Definition: vpImagePoint.h:268
unsigned int getScreenDepth()
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
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)
double get_i() const
Definition: vpImagePoint.h:199
unsigned int getWidth() const
Definition: vpImage.h:226
unsigned char B
Blue component.
Definition: vpRGBa.h:155
Type * bitmap
points toward the bitmap
Definition: vpImage.h:134
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:257
static const vpColor lightGray
Definition: vpColor.h:159
void displayImage(const vpImage< unsigned char > &I)
double getHeight() const
Definition: vpRect.h:152
void flushDisplay()
static const vpColor darkBlue
Definition: vpColor.h:170
unsigned char G
Green component.
Definition: vpRGBa.h:154
bool m_displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:190
static const vpColor green
Definition: vpColor.h:166
static int round(const double x)
Definition: vpMath.h:249
double get_j() const
Definition: vpImagePoint.h:210
static const vpColor lightRed
Definition: vpColor.h:162
Definition: vpRGBa.h:66
static const vpColor red
Definition: vpColor.h:163
bool getPointerPosition(vpImagePoint &ip)
vpWin32Window window
The window.
static const vpColor orange
Definition: vpColor.h:173
unsigned int getScreenWidth()
bool getClick(bool blocking=true)
vpColorIdentifier id
Definition: vpColor.h:152
void set_i(const double ii)
Definition: vpImagePoint.h:163
double getWidth() const
Definition: vpRect.h:199
int m_windowXPosition
display position
Definition: vpDisplay.h:192
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
unsigned int m_scale
Definition: vpDisplay.h:198
void setScale(vpScaleType scaleType, unsigned int width, unsigned int height)
Definition: vpDisplay.cpp:276
void set_u(const double u)
Definition: vpImagePoint.h:221
#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)
void getScreenSize(unsigned int &width, unsigned int &height)
void set_v(const double v)
Definition: vpImagePoint.h:232
unsigned char A
Additionnal component.
Definition: vpRGBa.h:156
int getMsb(unsigned int u32val)
vpScaleType m_scaleType
Definition: vpDisplay.h:199
bool getKeyboardEvent(bool blocking=true)
std::string m_title
Definition: vpDisplay.h:197
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:903
static const vpColor gray
Definition: vpColor.h:160
void displayPoint(const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
void setWindowPosition(int winx, int winy)
virtual ~vpDisplayX()
Definition: vpDisplayX.cpp:240
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 set_j(const double jj)
Definition: vpImagePoint.h:174
Error that can be emited by the vpDisplay class and its derivates.
unsigned int m_width
Definition: vpDisplay.h:195
vpScaleType
Values that could be applied to a display to down scale the size of the display.
Definition: vpDisplay.h:173
unsigned char R
Red component.
Definition: vpRGBa.h:153
void setTitle(const std::string &title)
#define vpDEBUG_TRACE
Definition: vpDebug.h:478
unsigned int getHeight() const
Definition: vpImage.h:175
Defines a rectangle in the plane.
Definition: vpRect.h:82
void setFont(const std::string &font)
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
static const vpColor yellow
Definition: vpColor.h:171
static const vpColor lightBlue
Definition: vpColor.h:168
static const vpColor purple
Definition: vpColor.h:174
static const vpColor white
Definition: vpColor.h:158
double getLeft() const
Definition: vpRect.h:159
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:279
void getImage(vpImage< vpRGBa > &I)
get the window pixmap and put it in vpRGBa image
unsigned int getScreenHeight()
static const vpColor blue
Definition: vpColor.h:169
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)