Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

Graphics.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_Graphics_h_
00003 #define INCLUDED_Graphics_h_
00004 
00005 class FilterBankGenerator;
00006 
00007 //! Provides basic graphics capabilities for drawing into any bitmap, particularly FilterBankGenerators
00008 /*! Wherever possible, this should try to emulate the graphics API of Java 1 to minimize learning curve.
00009  *  For instance, the pen hangs down and to the right. */
00010 class Graphics {
00011 public:
00012   //! constructor, pass a FilterBankGenerator and layer/channel to draw into
00013   Graphics(FilterBankGenerator& fbg, unsigned int layer, unsigned int channel);
00014   //! constructor, directly specify an image buffer
00015   Graphics(unsigned char * base, unsigned int width, unsigned int height, unsigned int interval, unsigned int stride);
00016 
00017   //! If you want to reuse a graphics object across multiple frames from a FilterBankGenerator, call this after each new frame, but before you do any drawing
00018   /*! This is automatically called by the constructor, so you don't need to do it if you
00019    *  constructor a fresh Graphics object for each frame.  But otherwise you'll need this
00020    *  to update #img, #w, #h, #xInc, and #yInc from the current frame available in #gen */
00021   void updateFBG(); 
00022 
00023   //! Draws a rectange, upper left at @a x,@a y and extending right and down by @a width and @a height
00024   /*! This expects direct-pixel coordinates, so make sure you check the
00025    *  width and height of the layer you are drawing into */
00026   void drawRect(int x, int y, int width, int height);
00027   //! Draws a rectange, upper left at @a x,@a y and extending right and down by @a width and @a height
00028   /*  This expects resolution independent coordinates, such that
00029    *  x ranges [-1,1] and y ranges plus/minus the aspect ratio */
00030   void drawRect(float x, float y, float width, float height);
00031 
00032   //! Draws a line from (@a x1, @a y1) to (@a x2, @a y2)
00033   /*! This expects direct-pixel coordinates, so make sure you check the
00034    *  width and height of the layer you are drawing into */
00035   void drawLine(int x1, int y1, int x2, int y2);
00036   //! Draws a line from (@a x1, @a y1) to (@a x2, @a y2)
00037   /*  This expects resolution independent coordinates, such that
00038    *  x ranges [-1,1] and y ranges plus/minus the aspect ratio */
00039   void drawLine(float x1, float y1, float x2, float y2);
00040 
00041   //! Draws a single point at (@a x1, @a y1)
00042   /*! This expects direct-pixel coordinates, so make sure you check the
00043    *  width and height of the layer you are drawing into */
00044   inline void drawPoint(int x, int y) {
00045     if(x<0 || y<0 || (unsigned int)x>=w || (unsigned int)y>=h)
00046       return;
00047     *(img+y*yInc+x*xInc)=c;
00048   }
00049   //! Draws a single point at (@a x1, @a y1)
00050   /*  This expects resolution independent coordinates, such that
00051    *  x ranges [-1,1] and y ranges plus/minus the aspect ratio */
00052   void drawPoint(float x, float y) {
00053     unsigned int px,py;
00054     getPixelCoordinates(px,py,x,y);
00055     drawPoint((int)px,(int)py);
00056   }
00057 
00058   //! Sets the "color" of the pen
00059   /*! Currently we don't support multi-channel drawing, so you have to draw into
00060    *  each channel separately to do real color based drawing, but maybe someday
00061    *  we'll add a color class.\n
00062    *  In the mean time, this is just the byte that's going to be used to fill in
00063    *  wherever the pen traces */
00064   void setColor(char color) { c=color; }
00065   //! returns the "color" of the pen
00066   /*! Currently we don't support multi-channel drawing, so you have to draw into
00067    *  each channel separately to do real color based drawing, but maybe someday
00068    *  we'll add a color class.\n
00069    *  In the mean time, this is just the byte that's going to be used to fill in
00070    *  wherever the pen traces */
00071   char getColor() const { return c; }
00072 
00073   //! sets the pixel-coordinate px and py parameters to the corresponding value of x and y
00074   /*! @param[out] px      the pixel position, relative to left edge, positive right, ranges 0 through width-1
00075    *  @param[out] py      the pixel position, relative to top edge, positive down, ranges 0 through height-1
00076    *  @param[in]  x       the horizontal position, relative to center of the image, left edge is -1 and right edge is 1; no boundary checking is done
00077    *  @param[in]  y       the vertical pixel position, relative to center of the image, top edge is the negative aspect ratio, bottom edge is positive aspect ratio; no boundary checking is done
00078    *
00079    *  To keep the coordinate system square, the x is defined to range -1,1, but y's range depends on the
00080    *  aspect ratio of the image, height/width.  Thus typically y will approx. -.75,.75 */
00081   void getPixelCoordinates(unsigned int& px, unsigned int& py, float x, float y) const;
00082   
00083   //! sets the x and y parameters from the pixel-coordinates px and py
00084   /*! @param[out] x       the horizontal position, relative to center of the image, left edge is -1 and right edge is 1; no boundary checking is done
00085    *  @param[out] y       the vertical pixel position, relative to center of the image, top edge is the negative aspect ratio, bottom edge is positive aspect ratio; no boundary checking is done
00086    *  @param[in]  px      the pixel position, relative to left edge, positive right, ranges 0 through width-1
00087    *  @param[in]  py      the pixel position, relative to top edge, positive down, ranges 0 through height-1
00088    *
00089    *  To keep the coordinate system square, the x is defined to range -1,1, but y's range depends on the
00090    *  aspect ratio of the image, height/width.  Thus typically y will approx. -.75,.75 */
00091   void getRealCoordinates(float& x, float& y, unsigned int px, unsigned int py) const;
00092   
00093 protected:
00094   FilterBankGenerator * gen; //!< the filter bank generator we are drawing into, or NULL
00095   unsigned int genLayer; //!< the layer within #gen we are drawing into
00096   unsigned int genChan; //!< the channel within #gen we are drawing into
00097 
00098   unsigned char * img; //!< the image we are currently drawing into (may need to be updated if #gen is non-NULL, see updateFBG())
00099   unsigned int w; //!< the width of #img
00100   unsigned int h; //!< the height of #img
00101   unsigned int xInc; //!< the number of bytes to skip to move horizontally one pixel in #img
00102   unsigned int yInc; //!< the number of bytes to skip to move vertically one pixel in #img
00103 
00104   unsigned char c; //!< the current pen color
00105 
00106 private:
00107   // Providing declarations for these functions will avoid a compiler warning if
00108   // you have any class members which are pointers.  However, as it is, an error
00109   // will result if you inadvertantly cause a call to either (which is probably
00110   // a good thing, unless you really intended to copy/assign a behavior, in
00111   // which case simply provide implementations for the functions)
00112   Graphics(const Graphics&); //!< don't call (copy constructor)
00113   Graphics& operator=(const Graphics&); //!< don't call (assignment operator)
00114 };
00115 
00116 /*! @file
00117  * @brief 
00118  * @author ejt (Creator)
00119  *
00120  * $Author: ejt $
00121  * $Name: tekkotsu-2_4_1 $
00122  * $Revision: 1.4 $
00123  * $State: Exp $
00124  * $Date: 2005/08/07 04:11:04 $
00125  */
00126 
00127 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:47 2005 by Doxygen 1.4.4