PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/of/of_v0.9.3_vs_release/libs/openFrameworks/video/ofDirectShowGrabber.cpp

https://gitlab.com/cocoroac/walkingartists
C++ | 304 lines | 160 code | 74 blank | 70 comment | 26 complexity | 9f2ec08841b95ce20050977f2bfeb823 MD5 | raw file
  1. #include "ofDirectShowGrabber.h"
  2. #include "ofUtils.h"
  3. #ifdef TARGET_WIN32
  4. //--------------------------------------------------------------------
  5. ofDirectShowGrabber::ofDirectShowGrabber(){
  6. //---------------------------------
  7. #ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
  8. //---------------------------------
  9. bVerbose = false;
  10. bDoWeNeedToResize = false;
  11. //---------------------------------
  12. #endif
  13. //---------------------------------
  14. // common
  15. bIsFrameNew = false;
  16. bVerbose = false;
  17. bGrabberInited = false;
  18. bChooseDevice = false;
  19. deviceID = 0;
  20. width = 320; // default setting
  21. height = 240; // default setting
  22. attemptFramerate = -1;
  23. }
  24. //--------------------------------------------------------------------
  25. ofDirectShowGrabber::~ofDirectShowGrabber(){
  26. close();
  27. }
  28. //--------------------------------------------------------------------
  29. bool ofDirectShowGrabber::setup(int w, int h){
  30. //---------------------------------
  31. #ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
  32. //---------------------------------
  33. if (bChooseDevice){
  34. device = deviceID;
  35. ofLogNotice("ofDirectShowGrabber") << "initGrabber(): choosing " << deviceID;
  36. } else {
  37. device = 0;
  38. }
  39. width = w;
  40. height = h;
  41. bGrabberInited = false;
  42. if( attemptFramerate >= 0){
  43. VI.setIdealFramerate(device, attemptFramerate);
  44. }
  45. bool bOk = VI.setupDevice(device, width, height);
  46. int ourRequestedWidth = width;
  47. int ourRequestedHeight = height;
  48. if (bOk == true){
  49. bGrabberInited = true;
  50. width = VI.getWidth(device);
  51. height = VI.getHeight(device);
  52. if (width == ourRequestedWidth && height == ourRequestedHeight){
  53. bDoWeNeedToResize = false;
  54. } else {
  55. bDoWeNeedToResize = true;
  56. width = ourRequestedWidth;
  57. height = ourRequestedHeight;
  58. }
  59. pixels.allocate(width, height, 3);
  60. return true;
  61. } else {
  62. ofLogError("ofDirectShowGrabber") << "initGrabber(): error allocating a video device";
  63. ofLogError("ofDirectShowGrabber") << "initGrabber(): please check your camera with AMCAP or other software";
  64. bGrabberInited = false;
  65. return false;
  66. }
  67. //---------------------------------
  68. #endif
  69. //---------------------------------
  70. }
  71. //---------------------------------------------------------------------------
  72. bool ofDirectShowGrabber::setPixelFormat(ofPixelFormat pixelFormat){
  73. //note as we only support RGB we are just confirming that this pixel format is supported
  74. if( pixelFormat == OF_PIXELS_RGB ){
  75. return true;
  76. }
  77. ofLogWarning("ofDirectShowGrabber") << "setPixelFormat(): requested pixel format not supported";
  78. return false;
  79. }
  80. //---------------------------------------------------------------------------
  81. ofPixelFormat ofDirectShowGrabber::getPixelFormat() const {
  82. //note if you support more than one pixel format you will need to return a ofPixelFormat variable.
  83. return OF_PIXELS_RGB;
  84. }
  85. //--------------------------------------------------------------------
  86. vector<ofVideoDevice> ofDirectShowGrabber::listDevices() const {
  87. vector <ofVideoDevice> devices;
  88. //---------------------------------
  89. #ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
  90. //---------------------------------
  91. ofLogNotice() << "---";
  92. VI.listDevices();
  93. ofLogNotice() << "---";
  94. vector <string> devList = VI.getDeviceList();
  95. for(int i = 0; i < devList.size(); i++){
  96. ofVideoDevice vd;
  97. vd.deviceName = devList[i];
  98. vd.id = i;
  99. vd.bAvailable = true;
  100. devices.push_back(vd);
  101. }
  102. //---------------------------------
  103. #endif
  104. //---------------------------------
  105. return devices;
  106. }
  107. //--------------------------------------------------------------------
  108. void ofDirectShowGrabber::update(){
  109. //---------------------------------
  110. #ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
  111. //---------------------------------
  112. if (bGrabberInited == true){
  113. bIsFrameNew = false;
  114. if (VI.isFrameNew(device)){
  115. bIsFrameNew = true;
  116. /*
  117. rescale --
  118. currently this is nearest neighbor scaling
  119. not the greatest, but fast
  120. this can be optimized too
  121. with pointers, etc
  122. better --
  123. make sure that you ask for a "good" size....
  124. */
  125. unsigned char * viPixels = VI.getPixels(device, true, true);
  126. if (bDoWeNeedToResize == true){
  127. int inputW = VI.getWidth(device);
  128. int inputH = VI.getHeight(device);
  129. float scaleW = (float)inputW / (float)width;
  130. float scaleH = (float)inputH / (float)height;
  131. for(int i=0;i<width;i++){
  132. for(int j=0;j<height;j++){
  133. float posx = i * scaleW;
  134. float posy = j * scaleH;
  135. /*
  136. // start of calculating
  137. // for linear interpolation
  138. int xbase = (int)floor(posx);
  139. int xhigh = (int)ceil(posx);
  140. float pctx = (posx - xbase);
  141. int ybase = (int)floor(posy);
  142. int yhigh = (int)ceil(posy);
  143. float pcty = (posy - ybase);
  144. */
  145. int posPix = (((int)posy * inputW * 3) + ((int)posx * 3));
  146. pixels.getData()[(j*width*3) + i*3 ] = viPixels[posPix ];
  147. pixels.getData()[(j*width*3) + i*3 + 1] = viPixels[posPix+1];
  148. pixels.getData()[(j*width*3) + i*3 + 2] = viPixels[posPix+2];
  149. }
  150. }
  151. } else {
  152. pixels.setFromPixels(viPixels,width,height,OF_IMAGE_COLOR);
  153. }
  154. }
  155. }
  156. //---------------------------------
  157. #endif
  158. //---------------------------------
  159. }
  160. //--------------------------------------------------------------------
  161. void ofDirectShowGrabber::close(){
  162. //---------------------------------
  163. #ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
  164. //---------------------------------
  165. if (bGrabberInited == true){
  166. VI.stopDevice(device);
  167. bGrabberInited = false;
  168. }
  169. //---------------------------------
  170. #endif
  171. //---------------------------------
  172. clearMemory();
  173. }
  174. //--------------------------------------------------------------------
  175. void ofDirectShowGrabber::clearMemory(){
  176. pixels.clear();
  177. }
  178. //---------------------------------------------------------------------------
  179. ofPixels& ofDirectShowGrabber::getPixels(){
  180. return pixels;
  181. }
  182. //---------------------------------------------------------------------------
  183. const ofPixels& ofDirectShowGrabber::getPixels() const {
  184. return pixels;
  185. }
  186. //--------------------------------------------------------------------
  187. float ofDirectShowGrabber::getWidth() const {
  188. return width;
  189. }
  190. //--------------------------------------------------------------------
  191. float ofDirectShowGrabber::getHeight() const {
  192. return height;
  193. }
  194. //---------------------------------------------------------------------------
  195. bool ofDirectShowGrabber::isFrameNew() const{
  196. return bIsFrameNew;
  197. }
  198. //---------------------------------------------------------------------------
  199. bool ofDirectShowGrabber::isInitialized() const{
  200. return bGrabberInited;
  201. }
  202. //--------------------------------------------------------------------
  203. void ofDirectShowGrabber::setVerbose(bool bTalkToMe){
  204. bVerbose = bTalkToMe;
  205. }
  206. //--------------------------------------------------------------------
  207. void ofDirectShowGrabber::setDeviceID(int _deviceID){
  208. deviceID = _deviceID;
  209. bChooseDevice = true;
  210. }
  211. //--------------------------------------------------------------------
  212. void ofDirectShowGrabber::setDesiredFrameRate(int framerate){
  213. attemptFramerate = framerate;
  214. }
  215. //--------------------------------------------------------------------
  216. void ofDirectShowGrabber::videoSettings(void){
  217. //---------------------------------
  218. #ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
  219. //---------------------------------
  220. if (bGrabberInited == true) VI.showSettingsWindow(device);
  221. //---------------------------------
  222. #endif
  223. //---------------------------------
  224. }
  225. #endif