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.LGPL **
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 <ICLUtils/CompatMacros.h>
00035 #include <ICLUtils/SteppingRange.h>
00036 #include <ICLUtils/Function.h>
00037 #include <ICLUtils/Uncopyable.h>
00038 #include <ICLUtils/Configurable.h>
00039 #include <ICLUtils/ProgArg.h>
00040 #include <ICLUtils/Uncopyable.h>
00041 #include <ICLCore/ImgBase.h>
00042 #include <ICLIO/GrabberDeviceDescription.h>
00043 #include <ICLIO/ImageUndistortion.h>
00044 
00045 #include <string>
00046 #include <vector>
00047 #include <set>
00048 
00049 namespace icl {
00050   namespace io{
00051 
00053     namespace{
00054       template <class T> inline T grabber_get_null(){ return 0; }
00055       template <> inline core::format grabber_get_null<core::format>(){ return (core::format)-1; }
00056       template <> inline core::depth grabber_get_null<core::depth>(){ return (core::depth)-1; }
00057       template <> inline icl::utils::Size grabber_get_null<icl::utils::Size>(){ return icl::utils::Size::null; }
00058 
00059       struct grabber_get_xxx_dummy{
00060           grabber_get_xxx_dummy(){
00061             grabber_get_null<core::format>();
00062             grabber_get_null<core::depth>();
00063             grabber_get_null<icl::utils::Size>();
00064           }
00065       };
00066     }
00067     template <class T> class GrabberHandle;
00068     class GenericGrabber;
00071 
00072 
00153     class ICLIO_API Grabber : public utils::Uncopyable, public utils::Configurable{
00155         struct Data;
00156 
00158         Data *data;
00159 
00160       protected:
00162         virtual void setDesiredFormatInternal(core::format fmt);
00163 
00165         virtual void setDesiredSizeInternal(const utils::Size &size);
00166 
00168         virtual void setDesiredDepthInternal(core::depth d);
00169 
00171         virtual core::format getDesiredFormatInternal() const;
00172 
00174         virtual core::depth getDesiredDepthInternal() const;
00175 
00177         virtual utils::Size getDesiredSizeInternal() const;
00178 
00179       public:
00180 
00182         template<class X> friend class GrabberHandle;
00183 
00185         friend class GenericGrabber;
00186 
00188         Grabber();
00189 
00191         virtual ~Grabber();
00192 
00194 
00195         const core::ImgBase *grab(core::ImgBase **dst=0);
00196 
00198 
00199         template<class T>
00200         bool desiredUsed() const{ return false; }
00201 
00203         template<class T>
00204         void useDesired(const T &t){ (void)t;}
00205 
00207         void useDesired(core::depth d, const utils::Size &size, core::format fmt);
00208 
00210 
00211         template<class T>
00212         void ignoreDesired() {
00213           useDesired<T>(grabber_get_null<T>());
00214         }
00215 
00217         void ignoreDesired();
00218 
00220 
00221         template<class T>
00222         T getDesired() const { return T(); }
00223 
00226 
00228         static std::string translateSteppingRange(const utils::SteppingRange<double>& range);
00229 
00231         static utils::SteppingRange<double> translateSteppingRange(const std::string &rangeStr);
00232 
00234         static std::string translateDoubleVec(const std::vector<double> &doubleVec);
00235 
00237         static std::vector<double> translateDoubleVec(const std::string &doubleVecStr);
00238 
00240         static std::string translateStringVec(const std::vector<std::string> &stringVec);
00241 
00243         static std::vector<std::string> translateStringVec(const std::string &stringVecStr);
00244 
00246 
00248 
00250         void enableUndistortion(const std::string &filename);
00251 
00253         void enableUndistortion(const ImageUndistortion &udist);
00254 
00256 
00257         void enableUndistortion(const utils::ProgArg &pa);
00258 
00260         void enableUndistortion(const core::Img32f &warpMap);
00261 
00263 
00266         void setUndistortionInterpolationMode(core::scalemode mode);
00267 
00269         void disableUndistortion();
00270 
00272         bool isUndistortionEnabled() const;
00273 
00275         const core::Img32f *getUndistortionWarpMap() const;
00277 
00279         typedef utils::Function<void,const core::ImgBase*> callback;
00280 
00282 
00287         virtual void registerCallback(callback cb);
00288 
00290         virtual void removeAllCallbacks();
00291 
00293 
00294         virtual void notifyNewImageAvailable(const core::ImgBase *image);
00295 
00296       protected:
00297 
00298 
00300 
00302         virtual const core::ImgBase *acquireImage(){ return NULL; }
00303 
00305 
00306         const core::ImgBase *adaptGrabResult(const core::ImgBase *src, core::ImgBase **dst);
00307 
00308       private:
00310         void processPropertyChange(const utils::Configurable::Property &prop);
00311 
00312     };
00313     
00315     template<> inline void Grabber::useDesired<core::format>(const core::format &t) { setDesiredFormatInternal(t); }
00316     template<> inline void Grabber::useDesired<core::depth>(const core::depth &t) { setDesiredDepthInternal(t); }
00317     template<> inline void Grabber::useDesired<utils::Size>(const utils::Size &t) { setDesiredSizeInternal(t); }
00318 
00319     template<> inline core::depth Grabber::getDesired<core::depth>() const { return getDesiredDepthInternal(); }
00320     template<> inline utils::Size Grabber::getDesired<utils::Size>() const { return getDesiredSizeInternal(); }
00321     template<> inline core::format Grabber::getDesired<core::format>() const { return getDesiredFormatInternal(); }
00322 
00323     template<> inline bool Grabber::desiredUsed<core::format>() const{ return (int)getDesired<core::format>() != -1; }
00324     template<> inline bool Grabber::desiredUsed<core::depth>() const{ return (int)getDesired<core::depth>() != -1; }
00325     template<> inline bool Grabber::desiredUsed<utils::Size>() const{ return getDesired<utils::Size>() != utils::Size::null; }
00326 
00327     class ICLIO_API GrabberRegister : utils::Uncopyable {
00328       private:
00329         utils::Mutex mutex;
00330 
00331         struct GrabberFunctions{
00332           utils::Function<Grabber*,const std::string&> init;
00333           utils::Function<const std::vector<GrabberDeviceDescription> &,std::string,bool> list;
00334         };
00335 
00336         // grabber functions map
00337         typedef std::map<std::string, GrabberFunctions> GFM;
00338         GFM gfm;
00339 
00340         // grabber bus reset functions map
00341         typedef std::map<std::string, utils::Function<void,bool> > GBRM;
00342         GBRM gbrm;
00343 
00344         // grabber device descriptions map
00345         typedef std::set<std::string> GDS;
00346         GDS gds;
00347 
00348         // private constructor
00349         GrabberRegister(){}
00350 
00351       public:
00352         static GrabberRegister* getInstance();
00353 
00354         void registerGrabberType(const std::string &grabberid,
00355                                    utils::Function<Grabber *, const std::string &> creator,
00356                                    utils::Function<const std::vector<GrabberDeviceDescription> &,std::string,bool> device_list)
00357         throw (utils::ICLException);
00358 
00359         void registerGrabberBusReset(const std::string &grabberid,
00360                                    utils::Function<void, bool> reset_function)
00361         throw (utils::ICLException);
00362 
00363         void addGrabberDescription(const std::string &grabber_description)
00364         throw (utils::ICLException);
00365 
00366         Grabber* createGrabber(const std::string &grabberid, const std::string &param) throw (utils::ICLException);
00367 
00368         std::vector<std::string> getRegisteredGrabbers();
00369 
00370         std::vector<std::string> getGrabberInfos();
00371 
00372         const std::vector<GrabberDeviceDescription>& getDeviceList(std::string id, std::string hint="", bool rescan=true);
00373 
00374         void resetGrabberBus(const std::string &id, bool verbose);
00375     };
00376 
00379 
00380 
00381     #define REGISTER_GRABBER(NAME,CREATE_FUNC,DEVICE_LIST_FUNC,DESCRIPTION)                                        \
00382     struct StaticGrabberRegistrationFor_##NAME{                                                                    \
00383       StaticGrabberRegistrationFor_##NAME(){                                                                       \
00384         icl::io::GrabberRegister::getInstance() -> registerGrabberType(#NAME, CREATE_FUNC, DEVICE_LIST_FUNC);      \
00385         icl::io::GrabberRegister::getInstance() -> addGrabberDescription(DESCRIPTION);                             \
00386       }                                                                                                            \
00387     } staticGrabberRegistrationFor_##NAME;
00388 
00389     #define REGISTER_GRABBER_BUS_RESET_FUNCTION(NAME,BUS_RESET_FUNC)                                               \
00390     struct StaticGrabberBusResetRegistrationFor_##NAME{                                                            \
00391       StaticGrabberBusResetRegistrationFor_##NAME(){                                                               \
00392         icl::io::GrabberRegister::getInstance() -> registerGrabberBusReset(#NAME, BUS_RESET_FUNC);                 \
00393       }                                                                                                            \
00394     } staticGrabberBusResetRegistrationFor_##NAME;
00395 
00396   } // namespace io
00397 } // namespace icl
00398 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines