framework2
304 строки · 7.5 Кб
1#include "ofDirectShowGrabber.h"
2#include "ofUtils.h"
3#ifdef TARGET_WIN32
4//--------------------------------------------------------------------
5ofDirectShowGrabber::ofDirectShowGrabber(){
6
7//---------------------------------
8#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
9//---------------------------------
10
11bVerbose = false;
12bDoWeNeedToResize = false;
13
14//---------------------------------
15#endif
16//---------------------------------
17
18// common
19bIsFrameNew = false;
20bVerbose = false;
21bGrabberInited = false;
22bChooseDevice = false;
23deviceID = 0;
24width = 320; // default setting
25height = 240; // default setting
26attemptFramerate = -1;
27}
28
29
30//--------------------------------------------------------------------
31ofDirectShowGrabber::~ofDirectShowGrabber(){
32close();
33}
34
35
36//--------------------------------------------------------------------
37bool ofDirectShowGrabber::setup(int w, int h){
38
39//---------------------------------
40#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
41//---------------------------------
42
43if (bChooseDevice){
44device = deviceID;
45ofLogNotice("ofDirectShowGrabber") << "initGrabber(): choosing " << deviceID;
46} else {
47device = 0;
48}
49
50width = w;
51height = h;
52bGrabberInited = false;
53
54if( attemptFramerate >= 0){
55VI.setIdealFramerate(device, attemptFramerate);
56}
57bool bOk = VI.setupDevice(device, width, height);
58
59int ourRequestedWidth = width;
60int ourRequestedHeight = height;
61
62if (bOk == true){
63bGrabberInited = true;
64width = VI.getWidth(device);
65height = VI.getHeight(device);
66
67if (width == ourRequestedWidth && height == ourRequestedHeight){
68bDoWeNeedToResize = false;
69} else {
70bDoWeNeedToResize = true;
71width = ourRequestedWidth;
72height = ourRequestedHeight;
73}
74
75
76pixels.allocate(width, height, 3);
77return true;
78} else {
79ofLogError("ofDirectShowGrabber") << "initGrabber(): error allocating a video device";
80ofLogError("ofDirectShowGrabber") << "initGrabber(): please check your camera with AMCAP or other software";
81bGrabberInited = false;
82return false;
83}
84
85//---------------------------------
86#endif
87//---------------------------------
88
89}
90
91//---------------------------------------------------------------------------
92bool ofDirectShowGrabber::setPixelFormat(ofPixelFormat pixelFormat){
93//note as we only support RGB we are just confirming that this pixel format is supported
94if( pixelFormat == OF_PIXELS_RGB ){
95return true;
96}
97ofLogWarning("ofDirectShowGrabber") << "setPixelFormat(): requested pixel format not supported";
98return false;
99}
100
101//---------------------------------------------------------------------------
102ofPixelFormat ofDirectShowGrabber::getPixelFormat() const {
103//note if you support more than one pixel format you will need to return a ofPixelFormat variable.
104return OF_PIXELS_RGB;
105}
106
107//--------------------------------------------------------------------
108std::vector<ofVideoDevice> ofDirectShowGrabber::listDevices() const {
109
110std::vector <ofVideoDevice> devices;
111
112//---------------------------------
113#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
114//---------------------------------
115ofLogNotice() << "---";
116VI.listDevices();
117ofLogNotice() << "---";
118
119std::vector <std::string> devList = VI.getDeviceList();
120
121for(std::size_t i = 0; i < devList.size(); i++){
122ofVideoDevice vd;
123vd.deviceName = devList[i];
124vd.id = i;
125vd.bAvailable = true;
126devices.push_back(vd);
127}
128
129//---------------------------------
130#endif
131//---------------------------------
132
133return devices;
134}
135
136//--------------------------------------------------------------------
137void ofDirectShowGrabber::update(){
138
139//---------------------------------
140#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
141//---------------------------------
142
143if (bGrabberInited == true){
144bIsFrameNew = false;
145if (VI.isFrameNew(device)){
146
147bIsFrameNew = true;
148
149
150/*
151rescale --
152currently this is nearest neighbor scaling
153not the greatest, but fast
154this can be optimized too
155with pointers, etc
156
157better --
158make sure that you ask for a "good" size....
159
160*/
161
162unsigned char * viPixels = VI.getPixels(device, true, true);
163
164
165if (bDoWeNeedToResize == true){
166
167int inputW = VI.getWidth(device);
168int inputH = VI.getHeight(device);
169
170float scaleW = (float)inputW / (float)width;
171float scaleH = (float)inputH / (float)height;
172
173for(int i=0;i<width;i++){
174for(int j=0;j<height;j++){
175
176float posx = i * scaleW;
177float posy = j * scaleH;
178
179/*
180
181// start of calculating
182// for linear interpolation
183
184int xbase = (int)floor(posx);
185int xhigh = (int)ceil(posx);
186float pctx = (posx - xbase);
187
188int ybase = (int)floor(posy);
189int yhigh = (int)ceil(posy);
190float pcty = (posy - ybase);
191*/
192
193int posPix = (((int)posy * inputW * 3) + ((int)posx * 3));
194
195pixels.getData()[(j*width*3) + i*3 ] = viPixels[posPix ];
196pixels.getData()[(j*width*3) + i*3 + 1] = viPixels[posPix+1];
197pixels.getData()[(j*width*3) + i*3 + 2] = viPixels[posPix+2];
198
199}
200}
201
202} else {
203
204pixels.setFromPixels(viPixels,width,height,OF_IMAGE_COLOR);
205
206}
207
208
209}
210}
211
212//---------------------------------
213#endif
214//---------------------------------
215
216}
217
218//--------------------------------------------------------------------
219void ofDirectShowGrabber::close(){
220
221//---------------------------------
222#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
223//---------------------------------
224
225if (bGrabberInited == true){
226VI.stopDevice(device);
227bGrabberInited = false;
228}
229
230//---------------------------------
231#endif
232//---------------------------------
233
234clearMemory();
235
236}
237
238
239//--------------------------------------------------------------------
240void ofDirectShowGrabber::clearMemory(){
241pixels.clear();
242}
243
244//---------------------------------------------------------------------------
245ofPixels& ofDirectShowGrabber::getPixels(){
246return pixels;
247}
248
249//---------------------------------------------------------------------------
250const ofPixels& ofDirectShowGrabber::getPixels() const {
251return pixels;
252}
253
254//--------------------------------------------------------------------
255float ofDirectShowGrabber::getWidth() const {
256return width;
257}
258
259//--------------------------------------------------------------------
260float ofDirectShowGrabber::getHeight() const {
261return height;
262}
263
264//---------------------------------------------------------------------------
265bool ofDirectShowGrabber::isFrameNew() const{
266return bIsFrameNew;
267}
268
269//---------------------------------------------------------------------------
270bool ofDirectShowGrabber::isInitialized() const{
271return bGrabberInited;
272}
273
274//--------------------------------------------------------------------
275void ofDirectShowGrabber::setVerbose(bool bTalkToMe){
276bVerbose = bTalkToMe;
277}
278
279//--------------------------------------------------------------------
280void ofDirectShowGrabber::setDeviceID(int _deviceID){
281deviceID = _deviceID;
282bChooseDevice = true;
283}
284
285//--------------------------------------------------------------------
286void ofDirectShowGrabber::setDesiredFrameRate(int framerate){
287attemptFramerate = framerate;
288}
289
290
291//--------------------------------------------------------------------
292void ofDirectShowGrabber::videoSettings(void){
293
294//---------------------------------
295#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
296//---------------------------------
297
298if (bGrabberInited == true) VI.showSettingsWindow(device);
299
300//---------------------------------
301#endif
302//---------------------------------
303}
304#endif
305