Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/toolx/X11/pixwin is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef toolx_X11_pixwin
0005 #define toolx_X11_pixwin
0006 
0007 #include "colors"
0008 #include <tools/sg/zb_viewer>
0009 //#include <tools/sys/atime>
0010 
0011 #include <X11/Xlib.h>
0012 #include <X11/Xutil.h> //XPutPixel
0013 
0014 namespace toolx {
0015 namespace X11 {
0016 
0017 class pixwin {
0018 public:
0019   pixwin(std::ostream& a_out,unsigned int a_monitor,Display* a_display)
0020   :m_out(a_out)
0021   ,m_monitor(a_monitor)
0022   ,m_display(a_display)
0023   ,m_GC(0)
0024   ,m_image(0)
0025   {
0026     if(!m_display) return;
0027     m_GC = ::XCreateGC(m_display,XRootWindow(m_display,m_monitor),0,0);
0028   }
0029   virtual ~pixwin() {
0030     free_pixels();
0031     m_colors.clear();
0032     if(m_GC) ::XFreeGC(m_display,m_GC);
0033     free_XImage();
0034   }
0035 protected:
0036   pixwin(const pixwin& a_from)
0037   :m_out(a_from.m_out)
0038   ,m_monitor(0)
0039   ,m_display(0)
0040   ,m_GC(0)
0041   ,m_image(0)
0042   {}
0043   pixwin& operator=(const pixwin&){
0044     m_monitor = 0;
0045     m_display = 0;
0046     m_GC = 0;
0047     m_image = 0;
0048     return *this;
0049   }
0050 public:
0051   void put_buffer(Window a_win,unsigned int a_ww,unsigned int a_wh,const unsigned char* a_rgbas) {
0052     if(!m_display) return;
0053     if(!m_GC) return;
0054     if(!m_image) alloc_XImage(a_ww,a_wh);
0055     if(!m_image) return;
0056   //tools::atime start = tools::atime::now();
0057     const unsigned int* pos = (const unsigned int*)a_rgbas;
0058     unsigned int row,col;
0059     toolx::X11::Pixel pixel;
0060     for(row=0;row<a_wh;row++) {
0061     for(col=0;col<a_ww;col++) {
0062       if(!get_pixel(m_display,m_monitor,m_pixels,m_colors,*pos,pixel)) {}
0063       pos++;
0064       XPutPixel(m_image,col,row,pixel);
0065     }}
0066   //::printf("debug : map::colors %lu, pixels %lu\n",m_colors.size(),m_pixels.size());
0067     ::XPutImage(m_display,a_win,m_GC,m_image,0,0,0,0,a_ww,a_wh);
0068   //m_out << "pu_buffer : XImage " << tools::atime::elapsed(start) << std::endl;
0069   }
0070   void set_size(unsigned int a_ww,unsigned int a_wh) {
0071     free_XImage();
0072     alloc_XImage(a_ww,a_wh);
0073   }
0074 protected:
0075   void alloc_XImage(unsigned int a_ww,unsigned int a_wh) {
0076     if(m_image) return; //done.
0077     if(!m_display) return;
0078     Screen* screen = ::XScreenOfDisplay(m_display,m_monitor);
0079     m_image = ::XCreateImage(m_display,::XDefaultVisualOfScreen(screen),::XDefaultDepthOfScreen(screen),ZPixmap,0,NULL,a_ww,a_wh,8,0);
0080     if(!m_image) {
0081       m_out << "toolx::X11::pixwin::alloc_XImage : can't create an XImage." << std::endl;
0082       return;
0083     }
0084     //warning : a priori, a_ww*3 != m_image->bytes_per_line.
0085     m_image->data = new char[a_wh*m_image->bytes_per_line];
0086     if(!m_image->data) {
0087       m_out << "toolx::X11::pixwin::alloc_XImage : can't alloc buffer." << std::endl;
0088       ::XFree((char*)m_image);
0089       m_image = 0;
0090       return;
0091     }
0092   }
0093   void free_XImage() {
0094     if(!m_image) return;
0095     delete [] m_image->data;
0096     ::XFree((char*)m_image);
0097     m_image = 0;
0098   }
0099   void free_pixels() {
0100     if(!m_display) return;
0101     Screen* screen = ::XScreenOfDisplay(m_display,m_monitor);
0102     tools_vforit(toolx::X11::Pixel,m_pixels,it) {
0103       ::XFreeColors(m_display,XDefaultColormapOfScreen(screen),&(*it),1,0);
0104     }
0105     m_pixels.clear();
0106   }
0107 protected:
0108   std::ostream& m_out;
0109   unsigned int m_monitor;
0110   Display* m_display;
0111   GC m_GC;
0112   std::vector<toolx::X11::Pixel> m_pixels;
0113   colors_t m_colors;
0114   XImage* m_image;
0115 };
0116 
0117 }}
0118 
0119 
0120 #endif
0121