Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_DSHOW.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 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __BIASVIDEOSOURCE_DSHOW_HH__
26 #define __BIASVIDEOSOURCE_DSHOW_HH__
27 
28 //include first, avoid macro collision
29 #include <bias_config.h>
30 #include <Gui/StringConv.hh>
31 #include <windows.h>
32 #include <atlbase.h>
33 
34 #include <dshow.h>
35 #include <qedit.h>
36 #include <comutil.h>
37 #include <strmif.h>
38 
39 # ifdef Yield
40 # undef Yield
41 # endif
42 #include <VideoSource/VideoSource_Base.hh>
43 #include <VideoSource/VideoSourceCapabilities.hh>
44 #include <Image/Camera.hh>
45 
46 namespace BIAS{
47 
48  /**
49  \class VideoSource_DSHOW
50  \ingroup g_videosource
51  \author ischiller
52  \brief This class extends VideoSource for the use of DirectShow devices
53  you need Microsoft Windows SDK version 6.1 and Microsoft DirectX 9 SDK (November 2008)
54  */
55 
56 
57 #if !defined __MYSAMPLE_GRABBER_CB_H
58 #define __MYSAMPLE_GRABBER_CB_H
59 
60  class MySampleGrabberCB : public ISampleGrabberCB {
61 
62  protected:
63 
64  // The media type we are processing
65  AM_MEDIA_TYPE mediaType;
66 
67  public:
68  // constructor
69  MySampleGrabberCB(int width, int height, int depth);
70  // destructor
72 
73  bool HasNewImage();
74  void SetNewImageCheck(bool check=true){
75  bCheckNewImage_ = check;};
76 
78  return camera_;};
79  protected:
81 
82  unsigned char* lastMemorySample_;
83  unsigned char* currentMemorySample_;
86 
87  // This is the callback method called every time
88  STDMETHODIMP SampleCB(double SampleTime, IMediaSample *pSample);
89  // the other TO IMPLEMENT method
90  STDMETHODIMP BufferCB(double SampleTime, BYTE *pBuffer, long BufferLen);
91 
92  STDMETHODIMP QueryInterface(REFIID riid, void ** ppv);
93  STDMETHODIMP_(ULONG) AddRef() { return 2; }
94  STDMETHODIMP_(ULONG) Release() { return 1; }
95 
96  };
97 
98 
99 #endif
100 
101  class BIASVideoSource_EXPORT VideoSource_DSHOW : public VideoSource{
102 
103 
104  public:
105 
106  VideoSource_DSHOW(bool bShowDialogs = false);
107  VideoSource_DSHOW(int width, int height,int depth,bool bShowDialogs = false);
108 
110 
111  // query list of all available devices
112  int GetAllDevices(std::vector<std::string> &devices);
113 
114  int OpenDevice(unsigned int width, unsigned int height, unsigned int channels,
115  const char * device,BIAS::ImageBase::EColorModel model = BIAS::ImageBase::CM_RGB);
116  int OpenDevice(const char * device);
117  int OpenDevice();
118  int CloseDevice();
119  /** overwrites Base class function */
120  void SetWaitForNew(bool waitForNew) {
121  WaitForNew_=waitForNew;
122  if(FGCB != NULL)
123  FGCB->SetNewImageCheck(waitForNew);
124  };
125  int GrabSingle(BIAS::Camera <unsigned char> &image);
126  int InitImage(BIAS::ImageBase &Image);
127 
128  void ResetPanTiltZoom(void);
129 
130  int PreGrab();
131  int PostGrab();
132 
133  inline void SetCaptureDeviceName(std::string sName){
134  sCaptureDeviceName_ = sName;};
135 
136  inline void SetSizeAndColorModel(int width, int height,
137  int bytesperpixel=1, BIAS::ImageBase::EColorModel model = BIAS::ImageBase::CM_invalid){
138  SetSize( width, height, bytesperpixel);
139  SetColorModel(model);
140  BIASColorModel_= model;
141  };
142 
143  bool DisplayCameraProperties();
144 
145  void DisplaySourcePinProperties();
146 
147  bool DisplayFilterProperties( CComPtr<IBaseFilter> pFilter);
148 
149 
150  int GetCapabilities(VideoSourceCapabilities &caps);
151  int GetCapabilities(const char *device, VideoSourceCapabilities &caps);
152 
153  //shutter
154  bool HasControlShutter();
155  float GetShutter();
156  /// Set shutter (exposure time) to exptime in seconds
157  int SetShutter(float exptime);
158  void SetAutoShutter(bool b);
159  bool GetAutoShutter();
160 
161 
162  //Gain
163  bool HasControlGain(){return false;};
164  void SetAutoGain(bool b){BIASWARN("Unimplemented!");};
165  bool GetAutoGain(){return false;};
166  float GetGain(){return -1.0;};
167  /// Set gain to g with g[dB]
168  int SetGain(float g){return -1;};
169 
170  //Brightness
171  bool HasControlBrightness();
172  /* Value: 0<=g<=100 */
173  float GetBrightness();
174  /* Value: 0<=g<=100 */
175  int SetBrightness(float g);
176  void SetAutoBrightness(bool b);
177  bool GetAutoBrightness();
178 
179  //Contrast
180  bool HasControlContrast();
181  /* Value: 0<=g<=100 */
182  float GetContrast();
183  /* Value: 0<=g<=100 */
184  int SetContrast(float g);
185  void SetAutoContrast(bool b);
186  bool GetAutoContrast();
187 
188 
189  protected:
190 
191  struct CameraControl {
192  long int id; // property number on Win32, -1 means "unavailable
193  long int min,max,stepping,standard,flags;
194  };
198  long * pDsBuffer_;
199  unsigned char * pImageBuffer_;
200  int dWidth_;
201  int dHeight_;
202  int dDepth_;
204 
206 
207  std::string sCaptureDeviceName_;
208 
209  HRESULT InitVideoCapture_();
210  HRESULT GetInterfaces_(void);
211  HRESULT FindCaptureDevice_(IBaseFilter ** ppSrcFilter);
212  HRESULT EnumFilters_(IFilterGraph *pGraph);
213 
214  void InitCamControls_();
215 
216  void DetermineColorModel_();
217  void DetermineBIASColorModel_();
218 
219  void DeleteMediaType_(AM_MEDIA_TYPE *pmt);
220 
221  bool DisplayPinProperties_( CComPtr<IPin> pSrcPin );
222 
223  HRESULT SaveGraphFile_(IGraphBuilder *pGraph, WCHAR *wszPath);
224 
227 
228  CComPtr< IMediaControl > pMediaControl_;
229  CComPtr< IMediaEventEx > pMediaEvent_;
230  // for shutter
231  CComPtr< IAMCameraControl > pCameraControl_;
232  // for Brightness, Contrast, Gain, ...
233  CComPtr< IAMVideoProcAmp > pVideoProcControl_;
234  CComPtr< IPin > pOutPin_;
235  CComPtr< IPin > pSampleOutPin_;
236  IPin *pInPin_;
237  CComPtr< ISampleGrabber > pGrabber_;
238  CComPtr< IBaseFilter > pNullRenderer_;
239  CComPtr< IGraphBuilder > pGraphBuilder_;
240  CComPtr< ICaptureGraphBuilder2 > pCaptureGraphBuilder2_;
241  CComPtr< IBaseFilter > pSrcFilter_;
242 
249 
250 
251  // believe it or not: some cameras use -480 as value for height. Store that sign here
253  bool OverrideColorMode_; // false: take colormodel suggested by cam, true: take colormodel from SetColorModel()
254 
256  bool GetCapabilitiesOnly_; // during open, scan all modes
257 
258  }; //end class
259 }//end namespace mip
260 #endif
STDMETHODIMP SampleCB(double SampleTime, IMediaSample *pSample)
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
CComPtr< IAMVideoProcAmp > pVideoProcControl_
Defines a common interface to different devices.
STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
STDMETHODIMP BufferCB(double SampleTime, BYTE *pBuffer, long BufferLen)
CComPtr< IBaseFilter > pNullRenderer_
void SetSizeAndColorModel(int width, int height, int bytesperpixel=1, BIAS::ImageBase::EColorModel model=BIAS::ImageBase::CM_invalid)
BIAS::Camera< unsigned char > camera_
unsigned char * lastMemorySample_
CComPtr< IGraphBuilder > pGraphBuilder_
CComPtr< IAMCameraControl > pCameraControl_
CComPtr< IBaseFilter > pSrcFilter_
Image< unsigned char > & GetImage()
MySampleGrabberCB(int width, int height, int depth)
CComPtr< ICaptureGraphBuilder2 > pCaptureGraphBuilder2_
float GetGain()
Get gain in dB.
This class extends VideoSource for the use of DirectShow devices you need Microsoft Windows SDK versi...
MySampleGrabberCB * FGCB
void SetCaptureDeviceName(std::string sName)
void SetWaitForNew(bool waitForNew)
overwrites Base class function
BIAS::ImageBase::EColorModel BIASColorModel_
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
int SetGain(float g)
Set gain to g with g[dB].
CComPtr< ISampleGrabber > pGrabber_
CComPtr< IMediaEventEx > pMediaEvent_
BIAS::Camera< unsigned char > camera_
unsigned char * currentMemorySample_
STDMETHODIMP_(ULONG) AddRef()
invalid (not set) image format
Definition: ImageBase.hh:129
Checks for VideoSource capabilities.
void SetNewImageCheck(bool check=true)
CComPtr< IPin > pSampleOutPin_
STDMETHODIMP_(ULONG) Release()
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
CComPtr< IMediaControl > pMediaControl_
VideoSourceCapabilities Capabilities_