Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

JPEGGenerator.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_JPEGGenerator_h_
00003 #define INCLUDED_JPEGGenerator_h_
00004 
00005 #include "Vision/FilterBankGenerator.h"
00006 
00007 extern "C" {
00008 #include <jpeglib.h>
00009 }
00010 
00011 //! Generates FilterBankEvents containing JPEG compressed images
00012 /*! There's only one channel per resolution layer, which holds the
00013  *  compressed data.  This is mostly intended for being sent over
00014  *  wireless, but if you know how JPEG works, you may be able to
00015  *  interpret the compressed data directly and glean useful
00016  *  information from it.  After all, compression is all about throwing
00017  *  away unimportant details and storing the salient information.
00018  *  
00019  *  The generated events use 0 for their event source IDs.  The row
00020  *  skip and row stride are 0. (they don't really apply here)
00021  *
00022  *  This can either compress a greyscale image or an interleaved YUV
00023  *  image.  If the source generator's type is not
00024  *  InterleavedYUVGenerator, it will assume greyscale.  Call
00025  *  setSource() to override this.
00026  *
00027  *  The InterleavedYUVGenerator is separated from this because it
00028  *  wouldn't really make things much faster to combine the algorithms,
00029  *  and others might find the interleaved format handy for passing to
00030  *  other libraries which expect that format, such as what happened
00031  *  with libjpeg.
00032  *  
00033  *  This class shares a lot of non-JPEG specific code with PNGGenerator,
00034  *  so you may want to replicate any changes made in that class as well.
00035  *
00036  *  @todo possible speedup by using jpeg_write_raw_data
00037  *  @todo create a common super class with PNGGenerator to hold common setup code
00038  */
00039 class JPEGGenerator : public FilterBankGenerator {
00040 public:
00041   static const unsigned int JPEG_HEADER_PAD=500; //!< add a bit to the expected size in getBinSize just to leave a little extra room for small images
00042   
00043   //! defines how to interpret the source images (see #srcMode and #curMode)
00044   enum src_mode_t {
00045     SRC_AUTO,       //!< if src is not a InterleavedYUVGenerator, SRC_GREYSCALE, otherwise SRC_COLOR
00046     SRC_GRAYSCALE,  //!< indicates each channel of source should be compressed individually as grayscale images
00047     SRC_COLOR       //!< indicates first channel should be in an interleaved layout which can be compressed into a "color" image
00048   };
00049 
00050   //! constructor
00051   JPEGGenerator(unsigned int mysid, FilterBankGenerator* fbg, EventBase::EventTypeID_t tid);
00052   //! constructor
00053   JPEGGenerator(unsigned int mysid, JPEGGenerator::src_mode_t sMode, FilterBankGenerator* fbg, EventBase::EventTypeID_t tid);
00054 
00055   //! destructor
00056   virtual ~JPEGGenerator();
00057 
00058   //! set #srcMode and #curMode as well if @a mode==SRC_AUTO
00059   virtual void setSourceMode(src_mode_t mode) { srcMode=mode; if(mode!=SRC_AUTO) curMode=mode;}
00060   //! returns #srcMode
00061   virtual src_mode_t getSourceMode() const { return srcMode; }
00062   //! returns #curMode
00063   virtual src_mode_t getCurrentSourceFormat() const { return curMode; }
00064 
00065   static std::string getClassDescription() { return "Compresses its source FilterBankGenerator's data into JPEG format"; }
00066 
00067   //! should receive FilterBankEvents from a RawCameraGenerator (or a subclass thereof)
00068   virtual void processEvent(const EventBase& event);
00069   
00070   //! if we don't already know bytesUsed, let's assume the size will be smaller than the original uncompressed. If we fail this assumption, probably better to fail anyway.
00071   virtual unsigned int getBinSize() const;
00072   
00073   virtual unsigned int loadBuffer(const char buf[], unsigned int len);
00074   
00075   //! you probably don't want to be calling this to access the JPEG -- use getImage() instead (saveBuffer will prepend some header information before the actual image data)
00076   virtual unsigned int saveBuffer(char buf[], unsigned int len) const;
00077 
00078   //! returns #quality
00079   virtual unsigned int getQuality() const { return quality; }
00080   //! sets #quality; this will invalidate the cache if @a q does not equal current #quality
00081   virtual void setQuality(unsigned int q) {
00082     if(quality!=q) {
00083       quality=q; 
00084       invalidateCaches();
00085     }
00086   }
00087 
00088   //! returns the number of bytes used for the image returned by getImage() - will return 0 if the image hasn't been calculated yet (so call it @e after getImage())
00089   virtual unsigned int getImageSize(unsigned int layer, unsigned int chan) const { return bytesUsed[layer][chan]; }
00090 
00091 protected:
00092   virtual void setNumImages(unsigned int nLayers, unsigned int nChannels);
00093   virtual unsigned char * createImageCache(unsigned int layer, unsigned int chan) const;
00094   virtual void calcImage(unsigned int layer, unsigned int chan);
00095   virtual void destruct();
00096 
00097   src_mode_t srcMode;   //!< how to interpret source channel of next filter bank event
00098   src_mode_t curMode;   //!< how to interpret getImage's current image
00099 
00100   unsigned int ** bytesUsed; //!< number of bytes used per image to actually store data;
00101 
00102   struct jpeg_compress_struct cinfo; //!< used to interface with libjpeg - holds compression parameters and state
00103   struct jpeg_error_mgr jerr;        //!< used to interface with libjpeg - gives us access to error information
00104 
00105   unsigned int quality; //!< quality level to pass to libjpeg; -1U causes Config::vision_config::rawcam_compress_quality to be used
00106   
00107 private:
00108   JPEGGenerator(const JPEGGenerator& fbk); //!< don't call
00109   const JPEGGenerator& operator=(const JPEGGenerator& fbk); //!< don't call
00110 };
00111 
00112 /*! @file 
00113  * @brief Describes JPEGGenerator, which generates FilterBankEvents containing JPEG compressed images
00114  * @author ejt (Creator)
00115  *
00116  * $Author: ejt $
00117  * $Name: tekkotsu-3_0 $
00118  * $Revision: 1.13 $
00119  * $State: Exp $
00120  * $Date: 2006/09/16 17:32:40 $
00121  */
00122 
00123 #endif

Tekkotsu v3.0
Generated Wed Oct 4 00:03:43 2006 by Doxygen 1.4.7