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