Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_Base.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 BIAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13 
14 BIAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with BIAS; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 
24 #ifndef __BIASVIDEOSOURCE_HH__
25 #define __BIASVIDEOSOURCE_HH__
26 
27 #include <bias_config.h>
28 #include <vector>
29 #include <Base/Image/Image.hh>
30 #include <Base/Common/BIASpragmaStart.hh>
31 #include <Image/VideoStream.hh>
32 
33 #define D_V4L (1<<0)
34 #define D_DCAM (1<<1)
35 #define D_VSO_DISK (1<<2)
36 #define D_DCAM_F7 (1<<3)
37 #define D_DV (1<<4)
38 #define D_VS_WHITEB (1<<5)
39 #define D_NET (1<<6)
40 #define D_DV2 (1<<8)
41 #define D_DTD (1<<9)
42 
43 namespace BIAS{
44 
45  // forward declaration
46  template <class T> class BIASVideoSource_EXPORT Camera;
47 
48  class BIASVideoSource_EXPORT VideoSourceCapabilities;
49 
50  /**
51  \class VideoSource
52  \ingroup g_videosource
53  \author evers
54  \brief Defines a common interface to different devices.
55 
56  Specialized classes for each deviceclass should be derived and most methods
57  have to be reimplemented.
58 
59  Usage:
60 
61  VideoSource *foo;
62 
63  Image<type> bar;
64 
65  /// assume to have a class for DCAM cams
66  foo = new VideoSource_DCAM;
67 
68  /// select a colormode with SetColorMode() if required
69  // the default colormode depends on the used Camera(kind of VideoSource)
70 
71  // set a image size for grabbing foo->SetSize(width,height)
72  foo->SetSize(768,576);
73 
74  /// Want to take the second channel (what ever this means)
75  foo->SetChannel(2);
76 
77  /// take the first appropriate device
78  foo->OpenDevice();
79 
80  foo->InitImage(bar);
81 
82  // do last preparations
83  foo->PreGrab();
84 
85  // get one frame
86  foo->GrabSingle(bar);
87 
88  // stop enetually running things (isochronous transfers for example)
89  foo->PostGrab();
90 
91  // clean up
92  foo->CloseDevice();
93  */
94 
95  class BIASVideoSource_EXPORT VideoSource : public BIAS::Debug
96  {
97 
98  public:
99  VideoSource();
100  virtual ~VideoSource();
101 
102  /// selects the first available device to open (e.g. /dev/video0 for V4L,
103  /// id 0 = first camera for DCAM/DV)
104  virtual int OpenDevice();
105 
106  /// a device name like "/dev/video1" can be specified here
107  virtual int OpenDevice(const char *device);
108 
109  /// a device number can be specified here (DCAM) or port number (Net)
110  virtual int OpenDevice(int device);
111 
112  /// used by derived class VideoSource_Disk
113  virtual int OpenDevice(std::string Prefix, std::string Postfix,
114  int DigitCount, int PictureCount,int StartCount=0);
115 
116  /// used by derived class VideoSource_Disk
117  virtual int OpenDevice(const std::vector<std::string> &FileNames);
118 
119  virtual int CloseDevice();
120 
121  virtual int GetAllDevices(std::vector<std::string> &devices);
122 
123  virtual int GrabSingle(BIAS::Camera <unsigned char> &image);
124  virtual int GrabSingle(BIAS::Camera <float> &image);
125 #ifdef BUILD_IMAGE_CHAR
126  virtual int GrabSingle(BIAS::Camera <char> &image);
127 #endif
128 #ifdef BUILD_IMAGE_SHORT
129  virtual int GrabSingle(BIAS::Camera <short int> &image);
130 #endif
131 #ifdef BUILD_IMAGE_INT
132  virtual int GrabSingle(BIAS::Camera <int> &image);
133 #endif
134 #ifdef BUILD_IMAGE_USHORT
135  virtual int GrabSingle(BIAS::Camera <unsigned short int> &image);
136 #endif
137 #ifdef BUILD_IMAGE_UINT
138  virtual int GrabSingle(BIAS::Camera <unsigned int> &image);
139 #endif
140 #ifdef BUILD_IMAGE_DOUBLE
141  virtual int GrabSingle(BIAS::Camera <double> &image);
142 #endif
143  /// Do last preparations before grabbing (e.g. start ISO transfer)
144  virtual int PreGrab();
145 
146  /// Stop anything started in PreGrab()
147  virtual int PostGrab();
148 
149  // The following functions are not inline to let other class overload them,
150  // but they are still implemented for this class.
151 
152  /** Set image size and number of bytes per pixel (e.g. 1 = 8 bit gray image)
153  @attention Must be used before OpenDevice()!
154  */
155  virtual void SetSize(int w, int h, int bytesperpixel=1);
156 
157  /** Select colormodel to use. Not all sources support all colormodels.
158  @attention Must be used before OpenDevice()!
159 + */
160  virtual int SetColorModel(BIAS::ImageBase::EColorModel mode);
161 
162  /** Use channel to select one of many cameras on the same
163  @attention Must be used before OpenDevice()!
164  */
165  void SetDeviceChannel(int channel) { DeviceChannel_ = channel; }
166 
167  /** If more than one channel is present, names can be returned here.
168  The position in the vector corresponds to the channel number in SetChannel().
169  */
170  virtual void GetDeviceChannelNames(std::vector<std::string> &names) {}
171 
172  /** Set frame per second for image capturing.
173  @attention Must be used before OpenDevice()!
174  */
175  void SetFPS(float fps) { FramesPerSecond_ = fps; }
176 
177  /** @brief Specify if there is an external trigger to be used.
178  @param[in] trigger Specifies if external trigger should be acticated
179  */
180  virtual void SetExternalTrigger(bool trigger = true) { bExternalTrigger_ = trigger; }
181 
182  /** @attention Must be used before OpenDevice()! */
183  void SetBytesPerPixel(unsigned char bpp) { BytesPerPixel_ = (float) bpp; }
184 
185  inline int GetWidth() const { return Width_; }
186 
187  inline int GetHeight() const { return Height_; }
188 
189  inline int GetColorChannels() const { return ColorChannels_; }
190 
191  inline BIAS::ImageBase::EColorModel GetColorModel() const { return ColorMode_; }
192 
193  inline float GetBytesPerPixel() const { return BytesPerPixel_; }
194 
195  /** @attention Call this only after opening the device successfully. */
196  virtual int InitImage(BIAS::ImageBase &Image);
197 
198  /** Use this method to learn something about the capabilities of the source
199  (only useful for V4L sources, e.g. /dev/video0). Output is send to stdout.
200  */
201  virtual int GetCapabilities(VideoSourceCapabilities &caps);
202  virtual int GetCapabilities(const char *device, VideoSourceCapabilities &caps);
203 
204  /// Use this to enable/disable any automatic modes addressed via enumerations
205  virtual int AutoMode(int a, bool b);
206  inline int GetID() const { return ID_; }
207 
208  inline bool IsActive() const { return Active_; }
209 
210  /// Specify if SetShutter(), SetGain() ... would work for this camera
211  inline bool IsControllable() { return IsControllable_; }
212 
213  /// This function is used to control features like shutter, zoom, etc.
214  virtual int SetFeature(int feature, unsigned int value);
215 
216  /// This function is used to query features like shutter, zoom, etc.
217  virtual unsigned int GetFeature( int feature);
218 
219  // The following functions define more convenient camera parameter control
220 
221  virtual bool HasControlShutter() { return false; }
222  /// Get shutter (exposure time) in seconds.
223  virtual float GetShutter();
224  /// Set shutter (exposure time) in seconds.
225  virtual int SetShutter(float exptime);
226  virtual void SetAutoShutter(bool b) { BIASERR("Please overload function!"); }
227  virtual bool GetAutoShutter() { BIASERR("Please overload function!"); return false; }
228  virtual void GetShutterRange(double &min, double &max) {
229  min = minShutter_; max = maxShutter_;
230  }
231  virtual bool HasControlGain() { return false; }
232  /// Get gain in dB.
233  virtual float GetGain();
234  /// Set gain in dB.
235  virtual int SetGain(float g);
236  virtual void SetAutoGain(bool b) { BIASERR("Please overload function!"); }
237  virtual bool GetAutoGain() { BIASERR("Please overload function!"); return false; }
238  virtual void GetGainRange(double &min, double &max) {
239  min = minGain_; max = maxGain_;
240  }
241  virtual bool HasControlBrightness() { return false; }
242  /// Get brightness as value in interval [0, 100]. */
243  virtual float GetBrightness() { BIASERR("Please overload function!"); return 0.0f; }
244  /// Set brightness as value in interval [0, 100]. */
245  virtual int SetBrightness(float g) { BIASERR("Please overload function!"); return 0; }
246  virtual void SetAutoBrightness(bool b) { BIASERR("Please overload function!"); }
247  virtual bool GetAutoBrightness() { BIASERR("Please overload function!"); return false; }
248  virtual void GetBrightnessRange(double &min,double &max) {
249  min = minBrightness_; max = maxBrightness_;
250  }
251  virtual bool HasControlContrast() { return false; }
252  /// Get contrast as value in interval [0, 100]. */
253  virtual float GetContrast() { BIASERR("Please overload function!"); return 0.0f; }
254  /// Set contrast as value in interval [0, 100]. */
255  virtual int SetContrast(float g) { BIASERR("Please overload function!"); return 0; }
256  virtual void SetAutoContrast(bool b) { BIASERR("Please overload function!"); }
257  virtual bool GetAutoContrast() { BIASERR("Please overload function!"); return false; }
258  virtual void GetContrastRange(double &min, double &max) {
259  min = minContrast_; max = maxContrast_;
260  }
261 
262  virtual bool HasControlWhiteBalance() { BIASERR("Please overload function!"); return false; }
263 
264  /// Reset digital zoom, pan, and tilt to defaults to ensure compliance with calibration.
265  virtual void ResetPanTiltZoom() {}
266 
267  virtual int OnePushAuto(int feature);
268 
269  /// Set white balance as values in interval [0, 100]. */
270  virtual int SetWhiteBalance(float rvalue, float bvalue);
271  /// Get white balance as values in interval [0, 100]. */
272  virtual int GetWhiteBalance(float &rvalue, float &bvalue);
273 
274  /// Set white balance to optimal value in software.
275  virtual int SoftwareWhiteBalance();
276 
277  /** @brief Enables the very fast direct-to-disk mode.
278  @attention Use this _after_ OpenDevice() because the image size must be known!
279  @author evers
280  */
281  int SetDirectToDisk(const std::string &filename);
282 
283  virtual void EnsureLatestFrame(const bool b) {}
284 
285  std::string GetIdentifier() const { return Identifier_; }
286 
287  /** @brief Specify if grab should wait for new frames.
288  If you want as many images as possible and don't care about equal ones,
289  call this with waitForNew = false. By default, GrabSingle() blocks until
290  a new frame arrives from the camera. Only used in V4L and DSHOW.
291  @author grest
292  */
293  inline virtual void SetWaitForNew(bool waitForNew) { WaitForNew_ = waitForNew; }
294 
295  protected:
296 
297  int WriteDirect_(const unsigned char *data, const BIAS::UUID &uuid,
298  const long int &sec, const long int &usec);
299 
300  /// Image format
301  int Width_;
302  int Height_;
304 
305  /// Capturing framerate
307 
308  /// BytesPerPixel cannot be int (e.g. it is 1.5 for YUV420p)
310 
311  /// Device name
312  const char *DefaultDevice_;
313 
314  /// Device channel
316 
317  /// Color mode used by camera
319 
320  /// Complete_initialized_ is set when OpenDevice(), UseChannel() etc. are done
322 
323  /// Active flag is set in PreGrab() und unset in PostGrab()
324  bool Active_;
325 
326  /// Grabbing flag is set and unset in GrabSingle() methods
327  bool Grabbing_;
328 
329  /// External trigger flag
331 
332  /// Camera ID
333  int ID_;
334 
335  /// Wait for new frames in GrabSingle()
337 
338  /// Write directly to disk
340 
341  /// File descriptor for direct-to-disk mode
342  int DtDFd_;
343 
344  /// 512 byte aligned buffer, contains VSImageHeader and image data
345  char *DtDBuffer_;
346 
347  /// Buffer size must be multiple of 512
348  unsigned int DtDBufferSize_;
349 
350  /// Image size, useful for allocating and copying memory
351  unsigned int ImageSize_;
352 
353  /// Pointer to the beginning of DtDBuffer_
355 
356  /// Pointer into DtDBuffer after VSImageHeader
358 
359  /// Must be initialized be the derived classes
361 
362  /// Feature ranges
363  double minContrast_, maxContrast_;
364  double minGain_, maxGain_;
365  double minShutter_, maxShutter_;
366  double minBrightness_, maxBrightness_;
367 
368  /// Verbose camera descriptor
369  std::string Identifier_;
370 
371  };
372 
373 }// namespace MIP
374 #include <Base/Common/BIASpragmaEnd.hh>
375 #endif
376 
377 
378 
379 
380 
381 
382 
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
virtual void GetShutterRange(double &min, double &max)
virtual bool GetAutoBrightness()
void SetFPS(float fps)
Set frame per second for image capturing.
virtual void SetAutoContrast(bool b)
Defines a common interface to different devices.
char * DtDBuffer_
512 byte aligned buffer, contains VSImageHeader and image data
bool IsActive() const
BIAS::VideoStream::VSImageHeader * DtDImageHeader_
Pointer to the beginning of DtDBuffer_.
void SetDeviceChannel(int channel)
Use channel to select one of many cameras on the same.
virtual bool GetAutoShutter()
bool IsControllable_
Must be initialized be the derived classes.
extends the Image by MetaData support (e.g.
Definition: Camera.hh:74
char * DtDStartOfImage_
Pointer into DtDBuffer after VSImageHeader.
virtual void SetExternalTrigger(bool trigger=true)
Specify if there is an external trigger to be used.
virtual bool GetAutoGain()
const char * DefaultDevice_
Device name.
virtual void GetBrightnessRange(double &min, double &max)
virtual bool HasControlBrightness()
BIAS::ImageBase::EColorModel ColorMode_
Color mode used by camera.
int DeviceChannel_
Device channel.
bool WaitForNew_
Wait for new frames in GrabSingle()
virtual void SetAutoShutter(bool b)
float BytesPerPixel_
BytesPerPixel cannot be int (e.g. it is 1.5 for YUV420p)
int GetColorChannels() const
virtual int SetContrast(float g)
Set contrast as value in interval [0, 100]. */.
bool CompleteInitialized_
Complete_initialized_ is set when OpenDevice(), UseChannel() etc. are done.
int GetHeight() const
virtual void GetContrastRange(double &min, double &max)
int Width_
Image format.
virtual bool HasControlContrast()
std::string Identifier_
Verbose camera descriptor.
bool Grabbing_
Grabbing flag is set and unset in GrabSingle() methods.
virtual void SetWaitForNew(bool waitForNew)
Specify if grab should wait for new frames.
void SetBytesPerPixel(unsigned char bpp)
std::string GetIdentifier() const
float FramesPerSecond_
Capturing framerate.
int ID_
Camera ID.
virtual void GetGainRange(double &min, double &max)
double minContrast_
Feature ranges.
bool IsControllable()
Specify if SetShutter(), SetGain() ... would work for this camera.
virtual float GetBrightness()
Get brightness as value in interval [0, 100]. */.
virtual void EnsureLatestFrame(const bool b)
The image template class for specific storage types.
Definition: Image.hh:78
bool DirectToDisk_
Write directly to disk.
bool bExternalTrigger_
External trigger flag.
float GetBytesPerPixel() const
unsigned int DtDBufferSize_
Buffer size must be multiple of 512.
bool Active_
Active flag is set in PreGrab() und unset in PostGrab()
virtual bool HasControlGain()
virtual void ResetPanTiltZoom()
Reset digital zoom, pan, and tilt to defaults to ensure compliance with calibration.
virtual void SetAutoBrightness(bool b)
virtual void SetAutoGain(bool b)
virtual bool HasControlWhiteBalance()
virtual bool GetAutoContrast()
unsigned int ImageSize_
Image size, useful for allocating and copying memory.
BIAS::ImageBase::EColorModel GetColorModel() const
Checks for VideoSource capabilities.
int DtDFd_
File descriptor for direct-to-disk mode.
virtual void GetDeviceChannelNames(std::vector< std::string > &names)
If more than one channel is present, names can be returned here.
interface class for producing/storing Universally Unique IDentifiers
Definition: UUID.hh:98
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
virtual int SetBrightness(float g)
Set brightness as value in interval [0, 100]. */.
virtual bool HasControlShutter()
virtual float GetContrast()
Get contrast as value in interval [0, 100]. */.