xbmaprenderer.h

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 1999-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Wed Dec  1 17:19:23 1999
00011 // commit: $Id: xbmaprenderer.h 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/gfx/xbmaprenderer.h $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00034 
00035 #ifndef GROOVX_GFX_XBMAPRENDERER_H_UTC20050626084024_DEFINED
00036 #define GROOVX_GFX_XBMAPRENDERER_H_UTC20050626084024_DEFINED
00037 
00038 #include <X11/Xlib.h>
00039 
00040 namespace geom
00041 {
00042   template <class V> class vec2;
00043 }
00044 
00045 namespace media
00046 {
00047   class bmap_data;
00048 }
00049 
00050 namespace Gfx
00051 {
00052   class Canvas;
00053 }
00054 
00056 namespace XBmapRenderer
00057 {
00058    void doRender(Display* display,
00059                  Window win,
00060                  int screen,
00061                  Gfx::Canvas& canvas,
00062                  const media::bmap_data& data,
00063                  const geom::vec2<double>& world_pos,
00064                  const geom::vec2<double>& zoom);
00065 };
00066 
00067 //---------------------------------------------------------------------
00068 //
00069 // Implementation
00070 //
00071 //---------------------------------------------------------------------
00072 
00073 #include "geom/vec2.h"
00074 
00075 #include "gfx/canvas.h"
00076 
00077 #include "media/bmapdata.h"
00078 
00079 #include "rutz/error.h"
00080 
00081 #include <GL/glx.h>
00082 
00083 #include "rutz/trace.h"
00084 #include "rutz/debug.h"
00085 GVX_DBG_REGISTER
00086 
00087 void XBmapRenderer::doRender(Display* display,
00088                              Window win,
00089                              int screen,
00090                              Gfx::Canvas& canvas,
00091                              const media::bmap_data& data,
00092                              const geom::vec2<double>& world_pos,
00093                              const geom::vec2<double>& /* zoom */)
00094 {
00095 GVX_TRACE("XBmapRenderer::doRender");
00096 
00097   XWindowAttributes xwa;
00098   Status status = XGetWindowAttributes(display, win, &xwa);
00099   if (status == 0)
00100     {
00101       throw rutz::error("couldn't get X11 window attributes", SRC_POS);
00102     }
00103   Visual* visual = xwa.visual;
00104 
00105   int format = (data.bits_per_pixel() == 1) ? XYBitmap : ZPixmap;
00106 
00107   data.set_row_order(media::bmap_data::TOP_FIRST);
00108 
00109   // Calculate GL window coordinates of lower left corner of image
00110   geom::vec2i screen_pos = geom::vec2i(canvas.screenFromWorld2(world_pos));
00111 
00112   dbg_dump(3, world_pos);
00113   dbg_dump(3, screen_pos);
00114 
00115   // Calculate GL window coordinates for upper left corner of image
00116   screen_pos.y() += data.height();
00117 
00118   // Calculate X11 window coordinates for upper left corner of image
00119   // (X11 coordinates put 0 at the top of the window, while GL
00120   // coordinates put 0 at the bottom of the window)
00121   screen_pos.y() = xwa.height - screen_pos.y();
00122 
00123   // Create the XImage
00124   XImage* image = XCreateImage(display, visual,
00125                                data.bits_per_pixel(), /* bit depth */
00126                                format, /* format = XYBitmap, XYPixmap, ZPixmap */
00127                                0, /* offset */
00128                                reinterpret_cast<char*>(data.bytes_ptr()),
00129                                data.width(), data.height(),
00130                                data.byte_alignment()*8, /* bitmap_pad */
00131                                0); /* bytes_per_line */
00132 
00133   if (image == NULL)
00134     {
00135       throw rutz::error("couldn't create an XImage", SRC_POS);
00136     }
00137 
00138   // Create a graphics context
00139   XGCValues gc_values;
00140 
00141   gc_values.foreground = XBlackPixel(display, screen);
00142   gc_values.background = XWhitePixel(display, screen);
00143 
00144   unsigned long gc_valuemask = GCForeground | GCBackground;
00145 
00146   GC gfx_context_black = XCreateGC(display, win, gc_valuemask, &gc_values);
00147 
00148   // Draw the image
00149   glXWaitGL();
00150   XPutImage(display, win, gfx_context_black, image,
00151             0, 0, screen_pos.x(), screen_pos.y(),
00152             data.width(), data.height());
00153   glXWaitX();
00154 
00155   XFreeGC(display, gfx_context_black);
00156   XFree(image);
00157 }
00158 
00159 static const char __attribute__((used)) vcid_groovx_gfx_xbmaprenderer_h_utc20050626084024[] = "$Id: xbmaprenderer.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00160 #endif // !GROOVX_GFX_XBMAPRENDERER_H_UTC20050626084024_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:39 2008 by Doxygen version 1.5.5.