Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Grabber.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLIO/src/ICLIO/Grabber.h                              **
00010 ** Module : ICLIO                                                  **
00011 ** Authors: Christof Elbrechter, Michael Goetting, Robert          **
00012 **          Haschke, Viktor Richter                                **
00013 **                                                                 **
00014 **                                                                 **
00015 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00016 ** This file may be used under the terms of the GNU Lesser General **
00017 ** Public License version 3.0 as published by the                  **
00018 **                                                                 **
00019 ** Free Software Foundation and appearing in the file LICENSE.GPL  **
00020 ** included in the packaging of this file.  Please review the      **
00021 ** following information to ensure the license requirements will   **
00022 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00023 **                                                                 **
00024 ** The development of this software was supported by the           **
00025 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00026 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00027 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00028 ** Excellence Initiative.                                          **
00029 **                                                                 **
00030 ********************************************************************/
00031 
00032 #pragma once
00033 
00034 #include <string>
00035 #include <vector>
00036 #include <set>
00037 
00038 #include <ICLCore/ImgBase.h>
00039 #include <ICLUtils/SteppingRange.h>
00040 #include <ICLUtils/Function.h>
00041 #include <ICLUtils/Uncopyable.h>
00042 #include <ICLUtils/Configurable.h>
00043 #include <ICLUtils/ProgArg.h>
00044 #include <ICLIO/GrabberDeviceDescription.h>
00045 #include <ICLIO/ImageUndistortion.h>
00046 #include <ICLUtils/Uncopyable.h>
00047 
00048 namespace icl {
00049   namespace io{
00050 
00052     namespace{
00053       template <class T> inline T grabber_get_null(){ return 0; }
00054       template <> inline core::format grabber_get_null<core::format>(){ return (core::format)-1; }
00055       template <> inline core::depth grabber_get_null<core::depth>(){ return (core::depth)-1; }
00056       template <> inline icl::utils::Size grabber_get_null<icl::utils::Size>(){ return icl::utils::Size::null; }
00057 
00058       struct grabber_get_xxx_dummy{
00059           grabber_get_xxx_dummy(){
00060             grabber_get_null<core::format>();
00061             grabber_get_null<core::depth>();
00062             grabber_get_null<icl::utils::Size>();
00063           }
00064       };
00065     }
00066     template <class T> class GrabberHandle;
00067     class GenericGrabber;
00070 
00071 
00152     class Grabber : public utils::Uncopyable, public utils::Configurable{
00154         struct Data;
00155 
00157         Data *data;
00158 
00159       protected:
00161         virtual void setDesiredFormatInternal(core::format fmt);
00162 
00164         virtual void setDesiredSizeInternal(const utils::Size &size);
00165 
00167         virtual void setDesiredDepthInternal(core::depth d);
00168 
00170         virtual core::format getDesiredFormatInternal() const;
00171 
00173         virtual core::depth getDesiredDepthInternal() const;
00174 
00176         virtual utils::Size getDesiredSizeInternal() const;
00177 
00178       public:
00179 
00181         template<class X> friend class GrabberHandle;
00182 
00184         friend class GenericGrabber;
00185 
00187         Grabber();
00188 
00190         virtual ~Grabber();
00191 
00193 
00194         const core::ImgBase *grab(core::ImgBase **dst=0);
00195 
00197 
00198         template<class T>
00199         bool desiredUsed() const{ return false; }
00200 
00202         template<class T>
00203         void useDesired(const T &t){ (void)t;}
00204 
00206         void useDesired(core::depth d, const utils::Size &size, core::format fmt);
00207 
00209 
00210         template<class T>
00211         void ignoreDesired() {
00212           useDesired<T>(grabber_get_null<T>());
00213         }
00214 
00216         void ignoreDesired();
00217 
00219 
00220         template<class T>
00221         T getDesired() const { return T(); }
00222 
00225 
00227         static std::string translateSteppingRange(const utils::SteppingRange<double>& range);
00228 
00230         static utils::SteppingRange<double> translateSteppingRange(const std::string &rangeStr);
00231 
00233         static std::string translateDoubleVec(const std::vector<double> &doubleVec);
00234 
00236         static std::vector<double> translateDoubleVec(const std::string &doubleVecStr);
00237 
00239         static std::string translateStringVec(const std::vector<std::string> &stringVec);
00240 
00242         static std::vector<std::string> translateStringVec(const std::string &stringVecStr);
00243 
00245 
00247 
00249         void enableUndistortion(const std::string &filename);
00250 
00252         void enableUndistortion(const ImageUndistortion &udist);
00253 
00255 
00256         void enableUndistortion(const utils::ProgArg &pa);
00257 
00259         void enableUndistortion(const core::Img32f &warpMap);
00260 
00262 
00265         void setUndistortionInterpolationMode(core::scalemode mode);
00266 
00268         void disableUndistortion();
00269 
00271         bool isUndistortionEnabled() const;
00272 
00274         const core::Img32f *getUndistortionWarpMap() const;
00276 
00278         typedef utils::Function<void,const core::ImgBase*> callback;
00279 
00281 
00286         virtual void registerCallback(callback cb);
00287 
00289         virtual void removeAllCallbacks();
00290 
00292 
00293         virtual void notifyNewImageAvailable(const core::ImgBase *image);
00294 
00295       protected:
00296 
00297 
00299 
00301         virtual const core::ImgBase *acquireImage(){ return NULL; }
00302 
00304 
00305         const core::ImgBase *adaptGrabResult(const core::ImgBase *src, core::ImgBase **dst);
00306 
00307       private:
00309         void processPropertyChange(const utils::Configurable::Property &prop);
00310 
00311     };
00312     
00314     template<> inline void Grabber::useDesired<core::format>(const core::format &t) { setDesiredFormatInternal(t); }
00315     template<> inline void Grabber::useDesired<core::depth>(const core::depth &t) { setDesiredDepthInternal(t); }
00316     template<> inline void Grabber::useDesired<utils::Size>(const utils::Size &t) { setDesiredSizeInternal(t); }
00317 
00318     template<> inline core::depth Grabber::getDesired<core::depth>() const { return getDesiredDepthInternal(); }
00319     template<> inline utils::Size Grabber::getDesired<utils::Size>() const { return getDesiredSizeInternal(); }
00320     template<> inline core::format Grabber::getDesired<core::format>() const { return getDesiredFormatInternal(); }
00321 
00322     template<> inline bool Grabber::desiredUsed<core::format>() const{ return (int)getDesired<core::format>() != -1; }
00323     template<> inline bool Grabber::desiredUsed<core::depth>() const{ return (int)getDesired<core::depth>() != -1; }
00324     template<> inline bool Grabber::desiredUsed<utils::Size>() const{ return getDesired<utils::Size>() != utils::Size::null; }
00325 
00326     class GrabberRegister : utils::Uncopyable {
00327       private:
00328         utils::Mutex mutex;
00329 
00330         struct GrabberFunctions{
00331           utils::Function<Grabber*,const std::string&> init;
00332           utils::Function<const std::vector<GrabberDeviceDescription> &,std::string,bool> list;
00333         };
00334 
00335         // grabber functions map
00336         typedef std::map<std::string, GrabberFunctions> GFM;
00337         GFM gfm;
00338 
00339         // grabber bus reset functions map
00340         typedef std::map<std::string, utils::Function<void,bool> > GBRM;
00341         GBRM gbrm;
00342 
00343         // grabber device descriptions map
00344         typedef std::set<std::string> GDS;
00345         GDS gds;
00346 
00347         // private constructor
00348         GrabberRegister(){}
00349 
00350       public:
00351         static GrabberRegister* getInstance();
00352 
00353         void registerGrabberType(const std::string &grabberid,
00354                                    utils::Function<Grabber *, const std::string &> creator,
00355                                    utils::Function<const std::vector<GrabberDeviceDescription> &,std::string,bool> device_list)
00356         throw (utils::ICLException);
00357 
00358         void registerGrabberBusReset(const std::string &grabberid,
00359                                    utils::Function<void, bool> reset_function)
00360         throw (utils::ICLException);
00361 
00362         void addGrabberDescription(const std::string &grabber_description)
00363         throw (utils::ICLException);
00364 
00365         Grabber* createGrabber(const std::string &grabberid, const std::string &param) throw (utils::ICLException);
00366 
00367         std::vector<std::string> getRegisteredGrabbers();
00368 
00369         std::vector<std::string> getGrabberInfos();
00370 
00371         const std::vector<GrabberDeviceDescription>& getDeviceList(std::string id, std::string hint="", bool rescan=true);
00372 
00373         void resetGrabberBus(const std::string &id, bool verbose);
00374     };
00375 
00378 
00379 
00380     #define REGISTER_GRABBER(NAME,CREATE_FUNC,DEVICE_LIST_FUNC,DESCRIPTION)                                        \
00381     struct StaticGrabberRegistrationFor_##NAME{                                                                    \
00382       StaticGrabberRegistrationFor_##NAME(){                                                                       \
00383         icl::io::GrabberRegister::getInstance() -> registerGrabberType(#NAME, CREATE_FUNC, DEVICE_LIST_FUNC);      \
00384         icl::io::GrabberRegister::getInstance() -> addGrabberDescription(DESCRIPTION);                             \
00385       }                                                                                                            \
00386     } staticGrabberRegistrationFor_##NAME;
00387 
00388     #define REGISTER_GRABBER_BUS_RESET_FUNCTION(NAME,BUS_RESET_FUNC)                                               \
00389     struct StaticGrabberBusResetRegistrationFor_##NAME{                                                            \
00390       StaticGrabberBusResetRegistrationFor_##NAME(){                                                               \
00391         icl::io::GrabberRegister::getInstance() -> registerGrabberBusReset(#NAME, BUS_RESET_FUNC);                 \
00392       }                                                                                                            \
00393     } staticGrabberBusResetRegistrationFor_##NAME;
00394 
00395   } // namespace io
00396 } // namespace icl
00397 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines