1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "precomp.hpp"
49 // On Windows, we have no sys/select.h, but we need to pick up
50 // select() which is in winsock2.
51 #ifndef __SYS_SELECT_H__
52 #define __SYS_SELECT_H__ 1
56 #include <sys/select.h>
58 #include <dc1394/dc1394.h>
62 static dc1394error_t adaptBufferStereoLocal(dc1394video_frame_t *in, dc1394video_frame_t *out)
66 // buffer position is not changed. Size is boubled in Y
67 out->size[0] = in->size[0];
68 out->size[1] = in->size[1] * 2;
69 out->position[0] = in->position[0];
70 out->position[1] = in->position[1];
72 // color coding is set to mono8 or raw8.
73 switch (in->color_coding)
75 case DC1394_COLOR_CODING_RAW16:
76 out->color_coding = DC1394_COLOR_CODING_RAW8;
78 case DC1394_COLOR_CODING_MONO16:
79 case DC1394_COLOR_CODING_YUV422:
80 out->color_coding = DC1394_COLOR_CODING_MONO8;
83 return DC1394_INVALID_COLOR_CODING;
86 // keep the color filter value in all cases. if the format is not raw it will not be further used anyway
87 out->color_filter = in->color_filter;
89 // the output YUV byte order must be already set if the buffer is YUV422 at the output
90 // if the output is not YUV we don't care about this field.
91 // Hence nothing to do.
92 // we always convert to 8bits (at this point) we can safely set this value to 8.
95 // don't know what to do with stride... >>>> TODO: STRIDE SHOULD BE TAKEN INTO ACCOUNT... <<<<
97 // the video mode should not change. Color coding and other stuff can be accessed in specific fields of this struct
98 out->video_mode = in->video_mode;
101 out->padding_bytes = in->padding_bytes;
103 // image bytes changes: >>>> TODO: STRIDE SHOULD BE TAKEN INTO ACCOUNT... <<<<
104 dc1394_get_color_coding_bit_size(out->color_coding, &bpp);
105 out->image_bytes = (out->size[0] * out->size[1] * bpp) / 8;
107 // total is image_bytes + padding_bytes
108 out->total_bytes = out->image_bytes + out->padding_bytes;
110 // bytes-per-packet and packets_per_frame are internal data that can be kept as is.
111 out->packet_size = in->packet_size;
112 out->packets_per_frame = in->packets_per_frame;
114 // timestamp, frame_behind, id and camera are copied too:
115 out->timestamp = in->timestamp;
116 out->frames_behind = in->frames_behind;
117 out->camera = in->camera;
120 // verify memory allocation:
121 if (out->total_bytes > out->allocated_image_bytes)
124 out->image = (uint8_t*)malloc(out->total_bytes * sizeof(uint8_t));
125 out->allocated_image_bytes = out->total_bytes;
128 // Copy padding bytes:
129 memcpy(&(out->image[out->image_bytes]), &(in->image[in->image_bytes]), out->padding_bytes);
130 out->little_endian = DC1394_FALSE; // not used before 1.32 is out.
131 out->data_in_padding = DC1394_FALSE; // not used before 1.32 is out.
132 return DC1394_SUCCESS;
135 static dc1394error_t dc1394_deinterlace_stereo_frames_fixed(dc1394video_frame_t *in,
136 dc1394video_frame_t *out, dc1394stereo_method_t method)
138 if((in->color_coding == DC1394_COLOR_CODING_RAW16) ||
139 (in->color_coding == DC1394_COLOR_CODING_MONO16) ||
140 (in->color_coding == DC1394_COLOR_CODING_YUV422))
145 case DC1394_STEREO_METHOD_INTERLACED:
146 adaptBufferStereoLocal(in, out);
148 // dc1394_deinterlace_stereo(in->image, out->image, in->size[0], in->size[1]);
149 dc1394_deinterlace_stereo(in->image, out->image, out->size[0], out->size[1]);
152 case DC1394_STEREO_METHOD_FIELD:
153 adaptBufferStereoLocal(in, out);
154 memcpy(out->image, in->image, out->image_bytes);
158 return DC1394_INVALID_STEREO_METHOD;
161 return DC1394_FUNCTION_NOT_SUPPORTED;
164 static uint32_t getControlRegister(dc1394camera_t *camera, uint64_t offset)
167 dc1394error_t err = dc1394_get_control_register(camera, offset, &value);
169 assert(err == DC1394_SUCCESS);
170 return err == DC1394_SUCCESS ? value : 0xffffffff;
188 CvDC1394::~CvDC1394()
195 static CvDC1394 dc1394;
197 class CvCaptureCAM_DC1394_v2_CPP : public CvCapture
200 static int dc1394properties[CV_CAP_PROP_MAX_DC1394];
201 CvCaptureCAM_DC1394_v2_CPP();
202 virtual ~CvCaptureCAM_DC1394_v2_CPP()
207 virtual bool open(int index);
208 virtual void close();
210 virtual double getProperty(int);
211 virtual bool setProperty(int, double);
212 virtual bool grabFrame();
213 virtual IplImage* retrieveFrame(int);
214 virtual int getCaptureDomain() { return CV_CAP_DC1394; } // Return the type of the capture object: CV_CAP_VFW, etc...
218 virtual bool startCapture();
219 virtual bool getVidereCalibrationInfo( char* buf, int bufSize );
220 virtual bool initVidereRectifyMaps( const char* info, IplImage* ml[2], IplImage* mr[2] );
223 dc1394camera_t* dcCam;
226 int frameWidth, frameHeight;
232 enum { VIDERE = 0x5505 };
236 dc1394bayer_method_t bayer;
237 dc1394color_filter_t bayerFilter;
241 dc1394video_frame_t* frameC;
246 IplImage *maps[NIMG][2];
247 dc1394featureset_t feature_set;
249 //mapping CV_CAP_PROP_ to DC1394_FEATUREs
250 int CvCaptureCAM_DC1394_v2_CPP::dc1394properties[CV_CAP_PROP_MAX_DC1394] = {
251 -1, //no corresponding feature for CV_CAP_PROP_POS_MSEC
253 DC1394_FEATURE_FRAME_RATE, //CV_CAP_PROP_FPS - fps can be set for format 7 only!
255 DC1394_FEATURE_BRIGHTNESS, //CV_CAP_PROP_BRIGHTNESS 10
257 DC1394_FEATURE_SATURATION, //CV_CAP_PROP_SATURATION
260 DC1394_FEATURE_SHUTTER, //CV_CAP_PROP_EXPOSURE
261 -1, //CV_CAP_PROP_CONVERT_RGB
262 DC1394_FEATURE_WHITE_BALANCE, //corresponds to CV_CAP_PROP_WHITE_BALANCE_BLUE_U and CV_CAP_PROP_WHITE_BALANCE_RED_V, see set function to check these props are set
264 DC1394_FEATURE_SHARPNESS, //20
265 DC1394_FEATURE_EXPOSURE, //CV_CAP_PROP_AUTO_EXPOSURE - this is auto exposure according to the IIDC standard
266 DC1394_FEATURE_GAMMA, //CV_CAP_PROP_GAMMA
267 DC1394_FEATURE_TEMPERATURE, //CV_CAP_PROP_TEMPERATURE
268 DC1394_FEATURE_TRIGGER, //CV_CAP_PROP_TRIGGER
269 DC1394_FEATURE_TRIGGER_DELAY, //CV_CAP_PROP_TRIGGER_DELAY
270 DC1394_FEATURE_WHITE_BALANCE, //CV_CAP_PROP_WHITE_BALANCE_RED_V
271 DC1394_FEATURE_ZOOM, //CV_CAP_PROP_ZOOM
272 DC1394_FEATURE_FOCUS, //CV_CAP_PROP_FOCUS
273 -1 //CV_CAP_PROP_GUID
275 CvCaptureCAM_DC1394_v2_CPP::CvCaptureCAM_DC1394_v2_CPP()
285 bayer = DC1394_BAYER_METHOD_BILINEAR;
286 bayerFilter = DC1394_COLOR_FILTER_GRBG;
290 for (int i = 0; i < NIMG; i++)
291 img[i] = maps[i][0] = maps[i][1] = 0;
299 bool CvCaptureCAM_DC1394_v2_CPP::startCapture()
307 code = dc1394_video_set_iso_speed(dcCam,
308 isoSpeed <= 100 ? DC1394_ISO_SPEED_100 :
309 isoSpeed <= 200 ? DC1394_ISO_SPEED_200 :
310 isoSpeed <= 400 ? DC1394_ISO_SPEED_400 :
311 isoSpeed <= 800 ? DC1394_ISO_SPEED_800 :
312 isoSpeed == 1600 ? DC1394_ISO_SPEED_1600 :
313 DC1394_ISO_SPEED_3200);
316 // should a specific mode be used
320 dc1394video_mode_t wantedMode;
321 dc1394video_modes_t videoModes;
322 dc1394_video_get_supported_modes(dcCam, &videoModes);
324 //set mode from number, for example the second supported mode, i.e userMode = 1
326 if (userMode < (int)videoModes.num)
328 wantedMode = videoModes.modes[userMode];
331 //set modes directly from DC134 constants (from dc1394video_mode_t)
332 else if ((userMode >= DC1394_VIDEO_MODE_MIN) && (userMode <= DC1394_VIDEO_MODE_MAX ))
334 //search for wanted mode, to check if camera supports it
336 while ((j< (int)videoModes.num) && videoModes.modes[j]!=userMode)
341 if ((int)videoModes.modes[j]==userMode)
343 wantedMode = videoModes.modes[j];
347 userMode = -1; // wanted mode not supported, search for best mode
352 userMode = -1; // wanted mode not supported, search for best mode
354 //if userMode is available: set it and update size
357 code = dc1394_video_set_mode(dcCam, wantedMode);
358 uint32_t width, height;
359 dc1394_get_image_size_from_video_mode(dcCam, wantedMode, &width, &height);
360 frameWidth = (int)width;
361 frameHeight = (int)height;
365 if (userMode == -1 && (frameWidth > 0 || frameHeight > 0))
367 dc1394video_mode_t bestMode = (dc1394video_mode_t) - 1;
368 dc1394video_modes_t videoModes;
369 dc1394_video_get_supported_modes(dcCam, &videoModes);
370 for (i = 0; i < (int)videoModes.num; i++)
372 dc1394video_mode_t mode = videoModes.modes[i];
373 if (mode >= DC1394_VIDEO_MODE_FORMAT7_MIN && mode <= DC1394_VIDEO_MODE_FORMAT7_MAX)
376 dc1394color_coding_t colorCoding;
377 dc1394_get_color_coding_from_video_mode(dcCam, mode, &colorCoding);
379 uint32_t width, height;
380 dc1394_get_image_size_from_video_mode(dcCam, mode, &width, &height);
381 if ((int)width == frameWidth || (int)height == frameHeight)
383 if (colorCoding == DC1394_COLOR_CODING_RGB8 ||
384 colorCoding == DC1394_COLOR_CODING_RAW8)
390 if (colorCoding == DC1394_COLOR_CODING_YUV411 ||
391 colorCoding == DC1394_COLOR_CODING_YUV422 ||
392 (colorCoding == DC1394_COLOR_CODING_YUV444 &&
400 if (colorCoding == DC1394_COLOR_CODING_MONO8)
407 if ((int)bestMode >= 0)
408 code = dc1394_video_set_mode(dcCam, bestMode);
413 dc1394video_mode_t mode;
414 dc1394framerates_t framerates;
415 double minDiff = DBL_MAX;
416 dc1394framerate_t bestFps = (dc1394framerate_t) - 1;
418 dc1394_video_get_mode(dcCam, &mode);
419 dc1394_video_get_supported_framerates(dcCam, mode, &framerates);
421 for (i = 0; i < (int)framerates.num; i++)
423 dc1394framerate_t ifps = framerates.framerates[i];
424 double fps1 = (1 << (ifps - DC1394_FRAMERATE_1_875)) * 1.875;
425 double diff = fabs(fps1 - fps);
432 if ((int)bestFps >= 0)
433 code = dc1394_video_set_framerate(dcCam, bestFps);
436 if (cameraId == VIDERE)
438 bayerFilter = DC1394_COLOR_FILTER_GBRG;
441 dc1394_get_control_register(dcCam, 0x50c, &value);
442 colorStereo = (value & 0x80000000) != 0;
445 code = dc1394_capture_setup(dcCam, nDMABufs, DC1394_CAPTURE_FLAGS_DEFAULT);
448 FD_SET(dc1394_capture_get_fileno(dcCam), &dc1394.camFds);
449 dc1394_video_set_transmission(dcCam, DC1394_ON);
450 if (cameraId == VIDERE)
452 enum { PROC_MODE_OFF, PROC_MODE_NONE, PROC_MODE_TEST, PROC_MODE_RECTIFIED, PROC_MODE_DISPARITY, PROC_MODE_DISPARITY_RAW };
453 int procMode = PROC_MODE_RECTIFIED;
455 uint32_t qval1 = 0x08000000 | (0x90 << 16) | ((procMode & 0x7) << 16);
456 uint32_t qval2 = 0x08000000 | (0x9C << 16);
457 dc1394_set_control_register(dcCam, 0xFF000, qval1);
458 dc1394_set_control_register(dcCam, 0xFF000, qval2);
466 bool CvCaptureCAM_DC1394_v2_CPP::open(int index)
469 dc1394camera_list_t* cameraList = 0;
477 err = dc1394_camera_enumerate(dc1394.dc, &cameraList);
478 if (err < 0 || !cameraList || (unsigned)index >= (unsigned)cameraList->num)
481 guid = cameraList->ids[index].guid;
482 dcCam = dc1394_camera_new(dc1394.dc, guid);
486 cameraId = dcCam->vendor_id;
488 if (dc1394_feature_get_all(dcCam,&feature_set) == DC1394_SUCCESS)
495 dc1394_camera_free_list(cameraList);
500 void CvCaptureCAM_DC1394_v2_CPP::close()
504 // check for fileno valid before using
505 int fileno=dc1394_capture_get_fileno(dcCam);
507 if (fileno>=0 && FD_ISSET(fileno, &dc1394.camFds))
508 FD_CLR(fileno, &dc1394.camFds);
509 dc1394_video_set_transmission(dcCam, DC1394_OFF);
510 dc1394_capture_stop(dcCam);
511 dc1394_camera_free(dcCam);
516 for (int i = 0; i < NIMG; i++)
518 cvReleaseImage(&img[i]);
519 cvReleaseImage(&maps[i][0]);
520 cvReleaseImage(&maps[i][1]);
532 bool CvCaptureCAM_DC1394_v2_CPP::grabFrame()
534 dc1394capture_policy_t policy = DC1394_CAPTURE_POLICY_WAIT;
535 bool code = false, isColor;
536 dc1394video_frame_t *dcFrame = 0, *fs = 0;
539 if (!dcCam || (!started && !startCapture()))
542 dc1394_capture_dequeue(dcCam, policy, &dcFrame);
547 if (/*dcFrame->frames_behind > 1 ||*/ dc1394_capture_is_frame_corrupt(dcCam, dcFrame) == DC1394_TRUE)
552 isColor = dcFrame->color_coding != DC1394_COLOR_CODING_MONO8 &&
553 dcFrame->color_coding != DC1394_COLOR_CODING_MONO16 &&
554 dcFrame->color_coding != DC1394_COLOR_CODING_MONO16S;
558 fs = (dc1394video_frame_t*)calloc(1, sizeof(*fs));
560 //dc1394_deinterlace_stereo_frames(dcFrame, fs, DC1394_STEREO_METHOD_INTERLACED);
561 dc1394_deinterlace_stereo_frames_fixed(dcFrame, fs, DC1394_STEREO_METHOD_INTERLACED);
563 dc1394_capture_enqueue(dcCam, dcFrame); // release the captured frame as soon as possible
567 isColor = colorStereo;
569 nch = isColor ? 3 : 1;
571 for (i = 0; i < nimages; i++)
574 dc1394video_frame_t f = fs ? *fs : *dcFrame, *fc = &f;
575 f.size[1] /= nimages;
576 f.image += f.size[0] * f.size[1] * i; // TODO: make it more universal
580 frameC = (dc1394video_frame_t*)calloc(1, sizeof(*frameC));
581 frameC->color_coding = nch == 3 ? DC1394_COLOR_CODING_RGB8 : DC1394_COLOR_CODING_MONO8;
584 dc1394_convert_frames(&f, frameC);
585 dc1394_capture_enqueue(dcCam, dcFrame);
590 f.color_filter = bayerFilter;
591 dc1394_debayer_frames(&f, frameC, bayer);
596 img[i] = cvCreateImage(cvSize(fc->size[0], fc->size[1]), 8, nch);
597 cvInitImageHeader(&fhdr, cvSize(fc->size[0], fc->size[1]), 8, nch);
598 cvSetData(&fhdr, fc->image, fc->size[0]*nch);
600 // Swap R&B channels:
602 cvConvertImage(&fhdr,&fhdr,CV_CVTIMG_SWAP_RB);
604 if( rectify && cameraId == VIDERE && nimages == 2 )
606 if( !maps[0][0] || maps[0][0]->width != img[i]->width || maps[0][0]->height != img[i]->height )
608 CvSize size = cvGetSize(img[i]);
609 cvReleaseImage(&maps[0][0]);
610 cvReleaseImage(&maps[0][1]);
611 cvReleaseImage(&maps[1][0]);
612 cvReleaseImage(&maps[1][1]);
613 maps[0][0] = cvCreateImage(size, IPL_DEPTH_16S, 2);
614 maps[0][1] = cvCreateImage(size, IPL_DEPTH_16S, 1);
615 maps[1][0] = cvCreateImage(size, IPL_DEPTH_16S, 2);
616 maps[1][1] = cvCreateImage(size, IPL_DEPTH_16S, 1);
618 if( getVidereCalibrationInfo( buf, (int)sizeof(buf) ) &&
619 initVidereRectifyMaps( buf, maps[0], maps[1] ))
624 cvRemap(&fhdr, img[i], maps[i][0], maps[i][1]);
627 cvCopy(&fhdr, img[i]);
634 dc1394_capture_enqueue(dcCam, dcFrame);
645 IplImage* CvCaptureCAM_DC1394_v2_CPP::retrieveFrame(int idx)
647 return 0 <= idx && idx < nimages ? img[idx] : 0;
650 double CvCaptureCAM_DC1394_v2_CPP::getProperty(int propId)
654 case CV_CAP_PROP_FRAME_WIDTH:
655 return frameWidth ? frameWidth : frameHeight*4 / 3;
656 case CV_CAP_PROP_FRAME_HEIGHT:
657 return frameHeight ? frameHeight : frameWidth*3 / 4;
658 case CV_CAP_PROP_FPS:
660 case CV_CAP_PROP_RECTIFICATION:
661 return rectify ? 1 : 0;
662 case CV_CAP_PROP_WHITE_BALANCE_BLUE_U:
663 if (dc1394_feature_whitebalance_get_value(dcCam,
664 &feature_set.feature[DC1394_FEATURE_WHITE_BALANCE-DC1394_FEATURE_MIN].BU_value,
665 &feature_set.feature[DC1394_FEATURE_WHITE_BALANCE-DC1394_FEATURE_MIN].RV_value) == DC1394_SUCCESS)
666 return feature_set.feature[DC1394_FEATURE_WHITE_BALANCE-DC1394_FEATURE_MIN].BU_value;
668 case CV_CAP_PROP_WHITE_BALANCE_RED_V:
669 if (dc1394_feature_whitebalance_get_value(dcCam,
670 &feature_set.feature[DC1394_FEATURE_WHITE_BALANCE-DC1394_FEATURE_MIN].BU_value,
671 &feature_set.feature[DC1394_FEATURE_WHITE_BALANCE-DC1394_FEATURE_MIN].RV_value) == DC1394_SUCCESS)
672 return feature_set.feature[DC1394_FEATURE_WHITE_BALANCE-DC1394_FEATURE_MIN].RV_value;
674 case CV_CAP_PROP_GUID:
675 //the least 32 bits are enough to identify the camera
676 return (double) (guid & 0x00000000FFFFFFFF);
678 case CV_CAP_PROP_MODE:
679 return (double) userMode;
681 case CV_CAP_PROP_ISO_SPEED:
682 return (double) isoSpeed;
684 if (propId<CV_CAP_PROP_MAX_DC1394 && dc1394properties[propId]!=-1
686 //&& feature_set.feature[dc1394properties[propId]-DC1394_FEATURE_MIN].on_off_capable)
687 if (dc1394_feature_get_value(dcCam,(dc1394feature_t)dc1394properties[propId],
688 &feature_set.feature[dc1394properties[propId]-DC1394_FEATURE_MIN].value) == DC1394_SUCCESS)
689 return feature_set.feature[dc1394properties[propId]-DC1394_FEATURE_MIN].value;
691 return -1; // the value of the feature can be 0, so returning 0 as an error is wrong
694 bool CvCaptureCAM_DC1394_v2_CPP::setProperty(int propId, double value)
698 case CV_CAP_PROP_FRAME_WIDTH:
701 frameWidth = cvRound(value);
704 case CV_CAP_PROP_FRAME_HEIGHT:
708 frameHeight = cvRound(value);
710 case CV_CAP_PROP_FPS:
715 case CV_CAP_PROP_RECTIFICATION:
716 if( cameraId != VIDERE )
718 rectify = fabs(value) > FLT_EPSILON;
720 case CV_CAP_PROP_MODE:
723 userMode = cvRound(value);
725 case CV_CAP_PROP_ISO_SPEED:
728 isoSpeed = cvRound(value);
730 //The code below is based on coriander, callbacks.c:795, refer to case RANGE_MENU_MAN :
732 if (propId<CV_CAP_PROP_MAX_DC1394 && dc1394properties[propId]!=-1
735 //get the corresponding feature from property-id
736 dc1394feature_info_t *act_feature = &feature_set.feature[dc1394properties[propId]-DC1394_FEATURE_MIN];
738 if (cvRound(value) == CV_CAP_PROP_DC1394_OFF)
740 if ( (act_feature->on_off_capable)
741 && (dc1394_feature_set_power(dcCam, act_feature->id, DC1394_OFF) == DC1394_SUCCESS))
743 act_feature->is_on=DC1394_OFF;
748 //try to turn the feature ON, feature can be ON and at the same time it can be not capable to change state to OFF
749 if ( (act_feature->is_on == DC1394_OFF) && (act_feature->on_off_capable == DC1394_TRUE))
751 if (dc1394_feature_set_power(dcCam, act_feature->id, DC1394_ON) == DC1394_SUCCESS)
752 feature_set.feature[dc1394properties[propId]-DC1394_FEATURE_MIN].is_on=DC1394_ON;
754 //turn off absolute mode - the actual value will be stored in the value field,
755 //otherwise it would be stored into CSR (control and status register) absolute value
756 if (act_feature->absolute_capable
757 && dc1394_feature_set_absolute_control(dcCam, act_feature->id, DC1394_OFF) !=DC1394_SUCCESS)
760 act_feature->abs_control=DC1394_OFF;
762 if (cvRound(value) == CV_CAP_PROP_DC1394_MODE_AUTO)
764 if (dc1394_feature_set_mode(dcCam, act_feature->id, DC1394_FEATURE_MODE_AUTO)!=DC1394_SUCCESS)
766 act_feature->current_mode=DC1394_FEATURE_MODE_AUTO;
770 if (cvRound(value) == CV_CAP_PROP_DC1394_MODE_ONE_PUSH_AUTO)
772 //have to set to manual first, otherwise one push will be ignored (AVT manual 4.3.0 p. 115)
773 if (dc1394_feature_set_mode(dcCam, act_feature->id, DC1394_FEATURE_MODE_ONE_PUSH_AUTO)!=DC1394_SUCCESS)
776 act_feature->current_mode=DC1394_FEATURE_MODE_ONE_PUSH_AUTO;
779 //set the feature to MANUAL mode,
780 if (dc1394_feature_set_mode(dcCam, act_feature->id, DC1394_FEATURE_MODE_MANUAL)!=DC1394_SUCCESS)
783 act_feature->current_mode=DC1394_FEATURE_MODE_MANUAL;
784 // if property is one of the white balance features treat it in different way
785 if (propId == CV_CAP_PROP_WHITE_BALANCE_BLUE_U)
787 if (dc1394_feature_whitebalance_set_value(dcCam,cvRound(value), act_feature->RV_value)!=DC1394_SUCCESS)
791 act_feature->BU_value = cvRound(value);
795 if (propId == CV_CAP_PROP_WHITE_BALANCE_RED_V)
797 if (dc1394_feature_whitebalance_set_value(dcCam, act_feature->BU_value, cvRound(value))!=DC1394_SUCCESS)
801 act_feature->RV_value = cvRound(value);
806 //first: check boundaries
807 if (value < act_feature->min)
809 value = act_feature->min;
811 else if (value > act_feature->max)
813 value = act_feature->max;
816 if (dc1394_feature_set_value(dcCam, act_feature->id, cvRound(value)) == DC1394_SUCCESS)
818 act_feature->value = value;
828 bool CvCaptureCAM_DC1394_v2_CPP::getVidereCalibrationInfo( char* buf, int bufSize )
832 for( pos = 0; pos < bufSize - 4; pos += 4 )
834 uint32_t quad = getControlRegister(dcCam, 0xF0800 + pos);
835 if( quad == 0 || quad == 0xffffffff )
837 buf[pos] = (uchar)(quad >> 24);
838 buf[pos+1] = (uchar)(quad >> 16);
839 buf[pos+2] = (uchar)(quad >> 8);
840 buf[pos+3] = (uchar)(quad);
851 bool CvCaptureCAM_DC1394_v2_CPP::initVidereRectifyMaps( const char* info,
852 IplImage* ml[2], IplImage* mr[2] )
854 float identity_data[] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
855 CvMat l_rect = cvMat(3, 3, CV_32F, identity_data), r_rect = l_rect;
856 float l_intrinsic_data[] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
857 float r_intrinsic_data[] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
858 CvMat l_intrinsic = cvMat(3, 3, CV_32F, l_intrinsic_data);
859 CvMat r_intrinsic = cvMat(3, 3, CV_32F, r_intrinsic_data);
860 float l_distortion_data[] = {0,0,0,0,0}, r_distortion_data[] = {0,0,0,0,0};
861 CvMat l_distortion = cvMat(1, 5, CV_32F, l_distortion_data);
862 CvMat r_distortion = cvMat(1, 5, CV_32F, r_distortion_data);
863 IplImage* mx = cvCreateImage(cvGetSize(ml[0]), IPL_DEPTH_32F, 1);
864 IplImage* my = cvCreateImage(cvGetSize(ml[0]), IPL_DEPTH_32F, 1);
867 for( k = 0; k < 2; k++ )
869 const char* section_name = k == 0 ? "[left_camera]" : "[right_camera]";
870 static const char* param_names[] = { "f ", "fy", "Cx", "Cy" "kappa1", "kappa2", "tau1", "tau2", "kappa3", 0 };
871 const char* section_start = strstr( info, section_name );
872 CvMat* intrinsic = k == 0 ? &l_intrinsic : &r_intrinsic;
873 CvMat* distortion = k == 0 ? &l_distortion : &r_distortion;
874 CvMat* rectification = k == 0 ? &l_rect : &r_rect;
875 IplImage** dst = k == 0 ? ml : mr;
878 section_start += strlen(section_name);
879 for( j = 0; param_names[j] != 0; j++ )
881 const char* param_value_start = strstr(section_start, param_names[j]);
883 if(!param_value_start)
885 sscanf(param_value_start + strlen(param_names[j]), "%f", &val);
887 intrinsic->data.fl[j == 0 ? 0 : j == 1 ? 4 : j == 2 ? 2 : 5] = val;
889 distortion->data.fl[j - 4] = val;
891 if( param_names[j] != 0 )
894 // some sanity check for the principal point
895 if( fabs(mx->width*0.5 - intrinsic->data.fl[2]) > mx->width*0.1 ||
896 fabs(my->height*0.5 - intrinsic->data.fl[5]) > my->height*0.1 )
898 cvScale( &intrinsic, &intrinsic, 0.5 ); // try the corrected intrinsic matrix for 2x lower resolution
899 if( fabs(mx->width*0.5 - intrinsic->data.fl[2]) > mx->width*0.05 ||
900 fabs(my->height*0.5 - intrinsic->data.fl[5]) > my->height*0.05 )
901 cvScale( &intrinsic, &intrinsic, 2 ); // revert it back if the new variant is not much better
902 intrinsic->data.fl[8] = 1;
905 cvInitUndistortRectifyMap( intrinsic, distortion,
906 rectification, intrinsic, mx, my );
907 cvConvertMaps( mx, my, dst[0], dst[1] );
910 cvReleaseImage( &mx );
911 cvReleaseImage( &my );
916 CvCapture* cvCreateCameraCapture_DC1394_2(int index)
918 CvCaptureCAM_DC1394_v2_CPP* capture = new CvCaptureCAM_DC1394_v2_CPP;
920 if (capture->open(index))