Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

BufferedImageGenerator.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_BufferedImageGenerator_h_
00003 #define INCLUDED_BufferedImageGenerator_h_
00004 
00005 #include "FilterBankGenerator.h"
00006 #include "RawCameraGenerator.h"
00007 
00008 //! Receives camera frames as they are loaded by the simulator -- or eventually other sources
00009 class BufferedImageGenerator : public FilterBankGenerator {
00010 public:
00011   //!Stores information about the current frame, (not the image itself, but meta data a pointer to it)
00012   struct ImageSource {
00013     //!constructor
00014     ImageSource() : width(0), height(0), channels(0), frameIndex(0), layer(0), img(NULL) {}
00015     //!copy constructor
00016     ImageSource(const ImageSource& src) : width(src.width), height(src.height), channels(src.channels), frameIndex(src.frameIndex), layer(src.layer), img(src.img) {}
00017     //!assignment operator
00018     ImageSource& operator=(const ImageSource& src) { width=src.width; height=src.height; channels=src.channels; frameIndex=src.frameIndex; layer=src.layer; img=src.img; return *this; } 
00019     unsigned int width; //!< the width of #img
00020     unsigned int height; //!< the height of #img
00021     unsigned int channels; //!< the number of color channels in #img
00022     unsigned int frameIndex; //!< the serial number of the current frame (should be a unique, increasing ID)
00023 
00024     //! indicates what resolution layer of the pipeline this should be used at
00025     /*! Negative values are interpreted as "from the top", so -1 is the topmost layer, -2 is next-to-top, and so on.\n
00026      *  Non-negative values are interpreted as direct layer values, so 0 indicates bottommost layer, 1 indicates next-to-bottom, and so on. */
00027     unsigned int layer; 
00028 
00029     //! pointer to the first byte of the image buffer
00030     /*! img should be stored in a channel-interleaved format, e.g. RGBRGBRGB... */
00031     unsigned char * img;
00032   };
00033 
00034   //! constructor
00035   BufferedImageGenerator(const std::string& name,EventBase::EventGeneratorID_t mgid, unsigned int msid, unsigned int nLayers, EventBase::EventGeneratorID_t srcgid, unsigned int srcsid)
00036     : FilterBankGenerator("BufferedImageGenerator",name,mgid,msid,srcgid,srcsid), imgsrc(), isAllocated(NULL)
00037   { 
00038     /* As a root stage, we need to listen to all incoming image
00039      * events, even if we don't currently have listeners of our own --
00040      * This is just in case user code directly accesses a generator
00041      * and we have to retroactively go back and dig up the previous
00042      * frame */
00043     unsetAutoListen();
00044     
00045     setNumImages(nLayers,RawCameraGenerator::NUM_CHANNELS);
00046   }
00047   //!destructor
00048   virtual ~BufferedImageGenerator() { destruct(); }
00049   
00050   //! need to override EventGeneratorBase's lazy listening -- as a root stage, need to remember each frame, just in case it might be used
00051   virtual void DoStart() { FilterBankGenerator::DoStart(); addSrcListener(); }
00052 
00053   virtual void processEvent(const EventBase & event);
00054   
00055   virtual unsigned int getBinSize() const;
00056   
00057   virtual unsigned int loadBuffer(const char buf[], unsigned int len);
00058   
00059   virtual unsigned int saveBuffer(char buf[], unsigned int len) const;
00060   
00061   virtual unsigned int saveFileStream(FILE* f) const; //!< overrridden to allow saving direct to file without an extra buffer copy
00062 
00063   virtual void freeCaches();
00064   virtual void invalidateCaches();
00065   
00066 protected:
00067   //! constructor for subclasses
00068   BufferedImageGenerator(const std::string& classname,const std::string& instancename,EventBase::EventGeneratorID_t mgid, unsigned int msid, unsigned int nLayers,EventBase::EventGeneratorID_t srcgid, unsigned int srcsid)
00069     : FilterBankGenerator(classname,instancename,mgid,msid,srcgid,srcsid), imgsrc(), isAllocated(NULL)
00070   {setNumImages(nLayers,RawCameraGenerator::NUM_CHANNELS);}
00071   
00072   virtual unsigned char * createImageCache(unsigned int layer, unsigned int channel) const;
00073   virtual void calcImage(unsigned int layer, unsigned int channel) ;
00074   virtual void setDimensions();
00075   virtual void destruct();
00076   virtual void setNumImages(unsigned int nLayers, unsigned int nChannels);
00077 
00078   //! duplicates pixels to make a higher resolution version of @a srcLayer, @a chan into @a destLayer, @a chan
00079   /*! Doesn't do anything fancy like blurring or smoothing */
00080   virtual void upsampleImage(unsigned int srcLayer, unsigned int chan, unsigned int destLayer);
00081   //! averages blocks of pixels to make smaller images
00082   virtual void downsampleImage(unsigned int destLayer, unsigned int chan);
00083   //! calculates the x-derivative
00084   virtual void calcDx(unsigned int layer, unsigned int srcChan=RawCameraGenerator::CHAN_Y, unsigned int dstChan=RawCameraGenerator::CHAN_Y_DX);
00085   //! calculates the x-derivative
00086   virtual void calcDy(unsigned int layer, unsigned int srcChan=RawCameraGenerator::CHAN_Y, unsigned int dstChan=RawCameraGenerator::CHAN_Y_DY);
00087   //! calculates the diagonal derivative
00088   virtual void calcDxDy(unsigned int layer);
00089   
00090   ImageSource imgsrc; //!< the data storage of the current image (not the image itself, but meta data a pointer to it)
00091   bool ** isAllocated; //!< for each image in the filterbank, a bool to account whether the pointer is to an external resource or a self-allocated resource
00092   
00093 private:
00094   BufferedImageGenerator(const BufferedImageGenerator&); //!< don't call
00095   BufferedImageGenerator& operator=(const BufferedImageGenerator&); //!< don't call
00096 };
00097 
00098 /*! @file
00099  * @brief Describes BufferedImageGenerator, which receives camera frames as they are loaded by the simulator -- or eventually other sources
00100  * @author Ethan Tira-Thompson (ejt) (Creator)
00101  *
00102  * $Author: ejt $
00103  * $Name: tekkotsu-4_0 $
00104  * $Revision: 1.10 $
00105  * $State: Exp $
00106  * $Date: 2006/09/09 04:33:04 $
00107  */
00108 
00109 #endif

Tekkotsu v4.0
Generated Thu Nov 22 00:54:51 2007 by Doxygen 1.5.4