ViSP  2.8.0
vpDirectShowSampleGrabberI.cpp
1 #ifndef DOXYGEN_SHOULD_SKIP_THIS
2 
3 #include <visp/vpDirectShowSampleGrabberI.h>
4 #include <visp/vpImageConvert.h>
5 
6 #include <visp/vpConfig.h>
7 #if ( defined(VISP_HAVE_DIRECTSHOW) )
8 
9 
10 
14 vpDirectShowSampleGrabberI::vpDirectShowSampleGrabberI()
15  : acqGrayDemand(false), acqRGBaDemand(false), specialMediaType(false), invertedSource(false)
16 {
17  //semaphore(0), max value = 1
18  copySem = CreateSemaphore (NULL,0,1,NULL);
19 }
20 
24 vpDirectShowSampleGrabberI::~vpDirectShowSampleGrabberI()
25 {
26  //destroys the semaphore
27  CloseHandle(copySem);
28 }
29 
30 
31 STDMETHODIMP vpDirectShowSampleGrabberI::QueryInterface(REFIID riid, void **ppvObject)
32 {
33  if (NULL == ppvObject) return E_POINTER;
34  if (riid == __uuidof(IUnknown))
35  {
36  *ppvObject = static_cast<IUnknown*>(this);
37  return S_OK;
38  }
39  if (riid == __uuidof(ISampleGrabberCB))
40  {
41  *ppvObject = static_cast<ISampleGrabberCB*>(this);
42  return S_OK;
43  }
44  return E_NOTIMPL;
45 }
46 
52 STDMETHODIMP vpDirectShowSampleGrabberI::BufferCB(double Time, BYTE *pBuffer, long BufferLen)
53 {
54  //if there has been a frame demand
55  if(acqGrayDemand || acqRGBaDemand)
56  {
57  //check if the connected media is compatible
58  if(connectedMediaType.formattype==FORMAT_VideoInfo)
59  {
60  //retrieve the image information
61  VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(connectedMediaType.pbFormat);
62  BITMAPINFOHEADER bmpInfo = pVih->bmiHeader;
63 
64  //if biHeight > 0 and the source is not special
65  //then the image needs to be verticaly flipped
66  bool flip;
67  if(!specialMediaType)
68  flip = bmpInfo.biHeight>=0;
69  //the source is fourcc and the image is inverted with this compression
70  else if(invertedSource)
71  flip = true;
72  //fourcc and the image doesn't need to be flipped
73  else
74  flip = false;
75 
76  //if the buffer contains a RGB24 image (DS RGB24 <=> BGR)
77  if(connectedMediaType.subtype==MEDIASUBTYPE_RGB24)
78  {
79  //if it was an RGBa image demand
80  if(acqRGBaDemand)
81  {
82  //first, resizes the image as needed
83  rgbaIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
84  //copy and convert the image
85  vpImageConvert::BGRToRGBa(pBuffer,(unsigned char*) rgbaIm->bitmap,
86  rgbaIm->getWidth() , rgbaIm->getHeight(), flip);
87  //reset the demand boolean
88  acqRGBaDemand = false;
89  }
90  else//if it was a grayscale image demand
91  {
92  //first, resizes the image as needed
93  grayIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
94  //copy and convert the image
95  vpImageConvert::BGRToGrey(pBuffer, grayIm->bitmap,
96  grayIm->getWidth(), grayIm->getHeight(), flip);
97  //reset the demand boolean
98  acqGrayDemand = false;
99  }
100  }
101  else
102  {
103  unsigned long FourCC;
104  FourCC = ((bmpInfo.biCompression&0xFF000000)>>24) |
105  ((bmpInfo.biCompression&0x00FF0000)>>8) |
106  ((bmpInfo.biCompression&0x0000FF00)<<8) |
107  (bmpInfo.biCompression&0x000000FF)<<24;
108  //if the buffer contains a like YUV420 image
109  if(connectedMediaType.subtype==MEDIASUBTYPE_IYUV || FourCC == 'I420')
110  {
111  //if it was an RGBa image demand
112  if(acqRGBaDemand)
113  {
114  //first, resizes the image as needed
115  rgbaIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
116  //copy and convert the image
117  vpImageConvert::YUV420ToRGBa(pBuffer,(unsigned char*) rgbaIm->bitmap,
118  rgbaIm->getWidth() , rgbaIm->getHeight());
119  //reset the demand boolean
120  acqRGBaDemand = false;
121  }
122  else//if it was a grayscale image demand
123  {
124  //first, resizes the image as needed
125  grayIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
126  //copy and convert the image
127  vpImageConvert::YUV420ToGrey(pBuffer, grayIm->bitmap,
128  grayIm->getWidth() * grayIm->getHeight());
129  //reset the demand boolean
130  acqGrayDemand = false;
131  }
132 
133  }
134  else if(connectedMediaType.subtype==MEDIASUBTYPE_YV12)
135  {
136  //if it was an RGBa image demand
137  if(acqRGBaDemand)
138  {
139  //first, resizes the image as needed
140  rgbaIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
141  //copy and convert the image
142  vpImageConvert::YV12ToRGBa(pBuffer,(unsigned char*) rgbaIm->bitmap,
143  rgbaIm->getWidth() , rgbaIm->getHeight());
144  //reset the demand boolean
145  acqRGBaDemand = false;
146  }
147  else//if it was a grayscale image demand
148  {
149  //first, resizes the image as needed
150  grayIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
151  //copy and convert the image
152  vpImageConvert::YUV420ToGrey(pBuffer, grayIm->bitmap,
153  grayIm->getWidth() * grayIm->getHeight());
154  //reset the demand boolean
155  acqGrayDemand = false;
156  }
157  }
158  else if(connectedMediaType.subtype==MEDIASUBTYPE_YVU9)
159  {
160  //if it was an RGBa image demand
161  if(acqRGBaDemand)
162  {
163  //first, resizes the image as needed
164  rgbaIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
165  //copy and convert the image
166  vpImageConvert::YVU9ToRGBa(pBuffer,(unsigned char*) rgbaIm->bitmap,
167  rgbaIm->getWidth() , rgbaIm->getHeight());
168  //reset the demand boolean
169  acqRGBaDemand = false;
170  }
171  else//if it was a grayscale image demand
172  {
173  //first, resizes the image as needed
174  grayIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
175  //copy and convert the image
176  vpImageConvert::YUV420ToGrey(pBuffer, grayIm->bitmap,
177  grayIm->getWidth() * grayIm->getHeight());
178  //reset the demand boolean
179  acqGrayDemand = false;
180  }
181  }
182  else if(connectedMediaType.subtype==MEDIASUBTYPE_YUY2 ||
183  connectedMediaType.subtype==MEDIASUBTYPE_YUYV)
184  {
185  //if it was an RGBa image demand
186  if(acqRGBaDemand)
187  {
188  //first, resizes the image as needed
189  rgbaIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
190  //copy and convert the image
191  vpImageConvert::YCbCrToRGBa(pBuffer,(unsigned char*) rgbaIm->bitmap,
192  rgbaIm->getWidth()*rgbaIm->getHeight());
193  //reset the demand boolean
194  acqRGBaDemand = false;
195  }
196  else//if it was a grayscale image demand
197  {
198  //first, resizes the image as needed
199  grayIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
200  //copy and convert the image
201  vpImageConvert::YCbCrToGrey(pBuffer, grayIm->bitmap,
202  grayIm->getWidth() * grayIm->getHeight());
203  //reset the demand boolean
204  acqGrayDemand = false;
205  }
206  }
207  else if(connectedMediaType.subtype==MEDIASUBTYPE_YVYU)
208  {
209  //if it was an RGBa image demand
210  if(acqRGBaDemand)
211  {
212  //first, resizes the image as needed
213  rgbaIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
214  //copy and convert the image
215  vpImageConvert::YCrCbToRGBa(pBuffer,(unsigned char*) rgbaIm->bitmap,
216  rgbaIm->getWidth()*rgbaIm->getHeight());
217  //reset the demand boolean
218  acqRGBaDemand = false;
219  }
220  else//if it was a grayscale image demand
221  {
222  //first, resizes the image as needed
223  grayIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
224  //copy and convert the image
225  vpImageConvert::YCbCrToGrey(pBuffer, grayIm->bitmap,
226  grayIm->getWidth() * grayIm->getHeight());
227  //reset the demand boolean
228  acqGrayDemand = false;
229  }
230  }
231  else if(connectedMediaType.subtype==MEDIASUBTYPE_UYVY)
232  {
233  //if it was an RGBa image demand
234  if(acqRGBaDemand)
235  {
236  //first, resizes the image as needed
237  rgbaIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
238  //copy and convert the image
239  vpImageConvert::YUV422ToRGBa(pBuffer,(unsigned char*) rgbaIm->bitmap,
240  rgbaIm->getWidth()*rgbaIm->getHeight());
241  //reset the demand boolean
242  acqRGBaDemand = false;
243  }
244  else//if it was a grayscale image demand
245  {
246  //first, resizes the image as needed
247  grayIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
248  //copy and convert the image
249  vpImageConvert::YUV422ToGrey(pBuffer, grayIm->bitmap,
250  grayIm->getWidth() * grayIm->getHeight());
251  //reset the demand boolean
252  acqGrayDemand = false;
253  }
254  }
255  else if(connectedMediaType.subtype==MEDIASUBTYPE_RGB32)
256  {
257  //if it was an RGBa image demand
258  if(acqRGBaDemand)
259  {
260  //first, resizes the image as needed
261  rgbaIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
262  //copy and convert the image
263  //copy(pBuffer ,pBuffer + 4*rgbaIm->getWidth()*rgbaIm->getHeight(),rgbaIm->bitmap);
264  memcpy(rgbaIm->bitmap,pBuffer ,4*rgbaIm->getWidth()*rgbaIm->getHeight());
265  //reset the demand boolean
266  acqRGBaDemand = false;
267  }
268  else//if it was a grayscale image demand
269  {
270  //first, resizes the image as needed
271  grayIm->resize(abs(bmpInfo.biHeight), bmpInfo.biWidth);
272  //copy and convert the image
273  vpImageConvert::RGBaToGrey(pBuffer, grayIm->bitmap,
274  grayIm->getWidth() * grayIm->getHeight());
275  //reset the demand boolean
276  acqGrayDemand = false;
277  }
278  }
279  }
280  }
281 
282  //increment the semaphore - allows acquire to continue execution
283  ReleaseSemaphore(copySem, 1, NULL);
284  }
285  return S_OK;
286 }
287 #endif
288 
289 #endif
static void YUV420ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
static void YUV422ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
static void YUV420ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height)
static void YVU9ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height)
static void YUV422ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
static void YV12ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height)
static void YCbCrToGrey(unsigned char *ycbcr, unsigned char *grey, unsigned int size)
static void BGRToGrey(unsigned char *bgr, unsigned char *grey, unsigned int width, unsigned int height, bool flip)
static void YCbCrToRGBa(unsigned char *ycbcr, unsigned char *rgb, unsigned int size)
static void YCrCbToRGBa(unsigned char *ycbcr, unsigned char *rgb, unsigned int size)
static void RGBaToGrey(unsigned char *rgba, unsigned char *grey, unsigned int size)
static void BGRToRGBa(unsigned char *bgr, unsigned char *rgba, unsigned int width, unsigned int height, bool flip)