CVD 0.8
cvd/Linux/v4l1buffer.h
00001 /*
00002     This file is part of the CVD Library.
00003 
00004     Copyright (C) 2005 The Authors
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2.1 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to the Free Software
00018     Foundation, Inc.,
00019     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 //-*- c++ -*-
00022 #ifndef __CVD_V4L1BUFFER_H
00023 #define __CVD_V4L1BUFFER_H
00024 
00025 #include <vector>
00026 
00027 #include <cvd/Linux/v4l1frame.h>
00028 #include <cvd/videobuffer.h>
00029 #include <cvd/byte.h>
00030 #include <cvd/rgb.h>
00031 #include <cvd/timer.h>
00032 #include <cvd/colourspaces.h>
00033 
00034 namespace CVD {
00035 
00036 namespace Exceptions
00037 {
00040     namespace V4L1Buffer
00041     {
00044         struct All: public CVD::Exceptions::VideoBuffer::All{};
00047         struct DeviceOpen: public All {DeviceOpen(std::string dev); 
00048         };
00051         struct DeviceSetup: public All {DeviceSetup(std::string dev, std::string action);  
00052         };
00055         struct PutFrame: public All {PutFrame(std::string dev, unsigned int number); 
00056         };
00059         struct GetFrame: public All {GetFrame(std::string dev, unsigned int number); 
00060         };
00061     }
00062 }
00063 
00065 namespace V4L1
00066 {
00067     #ifndef DOXYGEN_IGNORE_INTERNAL
00068     template<class C> struct cam_type
00069     {
00070         static const int mode = C::Error__type_not_valid_for_camera___Use_byte_or_yuv411_or_rgb_of_byte;
00071     };
00072 
00073     template<> struct cam_type<byte>
00074     {
00075         static const unsigned int mode = VIDEO_PALETTE_GREY;
00076     };
00077 
00078     template<> struct cam_type<bayer>
00079     {
00080         static const unsigned int mode = VIDEO_PALETTE_RAW;
00081     };
00082 
00083     template<> struct cam_type<yuv422>
00084     {
00085         static const unsigned int mode = VIDEO_PALETTE_YUV422;
00086     };
00087 
00088     template<> struct cam_type<yuv420p>
00089     {
00090         static const unsigned int mode = VIDEO_PALETTE_YUV420P;
00091     };
00092 
00093 
00094     template<> struct cam_type<Rgb<byte> >
00095     {
00096         static const unsigned int mode = VIDEO_PALETTE_RGB24;
00097     };
00098     #endif
00099 
00103     class RawV4L1
00104     {
00105     public:
00110         RawV4L1(const std::string & dev, unsigned int mode, const ImageRef&);
00111         virtual ~RawV4L1();
00113         const ImageRef& get_size() const;
00115         void set_size(const ImageRef& size);
00117         void set_palette(unsigned int palette);
00119         void set_brightness(double brightness);
00121         double get_brightness(void) { return myBrightness; };
00123         void set_whiteness(double whiteness);
00125         double get_whiteness(void) { return myWhiteness; };
00127         void set_hue(double hue);
00129         double get_hue(void) { return myHue; };
00131         void set_contrast(double contrast);
00133         double get_contrast(void) { return myContrast; };
00135         void set_saturation(double saturation);
00137         double get_saturation(void) { return mySaturation; };
00139         void set_auto_exp(bool on);
00141         bool get_auto_exp(void);
00143         void retrieveSettings();
00145         void commitSettings();
00147         void captureFrame(unsigned int buffer);
00149         unsigned char* get_frame();
00151         void put_frame( unsigned char * );
00153         double frame_rate();
00155         bool frame_pending();
00157         int get_handle() const { return myDevice; };
00158 
00159     private:
00160         std::string deviceName;
00161         int myDevice;
00162         ImageRef mySize;
00163         unsigned int myPalette;
00164         double myBrightness, myWhiteness, myContrast, myHue, mySaturation;
00165         unsigned int myBpp;
00166         int autoexp;
00167 
00168         void *mmaped_memory;
00169         size_t mmaped_len;
00170 
00171         std::vector<unsigned char*> myFrameBuf;
00172         std::vector<bool> myFrameBufState;
00173         unsigned int myNextRetrieveBuf;
00174     };
00175 };
00176 
00190 template <class T> class V4L1Buffer : public VideoBuffer<T>, public V4L1::RawV4L1
00191 {
00192 public:
00195     V4L1Buffer(const std::string & dev) 
00196     :VideoBuffer<T>(VideoBufferType::Flushable),
00197      RawV4L1( dev, V4L1::cam_type<T>::mode, ImageRef(0,0)) 
00198     {}
00199 
00203     V4L1Buffer(const std::string & dev, ImageRef size) 
00204     :VideoBuffer<T>(VideoBufferType::Flushable),
00205      RawV4L1( dev, V4L1::cam_type<T>::mode,size ) 
00206     {}
00207 
00208     virtual ImageRef size()
00209     {
00210         return RawV4L1::get_size();
00211     }
00212     virtual VideoFrame<T> * get_frame()
00213     {
00214         return new V4L1Frame<T>(timer.get_time(), (T *)RawV4L1::get_frame(), RawV4L1::get_size());
00215     }
00216     virtual void put_frame(VideoFrame<T>* f)
00217     {
00218         RawV4L1::put_frame((unsigned char *)f->data());
00219         delete reinterpret_cast<V4L1Frame<T> *>(f);
00220     }
00221     virtual bool frame_pending()
00222     {
00223         return RawV4L1::frame_pending();
00224     }
00225     virtual double frame_rate()
00226     {
00227         return RawV4L1::frame_rate();
00228     }
00229 
00230     virtual ~V4L1Buffer()
00231     {
00232     }
00233 
00234 private:
00235     V4L1Buffer( V4L1Buffer& copyof );
00236     int operator = ( V4L1Buffer& copyof );
00237 };
00238 
00241 typedef V4L1Buffer<byte> V4L1BufferByte;
00242 
00243 };
00244 #endif