Update to 2.0.0 tree from current Fremantle build
[opencv] / src / highgui / cvcap_dc1394_v2.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include "_highgui.h"
43
44 #ifdef HAVE_DC1394_2
45
46 #include <unistd.h>
47 #include <stdint.h>
48 #include <sys/select.h>
49 #include <dc1394/dc1394.h>
50 #include <cv.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 static dc1394error_t adaptBufferStereoLocal(dc1394video_frame_t *in, dc1394video_frame_t *out)
55 {
56     uint32_t bpp;
57
58     // buffer position is not changed. Size is boubled in Y
59     out->size[0] = in->size[0];
60     out->size[1] = in->size[1] * 2;
61     out->position[0] = in->position[0];
62     out->position[1] = in->position[1];
63
64     // color coding is set to mono8 or raw8.
65     switch (in->color_coding)
66     {
67     case DC1394_COLOR_CODING_RAW16:
68         out->color_coding = DC1394_COLOR_CODING_RAW8;
69         break;
70     case DC1394_COLOR_CODING_MONO16:
71     case DC1394_COLOR_CODING_YUV422:
72         out->color_coding = DC1394_COLOR_CODING_MONO8;
73         break;
74     default:
75         return DC1394_INVALID_COLOR_CODING;
76     }
77
78     // keep the color filter value in all cases. if the format is not raw it will not be further used anyway
79     out->color_filter = in->color_filter;
80
81     // the output YUV byte order must be already set if the buffer is YUV422 at the output
82     // if the output is not YUV we don't care about this field.
83     // Hence nothing to do.
84     // we always convert to 8bits (at this point) we can safely set this value to 8.
85     out->data_depth = 8;
86
87     // don't know what to do with stride... >>>> TODO: STRIDE SHOULD BE TAKEN INTO ACCOUNT... <<<<
88     // out->stride=??
89     // the video mode should not change. Color coding and other stuff can be accessed in specific fields of this struct
90     out->video_mode = in->video_mode;
91
92     // padding is kept:
93     out->padding_bytes = in->padding_bytes;
94
95     // image bytes changes:    >>>> TODO: STRIDE SHOULD BE TAKEN INTO ACCOUNT... <<<<
96     dc1394_get_color_coding_bit_size(out->color_coding, &bpp);
97     out->image_bytes = (out->size[0] * out->size[1] * bpp) / 8;
98
99     // total is image_bytes + padding_bytes
100     out->total_bytes = out->image_bytes + out->padding_bytes;
101
102     // bytes-per-packet and packets_per_frame are internal data that can be kept as is.
103     out->packet_size  = in->packet_size;
104     out->packets_per_frame = in->packets_per_frame;
105
106     // timestamp, frame_behind, id and camera are copied too:
107     out->timestamp = in->timestamp;
108     out->frames_behind = in->frames_behind;
109     out->camera = in->camera;
110     out->id = in->id;
111
112     // verify memory allocation:
113     if (out->total_bytes > out->allocated_image_bytes)
114     {
115         free(out->image);
116         out->image = (uint8_t*)malloc(out->total_bytes * sizeof(uint8_t));
117         out->allocated_image_bytes = out->total_bytes;
118     }
119
120     // Copy padding bytes:
121     memcpy(&(out->image[out->image_bytes]), &(in->image[in->image_bytes]), out->padding_bytes);
122     out->little_endian = DC1394_FALSE; // not used before 1.32 is out.
123     out->data_in_padding = DC1394_FALSE; // not used before 1.32 is out.
124     return DC1394_SUCCESS;
125 }
126
127 static dc1394error_t dc1394_deinterlace_stereo_frames_fixed(dc1394video_frame_t *in,
128     dc1394video_frame_t *out, dc1394stereo_method_t method)
129 {
130     dc1394error_t err;
131
132     if((in->color_coding == DC1394_COLOR_CODING_RAW16) ||
133        (in->color_coding == DC1394_COLOR_CODING_MONO16) ||
134        (in->color_coding == DC1394_COLOR_CODING_YUV422))
135     {
136         switch (method)
137         {
138
139         case DC1394_STEREO_METHOD_INTERLACED:
140             err = adaptBufferStereoLocal(in, out);
141 //FIXED by AB:
142 //          dc1394_deinterlace_stereo(in->image, out->image, in->size[0], in->size[1]);
143             dc1394_deinterlace_stereo(in->image, out->image, out->size[0], out->size[1]);
144             break;
145
146         case DC1394_STEREO_METHOD_FIELD:
147             err = adaptBufferStereoLocal(in, out);
148             memcpy(out->image, in->image, out->image_bytes);
149             break;
150         }
151
152         return DC1394_INVALID_STEREO_METHOD;
153     }
154     else
155         return DC1394_FUNCTION_NOT_SUPPORTED;
156 }
157
158 static uint32_t getControlRegister(dc1394camera_t *camera, uint64_t offset)
159 {
160     uint32_t value = 0;
161     dc1394error_t err = dc1394_get_control_register(camera, offset, &value);
162
163     assert(err == DC1394_SUCCESS);
164     return err == DC1394_SUCCESS ? value : 0xffffffff;
165 }
166
167 struct CvDC1394
168 {
169     CvDC1394();
170     ~CvDC1394();
171
172     dc1394_t* dc;
173     fd_set camFds;
174 };
175
176 CvDC1394::CvDC1394()
177 {
178     dc = dc1394_new();
179     FD_ZERO(&camFds);
180 }
181
182 CvDC1394::~CvDC1394()
183 {
184     if (dc)
185         dc1394_free(dc);
186     dc = 0;
187 }
188
189 static CvDC1394 dc1394;
190
191 class CvCaptureCAM_DC1394_v2_CPP : public CvCapture
192 {
193 public:
194     CvCaptureCAM_DC1394_v2_CPP();
195     virtual ~CvCaptureCAM_DC1394_v2_CPP()
196     {
197         close();
198     }
199
200     virtual bool open(int index);
201     virtual void close();
202
203     virtual double getProperty(int);
204     virtual bool setProperty(int, double);
205     virtual bool grabFrame();
206     virtual IplImage* retrieveFrame(int);
207         virtual int getCaptureDomain() { return CV_CAP_DC1394; } // Return the type of the capture object: CV_CAP_VFW, etc...
208         
209
210 protected:
211     virtual bool startCapture();
212     virtual bool getVidereCalibrationInfo( char* buf, int bufSize );
213     virtual bool initVidereRectifyMaps( const char* info, IplImage* ml[2], IplImage* mr[2] );
214
215     uint64_t guid;
216     dc1394camera_t* dcCam;
217     int isoSpeed;
218     int videoMode;
219     int frameWidth, frameHeight;
220     double fps;
221     int nDMABufs;
222     bool started;
223
224     enum { VIDERE = 0x5505 };
225
226     int cameraId;
227     bool colorStereo;
228     dc1394bayer_method_t bayer;
229     dc1394color_filter_t bayerFilter;
230
231     enum { NIMG = 2 };
232     IplImage *img[NIMG];
233     dc1394video_frame_t* frameC;
234     int nimages;
235
236     bool rectify;
237     bool init_rectify;
238     IplImage *maps[NIMG][2];
239 };
240
241 CvCaptureCAM_DC1394_v2_CPP::CvCaptureCAM_DC1394_v2_CPP()
242 {
243     guid = 0;
244     dcCam = 0;
245     isoSpeed = 400;
246     fps = 15;
247     nDMABufs = 8;
248     started = false;
249     cameraId = 0;
250     colorStereo = false;
251     bayer = DC1394_BAYER_METHOD_BILINEAR;
252     bayerFilter = DC1394_COLOR_FILTER_GRBG;
253     frameWidth = 640;
254     frameHeight = 480;
255
256     for (int i = 0; i < NIMG; i++)
257         img[i] = maps[i][0] = maps[i][1] = 0;
258     frameC = 0;
259     nimages = 1;
260     rectify = false;
261 }
262
263
264 bool CvCaptureCAM_DC1394_v2_CPP::startCapture()
265 {
266     int i;
267     int code = 0;
268     if (!dcCam)
269         return false;
270     if (isoSpeed > 0)
271     {
272         code = dc1394_video_set_iso_speed(dcCam,
273                                           isoSpeed <= 100 ? DC1394_ISO_SPEED_100 :
274                                           isoSpeed <= 200 ? DC1394_ISO_SPEED_200 :
275                                           isoSpeed <= 400 ? DC1394_ISO_SPEED_400 :
276                                           isoSpeed <= 800 ? DC1394_ISO_SPEED_800 :
277                                           isoSpeed == 1600 ? DC1394_ISO_SPEED_1600 :
278                                           DC1394_ISO_SPEED_3200);
279     }
280
281     if (frameWidth > 0 || frameHeight > 0)
282     {
283         dc1394video_mode_t bestMode = (dc1394video_mode_t) - 1;
284         dc1394video_modes_t videoModes;
285         dc1394_video_get_supported_modes(dcCam, &videoModes);
286         for (i = 0; i < (int)videoModes.num; i++)
287         {
288             dc1394video_mode_t mode = videoModes.modes[i];
289             int pref = -1;
290             dc1394color_coding_t colorCoding;
291             dc1394_get_color_coding_from_video_mode(dcCam, mode, &colorCoding);
292
293             uint32_t width, height;
294             dc1394_get_image_size_from_video_mode(dcCam, mode, &width, &height);
295             if ((int)width == frameWidth || (int)height == frameHeight)
296             {
297                 if (colorCoding == DC1394_COLOR_CODING_RGB8 ||
298                         colorCoding == DC1394_COLOR_CODING_RGB16 ||
299                         colorCoding == DC1394_COLOR_CODING_RAW8 ||
300                         colorCoding == DC1394_COLOR_CODING_RAW16)
301                 {
302                     bestMode = mode;
303                     break;
304                 }
305
306                 if (colorCoding == DC1394_COLOR_CODING_YUV411 ||
307                         colorCoding == DC1394_COLOR_CODING_YUV422 ||
308                         (colorCoding == DC1394_COLOR_CODING_YUV444 &&
309                         pref < 1))
310                 {
311                     bestMode = mode;
312                     pref = 1;
313                 }
314
315                 if (colorCoding == DC1394_COLOR_CODING_MONO8 ||
316                         (colorCoding == DC1394_COLOR_CODING_MONO16 &&
317                         pref < 0))
318                 {
319                     bestMode = mode;
320                     pref = 0;
321                 }
322             }
323         }
324         if ((int)bestMode >= 0)
325             code = dc1394_video_set_mode(dcCam, bestMode);
326     }
327
328     if (fps > 0)
329     {
330         dc1394video_mode_t mode;
331         dc1394framerates_t framerates;
332         double minDiff = DBL_MAX;
333         dc1394framerate_t bestFps = (dc1394framerate_t) - 1;
334
335         dc1394_video_get_mode(dcCam, &mode);
336         dc1394_video_get_supported_framerates(dcCam, mode, &framerates);
337
338         for (i = 0; i < (int)framerates.num; i++)
339         {
340             dc1394framerate_t ifps = framerates.framerates[i];
341             double fps1 = (1 << (ifps - DC1394_FRAMERATE_1_875)) * 1.875;
342             double diff = fabs(fps1 - fps);
343             if (diff < minDiff)
344             {
345                 minDiff = diff;
346                 bestFps = ifps;
347             }
348         }
349         if ((int)bestFps >= 0)
350             code = dc1394_video_set_framerate(dcCam, bestFps);
351     }
352
353     if (cameraId == VIDERE)
354     {
355         bayerFilter = DC1394_COLOR_FILTER_GBRG;
356         nimages = 2;
357         uint32_t value = 0;
358         dc1394_get_control_register(dcCam, 0x50c, &value);
359         colorStereo = (value & 0x80000000) != 0;
360     }
361
362     code = dc1394_capture_setup(dcCam, nDMABufs, DC1394_CAPTURE_FLAGS_DEFAULT);
363     if (code >= 0)
364     {
365         FD_SET(dc1394_capture_get_fileno(dcCam), &dc1394.camFds);
366         dc1394_video_set_transmission(dcCam, DC1394_ON);
367         if (cameraId == VIDERE)
368         {
369             enum { PROC_MODE_OFF, PROC_MODE_NONE, PROC_MODE_TEST, PROC_MODE_RECTIFIED, PROC_MODE_DISPARITY, PROC_MODE_DISPARITY_RAW };
370             int procMode = PROC_MODE_RECTIFIED;
371             usleep(100000);
372             uint32_t qval1 = 0x08000000 | (0x90 << 16) | ((procMode & 0x7) << 16);
373             uint32_t qval2 = 0x08000000 | (0x9C << 16);
374             dc1394_set_control_register(dcCam, 0xFF000, qval1);
375             dc1394_set_control_register(dcCam, 0xFF000, qval2);
376         }
377         started = true;
378     }
379
380     return code >= 0;
381 }
382
383 bool CvCaptureCAM_DC1394_v2_CPP::open(int index)
384 {
385     bool result = false;
386     dc1394camera_list_t* cameraList = 0;
387     dc1394error_t err;
388
389     close();
390
391     if (!dc1394.dc)
392         goto _exit_;
393
394     err = dc1394_camera_enumerate(dc1394.dc, &cameraList);
395     if (err < 0 || !cameraList || (unsigned)index >= (unsigned)cameraList->num)
396         goto _exit_;
397
398     guid = cameraList->ids[index].guid;
399     dcCam = dc1394_camera_new(dc1394.dc, guid);
400     if (!dcCam)
401         goto _exit_;
402
403     cameraId = dcCam->vendor_id;
404     result = true;
405
406 _exit_:
407     if (cameraList)
408         dc1394_camera_free_list(cameraList);
409
410     return result;
411 }
412
413 void CvCaptureCAM_DC1394_v2_CPP::close()
414 {
415     if (dcCam)
416     {
417         if (FD_ISSET(dc1394_capture_get_fileno(dcCam), &dc1394.camFds))
418             FD_CLR(dc1394_capture_get_fileno(dcCam), &dc1394.camFds);
419         dc1394_video_set_transmission(dcCam, DC1394_OFF);
420         dc1394_capture_stop(dcCam);
421         dc1394_camera_free(dcCam);
422         dcCam = 0;
423         started = false;
424     }
425
426     for (int i = 0; i < NIMG; i++)
427     {
428         cvReleaseImage(&img[i]);
429         cvReleaseImage(&maps[i][0]);
430         cvReleaseImage(&maps[i][1]);
431     }
432     if (frameC)
433     {
434         if (frameC->image)
435             free(frameC->image);
436         free(frameC);
437         frameC = 0;
438     }
439 }
440
441
442 bool CvCaptureCAM_DC1394_v2_CPP::grabFrame()
443 {
444     dc1394capture_policy_t policy = DC1394_CAPTURE_POLICY_WAIT;
445     bool code = false, isColor;
446     dc1394video_frame_t *dcFrame = 0, *fs = 0;
447     int i, nch;
448
449     if (!dcCam || (!started && !startCapture()))
450         return false;
451
452     dc1394_capture_dequeue(dcCam, policy, &dcFrame);
453
454     if (!dcFrame)
455         return false;
456
457     if (/*dcFrame->frames_behind > 1 ||*/ dc1394_capture_is_frame_corrupt(dcCam, dcFrame) == DC1394_TRUE)
458     {
459         goto _exit_;
460     }
461
462     isColor = dcFrame->color_coding != DC1394_COLOR_CODING_MONO8 &&
463               dcFrame->color_coding != DC1394_COLOR_CODING_MONO16 &&
464               dcFrame->color_coding != DC1394_COLOR_CODING_MONO16S;
465
466     if (nimages == 2)
467     {
468         fs = (dc1394video_frame_t*)calloc(1, sizeof(*fs));
469
470         //dc1394_deinterlace_stereo_frames(dcFrame, fs, DC1394_STEREO_METHOD_INTERLACED);
471         dc1394_deinterlace_stereo_frames_fixed(dcFrame, fs, DC1394_STEREO_METHOD_INTERLACED);
472
473         dc1394_capture_enqueue(dcCam, dcFrame); // release the captured frame as soon as possible
474         dcFrame = 0;
475         if (!fs->image)
476             goto _exit_;
477         isColor = colorStereo;
478     }
479     nch = isColor ? 3 : 1;
480
481     for (i = 0; i < nimages; i++)
482     {
483         IplImage fhdr;
484         dc1394video_frame_t f = fs ? *fs : *dcFrame, *fc = &f;
485         f.size[1] /= nimages;
486         f.image += f.size[0] * f.size[1] * i; // TODO: make it more universal
487         if (isColor)
488         {
489             if (!frameC)
490                 frameC = (dc1394video_frame_t*)calloc(1, sizeof(*frameC));
491             frameC->color_coding = nch == 3 ? DC1394_COLOR_CODING_RGB8 : DC1394_COLOR_CODING_MONO8;
492             if (nimages == 1)
493             {
494                 dc1394_convert_frames(&f, frameC);
495                 dc1394_capture_enqueue(dcCam, dcFrame);
496                 dcFrame = 0;
497             }
498             else
499             {
500                 f.color_filter = bayerFilter;
501                 dc1394_debayer_frames(&f, frameC, bayer);
502             }
503             fc = frameC;
504         }
505         if (!img[i])
506             img[i] = cvCreateImage(cvSize(fc->size[0], fc->size[1]), 8, nch);
507         cvInitImageHeader(&fhdr, cvSize(fc->size[0], fc->size[1]), 8, nch);
508         cvSetData(&fhdr, fc->image, fc->size[0]*nch);
509
510         // Swap R&B channels:
511         if (nch==3)
512                 cvConvertImage(&fhdr,&fhdr,CV_CVTIMG_SWAP_RB);
513
514         if( rectify && cameraId == VIDERE && nimages == 2 )
515         {
516             if( !maps[0][0] || maps[0][0]->width != img[i]->width || maps[0][0]->height != img[i]->height )
517             {
518                 CvSize size = cvGetSize(img[i]);
519                 cvReleaseImage(&maps[0][0]);
520                 cvReleaseImage(&maps[0][1]);
521                 cvReleaseImage(&maps[1][0]);
522                 cvReleaseImage(&maps[1][1]);
523                 maps[0][0] = cvCreateImage(size, IPL_DEPTH_16S, 2);
524                 maps[0][1] = cvCreateImage(size, IPL_DEPTH_16S, 1);
525                 maps[1][0] = cvCreateImage(size, IPL_DEPTH_16S, 2);
526                 maps[1][1] = cvCreateImage(size, IPL_DEPTH_16S, 1);
527                 char buf[4*4096];
528                 if( getVidereCalibrationInfo( buf, (int)sizeof(buf) ) &&
529                     initVidereRectifyMaps( buf, maps[0], maps[1] ))
530                     ;
531                 else
532                     rectify = false;
533             }
534             cvRemap(&fhdr, img[i], maps[i][0], maps[i][1]);
535         }
536         else
537             cvCopy(&fhdr, img[i]);
538     }
539
540     code = true;
541
542 _exit_:
543     if (dcFrame)
544         dc1394_capture_enqueue(dcCam, dcFrame);
545     if (fs)
546     {
547         if (fs->image)
548             free(fs->image);
549         free(fs);
550     }
551
552     return code;
553 }
554
555 IplImage* CvCaptureCAM_DC1394_v2_CPP::retrieveFrame(int idx)
556 {
557     return 0 <= idx && idx < nimages ? img[idx] : 0;
558 }
559
560 double CvCaptureCAM_DC1394_v2_CPP::getProperty(int propId)
561 {
562     switch (propId)
563     {
564     case CV_CAP_PROP_FRAME_WIDTH:
565         return frameWidth ? frameWidth : frameHeight*4 / 3;
566     case CV_CAP_PROP_FRAME_HEIGHT:
567         return frameHeight ? frameHeight : frameWidth*3 / 4;
568     case CV_CAP_PROP_FPS:
569         return fps;
570     case CV_CAP_PROP_RECTIFICATION:
571         return rectify ? 1 : 0;
572 //    case CV_CAP_PROP_BRIGHTNESS :
573 //    case CV_CAP_PROP_CONTRAST :
574 //    case CV_CAP_PROP_WHITE_BALANCE :
575     default:
576         ;
577     }
578     return 0;
579 }
580
581 bool CvCaptureCAM_DC1394_v2_CPP::setProperty(int propId, double value)
582 {
583     switch (propId)
584     {
585     case CV_CAP_PROP_FRAME_WIDTH:
586         if(started)
587                         return false;
588         frameWidth = cvRound(value);
589         frameHeight = 0;
590         break;
591     case CV_CAP_PROP_FRAME_HEIGHT:
592         if(started)
593                         return false;
594         frameWidth = 0;
595         frameHeight = cvRound(value);
596         break;
597     case CV_CAP_PROP_FPS:
598         if(started)
599                         return false;
600         fps = value;
601         break;
602     case CV_CAP_PROP_RECTIFICATION:
603         if( cameraId != VIDERE )
604             return false;
605         rectify = fabs(value) > FLT_EPSILON;
606         break;
607     default:
608         return false;
609     }
610     return true;
611 }
612
613
614 bool CvCaptureCAM_DC1394_v2_CPP::getVidereCalibrationInfo( char* buf, int bufSize )
615 {
616     int pos;
617
618     for( pos = 0; pos < bufSize - 4; pos += 4 )
619     {
620         uint32_t quad = getControlRegister(dcCam, 0xF0800 + pos);
621         if( quad == 0 || quad == 0xffffffff )
622             break;
623         buf[pos] = (uchar)(quad >> 24);
624         buf[pos+1] = (uchar)(quad >> 16);
625         buf[pos+2] = (uchar)(quad >> 8);
626         buf[pos+3] = (uchar)(quad);
627     }
628
629     if( pos == 0 )
630         return false;
631
632     buf[pos] = '\0';
633     return true;
634 }
635
636
637 bool CvCaptureCAM_DC1394_v2_CPP::initVidereRectifyMaps( const char* info,
638     IplImage* ml[2], IplImage* mr[2] )
639 {
640     float identity_data[] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
641     CvMat l_rect = cvMat(3, 3, CV_32F, identity_data), r_rect = l_rect;
642     float l_intrinsic_data[] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
643     float r_intrinsic_data[] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
644     CvMat l_intrinsic = cvMat(3, 3, CV_32F, l_intrinsic_data);
645     CvMat r_intrinsic = cvMat(3, 3, CV_32F, r_intrinsic_data);
646     float l_distortion_data[] = {0,0,0,0,0}, r_distortion_data[] = {0,0,0,0,0};
647     CvMat l_distortion = cvMat(1, 5, CV_32F, l_distortion_data);
648     CvMat r_distortion = cvMat(1, 5, CV_32F, r_distortion_data);
649     IplImage* mx = cvCreateImage(cvGetSize(ml[0]), IPL_DEPTH_32F, 1);
650     IplImage* my = cvCreateImage(cvGetSize(ml[0]), IPL_DEPTH_32F, 1);
651     int k, j;
652
653     for( k = 0; k < 2; k++ )
654     {
655         const char* section_name = k == 0 ? "[left_camera]" : "[right_camera]";
656         static const char* param_names[] = { "f ", "fy", "Cx", "Cy" "kappa1", "kappa2", "tau1", "tau2", "kappa3", 0 };
657         const char* section_start = strstr( info, section_name );
658         CvMat* intrinsic = k == 0 ? &l_intrinsic : &r_intrinsic;
659         CvMat* distortion = k == 0 ? &l_distortion : &r_distortion;
660         CvMat* rectification = k == 0 ? &l_rect : &r_rect;
661         IplImage** dst = k == 0 ? ml : mr;
662         if( !section_start )
663             break;
664         section_start += strlen(section_name);
665         for( j = 0; param_names[j] != 0; j++ )
666         {
667             const char* param_value_start = strstr(section_start, param_names[j]);
668             float val=0;
669             if(!param_value_start)
670                 break;
671             sscanf(param_value_start + strlen(param_names[j]), "%f", &val);
672             if( j < 4 )
673                 intrinsic->data.fl[j == 0 ? 0 : j == 1 ? 4 : j == 2 ? 2 : 5] = val;
674             else
675                 distortion->data.fl[j - 4] = val;
676         }
677         if( param_names[j] != 0 )
678             break;
679
680         // some sanity check for the principal point
681         if( fabs(mx->width*0.5 - intrinsic->data.fl[2]) > mx->width*0.1 ||
682             fabs(my->height*0.5 - intrinsic->data.fl[5]) > my->height*0.1 )
683         {
684             cvScale( &intrinsic, &intrinsic, 0.5 ); // try the corrected intrinsic matrix for 2x lower resolution
685             if( fabs(mx->width*0.5 - intrinsic->data.fl[2]) > mx->width*0.05 ||
686                 fabs(my->height*0.5 - intrinsic->data.fl[5]) > my->height*0.05 )
687                 cvScale( &intrinsic, &intrinsic, 2 ); // revert it back if the new variant is not much better
688             intrinsic->data.fl[8] = 1;
689         }
690
691         cvInitUndistortRectifyMap( intrinsic, distortion,
692                     rectification, intrinsic, mx, my );
693         cvConvertMaps( mx, my, dst[0], dst[1] );
694     }
695
696     cvReleaseImage( &mx );
697     cvReleaseImage( &my );
698     return k >= 2;
699 }
700
701
702 CvCapture* cvCreateCameraCapture_DC1394_2(int index)
703 {
704     CvCaptureCAM_DC1394_v2_CPP* capture = new CvCaptureCAM_DC1394_v2_CPP;
705
706     if (capture->open(index))
707         return capture;
708
709     delete capture;
710     return 0;
711 }
712
713 #endif