framework2

Форк
0
304 строки · 7.5 Кб
1
#include "ofDirectShowGrabber.h"
2
#include "ofUtils.h"
3
#ifdef TARGET_WIN32
4
//--------------------------------------------------------------------
5
ofDirectShowGrabber::ofDirectShowGrabber(){
6

7
	//---------------------------------
8
	#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
9
	//---------------------------------
10

11
		bVerbose 			= false;
12
		bDoWeNeedToResize 	= false;
13

14
	//---------------------------------
15
	#endif
16
	//---------------------------------
17

18
	// common
19
	bIsFrameNew				= false;
20
	bVerbose 				= false;
21
	bGrabberInited 			= false;
22
	bChooseDevice			= false;
23
	deviceID				= 0;
24
	width 					= 320;	// default setting
25
	height 					= 240;	// default setting
26
	attemptFramerate		= -1;
27
}
28

29

30
//--------------------------------------------------------------------
31
ofDirectShowGrabber::~ofDirectShowGrabber(){
32
	close();
33
}
34

35

36
//--------------------------------------------------------------------
37
bool ofDirectShowGrabber::setup(int w, int h){
38

39
	//---------------------------------
40
	#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
41
	//---------------------------------
42

43
		if (bChooseDevice){
44
			device = deviceID;
45
			ofLogNotice("ofDirectShowGrabber") << "initGrabber(): choosing " << deviceID;
46
		} else {
47
			device = 0;
48
		}
49

50
		width = w;
51
		height = h;
52
		bGrabberInited = false;
53

54
		if( attemptFramerate >= 0){
55
			VI.setIdealFramerate(device, attemptFramerate);
56
		}
57
		bool bOk = VI.setupDevice(device, width, height);
58

59
		int ourRequestedWidth = width;
60
		int ourRequestedHeight = height;
61

62
		if (bOk == true){
63
			bGrabberInited = true;
64
			width 	= VI.getWidth(device);
65
			height 	= VI.getHeight(device);
66

67
			if (width == ourRequestedWidth && height == ourRequestedHeight){
68
				bDoWeNeedToResize = false;
69
			} else {
70
				bDoWeNeedToResize = true;
71
				width = ourRequestedWidth;
72
				height = ourRequestedHeight;
73
			}
74

75

76
			pixels.allocate(width, height, 3);
77
			return true;
78
		} else {
79
			ofLogError("ofDirectShowGrabber") << "initGrabber(): error allocating a video device";
80
			ofLogError("ofDirectShowGrabber") << "initGrabber(): please check your camera with AMCAP or other software";
81
			bGrabberInited = false;
82
			return false;
83
		}
84

85
	//---------------------------------
86
	#endif
87
	//---------------------------------
88

89
}
90

91
//---------------------------------------------------------------------------
92
bool ofDirectShowGrabber::setPixelFormat(ofPixelFormat pixelFormat){
93
	//note as we only support RGB we are just confirming that this pixel format is supported
94
	if( pixelFormat == OF_PIXELS_RGB ){
95
		return true;
96
	}
97
	ofLogWarning("ofDirectShowGrabber") << "setPixelFormat(): requested pixel format not supported";
98
	return false;
99
}
100

101
//---------------------------------------------------------------------------
102
ofPixelFormat ofDirectShowGrabber::getPixelFormat() const {
103
	//note if you support more than one pixel format you will need to return a ofPixelFormat variable. 
104
	return OF_PIXELS_RGB;
105
}
106

107
//--------------------------------------------------------------------
108
std::vector<ofVideoDevice> ofDirectShowGrabber::listDevices() const {
109
    
110
    std::vector <ofVideoDevice> devices; 
111
	
112
    //---------------------------------
113
	#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
114
	//---------------------------------
115
		ofLogNotice() << "---";
116
        VI.listDevices();
117
        ofLogNotice() << "---";
118
        
119
		std::vector <std::string> devList = VI.getDeviceList(); 
120
        
121
        for(std::size_t i = 0; i < devList.size(); i++){
122
            ofVideoDevice vd; 
123
            vd.deviceName = devList[i]; 
124
            vd.id = i;  
125
            vd.bAvailable = true; 
126
            devices.push_back(vd); 
127
        }
128

129
	//---------------------------------
130
	#endif
131
	//---------------------------------
132
    
133
    return devices;
134
}
135

