00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "webcamVideoOpenCV.h"
00016
00017 #ifdef HAVE_OPENCV
00018
00019 #include "pStatTimer.h"
00020
00021 TypeHandle WebcamVideoCursorOpenCV::_type_handle;
00022
00023
00024
00025
00026
00027
00028 WebcamVideoCursorOpenCV::
00029 WebcamVideoCursorOpenCV(WebcamVideoOpenCV *src) : MovieVideoCursor(src) {
00030 _size_x = src->_size_x;
00031 _size_y = src->_size_y;
00032 _num_components = 3;
00033 _length = 1.0E10;
00034 _can_seek = false;
00035 _can_seek_fast = false;
00036 _aborted = false;
00037 _streaming = true;
00038 _ready = false;
00039
00040 _capture = cvCaptureFromCAM(src->_camera_index);
00041 if (_capture != NULL) {
00042 _size_x = (int)cvGetCaptureProperty(_capture, CV_CAP_PROP_FRAME_WIDTH);
00043 _size_y = (int)cvGetCaptureProperty(_capture, CV_CAP_PROP_FRAME_HEIGHT);
00044 _ready = true;
00045 }
00046 }
00047
00048
00049
00050
00051
00052
00053 WebcamVideoCursorOpenCV::
00054 ~WebcamVideoCursorOpenCV() {
00055 if (_capture != NULL) {
00056 cvReleaseCapture(&_capture);
00057 _capture = NULL;
00058 }
00059 }
00060
00061
00062
00063
00064
00065
00066 PT(MovieVideoCursor::Buffer) WebcamVideoCursorOpenCV::
00067 fetch_buffer() {
00068 if (!_ready) {
00069 return NULL;
00070 }
00071
00072 PT(Buffer) buffer = get_standard_buffer();
00073 unsigned char *dest = buffer->_block;
00074 int num_components = get_num_components();
00075 nassertr(num_components == 3, NULL);
00076 int dest_x_pitch = num_components;
00077 int dest_y_pitch = _size_x * dest_x_pitch;
00078
00079 const unsigned char *r, *g, *b;
00080 int x_pitch, y_pitch;
00081 if (get_frame_data(r, g, b, x_pitch, y_pitch)) {
00082 if (num_components == 3 && x_pitch == 3) {
00083
00084 int copy_bytes = _size_x * dest_x_pitch;
00085 nassertr(copy_bytes <= dest_y_pitch && copy_bytes <= abs(y_pitch), NULL);
00086
00087 for (int y = 0; y < _size_y; ++y) {
00088 memcpy(dest, r, copy_bytes);
00089 dest += dest_y_pitch;
00090 r += y_pitch;
00091 }
00092
00093 } else {
00094
00095
00096
00097 for (int y = 0; y < _size_y; ++y) {
00098 int dx = 0;
00099 int sx = 0;
00100 for (int x = 0; x < _size_x; ++x) {
00101 dest[dx] = r[sx];
00102 dest[dx + 1] = g[sx];
00103 dest[dx + 2] = b[sx];
00104 dx += dest_x_pitch;
00105 sx += x_pitch;
00106 }
00107 dest += dest_y_pitch;
00108 r += y_pitch;
00109 g += y_pitch;
00110 b += y_pitch;
00111 }
00112 }
00113 }
00114
00115 return buffer;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 bool WebcamVideoCursorOpenCV::
00140 get_frame_data(const unsigned char *&r,
00141 const unsigned char *&g,
00142 const unsigned char *&b,
00143 int &x_pitch, int &y_pitch) {
00144 nassertr(ready(), false);
00145
00146 IplImage *image = cvQueryFrame(_capture);
00147 if (image == NULL) {
00148 return false;
00149 }
00150
00151 r = (const unsigned char *)image->imageData;
00152 g = r + 1;
00153 b = g + 1;
00154 x_pitch = 3;
00155 y_pitch = image->widthStep;
00156
00157 if (image->dataOrder == 1) {
00158
00159
00160 x_pitch = 1;
00161 g = r + image->height * y_pitch;
00162 b = g + image->height * y_pitch;
00163 }
00164
00165 if (image->origin == 0) {
00166
00167
00168
00169
00170 r += (image->height - 1) * y_pitch;
00171 g += (image->height - 1) * y_pitch;
00172 b += (image->height - 1) * y_pitch;
00173 y_pitch = -y_pitch;
00174 }
00175
00176 return true;
00177 }
00178
00179 #endif