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

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