Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_DSHOW.cpp
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 #include "VideoSource_DSHOW.hh"
26 #include <Base/Image/ImageIO.hh>
27 #include <Base/Image/ImageConvert.hh>
28 #include <ks.h>
29 #include <ksmedia.h>
30 
31 using namespace BIAS;
32 using namespace std;
33 
34 
35 #ifdef BIAS_HAVE_PTHREADS
36 #include <pthread.h>
37 pthread_mutex_t imageMutex_;
38 #endif
39 
40 bool UpSideDown_;
41 
43 {
44  #ifdef BIAS_HAVE_PTHREADS
45  pthread_mutex_init (&imageMutex_,NULL);
46  #endif
47 
48  dWidth_ = 640;
49  dHeight_ = 480;
50  UpSideDown_ = true; //RGB default
51  OverrideColorMode_ = false; // ignore SetColorModel(), accept cameras own CM
52  dDepth_ = 1;
53  dDsBufferSize_ = 0;
54  pDsBuffer_ = NULL;
55  pMediaControl_ = NULL;
56  pMediaEvent_ = NULL;
57  pGraphBuilder_ = NULL;
59  pGrabber_ = NULL;
60  pNullRenderer_ = NULL;
61  bShowDialogs_ = bShowDialogs;
62  pImageBuffer_ = NULL;
65  MediaSubTypeColorModel_ = MEDIASUBTYPE_None;
66  FGCB = NULL; //callback pointer
67  GetCapabilitiesOnly_ = false;
69 }
70 
71 VideoSource_DSHOW::VideoSource_DSHOW(int width, int height,int depth,bool bShowDialogs): VideoSource()
72 {
73  dWidth_ = width;
74  dHeight_ = height;
75  dDepth_ = depth;
76  UpSideDown_ = true;//RGB default
77  dDsBufferSize_ = 0;
78  pDsBuffer_ = NULL;
79  pMediaControl_ = NULL;
80  pMediaEvent_ = NULL;
81  pGraphBuilder_ = NULL;
83  pGrabber_ = NULL;
84  pNullRenderer_ = NULL;
85  bShowDialogs_ = bShowDialogs;
86  pImageBuffer_ = NULL;
89  MediaSubTypeColorModel_ = MEDIASUBTYPE_None;
90  FGCB = NULL; //callback pointer
91  GetCapabilitiesOnly_ = false;
93 }
94 
95 
97 {
98  if(pImageBuffer_!=NULL)
99  delete [] pImageBuffer_;
100  if(pDsBuffer_!=NULL)
101  delete [] pDsBuffer_;
102  if(FGCB != NULL)
103  delete FGCB;
104 }
105 
107  if (!Image.IsEmpty()){
108  BIASWARNONCE("VideoSource::InitImage() should be called with uninitialized image");
109  Image.Release();
110  }
111  Image.Init(GetWidth(), GetHeight(),GetColorChannels());
113 
114  return 0;
115 }
116 
118 {
119  return OpenDevice("");
120 }
121 
122 int VideoSource_DSHOW::OpenDevice(const char * device)
123 {
124  sCaptureDeviceName_ = device;
125  int ret=0;
126  try{
127  ret = InitVideoCapture_();
128  }catch(BIAS::BaseException e){
129  BIASERR("Error initialising Video capture:"<<e.what());
130  }catch(...){
131  BIASERR("Error initialising Video capture!");
132  }
133  return ret;
134 }
135 
136 int VideoSource_DSHOW::OpenDevice(unsigned int width, unsigned int height, unsigned int channels,
137  const char * device,BIAS::ImageBase::EColorModel model/* = BIAS::ImageBase::CM_RGB*/)
138 {
139  sCaptureDeviceName_ = device;
140  SetSize(width,height,channels);
141  SetColorModel(model);
142  int ret=0;
143  try{
144  ret = InitVideoCapture_();
145  }catch(BIAS::BaseException e){
146  BIASERR("Error initialising Video capture:"<<e.what());
147  }catch(...){
148  BIASERR("Error initialising Video capture!");
149  }
150  return ret;
151 }
152 
154 {
155  if (pGrabber_) pGrabber_.Release();
156  if (pNullRenderer_) pNullRenderer_.Release();
157  if (pSrcFilter_) pSrcFilter_.Release();
158  if (pMediaControl_) {
159  pMediaControl_->Stop();
160  pMediaControl_.Release();
161  }
162  if (pCameraControl_) pCameraControl_.Release();
163  if (pVideoProcControl_) pVideoProcControl_.Release();
164  if (pMediaEvent_) pMediaEvent_.Release();
165  if (pGraphBuilder_) pGraphBuilder_.Release();
167  pOutPin_.Release();
168  pSampleOutPin_.Release();
169  return 0;
170 }
171 
172 
174 {
175  long evCode=0;
176  LONG_PTR p1=NULL,p2=NULL;
177  HRESULT hr=0;
178  hr = pMediaEvent_->GetEvent(&evCode, &p1,&p2,1);
179  if (hr == S_OK) { // ok means: Event found.
180  switch (evCode) {
181  case EC_DEVICE_LOST: BEXCEPTION("Camera diconnected."); break;
182  }
183  }
184  pMediaEvent_->FreeEventParams(evCode, p1,p2);
185  //lock image
186  #ifdef BIAS_HAVE_PTHREADS
187  pthread_mutex_lock(&imageMutex_);
188  #endif
189  while(!FGCB->HasNewImage()){
190  //if no new image lock image and sleep
191  #ifdef BIAS_HAVE_PTHREADS
192  pthread_mutex_unlock(&imageMutex_);
193  #endif
194  Sleep(1);
195  #ifdef BIAS_HAVE_PTHREADS
196  //after wakeup lock again %
197  pthread_mutex_lock(&imageMutex_);
198  #endif
199  }
201  //static int count = 0;
202  //ostringstream oss;
203  //oss << "_flip_"<<setw(3)<<setfill('0')<<count++<<".mip";
204  //ImageIO::Save("before"+oss.str(), image);
205  //ImageIO::Save("after"+oss.str(), image);
207  #ifdef BIAS_HAVE_PTHREADS
208  //after wakeup lock again %
209  pthread_mutex_unlock(&imageMutex_);
210  #endif
211 
212  return 0;
213 
214 }
215 
216 
217 void VideoSource_DSHOW::DeleteMediaType_(AM_MEDIA_TYPE *pmt)
218 {
219  // allow NULL pointers for coding simplicity
220 
221  if (pmt == NULL) {
222  return;
223  }
224 
225  if (pmt->cbFormat != 0) {
226  CoTaskMemFree((PVOID)pmt->pbFormat);
227 
228  // Strictly unnecessary but tidier
229  pmt->cbFormat = 0;
230  pmt->pbFormat = NULL;
231  }
232  if (pmt->pUnk != NULL) {
233  pmt->pUnk->Release();
234  pmt->pUnk = NULL;
235  }
236 
237  CoTaskMemFree((PVOID)pmt);
238 }
239 
240 
242 {
243  return 0;
244 }
245 
247 {
248  while(pthread_mutex_trylock(&imageMutex_)==EBUSY)
249  Sleep(1);
250  pthread_mutex_destroy(&imageMutex_);
251  return 0;
252 }
253 
254 bool VideoSource_DSHOW::DisplayPinProperties_( CComPtr<IPin> pSrcPin )
255 {
256  CComPtr<ISpecifyPropertyPages> pPages;
257 
258  HRESULT hr = pSrcPin->QueryInterface(IID_ISpecifyPropertyPages, (void**)&pPages);
259  if (SUCCEEDED(hr))
260  {
261  PIN_INFO PinInfo;
262  pSrcPin->QueryPinInfo(&PinInfo);
263 
264  CAUUID caGUID;
265  pPages->GetPages(&caGUID);
266 
267  OleCreatePropertyFrame(
268  NULL,
269  0,
270  0,
271  L"Property Sheet",
272  1,
273  (IUnknown **)&(pSrcPin),
274  caGUID.cElems,
275  caGUID.pElems,
276  0,
277  0,
278  NULL);
279 
280  CoTaskMemFree(caGUID.pElems);
281  PinInfo.pFilter->Release();
282  }
283  return false;
284 }
285 
286 bool VideoSource_DSHOW::DisplayFilterProperties( CComPtr<IBaseFilter> pFilter)
287 {
288  CComPtr<ISpecifyPropertyPages> pProp;
289  HRESULT hr = pFilter->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pProp);
290  if (SUCCEEDED(hr))
291  {
292  // Get the filter's name and IUnknown pointer.
293  FILTER_INFO FilterInfo;
294  pFilter->QueryFilterInfo(&FilterInfo);
295 
296  // Show the page.
297  CAUUID caGUID;
298  pProp->GetPages(&caGUID);
299  OleCreatePropertyFrame(
300  NULL, // Parent window
301  0, 0, // (Reserved)
302  FilterInfo.achName, // Caption for the dialog box
303  1, // Number of objects (just the filter)
304  (IUnknown **)&pFilter, // Array of object pointers.
305  caGUID.cElems, // Number of property pages
306  caGUID.pElems, // Array of property page CLSIDs
307  0, // Locale identifier
308  0, NULL); // Reserved
309 
310  // Clean up.
311  if(FilterInfo.pGraph != NULL)
312  FilterInfo.pGraph->Release();
313  CoTaskMemFree(caGUID.pElems);
314  return( true );
315  }
316  else
317  return( false );
318 }
319 
321 {
322  if(pOutPin_)
323  {
325  }
326 }
327 
328 
330 {
331  IBaseFilter * pFilter = pSrcFilter_;
332  CComPtr<ISpecifyPropertyPages> pProp;
333  HRESULT hr = pFilter->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pProp);
334  if (SUCCEEDED(hr))
335  {
336  // Get the filter's name and IUnknown pointer.
337  FILTER_INFO FilterInfo;
338  pFilter->QueryFilterInfo(&FilterInfo);
339 
340  // Show the page.
341  CAUUID caGUID;
342  pProp->GetPages(&caGUID);
343  OleCreatePropertyFrame(
344  NULL, // Parent window
345  0, 0, // (Reserved)
346  FilterInfo.achName, // Caption for the dialog box
347  1, // Number of objects (just the filter)
348  (IUnknown **)&pFilter, // Array of object pointers.
349  caGUID.cElems, // Number of property pages
350  caGUID.pElems, // Array of property page CLSIDs
351  0, // Locale identifier
352  0, NULL); // Reserved
353 
354  // Clean up.
355  if(FilterInfo.pGraph != NULL)
356  FilterInfo.pGraph->Release();
357  CoTaskMemFree(caGUID.pElems);
358  return( true );
359  }
360  else
361  return( false );
362 }
363 
364 ///////////////////////////////////////////////////////////////
366 {
368  {
370  MediaSubTypeColorModel_ = MEDIASUBTYPE_RGB24;
372  MediaSubTypeColorModel_ = MEDIASUBTYPE_RGB32;
374  MediaSubTypeColorModel_ = MEDIASUBTYPE_RGB1;
376  MediaSubTypeColorModel_ = MEDIASUBTYPE_UYVY;
378  MediaSubTypeColorModel_ = MEDIASUBTYPE_Y41P;
379  }
380  else
381  MediaSubTypeColorModel_ = MEDIASUBTYPE_None;
382 }
383 
385 {
386  if(MediaSubTypeColorModel_ != MEDIASUBTYPE_None)
387  {
388  if(MediaSubTypeColorModel_ == MEDIASUBTYPE_RGB24 || MediaSubTypeColorModel_== MEDIASUBTYPE_RGB32)
390  else if(MediaSubTypeColorModel_ == MEDIASUBTYPE_RGB1)
392  else if(MediaSubTypeColorModel_ == MEDIASUBTYPE_UYVY)
394  else if(MediaSubTypeColorModel_ == MEDIASUBTYPE_Y41P)
396  else if(MediaSubTypeColorModel_ == MEDIASUBTYPE_IYUV)
398  else if(MediaSubTypeColorModel_ ==MEDIASUBTYPE_Y411)
400  else if(MediaSubTypeColorModel_ ==MEDIASUBTYPE_Y211)
402  else if(MediaSubTypeColorModel_ ==MEDIASUBTYPE_YUY2)
404  else
406  }
407  else
409 }
410 
412 {
413  HRESULT hr=S_OK;
414  // initialize the COM library.
415  CoInitialize(0);
416 
417  // Get DirectShow interfaces
418  hr = GetInterfaces_();
419  if (S_OK!=hr)
420  BEXCEPTION( "Failed to get DirectShow interfaces! ");
421 
422  // Use the system device enumerator and class enumerator to find
423  // a video capture/preview device, such as a desktop USB video camera.
425  if (S_OK!=hr)
426  {
427  // Don't display a message because FindCaptureDevice will handle it
428  return hr;
429  }
430 
431  // Add Capture filter to our graph.
432  hr = pGraphBuilder_->AddFilter(pSrcFilter_, L"Video Capture");
433  if (S_OK!=hr)
434  {
435  pSrcFilter_.Release();
436  BEXCEPTION("Couldn't add the capture filter to the graph! \n"<<
437  "If you have a working video capture device, please make sure\n"<<
438  "that it is connected and is not being used by another application.");
439 
440  }
441 
442  pOutPin_=NULL;
443  // get the output pin of the Capture device filter
444  if(pSrcFilter_->FindPin(L"Capture",&pOutPin_)!=S_OK)
445  if (pSrcFilter_->FindPin(L"0",&pOutPin_)!=S_OK)
446  if (pSrcFilter_->FindPin(L"Out",&pOutPin_)!=S_OK)
447  if (pSrcFilter_->FindPin(L"1",&pOutPin_)!=S_OK)
448  BEXCEPTION("Could not find Source Outpin");
449 
450  if( pOutPin_ ){
451  //cout<< "Found VideoSource Outpin" << endl;
452  if(bShowDialogs_)
454  }
455  else BEXCEPTION("Did not fine outpin");
456 
457  //DisplayCameraProperties();
458 
459  //here determine size and set
460  IAMStreamConfig * pConfig=0;
461  hr = pCaptureGraphBuilder2_->FindInterface(&PIN_CATEGORY_CAPTURE,
462  &MEDIATYPE_Video,
463  pSrcFilter_, IID_IAMStreamConfig,
464  (void **)&pConfig);
465  if(hr != S_OK) {
466  hr = pCaptureGraphBuilder2_->FindInterface(&PIN_CATEGORY_CAPTURE,
467  &MEDIATYPE_Interleaved,
468  pSrcFilter_,
469  IID_IAMStreamConfig,
470  (void **)&pConfig);
471  if (hr !=S_OK)
472  BEXCEPTION("FindInterface unsucessfull");
473  }
474  //----
475  // define two media types to detect correct one
476  AM_MEDIA_TYPE *pSourceVideoFormat = NULL, * pNewVideoFormat = NULL;
477  int ccount,csize;
478  bool validCamResFound = false;
479  pConfig->GetNumberOfCapabilities(&ccount,&csize);
480  BYTE* construct = new BYTE[csize];
481  AM_MEDIA_TYPE* tmpMediaType;
482  for(int c=0; c<ccount; c++)
483  {
484 
485  if(pConfig->GetStreamCaps(c,&tmpMediaType,construct)!=S_OK)
486  BEXCEPTION("GetStreamCaps failed");
487 
488  if(tmpMediaType->majortype==MEDIATYPE_Video)
489  {
490  if((tmpMediaType->formattype==FORMAT_VideoInfo) )
491 /* &&
492  (tmpMediaType->cbFormat > sizeof(VIDEOINFOHEADER) &&
493  (tmpMediaType->pbFormat != NULL)))
494  */
495  {
496  VIDEOINFOHEADER *vih = (VIDEOINFOHEADER *)tmpMediaType->pbFormat;
497 
500  MediaSubTypeColorModel_ = tmpMediaType->subtype;
503  Capabilities_.AddMode(vih->bmiHeader.biWidth,abs(vih->bmiHeader.biHeight),0,BIASColorModel_);
504  continue;
505  }
506  // extract sign and store separately
507  ImageHeightSign_ = vih->bmiHeader.biHeight / abs(vih->bmiHeader.biHeight);
508  if((vih->bmiHeader.biWidth==GetWidth())&&
509  (abs(vih->bmiHeader.biHeight)==GetHeight()))
510  {
511  cout <<"Matched width and height: "<<vih->bmiHeader.biWidth<<"x"<<vih->bmiHeader.biHeight<<endl;
512  pSourceVideoFormat = tmpMediaType; //store
513 
514  if (OverrideColorMode_) {
515  if(ColorMode_ == ImageBase::CM_RGB || ColorMode_ == ImageBase::CM_BGR) { // RGB requested
516  if((tmpMediaType->subtype==MEDIASUBTYPE_RGB24)||(tmpMediaType->subtype==MEDIASUBTYPE_RGB32)) {
517  cout <<"Found requested RGB type"<<endl;
518  validCamResFound = true;
519  break;
520  }
521  }
522  if(ColorMode_ == ImageBase::CM_YUYV422) { //YUYV422 requested
523  if(tmpMediaType->subtype==MEDIASUBTYPE_YUY2) {
524  cout <<"Found requested YUYV422"<<endl;
525  UpSideDown_ = false; // YUV is always up-right, dont flip.
526  validCamResFound = true;
527  break;
528  }
529  }
530  } // if(overrideColorMode)
531  else {
532  if((tmpMediaType->subtype==MEDIASUBTYPE_RGB24)
533  ||(tmpMediaType->subtype==MEDIASUBTYPE_RGB32)) {
534  cout <<"Accepting preferred RGB color model from camera"<<endl;
536  UpSideDown_ = true; // RGB is always upsidedown
537  validCamResFound = true;
538  break;
539  }
540  if(tmpMediaType->subtype==MEDIASUBTYPE_YUY2) {
541  cout <<"Accepting YUV422 color model from camera"<<endl;
543  UpSideDown_ = false; // YUV is always up-right, dont flip.
544  validCamResFound = true;
545  break;
546  }
547 
548  }// else if(OverrideCOlorMode)
549  } // if (width && height)
550  else
551  cout <<"Unmatched width and height: "<<vih->bmiHeader.biWidth<<"x"<<vih->bmiHeader.biHeight<<endl;
552  }// for ()
553  }
554  DeleteMediaType_(tmpMediaType);
555  }
556  delete construct;
557 
558  // if the task was to query all modes, we are finished.
559  if (GetCapabilitiesOnly_) return 0;
560 
561  if(!pSourceVideoFormat)
562  {
563  BEXCEPTION("no usable camera resolution format found");
564  }
565  if(!validCamResFound)
566  {
567  cout << "no required video resolution found\n"<<endl;
568  }
569 
570  //if(hr = pConfig->GetFormat(&pSourceVideoFormat)) == S_OK)
571  {
572  // Examine the media type for any information you need.
573  if(pSourceVideoFormat->majortype == MEDIATYPE_Video)
574  {
575  MediaSubTypeColorModel_ = pSourceVideoFormat->subtype;
576  if(MediaSubTypeColorModel_ == MEDIASUBTYPE_RGB32)
577  {
578  MediaSubTypeColorModel_ = MEDIASUBTYPE_RGB24;
579  pSourceVideoFormat->subtype = MediaSubTypeColorModel_;
580  }
581  }
582  else
583  {
584  //get mediatype from videosource
585  BEXCEPTION("The major format type is NOT video");
586  }
587  DetermineBIASColorModel_(); //determine corresponding BIAS ColorModel
588 
589  BIASASSERT(pSourceVideoFormat->formattype==FORMAT_VideoInfo);
590  //pSourceVideoFormat->formattype = FORMAT_VideoInfo; //??? sonst gehts gut?
591  VIDEOINFOHEADER *vih = (VIDEOINFOHEADER *)pSourceVideoFormat->pbFormat;
592  vih->bmiHeader.biWidth = GetWidth();
593  vih->bmiHeader.biHeight = GetHeight() * ImageHeightSign_;
594  cout << "requesting "<<vih->bmiHeader.biWidth<<"x"<<vih->bmiHeader.biHeight<<endl;
595  vih->bmiHeader.biBitCount = (WORD)GetColorChannels()*8;
596  vih->bmiHeader.biSizeImage= DIBSIZE(vih->bmiHeader); //
597  hr = pConfig->SetFormat( pSourceVideoFormat );
598  if(hr!=S_OK){
599  BEXCEPTION("Error Videoformat is inconsistent");
600  } else {
601  cout << "got requested format\n";
602  }
603  }
604  DeleteMediaType_(pSourceVideoFormat);
605 
606 // pConfig->Release(); //really release here?
607 
608  IEnumMediaTypes * pMedia=0;
609  vector<AM_MEDIA_TYPE *> vpfnt;
610 
611  //outpin is outpin from pSrcFilter_
612  hr = pOutPin_->EnumMediaTypes( &pMedia );
613  if( (hr = pOutPin_->EnumMediaTypes( &pMedia )) == S_OK)
614  {
615  cout<<"pOutPin_->EnumMediaTypes success"<< endl;
616  bool FoundOne = false;
617  while(pMedia->Next(1, &pSourceVideoFormat, 0) == S_OK && !FoundOne)
618  {
619  if( pSourceVideoFormat->formattype == FORMAT_VideoInfo )
620  {
621  VIDEOINFOHEADER *vih = (VIDEOINFOHEADER *)pSourceVideoFormat->pbFormat;
622  int dCurWidth = vih->bmiHeader.biWidth;
623  int dCurHeight = abs(vih->bmiHeader.biHeight);
624 
625  if( dCurWidth == GetWidth() && dCurHeight == GetHeight() )// && sfcc == "IYUV")
626  {
627  // this is the correct new Video Format
628  pNewVideoFormat = pSourceVideoFormat;
629  cout<<"copying pin format to newvideoformat"<<endl;
630  FoundOne= true;
631  }
632  else
633  {
634  BEXCEPTION("No useful format on pin found");
635  }
636  }
637  else if( pSourceVideoFormat->formattype == FORMAT_None )
638  cout<<"Format type: FORMAT_None"<<endl;
639  else if( pSourceVideoFormat->formattype == FORMAT_DvInfo )
640  cout<<"Format type: FORMAT_DvInfo"<<endl;
641  else if( pSourceVideoFormat->formattype == FORMAT_MPEGVideo )
642  cout<<"Format type: FORMAT_MPEGVideo"<<endl;
643  else if( pSourceVideoFormat->formattype == FORMAT_MPEG2Video )
644  cout<<"Format type: FORMAT_MPEG2Video"<<endl;
645  else if( pSourceVideoFormat->formattype == FORMAT_VideoInfo2 )
646  cout<<"Format type: FORMAT_VideoInfo2"<<endl;
647  }
648  pMedia->Release();
649  }
650  else
651  {
652  BEXCEPTION("pOutPin_->EnumMediaTypes failed");
653  }
654 
655  //finally set the new videoFormat
656  if( pNewVideoFormat != 0 )
657  {
658  hr = pConfig->SetFormat( pNewVideoFormat );
659  if (hr !=S_OK)
660  {
661  BEXCEPTION("errro setting format");
662  }
663  int dWidth = ((VIDEOINFOHEADER *)pNewVideoFormat->pbFormat)->bmiHeader.biWidth;
664  int dHeight = ((VIDEOINFOHEADER *)pNewVideoFormat->pbFormat)->bmiHeader.biHeight;
665  double dDepth = ((VIDEOINFOHEADER *)pNewVideoFormat->pbFormat)->bmiHeader.biBitCount;
666  if(dDepth == 12)
667  dDepth = 2;
668  else
669  dDepth /= 8;
670  SetSizeAndColorModel(dWidth,abs(dHeight),(int)dDepth,BIASColorModel_);
671  DeleteMediaType_( pNewVideoFormat );
672  }
673 
674  // add sample grabber
675  CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabberBase( pGrabber_ );
676  if ( (hr = pGraphBuilder_->AddFilter(pGrabberBase,L"SampleGrabber")) != S_OK)
677  {
678  BEXCEPTION("Couldn't add the grab filter.");
679  }
680  //set mediatype to sample grabber
681  hr = pConfig->GetFormat( &pNewVideoFormat );
682  if( (hr = pGrabber_->SetMediaType(pNewVideoFormat)) != S_OK )
683  {
684  BEXCEPTION("Setting Media Type failed!");
685  }
686  DeleteMediaType_( pNewVideoFormat );
687 
688 
689  // get the SampleGrabber Input Pin
690  if (pGrabberBase->FindPin(L"In",&pInPin_) != S_OK)
691  BEXCEPTION("Could not find SampleGrabber Input Pin");
692 
693  // connect SmartTee and SampleGrabber
694  if ((hr = pGraphBuilder_->Connect(pOutPin_,pInPin_))!=S_OK)
695  BEXCEPTION("Could not not connect Camera Source and SampleGrabber Pins")
696  else
697  cout<< "Succesfully connected Camera Source and SampleGrabber Pins" <<endl;
698 
699  // add the NullRenderer filter
700  hr = pGraphBuilder_->AddFilter(pNullRenderer_,L"NullRenderer");
701  if (FAILED(hr))
702  BEXCEPTION("Couldn't add the grab filter.");
703 
704 
705  // get the SampleGrabber Output Pin
706  if (pGrabberBase->FindPin(L"Out",&pSampleOutPin_)!=S_OK)
707  BEXCEPTION("Pin out of SampleGrabber not found");
708 
709  // get the NullRenderer Input Pin
710  if (pNullRenderer_->FindPin(L"In",&pInPin_)!=S_OK)
711  BEXCEPTION("Could not find NullRenderer Input Pin");
712 
713  // connect SampleGrabber and NullRenderer
714  if (pGraphBuilder_->Connect(pSampleOutPin_,pInPin_)!= S_OK)
715  BEXCEPTION("Could not not connect SampleGrabber and NullRenderer Pins")
716  else
717  cout<<"Succesfully connected SampleGrabber and NullRenderer Pins" << endl;
718 
719  pGrabber_->SetBufferSamples(FALSE);
720  pGrabber_->SetOneShot(FALSE);
721  if(FGCB == NULL)
724  pGrabber_->SetCallback(FGCB,0);
725 
728  // Start video data source
729  hr = pMediaControl_->Run();
730  if (FAILED(hr)) BEXCEPTION("Couldn't run the graph! ");
731 
732  SaveGraphFile_(pGraphBuilder_,L"C:\\MyGraf.grf");
733  return S_OK;
734 }
735 
738 {
739  return GetCapabilities("",caps);
740 }
741 
742 
744 GetCapabilities(const char *device, VideoSourceCapabilities &caps)
745 {
746  int ret =0;
747  sCaptureDeviceName_ = device;
748  GetCapabilitiesOnly_ = true;
749  try{
750  ret = InitVideoCapture_();
751  }catch(BIAS::BaseException e){
752  BIASERR("Error initialising Video capture:"<<e.what());
753  }
754  catch(...){
755  BIASERR("Error initialising Video capture!");
756  }
757  if(ret <0) return ret;
758 
759  GetCapabilitiesOnly_ = false;
760  CloseDevice();
761  caps = Capabilities_;
762  return 0;
763 }
764 
765 
766 inline string ConvertBSTRToString(BSTR pSrc)
767 {
768  DWORD cb,cwch = ::SysStringLen(pSrc);
769  /*
770  /////in datei kloppen zum testen
771  ofstream os;
772  os.open("E:\\USBIdentifier.txt");
773  for ( unsigned i=0; i<cwch; i++ ) {
774  os << pSrc[i] << endl;
775  }
776  os.flush();
777  os.close();
778  */
779  char *szOut = NULL;
780  string result;
781  cwch = cwch+1;
782  cb = ::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch, NULL, 0, 0, 0);
783  if(cb) {
784  szOut = new char[cb];
785  if(szOut) {
786  szOut[cb - 1] = '\0';
787  if(!::WideCharToMultiByte(CP_ACP, 0,pSrc, cwch, szOut, cb, 0, 0))
788  delete []szOut;
789  else {
790  result = szOut;
791  delete[] szOut;
792  }
793  }
794  }
795  return result;
796 }
797 
799 GetAllDevices(vector<string> &devices)
800 {
801  devices.clear();
802  HRESULT hr;
803  ICreateDevEnum *pSysDevEnum = NULL;
804  hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
805  IID_ICreateDevEnum, (void **)&pSysDevEnum);
806  if (FAILED(hr)) return -1;
807 
808  IEnumMoniker *pEnumCat = NULL;
809  hr = pSysDevEnum->CreateClassEnumerator((GUID)CLSID_VideoInputDeviceCategory,
810  &pEnumCat, 0);
811  if (hr == S_OK) {
812  // Enumerate the monikers.
813  IMoniker *pMoniker = NULL;
814  ULONG cFetched;
815  while(pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK) {
816  IPropertyBag *pPropBag;
817  hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag,
818  (void **)&pPropBag);
819  if (SUCCEEDED(hr)) {
820  VARIANT varName;
821  VariantInit(&varName);
822  hr = pPropBag->Read(L"Description", &varName, 0);
823  if(FAILED(hr))
824  {
825  hr = pPropBag->Read(L"FriendlyName", &varName, 0);
826  }
827  if (SUCCEEDED(hr)) {
828  devices.push_back(ConvertBSTRToString(varName.bstrVal));
829  }
830  }
831  }//while
832  pMoniker->Release();
833  }//if
834  pEnumCat->Release();
835  pSysDevEnum->Release();
836  return 0;
837 }
838 
839 
841 {
842  HRESULT hr=S_OK;
843  // Create the filter graph
844  try{
845  hr = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
846  IID_IGraphBuilder, (void **) &pGraphBuilder_);
847  }catch(...){
848  BIASERR("Could not create instance of Filtergraph for VideosourceDShow!");
849  return -1;
850  }
851  if (hr!=S_OK) return hr;
852 
853  // Create the capture graph builder
854  try{
855  hr = CoCreateInstance(CLSID_CaptureGraphBuilder2 , NULL, CLSCTX_INPROC,
856  IID_ICaptureGraphBuilder2, (void **) &pCaptureGraphBuilder2_);
857  }catch(...){
858  BIASERR("Could not create instance of CaptureGraphBuilder for VideosourceDShow!");
859  return -1;
860  }
861  if (hr!=S_OK)
862  return hr;
863  // Attach the filter graph to the capture graph
864  hr = pCaptureGraphBuilder2_->SetFiltergraph(pGraphBuilder_);
865  if (hr!=S_OK)
866  {
867  BIASERR("Failed to set capture filter graph!");
868  return hr;
869  }
870 
871  // Obtain interfaces for media control and Video Window
872  hr = pGraphBuilder_->QueryInterface(IID_IMediaControl,(LPVOID *) &pMediaControl_);
873  if (hr!=S_OK)
874  return hr;
875 
876  hr = pGraphBuilder_->QueryInterface(IID_IMediaEvent, (LPVOID *) &pMediaEvent_);
877  if (hr!=S_OK)
878  return hr;
879 
880  // Create NullRenderer Instance
881  hr = pNullRenderer_.CoCreateInstance(CLSID_NullRenderer);
882  if (hr!=S_OK)
883  return hr;
884 
885  // Create SampleGrabber Instance
886  // ISampleGrabber can Queried on this Filter to Grab Samples from the running Graph
887  hr = pGrabber_.CoCreateInstance(CLSID_SampleGrabber);
888  if(hr!=S_OK)
889  {
890  BIASERR("Could not create the SampleGrabber filter.");
891  //MessageBox( GetDesktopWindow(), "Could not create the SampleGrabber filter.", "ImageSourceDShow", MB_OK | MB_ICONERROR );
892  // exit( -1 );
893  BIASABORT;
894  }
895 
896  return hr;
897 }
898 
899 HRESULT VideoSource_DSHOW::FindCaptureDevice_(IBaseFilter ** ppSrcFilter)
900 {
901  HRESULT hr;
902  IBaseFilter * pSrc = NULL;
903  ULONG cFetched=0;
904 
905  if (!ppSrcFilter)
906  return E_POINTER;
907 
908  // Create the system device enumerator
909  CComPtr <ICreateDevEnum> pDevEnum =NULL;
910 
911  hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
912  IID_ICreateDevEnum, (void **) &pDevEnum);
913  if(S_OK!=hr)
914  {
915  BIASERR("Couldn't create system enumerator!");
916  return hr;
917  }
918 
919  // Create an enumerator for the video capture devices
920  IEnumMoniker* pClassEnum = 0;
921 
922  hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
923  if(S_OK!=hr)
924  {
925  BIASERR("Couldn't create class enumerator! No DirectShow hardware?");
926  return hr;
927  }
928 
929  //////////////////// new
930  pClassEnum->Reset();
931  IMoniker *pM =0;
932  while(hr = pClassEnum->Next(1, &pM, &cFetched), hr==S_OK )
933  {
934  IPropertyBag *pBag;
935  hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
936  if(SUCCEEDED(hr))
937  {
938  IErrorLog* errorLog=NULL;
939  VARIANT var;
940  VariantInit(&var);
941  var.vt = VT_BSTR;
942  hr = pBag->Read(L"FriendlyName", &var, errorLog);
943  if (hr == NOERROR)
944  {
945  string cDevName = (char*)((_bstr_t)(var.bstrVal));
946  // if the string is empty, take first device
947  // if device is equal to defined string take it as well
948  if(sCaptureDeviceName_ == "" ||
949  sCaptureDeviceName_.compare(cDevName)==0)
950  {
951  cout<< "Found CaptureDevice: " << cDevName.c_str() <<endl;
952  Identifier_ = cDevName;
953  hr = pM->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc);
954  if(!SUCCEEDED(hr))
955  cout<< "Failed to bind to object!" <<endl;
956  else
957  cout<< "Successfully bound to object!" <<endl;
958  break;
959  }
960  else
961  cout<<"The defined device is not found taking next.."<<endl;
962  SysFreeString(var.bstrVal);
963  }
964  pBag->Release();
965  }
966  pM->Release();
967  }
968  *ppSrcFilter = pSrc;
969  return hr;
970 }
971 
972 HRESULT VideoSource_DSHOW::EnumFilters_(IFilterGraph *pGraph)
973 {
974  IEnumFilters *pEnum = NULL;
975  IBaseFilter *pFilter;
976  ULONG cFetched;
977 
978  HRESULT hr = pGraph->EnumFilters(&pEnum);
979  if (FAILED(hr)) return hr;
980 
981  while(pEnum->Next(1, &pFilter, &cFetched) == S_OK)
982  {
983  FILTER_INFO FilterInfo;
984  hr = pFilter->QueryFilterInfo(&FilterInfo);
985  if (FAILED(hr))
986  {
987  MessageBox(NULL, TEXT("Could not get the filter info"),
988  TEXT("Error"), MB_OK | MB_ICONERROR);
989  continue; // Maybe the next one will work.
990  }
991 
992  // The FILTER_INFO structure holds a pointer to the Filter Graph
993  // Manager, with a reference count that must be released.
994  if (FilterInfo.pGraph != NULL)
995  {
996  FilterInfo.pGraph->Release();
997  }
998  if( bShowDialogs_ )
999  DisplayFilterProperties( pFilter );
1000 
1001  /* IPin * pPin = GetPin( pFilter, PINDIR_INPUT );
1002  pPin->Release();*/
1003 
1004  pFilter->Release();
1005  }
1006 
1007  pEnum->Release();
1008  return hr;
1009 }
1010 
1011 
1012 
1014 {
1015  HRESULT hr = pSrcFilter_->QueryInterface(IID_IAMCameraControl, (void**)&pCameraControl_ );
1016  if (SUCCEEDED(hr)){
1017  cout <<"Have a CameraControl"<<endl;
1018  }
1019  //CameraControl_Zoom;
1020  ControlZoom_.id = KSPROPERTY_CAMERACONTROL_ZOOM;
1021  if(pCameraControl_!=NULL)
1022  {
1025  &ControlZoom_.flags);
1026  if (!SUCCEEDED(hr)) ControlZoom_.id = -1;
1027 
1028  ControlPan_.id = CameraControl_Pan;
1029  hr = pCameraControl_->GetRange(ControlPan_.id,&ControlPan_.min,
1031  &ControlPan_.flags);
1032  if (!SUCCEEDED(hr)) ControlPan_.id = -1;
1033 
1034  ControlTilt_.id = CameraControl_Tilt;
1037  &ControlTilt_.flags);
1038  if (!SUCCEEDED(hr)) ControlTilt_.id = -1;
1039 
1040  ControlExposure_.id = CameraControl_Exposure;
1044  if (!SUCCEEDED(hr)) ControlExposure_.id = -1;
1045  else{
1046  //set variables of VideoSource_Base
1049 
1050  cout<<"Exposure control:"<<endl;
1051  cout<<"min:"<<ControlExposure_.min<<endl;
1052  cout<<"max:"<<ControlExposure_.max<<endl;
1053  cout<<"stepping:"<<ControlExposure_.stepping<<endl;
1054  cout<<"standard:"<<ControlExposure_.standard<<endl;
1055  }
1056  ControlBrightness_.id = VideoProcAmp_Brightness;
1057  }
1058 
1059  hr = pSrcFilter_->QueryInterface(IID_IAMVideoProcAmp, (void**)&pVideoProcControl_ );
1060  if (SUCCEEDED(hr)){
1061  cout <<"Have a VideoProcAmp"<<endl;
1062  }
1063  if(pVideoProcControl_!=NULL)
1064  {
1068  if (!SUCCEEDED(hr)) ControlBrightness_.id = -1;
1069  else{
1070  //set variables of VideoSource_Base
1073  cout<<"Brightness control:"<<endl;
1074  cout<<"min:"<<ControlBrightness_.min<<endl;
1075  cout<<"max:"<<ControlBrightness_.max<<endl;
1076  cout<<"stepping:"<<ControlBrightness_.stepping<<endl;
1077  cout<<"standard:"<<ControlBrightness_.standard<<endl;
1078  }
1079  /* Doesn't work for ICUBE Cameras
1080  ControlContrast_.id = VideoProcAmp_Contrast;
1081  ControlContrast_.standard = VideoProcAmp_Contrast;
1082  hr = pVideoProcControl_->GetRange(ControlContrast_.id,&ControlContrast_.min,
1083  &ControlContrast_.max,&ControlContrast_.stepping,&ControlContrast_.standard,
1084  &ControlContrast_.flags);
1085  if (!SUCCEEDED(hr)) ControlContrast_.id = -1;
1086  else{
1087  //set variables of VideoSource_Base
1088  minContrast_ = ControlContrast_.min;
1089  maxContrast_ = ControlContrast_.max;
1090  cout<<"Contrast control:"<<endl;
1091  cout<<"min:"<<ControlContrast_.min<<endl;
1092  cout<<"max:"<<ControlContrast_.max<<endl;
1093  cout<<"stepping:"<<ControlContrast_.stepping<<endl;
1094  cout<<"standard:"<<ControlContrast_.standard<<endl;
1095  }*/
1096  }
1097 }
1098 // Pass it a file name in wszPath, and it will save the filter graph
1099 // to that file.
1100 HRESULT VideoSource_DSHOW::SaveGraphFile_(IGraphBuilder *pGraph, WCHAR *wszPath)
1101 {
1102  const WCHAR wszStreamName[] = L"ActiveMovieGraph";
1103  HRESULT hr;
1104  IStorage *pStorage = NULL;
1105 
1106  // First, create a document file that will hold the GRF file
1107  hr = StgCreateDocfile(
1108  wszPath,
1109  STGM_CREATE | STGM_TRANSACTED | STGM_READWRITE |
1110  STGM_SHARE_EXCLUSIVE,
1111  0, &pStorage);
1112  if(FAILED(hr))
1113  {
1114  return hr;
1115  }
1116 
1117  // Next, create a stream to store.
1118  IStream *pStream;
1119  hr = pStorage->CreateStream(
1120  wszStreamName,
1121  STGM_WRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
1122  0, 0, &pStream);
1123  if (FAILED(hr))
1124  {
1125  pStorage->Release();
1126  return hr;
1127  }
1128 
1129  // The IpersistStream::Save method converts a stream
1130  // into a persistent object.
1131  IPersistStream *pPersist = NULL;
1132  pGraph->QueryInterface(IID_IPersistStream,
1133  reinterpret_cast<void**>(&pPersist));
1134  hr = pPersist->Save(pStream, TRUE);
1135  pStream->Release();
1136  pPersist->Release();
1137  if (SUCCEEDED(hr))
1138  {
1139  hr = pStorage->Commit(STGC_DEFAULT);
1140  }
1141  pStorage->Release();
1142  return hr;
1143 }
1144 
1145 
1147 {
1148  if (pCameraControl_ ) {
1149  long prop = CameraControl_Exposure;
1150  long value,flags;
1151  HRESULT hr = pCameraControl_->Get(prop,&value,&flags);
1152  //cout <<"camera shutter get: "<<value<<endl;
1153  //float exptime = pow(2.0f,float(value));
1154  //exptime/=1000.0;
1155  float exptime = float(value)/10000.0f;
1156  return exptime;
1157  }
1158  else
1159  return -1;
1160 }
1161 
1163 {
1164  if (pCameraControl_ ) {
1165  exptime*=10000.0;
1166  long prop = CameraControl_Exposure;
1167  long value,flags = CameraControl_Flags_Manual;
1168 
1169  //double tmp = log(exptime) / log(2.0);
1170  //value = long(rint(tmp));
1171 
1172  value = long(exptime);
1173  //cout <<"camera shutter set: "<<value<<" [ms]"<<endl;
1174  HRESULT hr = pCameraControl_->Set(prop,value,flags);
1175 
1176  if (SUCCEEDED(hr)) {
1177  return 0;
1178  }
1179  else
1180  return -1;
1181  }
1182  else return -1;
1183 }
1184 
1185 
1187 {
1188  if (pCameraControl_ ) {
1189  long prop = CameraControl_Exposure;
1190  long value,flags = CameraControl_Flags_Auto;
1191  if(!b) flags = CameraControl_Flags_Manual;
1192  value = 0;
1193  HRESULT hr = pCameraControl_->Set(prop,value,flags);
1194  }
1195 }
1196 
1198 {
1199  if (pCameraControl_ ) {
1200  long prop = CameraControl_Exposure;
1201  long value,flags = CameraControl_Flags_Auto;
1202  value =0;
1203  HRESULT hr = pCameraControl_->Get(prop,&value,&flags);
1204  if (SUCCEEDED(hr)) return true;
1205  else return false;
1206  }
1207  return false;
1208 }
1209 
1212  if(pCameraControl_!= NULL)
1213  return true;
1214  else
1215  return false;
1216 }
1217 
1219 {
1220  if (pVideoProcControl_ && ControlBrightness_.id >=0) return true;
1221  return false;
1222 }
1223 
1224 
1226 {
1228  long value,flags;
1229  pVideoProcControl_->Get(ControlBrightness_.id,&value,&flags);
1230  //cout <<"camera gain get: "<<value<<" [dB]"<<endl;
1231  float br = float(value);
1232  //br /=100.0f;
1233  return br;
1234  }
1235  return 0;
1236 }
1237 
1239 {
1241  // g*=100.0f;
1242  long value = long(g);
1243  //cout <<"camera gain set: "<<value<<" [dB]"<<endl;
1245  return 0;
1246  }else return -1;
1247  return 0;
1248 }
1249 
1251 {
1252  long value,flags;
1253  pVideoProcControl_->Get(ControlBrightness_.id,&value,&flags);
1254  if (b )
1255  flags = VideoProcAmp_Flags_Auto;
1256  else
1257  flags = VideoProcAmp_Flags_Manual;
1258  pVideoProcControl_->Set(ControlBrightness_.id,value,flags);
1259 }
1260 
1262 {
1263  long value;
1265  return ControlBrightness_.flags == VideoProcAmp_Flags_Auto;
1266 }
1267 
1269 {
1270  if (pVideoProcControl_ && ControlContrast_.id >=0) return true;
1271  return false;
1272 }
1273 
1275 {
1276  if (pVideoProcControl_ && ControlContrast_.id >=0) {
1277  long value;
1279  float br = (float(value-ControlContrast_.min) /
1280  float(ControlContrast_.max-ControlContrast_.min))* 100.0f;
1281  return br;
1282  }
1283  return 0;
1284 }
1285 
1287 {
1288  if (pVideoProcControl_ && ControlContrast_.id >=0) {
1289  long value;
1290  float br = (g/100.f) * float(ControlContrast_.max-ControlContrast_.min)
1291  + float(ControlContrast_.min);
1292  value = (int)rint(br);
1294  return (int)rint(br);
1295  }
1296  return 0;
1297 }
1298 
1299 
1301 {
1302  long value,flags;
1303  pVideoProcControl_->Get(ControlContrast_.id,&value,&flags);
1304  if (b )
1305  flags = VideoProcAmp_Flags_Auto;
1306  else
1307  flags = VideoProcAmp_Flags_Manual;
1308  pVideoProcControl_->Set(ControlContrast_.id,value,flags);
1309 }
1310 
1312 {
1313  long value;
1315  return ControlContrast_.flags == VideoProcAmp_Flags_Auto;
1316  return 0;
1317 }
1318 
1319 
1320 
1321 
1324 {
1325 
1326 }
1327 
1328 
1329 
1330 ////////////////////////////////////////////////////////////////////////
1331 
1332 //constructor
1333 MySampleGrabberCB::MySampleGrabberCB(int width, int height, int depth)
1334 {
1335  sampleLength_ = 1000;
1336  if(sampleLength_ > width*height/2) sampleLength_ = width*height/2;
1337  lastMemorySample_ = new unsigned char[sampleLength_];
1338  currentMemorySample_ = new unsigned char[sampleLength_];
1339  bCheckNewImage_ = false;
1340  if(!(camera_.GetImageData()==NULL))
1341  camera_.Release();
1342  camera_.Init(width,height,depth);
1343 }
1344 
1345 /****************************************************************/
1346 
1348  if(!(camera_.GetImageData()==NULL))
1349  camera_.Release();
1350  delete[] lastMemorySample_;
1351  delete[] currentMemorySample_;
1352 }
1353 /****************************************************************/
1354 STDMETHODIMP MySampleGrabberCB::BufferCB(double SampleTime, BYTE *pBuffer, long BufferLen)
1355 {
1356  #ifdef BIAS_HAVE_PTHREADS
1357  //after wakeup lock again %
1358  pthread_mutex_lock(&imageMutex_);
1359  #endif
1360  unsigned char *pImageData = camera_.GetImageData();
1361  BIASASSERT(BufferLen==(long)camera_.GetSizeByte());
1362  memcpy(pImageData,pBuffer,BufferLen);
1363 
1364  //reset pImageData to beginning of image
1365  pImageData = camera_.GetImageData()+(camera_.GetSizeByte()/2);
1366  //copy image data to current sample data
1367  memcpy(currentMemorySample_,pImageData,sampleLength_);
1368 
1369  if (UpSideDown_) camera_.Flip();
1370  #ifdef BIAS_HAVE_PTHREADS
1371  //after wakeup lock again %
1372  pthread_mutex_unlock(&imageMutex_);
1373  #endif
1374  return S_OK;
1375 }
1376 
1377 /****************************************************************/
1378 STDMETHODIMP MySampleGrabberCB::SampleCB(double SampleTime, IMediaSample *pSample)
1379 {
1380  #ifdef BIAS_HAVE_PTHREADS
1381  pthread_mutex_lock(&imageMutex_);
1382  #endif
1383 
1384  //set pImageData to beginning of image
1385  unsigned char *pImageData = camera_.GetImageData();
1386  BYTE *pSampleData;
1387  pSample->GetPointer(&pSampleData);
1388  long size = pSample->GetSize();
1389  BIASASSERT(size==(long)camera_.GetSizeByte());
1390  //copy sampleData to imageData of BIAS image
1391  memcpy(pImageData,pSampleData,size);
1392 
1393  //set pointer to middle of image for memory comparison
1394  pImageData = camera_.GetImageData()+(camera_.GetSizeByte()/2);;
1395  //copy image data to current memory sample data
1396  memcpy(currentMemorySample_,pImageData,sampleLength_);
1397 
1398  if (UpSideDown_) camera_.Flip();
1399  #ifdef BIAS_HAVE_PTHREADS
1400  pthread_mutex_unlock(&imageMutex_);
1401  #endif
1402  return S_OK;
1403 }
1404 
1405 /****************************************************************/
1406 
1407 STDMETHODIMP MySampleGrabberCB::QueryInterface(REFIID riid, void ** ppv) {
1408 
1409  if( riid == IID_ISampleGrabberCB || riid == IID_IUnknown ) {
1410  *ppv = (void *) static_cast<ISampleGrabberCB*> ( this );
1411  return NOERROR;
1412  }
1413  return E_NOINTERFACE;
1414 }
1415 
1418 {
1419  if(bCheckNewImage_){
1420  //check if they are identical
1422  //cout<<"old";
1423  return false;
1424  }
1425  else {
1426  //if they are different back up old
1428  //cout<<"new"<<endl;
1429  return true;
1430  }
1431  }
1432  else
1433  return true;
1434 }
STDMETHODIMP SampleCB(double SampleTime, IMediaSample *pSample)
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
YUYV422, 2 channels, full luminance Y, subsampled half U,V.
Definition: ImageBase.hh:133
virtual void SetSize(int w, int h, int bytesperpixel=1)
Set image size and number of bytes per pixel (e.g.
CComPtr< IAMVideoProcAmp > pVideoProcControl_
int GetCapabilities(VideoSourceCapabilities &caps)
Use this method to learn something about the capabilities of the source (only useful for V4L sources...
int GrabSingle(BIAS::Camera< unsigned char > &image)
int SetBrightness(float g)
Set brightness as value in interval [0, 100]. */.
int PostGrab()
Stop anything started in PreGrab()
int SetShutter(float exptime)
Set shutter (exposure time) to exptime in seconds.
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)
gray values, 1 channel
Definition: ImageBase.hh:130
BIAS::Camera< unsigned char > camera_
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
void CopyIn_NoInit(void *data)
Take some data and fill it into the Image.
Definition: ImageBase.cpp:827
void DeleteMediaType_(AM_MEDIA_TYPE *pmt)
YUV411, 2 channles, full luminance, 1 U, 1 V.
Definition: ImageBase.hh:137
unsigned char * lastMemorySample_
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
unsigned int GetSizeByte() const
returns the nr.
Definition: ImageBase.hh:352
BIAS::ImageBase::EColorModel ColorMode_
Color mode used by camera.
CComPtr< IGraphBuilder > pGraphBuilder_
CComPtr< IAMCameraControl > pCameraControl_
bool DisplayPinProperties_(CComPtr< IPin > pSrcPin)
bool WaitForNew_
Wait for new frames in GrabSingle()
CComPtr< IBaseFilter > pSrcFilter_
int GetColorChannels() const
float GetContrast()
Get contrast as value in interval [0, 100]. */.
Image< unsigned char > & GetImage()
MySampleGrabberCB(int width, int height, int depth)
YUV420P, 2 channels, full luminance Y, 1 U, 1 V. Y, U and V are grouped together for better compressi...
Definition: ImageBase.hh:135
CComPtr< ICaptureGraphBuilder2 > pCaptureGraphBuilder2_
int GetHeight() const
int InitImage(BIAS::ImageBase &Image)
color values, 3 channels, order: blue,green,red
Definition: ImageBase.hh:132
virtual int SetColorModel(BIAS::ImageBase::EColorModel mode)
Select colormodel to use.
void ResetPanTiltZoom(void)
Reset digital zoom, pan, and tilt to defaults to ensure compliance with calibration.
std::string Identifier_
Verbose camera descriptor.
MySampleGrabberCB * FGCB
int OpenDevice()
selects the first available device to open (e.g.
BIAS::ImageBase::EColorModel BIASColorModel_
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
bool DisplayFilterProperties(CComPtr< IBaseFilter > pFilter)
CComPtr< ISampleGrabber > pGrabber_
UYVY422, 2 channels, full luminance Y, subsampled half U,V inverse order.
Definition: ImageBase.hh:134
HRESULT SaveGraphFile_(IGraphBuilder *pGraph, WCHAR *wszPath)
The image template class for specific storage types.
Definition: Image.hh:78
CComPtr< IMediaEventEx > pMediaEvent_
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
void SetUID(const BIAS::UUID &id)
Definition: ImageBase.hh:589
RGBA, 4 channels, order: red,green,blue,alpha.
Definition: ImageBase.hh:141
void Release(const bool reset_storage_type=false)
Free the allocated data structures Hands off: Do !!NOT!! change the default of reset_storage_type: Im...
Definition: ImageBase.cpp:350
HRESULT FindCaptureDevice_(IBaseFilter **ppSrcFilter)
HRESULT EnumFilters_(IFilterGraph *pGraph)
void AddMode(unsigned int width, unsigned int height, float fps, ImageBase::EColorModel colormodel, bool interlaced=false)
unsigned char * currentMemorySample_
void Init(unsigned int width, unsigned int height, unsigned int nChannels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
Initialize image size and channels.
Definition: ImageBase.cpp:229
invalid (not set) image format
Definition: ImageBase.hh:129
Checks for VideoSource capabilities.
virtual const char * what() const
Returns a C-style character string describing the general cause of the current error.
Definition: Exception.hh:100
VideoSource_DSHOW(bool bShowDialogs=false)
int Flip()
flips the image vertically (row order is inverted) In place function return 0 in case of success...
Definition: ImageBase.cpp:834
int GetAllDevices(std::vector< std::string > &devices)
void SetNewImageCheck(bool check=true)
CComPtr< IPin > pSampleOutPin_
float GetBrightness()
Get brightness as value in interval [0, 100]. */.
int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
float GetShutter()
Get shutter (exposure time) in seconds.
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
static UUID GenerateUUID(const bool &consecutively=DEFAULT_UUID_CONSECUTIVELY)
static function which simply produces a uuid and returns
Definition: UUID.cpp:235
CComPtr< IMediaControl > pMediaControl_
generic exception
Definition: Exception.hh:77
int SetContrast(float g)
Set contrast as value in interval [0, 100]. */.
VideoSourceCapabilities Capabilities_