23 #include <opencv2/highgui/highgui.hpp>
25 TypeHandle WebcamVideoCursorOpenCV::_type_handle;
30 WebcamVideoCursorOpenCV::
32 _size_x = src->_size_x;
33 _size_y = src->_size_y;
37 _can_seek_fast =
false;
42 _capture = cvCaptureFromCAM(src->_camera_index);
43 if (_capture !=
nullptr) {
44 _size_x = (int)cvGetCaptureProperty(_capture, CV_CAP_PROP_FRAME_WIDTH);
45 _size_y = (int)cvGetCaptureProperty(_capture, CV_CAP_PROP_FRAME_HEIGHT);
53 WebcamVideoCursorOpenCV::
54 ~WebcamVideoCursorOpenCV() {
55 if (_capture !=
nullptr) {
56 cvReleaseCapture(&_capture);
70 PT(
Buffer) buffer = get_standard_buffer();
71 unsigned char *dest = buffer->_block;
72 int num_components = get_num_components();
73 nassertr(num_components == 3,
nullptr);
74 int dest_x_pitch = num_components;
75 int dest_y_pitch = _size_x * dest_x_pitch;
77 const unsigned char *r, *g, *b;
79 if (get_frame_data(r, g, b, x_pitch, y_pitch)) {
80 if (num_components == 3 && x_pitch == 3) {
82 int copy_bytes = _size_x * dest_x_pitch;
83 nassertr(copy_bytes <= dest_y_pitch && copy_bytes <= abs(y_pitch),
nullptr);
85 for (
int y = 0; y < _size_y; ++y) {
86 memcpy(dest, r, copy_bytes);
94 for (
int y = 0; y < _size_y; ++y) {
97 for (
int x = 0; x < _size_x; ++x) {
100 dest[dx + 2] = b[sx];
104 dest += dest_y_pitch;
129 bool WebcamVideoCursorOpenCV::
130 get_frame_data(
const unsigned char *&r,
131 const unsigned char *&g,
132 const unsigned char *&b,
133 int &x_pitch,
int &y_pitch) {
134 nassertr(ready(),
false);
136 IplImage *image = cvQueryFrame(_capture);
137 if (image ==
nullptr) {
141 r = (
const unsigned char *)image->imageData;
145 y_pitch = image->widthStep;
147 if (image->dataOrder == 1) {
151 g = r + image->height * y_pitch;
152 b = g + image->height * y_pitch;
155 if (image->origin == 0) {
160 r += (image->height - 1) * y_pitch;
161 g += (image->height - 1) * y_pitch;
162 b += (image->height - 1) * y_pitch;