Tekkotsu 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 #include "Shared/Measures.h"
00006 
00007 class FilterBankGenerator;
00008 
00009 //! Provides basic graphics capabilities for drawing into any bitmap, particularly FilterBankGenerators
00010 /*! Wherever possible, this should try to emulate the graphics API of Java 1 to minimize learning curve.
00011  *  For instance, the pen hangs down and to the right. */
00012 class Graphics {
00013 public:
00014   //! constructor, pass a FilterBankGenerator and layer/channel to draw into
00015   Graphics(FilterBankGenerator& fbg, unsigned int layer, unsigned int channel);
00016 
00017   //! constructor, pass a FilterBankGenerator and layer/channel to draw into
00018   Graphics(FilterBankGenerator& fbg, unsigned int layer, unsigned int chan1, unsigned int chan2, unsigned int chan3);
00019 
00020   //! constructor, directly specify an image buffer
00021   Graphics(unsigned char * base, unsigned int width, unsigned int height, unsigned int interval, unsigned int stride);
00022 
00023   //! 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
00024   /*! This is automatically called by the constructor, so you don't need to do it if you
00025    *  constructor a fresh Graphics object for each frame.  But otherwise you'll need this
00026    *  to update #img, #w, #h, #xInc, and #yInc from the current frame available in #gen */
00027   void updateFBG(); 
00028 
00029   //! Draws a rectange, upper left at @a x,@a y and extending right and down by @a width and @a height
00030   /*! This expects direct-pixel coordinates, so make sure you check the
00031    *  width and height of the layer you are drawing into */
00032   void drawRect(int x, int y, int width, int height);
00033 
00034   //! Draws a rectange, upper left at @a x,@a y and extending right and down by @a width and @a height
00035   /*  This expects resolution independent coordinates, such that
00036    *  x ranges [-1,1] and y ranges plus/minus the aspect ratio */
00037   void drawRect(float x, float y, float width, float height);
00038 
00039   //! Draws a line from (@a x1, @a y1) to (@a x2, @a y2)
00040   /*! This expects direct-pixel coordinates, so make sure you check the
00041    *  width and height of the layer you are drawing into */
00042   void drawLine(int x1, int y1, int x2, int y2);
00043 
00044   //! Draws a line from (@a x1, @a y1) to (@a x2, @a y2)
00045   /*  This expects resolution independent coordinates, such that
00046    *  x ranges [-1,1] and y ranges plus/minus the aspect ratio */
00047   void drawLine(float x1, float y1, float x2, float y2);
00048 
00049   //! Draws an ellipse at (x,y) with the specified parameters
00050   /*  This expects resolution independent coordinates, such that
00051    *  x ranges [-1,1] and y ranges plus/minus the aspect ratio */
00052   void drawEllipse(float x, float y, float semimajor, float semiminor, AngPi orientation);
00053 
00054   //! Draws an ellipse at (x,y) with the specified parameters
00055   /*! This expects direct-pixel coordinates, so make sure you check the
00056    *  width and height of the layer you are drawing into */
00057   void drawEllipse(int x, int y, float semimajor, float semiminor, AngPi orientation);
00058 
00059   //! Draws a quarter ellipse at (x,y) with the specified parameters; semimajor and/or semiminor may be negative.
00060   /*! This expects direct-pixel coordinates, so make sure you check the
00061    *  width and height of the layer you are drawing into */
00062   void drawQuarterEllipse(int x, int y, float semimajor, float semiminor, AngPi orientation);
00063 
00064   //! Draws a single point at (@a x1, @a y1)
00065   /*! This expects direct-pixel coordinates, so make sure you check the
00066    *  width and height of the layer you are drawing into */
00067   inline void drawPoint(int x, int y) {
00068     if(x<0 || y<0 || (unsigned int)x>=w || (unsigned int)y>=h)
00069       return;
00070     *(img1+y*yInc+x*xInc)=color.y;
00071     *(img2+y*yInc+x*xInc)=color.u;
00072     *(img3+y*yInc+x*xInc)=color.v;
00073   }
00074   //! Draws a single point at (@a x1, @a y1)
00075   /*  This expects resolution independent coordinates, such that
00076    *  x ranges [-1,1] and y ranges plus/minus the aspect ratio */
00077   void drawPoint(float x, float y) {
00078     unsigned int px,py;
00079     getPixelCoordinates(px,py,x,y);
00080     drawPoint((int)px,(int)py);
00081   }
00082 
00083   //! Sets the "color" of the pen
00084   /*! Currently we don't support multi-channel drawing, so you have to draw into
00085    *  each channel separately to do real color based drawing, but maybe someday
00086    *  we'll add a color class.\n
00087    *  In the mean time, this is just the byte that's going to be used to fill in
00088    *  wherever the pen traces */
00089   void setColor(rgb _color) { color=rgb2yuv(_color); }
00090 
00091   //! Sets the "color" of the pen when drawing into a SegCam buffer
00092   void setColor(unsigned char _color) { color=yuv(_color, _color, _color); }
00093 
00094   //! returns the "color" of the pen
00095   /*! Currently we don't support multi-channel drawing, so you have to draw into
00096    *  each channel separately to do real color based drawing, but maybe someday
00097    *  we'll add a color class.\n
00098    *  In the mean time, this is just the byte that's going to be used to fill in
00099    *  wherever the pen traces */
00100   yuv getColor() const { return color; }
00101 
00102   //! sets the pixel-coordinate px and py parameters to the corresponding value of x and y
00103   /*! @param[out] px      the pixel position, relative to left edge, positive right, ranges 0 through width-1
00104    *  @param[out] py      the pixel position, relative to top edge, positive down, ranges 0 through height-1
00105    *  @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
00106    *  @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
00107    *
00108    *  To keep the coordinate system square, the x is defined to range -1,1, but y's range depends on the
00109    *  aspect ratio of the image, height/width.  Thus typically y will approx. -.75,.75 */
00110   void getPixelCoordinates(unsigned int& px, unsigned int& py, float x, float y) const;
00111   
00112   //! sets the x and y parameters from the pixel-coordinates px and py
00113   /*! @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
00114    *  @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
00115    *  @param[in]  px      the pixel position, relative to left edge, positive right, ranges 0 through width-1
00116    *  @param[in]  py      the pixel position, relative to top edge, positive down, ranges 0 through height-1
00117    *
00118    *  To keep the coordinate system square, the x is defined to range -1,1, but y's range depends on the
00119    *  aspect ratio of the image, height/width.  Thus typically y will approx. -.75,.75 */
00120   void getRealCoordinates(float& x, float& y, unsigned int px, unsigned int py) const;
00121   
00122 protected:
00123   FilterBankGenerator* gen; //!< the filter bank generator we are drawing into, or NULL
00124   unsigned int genLayer; //!< the layer within #gen we are drawing into
00125   unsigned int genChan1; //!< the channel within #gen we are drawing into
00126   unsigned int genChan2; //!< the channel within #gen we are drawing into
00127   unsigned int genChan3; //!< the channel within #gen we are drawing into
00128 
00129   unsigned char* img1; //!< the image we are currently drawing into (may need to be updated if #gen is non-NULL, see updateFBG())
00130   unsigned char* img2; //!< the image we are currently drawing into (may need to be updated if #gen is non-NULL, see updateFBG())
00131   unsigned char* img3; //!< the image we are currently drawing into (may need to be updated if #gen is non-NULL, see updateFBG())
00132   unsigned int w; //!< the width of #img
00133   unsigned int h; //!< the height of #img
00134   unsigned int xInc; //!< the number of bytes to skip to move horizontally one pixel in #img
00135   unsigned int yInc; //!< the number of bytes to skip to move vertically one pixel in #img
00136 
00137   yuv color; //!< the current pen color
00138 
00139 private:
00140   // Providing declarations for these functions will avoid a compiler warning if
00141   // you have any class members which are pointers.  However, as it is, an error
00142   // will result if you inadvertantly cause a call to either (which is probably
00143   // a good thing, unless you really intended to copy/assign a behavior, in
00144   // which case simply provide implementations for the functions)
00145   Graphics(const Graphics&); //!< don't call (copy constructor)
00146   Graphics& operator=(const Graphics&); //!< don't call (assignment operator)
00147 };
00148 
00149 /*! @file
00150  * @brief 
00151  * @author ejt (Creator)
00152  */
00153 
00154 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:41 2016 by Doxygen 1.6.3