136
//--------------------------------------------------------------------
137
void ofDirectShowGrabber::update(){
138

139
	//---------------------------------
140
	#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
141
	//---------------------------------
142

143
		if (bGrabberInited == true){
144
			bIsFrameNew = false;
145
			if (VI.isFrameNew(device)){
146

147
				bIsFrameNew = true;
148

149

150
				/*
151
				 	rescale --
152
				 	currently this is nearest neighbor scaling
153
				 	not the greatest, but fast
154
				 	this can be optimized too
155
				 	with pointers, etc
156

157
				 	better --
158
				 	make sure that you ask for a "good" size....
159

160
				*/
161

162
				unsigned char * viPixels = VI.getPixels(device, true, true);
163

164

165
				if (bDoWeNeedToResize == true){
166

167
					int inputW = VI.getWidth(device);
168
					int inputH = VI.getHeight(device);
169

170
					float scaleW =	(float)inputW / (float)width;
171
					float scaleH =	(float)inputH / (float)height;
172

173
					for(int i=0;i<width;i++){
174
						for(int j=0;j<height;j++){
175

176
							float posx = i * scaleW;
177
							float posy = j * scaleH;
178

179
							/*
180

181
							// start of calculating
182
							// for linear interpolation
183

184
							int xbase = (int)floor(posx);
185
							int xhigh = (int)ceil(posx);
186
							float pctx = (posx - xbase);
187

188
							int ybase = (int)floor(posy);
189
							int yhigh = (int)ceil(posy);
190
							float pcty = (posy - ybase);
191
							*/
192

193
							int posPix = (((int)posy * inputW * 3) + ((int)posx * 3));
194

195
							pixels.getData()[(j*width*3) + i*3    ] = viPixels[posPix  ];
196
							pixels.getData()[(j*width*3) + i*3 + 1] = viPixels[posPix+1];
197
							pixels.getData()[(j*width*3) + i*3 + 2] = viPixels[posPix+2];
198

199
						}
200
					}
201

202
				} else {
203

204
					pixels.setFromPixels(viPixels,width,height,OF_IMAGE_COLOR);
205

206
				}
207

208

209
			}
210
		}
211

212
	//---------------------------------
213
	#endif
214
	//---------------------------------
215

216
}
217

218
//--------------------------------------------------------------------
219
void ofDirectShowGrabber::close(){
220

221
	//---------------------------------
222
	#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
223
	//---------------------------------
224

225
		if (bGrabberInited == true){
226
			VI.stopDevice(device);
227
			bGrabberInited = false;
228
		}
229

230
	//---------------------------------
231
	#endif
232
	//---------------------------------
233

234
	clearMemory();
235

236
}
237

238

239
//--------------------------------------------------------------------
240
void ofDirectShowGrabber::clearMemory(){
241
	pixels.clear();
242
}
243

244
//---------------------------------------------------------------------------
245
ofPixels& ofDirectShowGrabber::getPixels(){
246
	return pixels;
247
}
248

249
//---------------------------------------------------------------------------
250
const ofPixels& ofDirectShowGrabber::getPixels() const {
251
	return pixels;
252
}
253

254
//--------------------------------------------------------------------
255
float ofDirectShowGrabber::getWidth() const {
256
	return width;
257
}
258

259
//--------------------------------------------------------------------
260
float ofDirectShowGrabber::getHeight() const {
261
	return height;
262
}
263

264
//---------------------------------------------------------------------------
265
bool  ofDirectShowGrabber::isFrameNew() const{
266
	return bIsFrameNew;
267
}
268

269
//---------------------------------------------------------------------------
270
bool  ofDirectShowGrabber::isInitialized() const{
271
	return bGrabberInited;
272
}
273

274
//--------------------------------------------------------------------
275
void ofDirectShowGrabber::setVerbose(bool bTalkToMe){
276
	bVerbose = bTalkToMe;
277
}
278

279
//--------------------------------------------------------------------
280
void ofDirectShowGrabber::setDeviceID(int _deviceID){
281
	deviceID		= _deviceID;
282
	bChooseDevice	= true;
283
}
284

285
//--------------------------------------------------------------------
286
void ofDirectShowGrabber::setDesiredFrameRate(int framerate){
287
	attemptFramerate = framerate;
288
}
289

290

291
//--------------------------------------------------------------------
292
void ofDirectShowGrabber::videoSettings(void){
293

294
	//---------------------------------
295
	#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
296
	//---------------------------------
297

298
		if (bGrabberInited == true) VI.showSettingsWindow(device);
299

300
	//---------------------------------
301
	#endif
302
	//---------------------------------
303
}
304
#endif
305

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.