Linux1394Capture2.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: Linux1394Capture2.cpp
37 // Author: Kai Welke, Pedram Azad
38 // Date: 2007
39 // ****************************************************************************
40 // Requires: * libdc1394v2
41 // * libraw1394 >= 1.2.0
42 // ****************************************************************************
43 // Changes: 20.12.2007, Kai Welke
44 // * Built CLinux1394Capture2 on base of CLinux1394Capture
45 // * switched from libdc1394v1 to libdc1394v2 (solved problem
46 // with QT and allowed to use full firewire bandwidth)
47 // * added check for maximum bandwidth
48 // * generic camera feature access
49 // * Format7_0 implemented with ROI and framerate settings
50 // format7 support is camera dependant (dragonfly only
51 // supports two discrete framerates, dragonfly2 can be
52 // adjusted from 0.x ... 60.0 frames.
53 // 05.05.2011, Kai Welke
54 // * Corrected bus reset. Bus for all cameras reset on
55 // creation of first instance. The previous bus reset broke
56 // the support of multiple instances.
57 // ****************************************************************************
58 
59 
60 // ****************************************************************************
61 // Includes
62 // ****************************************************************************
63 
65 #include "Image/ByteImage.h"
66 #include "Image/ImageProcessor.h"
67 #include "Threading/Threading.h"
68 
69 #include <stdio.h>
70 #include <unistd.h>
71 #include <stdlib.h>
72 #include <stdarg.h>
73 #include <algorithm>
74 
75 
76 // ****************************************************************************
77 // Defines
78 // ****************************************************************************
79 
80 #define DROP_FRAMES 1
81 #define NUM_BUFFERS 2
82 
83 
84 // *****************************************************************
85 // Static members
86 // *****************************************************************
87 
95 
96 
97 
98 // *****************************************************************
99 // Constructors
100 // *****************************************************************
101 
102 CLinux1394Capture2::CLinux1394Capture2(int nCameras, VideoMode mode, ColorMode colorMode, ImageProcessor::BayerPatternType bayerPatternType, FrameRate frameRate) : m_mode(mode), m_colorMode(colorMode), m_bayerPatternType(bayerPatternType), m_frameRate(frameRate)
103 {
104  // number of cameras
105  m_nCameras = nCameras;
106 
107  // init images
109 
110  // init size
111  width = -1;
112  height = -1;
113 
114  // init features
115  m_bUseUIDs = false;
116  m_bFormat7Mode = false;
117 
118  // if we are the first instance
119  if(m_nInstances == 0)
121 
122  m_nInstances++;
123 }
124 
125 CLinux1394Capture2::CLinux1394Capture2(VideoMode mode, ColorMode colorMode, ImageProcessor::BayerPatternType bayerPatternType, FrameRate frameRate, int nNumberUIDs, ...) : m_mode(mode), m_colorMode(colorMode), m_bayerPatternType(bayerPatternType), m_frameRate(frameRate)
126 {
127  // number cameras
128  m_nCameras = nNumberUIDs;
129 
130  // fill camera UIDS
131  for(int i = 0 ; i < MAX_CAMERAS ; i++)
132  m_sCameraUID[i] = "";
133 
134  // read variable arguments
135  va_list ap;
136  va_start(ap, nNumberUIDs);
137 
138  for(int u = 0 ; u < nNumberUIDs ; u++)
139  {
140  const char* pch = va_arg(ap, const char*);
141  m_sCameraUID[u] += pch;
142  transform(m_sCameraUID[u].begin(), m_sCameraUID[u].end(), m_sCameraUID[u].begin(), (int(*)(int)) toupper);
143  }
144 
145  va_end(ap);
146 
147  // init images
149 
150  // init size
151  width = -1;
152  height = -1;
153 
154  // set features
155  m_bUseUIDs = true;
156  m_bFormat7Mode = false;
157 
158  // if we are the first instance
159  if(m_nInstances == 0)
161 
162  m_nInstances++;
163 }
164 
165 
166 // ****************************************************************************
167 // Constructors for Format7 Modes
168 // ****************************************************************************
169 
170 CLinux1394Capture2::CLinux1394Capture2(int nCameras, VideoMode mode, float fFormat7FrameRate, int nFormat7MinX, int nFormat7MinY, int nFormat7Width, int nFormat7Height, ColorMode colorMode, ImageProcessor::BayerPatternType bayerPatternType) :
171  m_mode(mode), m_colorMode(colorMode), m_bayerPatternType(bayerPatternType)
172 {
173  // number cameras
174  m_nCameras = nCameras;
175 
176  // init images
178 
179  // init size
180  width = -1;
181  height = -1;
182 
183  // set features
184  m_bFormat7Mode = true;
185  m_fFormat7FrameRate = fFormat7FrameRate;
186  m_nFormat7MinX = nFormat7MinX;
187  m_nFormat7MinY = nFormat7MinY;
188 
189  if (nFormat7Width == -1 || nFormat7Height == -1)
190  {
191  switch (mode)
192  {
193  case e640x480: m_nFormat7Width = 640; m_nFormat7Height = 480; break;
194  case e800x600: m_nFormat7Width = 800; m_nFormat7Height = 600; break;
195  case e1024x768: m_nFormat7Width = 1024; m_nFormat7Height = 768; break;
196  case e1280x960: m_nFormat7Width = 1280; m_nFormat7Height = 960; break;
197  case e1600x1200: m_nFormat7Width = 1600; m_nFormat7Height = 1200; break;
198  default: m_nFormat7Width = 0; m_nFormat7Height = 0; break;
199  }
200  }
201  else
202  {
203  m_nFormat7Width = nFormat7Width;
204  m_nFormat7Height = nFormat7Height;
205  }
206 
207  m_bUseUIDs = false;
208 
209  // if we are first instance
210  if (m_nInstances == 0)
212 
213  m_nInstances++;
214 }
215 
216 CLinux1394Capture2::CLinux1394Capture2(VideoMode mode, float fFormat7FrameRate, int nFormat7MinX, int nFormat7MinY, int nFormat7Width, int nFormat7Height,
217  ColorMode colorMode, ImageProcessor::BayerPatternType bayerPatternType, int nNumberUIDs, ...) :
218  m_mode(mode), m_colorMode(colorMode), m_bayerPatternType(bayerPatternType)
219 {
220  // number cameras
221  m_nCameras = nNumberUIDs;
222 
223  // fill camera UIDS
224  for(int i = 0 ; i < MAX_CAMERAS ; i++)
225  m_sCameraUID[i] = "";
226 
227  // read variable arguments
228  va_list ap;
229  va_start(ap, nNumberUIDs);
230 
231  for(int u = 0 ; u < nNumberUIDs ; u++)
232  {
233  const char* pch = va_arg(ap, const char*);
234  m_sCameraUID[u] += pch;
235  transform(m_sCameraUID[u].begin(), m_sCameraUID[u].end(), m_sCameraUID[u].begin(), (int(*)(int)) toupper);
236  }
237 
238  va_end(ap);
239 
240  // init images
242 
243  // init size
244  width = -1;
245  height = -1;
246 
247  // init features
248  m_bFormat7Mode = true;
249  m_fFormat7FrameRate = fFormat7FrameRate;
250  m_nFormat7MinX = nFormat7MinX;
251  m_nFormat7MinY = nFormat7MinY;
252 
253  if (nFormat7Width == -1 || nFormat7Height == -1)
254  {
255  switch (mode)
256  {
257  case e640x480: m_nFormat7Width = 640; m_nFormat7Height = 480; break;
258  case e800x600: m_nFormat7Width = 800; m_nFormat7Height = 600; break;
259  case e1024x768: m_nFormat7Width = 1024; m_nFormat7Height = 768; break;
260  case e1280x960: m_nFormat7Width = 1280; m_nFormat7Height = 960; break;
261  case e1600x1200: m_nFormat7Width = 1600; m_nFormat7Height = 1200; break;
262  default: m_nFormat7Width = 0; m_nFormat7Height = 0; break;
263  }
264  }
265  else
266  {
267  m_nFormat7Width = nFormat7Width;
268  m_nFormat7Height = nFormat7Height;
269  }
270 
271  m_bUseUIDs = true;
272  m_bFormat7Mode = true;
273 
274  // if we are first instance
275  if (m_nInstances == 0)
277 
278  m_nInstances++;
279 }
280 
281 
282 // *****************************************************************
283 // Destructor
284 // *****************************************************************
285 
287 {
288  // close all related cameras
289  CloseCamera();
290 
291  // delete images
292  delete m_pTempImageHeader;
293 
294  // remove this instance
295  m_nInstances--;
296 
297  // if we are last instance
298  if(m_nInstances == 0)
300 }
301 
302 
303 // ****************************************************************************
304 // Methods
305 // ****************************************************************************
306 
308 {
309  // set required fields to initial values
310  for (int i = 0 ; i < MAX_CAMERAS ; i++)
311  {
312  m_pCameraOpener[i] = NULL;
313  m_cameras[i] = NULL;
314  }
315 
316  m_nOverallCameras = 0;
317 
318  // create new dc handle
319  m_pDC1394 = dc1394_new();
320 
321  // reset all busses (disabled, since some firewire cards take obver 5 seconds to perform the reset)
322  //ResetAllCameras();
323 }
324 
326 {
327  // check if there were listed cameras with no opener and close them
328  for(int c = 0 ; c < m_nOverallCameras ; c++)
329  {
330  if(m_cameras[c] != NULL)
331  {
332  dc1394_camera_free(m_cameras[c]);
333  }
334 
335  m_cameras[c] = NULL;
336  }
337 
338  // free dc handle
339  dc1394_free(m_pDC1394);
340 }
341 
343 {
344  CloseCamera();
345 
346  // create temporary images and init modes
347  if(!InitCameraMode())
348  {
349  printf("error: selected camera mode not supported\n");
350  return false;
351  }
352 
353  // retrieve info for all cameras
354  if(!ListCameras())
355  {
356  CloseCamera();
357  return false;
358  }
359 
360  // open cameras
361  if(m_bUseUIDs)
362  {
363  for(int c = 0 ; c < m_nCameras ; c++)
364  {
365  bool bOpened = false;
366  for(int a = 0 ; a < m_nOverallCameras ; a++)
367  {
368  if(m_sCameraUID[c] == CamUIDToString(m_cameras[a]->guid))
369  {
370  if(m_pCameraOpener[a])
371  {
372  printf("error: camera with UID 0x%s already opened\n", m_sCameraUID[c].c_str());
373  return false;
374  }
375  if(!OpenCamera(a))
376  return false;
377 
378  m_pCameraOpener[a] = this;
379  m_nOpenedCameras[c] = a;
380  bOpened = true;
381  }
382  }
383 
384  if(!bOpened)
385  {
386  printf("error: could not find camera with UID 0x%s\n",m_sCameraUID[c].c_str());
387  CloseCamera();
388  return false;
389  }
390  }
391  } else {
392  int nNumberCameras = 0;
393  for(int c = 0 ; c < m_nOverallCameras ; c++)
394  {
395  if(m_pCameraOpener[c] == NULL)
396  {
397  if(!OpenCamera(c))
398  return false;
399 
400  m_pCameraOpener[c] = this;
401  m_nOpenedCameras[nNumberCameras] = c;
402  nNumberCameras++;
403  }
404 
405  if( (m_nCameras != -1) && (nNumberCameras == m_nCameras))
406  break;
407  }
408 
409  if(m_nCameras == -1)
410  {
411  if(nNumberCameras == 0)
412  {
413  printf("error: no camera found\n");
414  CloseCamera();
415  return false;
416  }
417  m_nCameras = nNumberCameras;
418  }
419 
420  if(nNumberCameras < m_nCameras)
421  {
422  printf("error: could not open requested number of cameras\n");
423  CloseCamera();
424  return false;
425  }
426  }
427 
428  return true;
429 }
430 
432 {
433  printf("Opening camera %d: Vendor %s, Model %s, UID 0x%s, Format7 = %i\n",nCamera, m_cameras[nCamera]->vendor, m_cameras[nCamera]->model, CamUIDToString(m_cameras[nCamera]->guid).c_str(), m_bFormat7Mode);
434 
435  if (dc1394_video_set_iso_speed(m_cameras[nCamera], DC1394_ISO_SPEED_400) != DC1394_SUCCESS)
436  {
437  printf("error: unable to set ISO speed to 400\n");
438  CloseCamera();
439  return false;
440  };
441 
442  if (dc1394_video_set_mode(m_cameras[nCamera],m_video_mode) != DC1394_SUCCESS)
443  {
444  printf("error: unable to set video mode\n");
445  CloseCamera();
446  return false;
447  }
448 
449  if (m_bFormat7Mode)
450  {
451  if (dc1394_format7_set_image_size(m_cameras[nCamera],m_video_mode, m_nFormat7Width, m_nFormat7Height) != DC1394_SUCCESS)
452  {
453  printf("error: unable to set format7 image size (%d, %d)\n", m_nFormat7Width, m_nFormat7Height);
454  CloseCamera();
455  return false;
456  }
457 
458  if (dc1394_format7_set_image_position(m_cameras[nCamera],m_video_mode, m_nFormat7MinX, m_nFormat7MinY) != DC1394_SUCCESS)
459  {
460  printf("error: unable to set format7 image position (%d, %d)\n", m_nFormat7MinX, m_nFormat7MinY);
461  CloseCamera();
462  return false;
463  }
464 
465  // calculate packet size from, framerate
466  unsigned int nPacketSize = 0;
467  if (m_fFormat7FrameRate < 0)
468  {
469  dc1394_format7_get_recommended_packet_size(m_cameras[nCamera], m_video_mode, &nPacketSize);
470  }
471  else
472  {
473  uint64_t frame_size;
474  unsigned int nMinBytes, nMaxBytes;
475  dc1394_format7_get_total_bytes(m_cameras[nCamera], m_video_mode, &frame_size);
476  dc1394_format7_get_packet_parameters(m_cameras[nCamera], m_video_mode, &nMinBytes, &nMaxBytes);
477 
478  int num_packets = (int) (1.0f / (0.0125f * m_fFormat7FrameRate) + 0.5f); // number of packets per frame period
479  int denominator = num_packets * 8;
480  nPacketSize = (unsigned int) ( ( frame_size + denominator - 1) / float(denominator) / 10.0);
481  nPacketSize = nMinBytes * ( nPacketSize / nMinBytes);
482 
483  if (nPacketSize < nMinBytes)
484  nPacketSize = nMinBytes;
485 
486  if (nPacketSize > nMaxBytes)
487  nPacketSize = nMaxBytes;
488  }
489 
490  if (dc1394_format7_set_packet_size(m_cameras[nCamera],m_video_mode, nPacketSize) != DC1394_SUCCESS)
491  {
492  printf("error: unable to set format7 packet size %d (framerate %f)\n", nPacketSize, m_fFormat7FrameRate);
493  CloseCamera();
494  return false;
495  }
496  }
497  else
498  {
499  if (dc1394_video_set_framerate(m_cameras[nCamera], GetDCFrameRateMode(m_frameRate)) != DC1394_SUCCESS)
500  {
501  printf("error: could not set framerate\n");
502  CloseCamera();
503  return false;
504  }
505  }
506 
507  // calculate bandwidth usage
508  unsigned int nBandwidth;
509  if(dc1394_video_get_bandwidth_usage(m_cameras[nCamera],&nBandwidth) != DC1394_SUCCESS)
510  {
511  printf("error: unable to calculate bandwidth usage\n");
512  CloseCamera();
513  return false;
514  }
515 
516  m_nCameraBandwidth[nCamera] = nBandwidth;
517  m_nRemainingBandwidth -= nBandwidth;
518  printf("Camera %d bandwidth usage: %d quadlets/cycle (%d quadlets/cycle left)\n",nCamera, nBandwidth, m_nRemainingBandwidth);
519 
520  if(m_nRemainingBandwidth < 0)
521  {
522  printf("warning: Opening camera %d would exceed maximum firewire bandwidth (%d quadlets/cycle).\n", nCamera, MAX_S400_BANDWIDTH);
523  m_nRemainingBandwidth += nBandwidth;
524 
525  // deprecated: using two firewire ports the calculation is wrong. Further several cameras do not provide proper bandwidth information
526  //CloseCamera();
527  //return false;
528  }
529 
530  // hack: port seems to be busy for some time. so give time and try multiple calls to setup
531  bool bSuccess = false;
532  int nTries = 5;
533 
534  while(!bSuccess && (nTries > 0))
535  {
536  if( dc1394_capture_setup(m_cameras[nCamera], NUM_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT) == DC1394_SUCCESS)
537  bSuccess = true;
538 
540  nTries--;
541  }
542 
543  if(!bSuccess)
544  {
545  printf("error: unable to setup camera (mode supported by camera?)\n");
546  CloseCamera();
547  return false;
548  }
549 
550 
551  if(dc1394_video_set_transmission(m_cameras[nCamera], DC1394_ON) != DC1394_SUCCESS)
552  {
553  printf("error: unable to start camera iso transmission\n");
554  CloseCamera();
555  return false;
556  }
557 
558  return true;
559 }
560 
562 {
563  for (int i = 0; i < m_nOverallCameras; i++)
564  {
565  if(m_pCameraOpener[i] == this)
566  {
567  // stop video transmission
568  if(dc1394_video_set_transmission(m_cameras[i], DC1394_OFF) != DC1394_SUCCESS)
569  printf("Error: could not stop iso transmission\n");
570 
571  // stop capturer
572  if(dc1394_capture_stop(m_cameras[i]) != DC1394_SUCCESS)
573  printf("Error: could not stop capturing\n");
574 
575  // no opener for this camera
576  m_pCameraOpener[i] = NULL;
577 
578  // adjust remaining bandwidth
580  }
581  }
582 
583  width = -1;
584  height = -1;
585 }
586 
588 {
589  width = -1;
590  height = -1;
591  int video_mode = 0;
592 
593  // for format 7 modes (only use mode 0)
594  if (m_bFormat7Mode)
595  {
596  // set video mode to format 7
597  m_video_mode = DC1394_VIDEO_MODE_FORMAT7_0;
598 
599  // set image size
602 
603  // set capture image header correctly
608 
609  return true;
610  }
611 
612  // standard modes
613  if (m_mode == e640x480)
614  {
615  width = 640;
616  height = 480;
617 
618  switch (m_colorMode)
619  {
620  case eGrayScale:
622  m_video_mode = DC1394_VIDEO_MODE_640x480_MONO8;
623  break;
624 
625  case eRGB24:
626  m_video_mode = DC1394_VIDEO_MODE_640x480_RGB8;
627  break;
628 
629  case eYUV411ToRGB24:
630  m_video_mode = DC1394_VIDEO_MODE_640x480_YUV411;
631  break;
632  }
633  }
634  else if (m_mode == e800x600)
635  {
636  width = 800;
637  height = 600;
638 
639  switch (m_colorMode)
640  {
641  case eGrayScale:
643  m_video_mode = DC1394_VIDEO_MODE_800x600_MONO8;
644  break;
645 
646  case eRGB24:
647  m_video_mode = DC1394_VIDEO_MODE_800x600_RGB8;
648  break;
649 
650  case eYUV411ToRGB24: return false;
651  }
652  }
653  else if (m_mode == e1024x768)
654  {
655  width = 1024;
656  height = 768;
657 
658  switch (m_colorMode)
659  {
660  case eGrayScale:
662  m_video_mode = DC1394_VIDEO_MODE_1024x768_MONO8;
663  break;
664 
665  case eRGB24:
666  m_video_mode = DC1394_VIDEO_MODE_1024x768_RGB8;
667  break;
668 
669  case eYUV411ToRGB24: return false;
670  }
671  }
672  else if (m_mode == e1280x960)
673  {
674  width = 1280;
675  height = 960;
676 
677  switch (m_colorMode)
678  {
679  case eGrayScale:
681  m_video_mode = DC1394_VIDEO_MODE_1280x960_MONO8;
682  break;
683 
684  case eRGB24:
685  m_video_mode = DC1394_VIDEO_MODE_1280x960_RGB8;
686  break;
687 
688  case eYUV411ToRGB24: return false;
689  }
690  }
691  else if (m_mode == e1600x1200)
692  {
693  width = 1600;
694  height = 1200;
695 
696  switch (m_colorMode)
697  {
698  case eGrayScale:
700  m_video_mode = DC1394_VIDEO_MODE_1600x1200_MONO8;
701  break;
702 
703  case eRGB24:
704  m_video_mode = DC1394_VIDEO_MODE_1600x1200_RGB8;
705  break;
706 
707  case eYUV411ToRGB24: return false;
708  }
709  }
710  else
711  {
712  // mode not supported
713  return false;
714  }
715 
718 
720  {
723  }
724  else if (m_colorMode == eRGB24 || m_colorMode == eYUV411ToRGB24)
725  {
728  }
729 
730  return true;
731 }
732 
733 
735 {
736  int width = pInput->width;
737  int height = pInput->height;
738 
739  int y1,y2,y3,y4,u,v;
740  int r,g,b;
741 
742  unsigned char* output = pOutput->pixels;
743  unsigned char* input = pInput->pixels;
744  // read chunks
745  for(int i = 0 ; i < height ; i++)
746  {
747  for(int j = 0 ; j < width / 4 ; j++)
748  {
749  u = input[0];
750  y1 = input[1];
751  y2 = input[2];
752 
753  v = input[3];
754  y3 = input[4];
755  y4 = input[5];
756 
757  YUVToRGB(y1,u,v,output);
758  YUVToRGB(y2,u,v,output + 3);
759  YUVToRGB(y3,u,v,output + 6);
760  YUVToRGB(y4,u,v,output + 9);
761 
762  input += 6;
763  output += 12;
764  }
765  }
766 }
767 
768 void CLinux1394Capture2::YUVToRGB(int y, int u, int v, unsigned char* output)
769 {
770  int r,g,b;
771 
772  // u and v are +-0.5
773  u -= 128;
774  v -= 128;
775 
776  // conversion
777  r = int(y + 1.370705 * v);
778  g = int(y - 0.698001 * v - 0.337633 * u);
779  b = int(y + 1.732446 * u);
780 
781  // clamp to 0..1
782  if (r < 0) r = 0;
783  if (g < 0) g = 0;
784  if (b < 0) b = 0;
785  if (r > 255) r = 255;
786  if (g > 255) g = 255;
787  if (b > 255) b = 255;
788 
789  output[0] = (unsigned char) r;
790  output[1] = (unsigned char) g;
791  output[2] = (unsigned char) b;
792 }
793 
794 
795 // *****************************************************************
796 // CaptureImage
797 // *****************************************************************
798 
800 {
801  if ( (width == -1) || (height == -1) )
802  return false;
803 
804  int i;
805 
806  for (i = 0; i < m_nCameras; i++)
807  {
808  int nCurrentIndex = m_nOpenedCameras[i];
809  dc1394video_frame_t* pCurrentFrame = NULL;
810 
811  while(pCurrentFrame == NULL)
812  {
813  if(dc1394_capture_dequeue(m_cameras[nCurrentIndex], DC1394_CAPTURE_POLICY_POLL, &pCurrentFrame) != DC1394_SUCCESS)
814  {
815  printf("Error: could not capture current frame on camera %d\n",nCurrentIndex);
816  return false;
817  }
818  }
819 
820  m_pTempImageHeader->pixels = (unsigned char *) pCurrentFrame->image;
821 
822  switch (m_colorMode)
823  {
826  break;
827 
828  case eGrayScale:
829  case eRGB24:
831  break;
832 
833  case eYUV411ToRGB24:
834  ConvertYUV411(m_pTempImageHeader, ppImages[i]);
835  break;
836  }
837 
838  if(dc1394_capture_enqueue(m_cameras[nCurrentIndex], pCurrentFrame) != DC1394_SUCCESS)
839  {
840  printf("Error: could not release current frame of camera %d\n", nCurrentIndex);
841  return false;
842  }
843  }
844 
845  return true;
846 }
847 
849 {
850  if ( (width == -1) || (height == -1) )
851  return false;
852 
853  int i;
854 
855  // free last frame
856  for (i = 0; i < m_nCameras; i++)
857  {
858  int nCurrentIndex = m_nOpenedCameras[i];
859 
860  dc1394video_frame_t* pCurrentFrame = NULL;
861  // retrieve frame (used poll also WAIT is possible)
862  while(pCurrentFrame == NULL)
863  {
864  if(dc1394_capture_dequeue(m_cameras[nCurrentIndex], DC1394_CAPTURE_POLICY_POLL, &pCurrentFrame) != DC1394_SUCCESS)
865  {
866  printf("Error: could not capture current frame on camera %d\n",nCurrentIndex);
867  return false;
868  }
869  }
870 
871  m_pTempImageHeader->pixels = (unsigned char *) pCurrentFrame->image;
872 
873  if(m_bFormat7Mode)
874  {
876  }
877 
878  if (m_mode == e640x480)
879  {
881  }
882  else if (m_mode == e320x240)
883  {
885  }
886 
887  if(dc1394_capture_enqueue(m_cameras[nCurrentIndex], pCurrentFrame) != DC1394_SUCCESS)
888  {
889  printf("Error: could not release current frame of camera %d\n",nCurrentIndex);
890  return false;
891  }
892  }
893 
894  return true;
895 }
896 
898 {
899  switch (m_colorMode)
900  {
901  case eYUV411ToRGB24:
903  case eRGB24:
904  return CByteImage::eRGB24;
905  break;
906 
907  case eGrayScale:
908  return CByteImage::eGrayScale;
909  break;
910  }
911 
912  return (CByteImage::ImageType) -1;
913 }
914 
915 void CLinux1394Capture2::SetCameraUids(std::vector<std::string> uids)
916 {
917  m_nCameras = uids.size();
918 
919  for(int u = 0 ; u < m_nCameras ; u++)
920  {
921  m_sCameraUID[u] += uids[u];
922  transform(m_sCameraUID[u].begin(), m_sCameraUID[u].end(), m_sCameraUID[u].begin(), (int(*)(int)) toupper);
923  }
924 }
925 
927 {
928  SetFeature(DC1394_FEATURE_GAIN, "gain", nValue);
929 }
930 
932 {
933  SetFeature(DC1394_FEATURE_EXPOSURE, "exposure", nValue);
934 }
935 
937 {
938  SetFeature(DC1394_FEATURE_SHUTTER, "shutter", nValue);
939 }
940 
941 void CLinux1394Capture2::SetWhiteBalance(int nU, int nV, int nCamera)
942 {
943  if(nCamera == -1)
944  {
945  for (int i = 0; i < m_nCameras; i++)
946  {
947  int nCurrentCamera = m_nOpenedCameras[i];
948  dc1394_feature_whitebalance_set_value(m_cameras[nCurrentCamera], nU, nV);
949  }
950  } else {
951  int nCurrentCamera = m_nOpenedCameras[nCamera];
952  dc1394_feature_whitebalance_set_value(m_cameras[nCurrentCamera], nU, nV);
953  }
954 }
955 
957 {
958  for (int i = 0; i < m_nCameras; i++)
959  {
960  int nCurrentCamera = m_nOpenedCameras[i];
961  dc1394_feature_temperature_set_value(m_cameras[nCurrentCamera], nTemperature);
962  }
963 }
964 
966 {
967  for (int i = 0; i < m_nCameras; i++)
968  {
969  printf("== Camera %d =========================================================================\n", i);
970  int nCurrentCamera = m_nOpenedCameras[i];
971  dc1394featureset_t feature_set;
972 
973  dc1394_feature_get_all(m_cameras[nCurrentCamera], &feature_set);
974  dc1394_feature_print_all(&feature_set,stdout);
975  }
976 }
977 
978 void CLinux1394Capture2::SetFeature(dc1394feature_t feature, std::string sName, int nValue)
979 {
980  dc1394bool_t bPresent;
981 
982  for (int i = 0; i < m_nCameras; i++)
983  {
984  int nCurrentCamera = m_nOpenedCameras[i];
985  dc1394_feature_is_present(m_cameras[nCurrentCamera],feature, &bPresent);
986 
987  if(!bPresent)
988  {
989  printf("warning: camera %d does not support %s feature\n", nCurrentCamera, sName.c_str());
990  return;
991  }
992 
993  // check boundaries
994  uint32_t nMin, nMax;
995  dc1394_feature_get_boundaries(m_cameras[nCurrentCamera],feature, &nMin, &nMax);
996 
997  if( (nValue != -1) && ((nValue < nMin) || (nValue > nMax)) )
998  {
999  printf("warning: requested %s %d for camera %d is not in the valid range [%d;%d]\n", sName.c_str(), nValue, nCurrentCamera, nMin, nMax);
1000  return;
1001  }
1002 
1003  dc1394feature_modes_t availableModes;
1004  dc1394_feature_get_modes(m_cameras[nCurrentCamera],feature, &availableModes);
1005 
1006  bool bHasAuto = false;
1007  bool bHasManual = false;
1008 
1009  for(int i = 0 ; i < availableModes.num ; i++)
1010  {
1011  if(availableModes.modes[i] == DC1394_FEATURE_MODE_AUTO)
1012  bHasAuto = true;
1013  if(availableModes.modes[i] == DC1394_FEATURE_MODE_MANUAL)
1014  bHasManual = true;
1015  }
1016 
1017  if (nValue == -1)
1018  {
1019  if(!bHasAuto)
1020  {
1021  printf("warning: camera %d has no auto %s feature\n", nCurrentCamera, sName.c_str());
1022  return;
1023  }
1024  dc1394_feature_set_mode(m_cameras[nCurrentCamera], feature, DC1394_FEATURE_MODE_AUTO);
1025  }
1026  else
1027  {
1028  if(!bHasManual)
1029  {
1030  printf("warning: camera %d has no manual %s feature\n", nCurrentCamera, sName.c_str());
1031  return;
1032  }
1033  dc1394_feature_set_mode(m_cameras[nCurrentCamera], feature, DC1394_FEATURE_MODE_MANUAL);
1034  dc1394_feature_set_value(m_cameras[nCurrentCamera], feature, nValue);
1035  }
1036  }
1037 }
1038 
1040 {
1041  switch(frameRate)
1042  {
1043  case e60fps: return DC1394_FRAMERATE_60;
1044  case e30fps: return DC1394_FRAMERATE_30;
1045  case e15fps: return DC1394_FRAMERATE_15;
1046  case e7_5fps: return DC1394_FRAMERATE_7_5;
1047  case e3_75fps: return DC1394_FRAMERATE_3_75;
1048  case e1_875fps: return DC1394_FRAMERATE_1_875;
1049 
1050  default: return DC1394_FRAMERATE_30;
1051  };
1052 }
1053 
1055 {
1056  dc1394camera_list_t * list;
1057  dc1394camera_t *camera;
1058  dc1394error_t err;
1059 
1060  bool bFinished = false;
1061  int nIndex = 0;
1062 
1063  while(!bFinished)
1064  {
1065  err=dc1394_camera_enumerate (m_pDC1394, &list);
1066 
1067  // check if last camera
1068  if (nIndex >= list->num)
1069  {
1070  break;
1071  }
1072 
1073  camera = dc1394_camera_new (m_pDC1394, list->ids[nIndex].guid);
1074 
1075  if (!camera)
1076  {
1077  break;
1078  }
1079 
1080  dc1394_camera_free_list (list);
1081 
1082  dc1394_reset_bus (camera);
1083 
1084  dc1394_camera_free (camera);
1085  nIndex++;
1086 
1087  // give time to bus for recovery after bus reset. Needed for some firewire cards.
1088  usleep(50000);
1089  }
1090 }
1091 
1093 {
1094  // already listed
1095  if(m_nOverallCameras != 0)
1096  return true;
1097 
1098  // enumarate cameras
1099  dc1394camera_list_t* camera_list;
1100 
1101  if(dc1394_camera_enumerate(m_pDC1394, &camera_list) != DC1394_SUCCESS)
1102  {
1103  printf("error: cannot enumerate cameras\n");
1104  return false;
1105  }
1106 
1107  int nNumberCams = camera_list->num;
1108 
1109  for(int i = 0 ; i < nNumberCams ; i++)
1110  {
1111  printf("= camera %d ========================================= :\n",i);
1112 
1113  m_cameras[i] = dc1394_camera_new(m_pDC1394, camera_list->ids[i].guid);
1114  m_pCameraOpener[i] = NULL;
1115 
1116  if(dc1394_camera_print_info(m_cameras[i], stdout) != DC1394_SUCCESS)
1117  {
1118  printf("error: could not retrieve info for camera %d\n",i);
1119 
1120  // free camera list
1121  dc1394_camera_free_list(camera_list);
1122 
1123  return false;
1124  }
1125  }
1126 
1127  // free camera list
1128  dc1394_camera_free_list(camera_list);
1129 
1130  m_nOverallCameras = nNumberCams;
1131 
1132  // check some stuff
1134  {
1135  printf("error: found more cameras than MAX_CAMERAS\n");
1136  CloseCamera();
1137  return false;
1138  }
1139 
1140  // output info and create camera structures
1141  printf("= Found %d cameras ==================================\n",nNumberCams);
1142  return true;
1143 }
1144 
1146 {
1147  std::string UID = "";
1148  int nLow = (int) uid;
1149  int nHigh = (int) (uid >> 32);
1150  char szUID[17];
1151  sprintf(szUID,"%08X%08X%c",nHigh,nLow, '\0');
1152  UID += szUID;
1153 
1154  return UID;
1155 }
void ConvertYUV411(CByteImage *pInput, CByteImage *pOutput)
GLubyte g
Definition: glext.h:5166
CByteImage * m_pTempImageHeader
BayerPatternType
The four possible variants for Bayer pattern conversion.
void SetWhiteBalance(int nU, int nV, int nCamera=-1)
CByteImage::ImageType GetType()
void SetCameraUids(std::vector< std::string > uids)
dc1394framerate_t GetDCFrameRateMode(FrameRate frameRate)
static dc1394_t * m_pDC1394
int width
The width of the image in pixels.
Definition: ByteImage.h:257
void SetExposure(int nValue)
static int m_nRemainingBandwidth
void SetFeature(dc1394feature_t feature, std::string sName, int nValue)
bool ConvertBayerPattern(const CByteImage *pInputImage, CByteImage *pOutputImage, BayerPatternType type)
Converts an 8 bit Bayer pattern CByteImage to an RGB24 color CByteImage.
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
void SetTemperature(int nTemperature)
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
bool CaptureBayerPatternImage(CByteImage **ppImages)
static int m_nCameraBandwidth[MAX_CAMERAS]
const GLubyte * c
Definition: glext.h:5181
void SetShutter(int nValue)
GLuint GLuint end
Definition: glext.h:3121
bool CaptureImage(CByteImage **ppImages)
bool CopyImage(const CByteImage *pInputImage, CByteImage *pOutputImage, const MyRegion *pROI=0, bool bUseSameSize=false)
Copies one CByteImage to another.
std::string CamUIDToString(uint64_t uid)
GLsizei const GLchar ** string
Definition: glext.h:3528
CLinux1394Capture2(int nCameras, VideoMode mode, ColorMode colorMode, ImageProcessor::BayerPatternType bayerPatternType=ImageProcessor::eBayerRG, FrameRate frameRate=e30fps)
int m_nOpenedCameras[MAX_CAMERAS]
std::string m_sCameraUID[MAX_CAMERAS]
GLubyte GLubyte GLubyte a
Definition: glext.h:5166
const VideoMode m_mode
int height
The height of the image in pixels.
Definition: ByteImage.h:264
GLint mode
Definition: glext.h:4669
GLubyte GLubyte b
Definition: glext.h:5166
void YUVToRGB(int y, int u, int v, unsigned char *output)
const ColorMode m_colorMode
int bytesPerPixel
The number of bytes used for encoding one pixel.
Definition: ByteImage.h:273
GLenum GLsizei width
Definition: glext.h:3122
GLenum GLsizei GLsizei height
Definition: glext.h:3132
ImageType
Enum specifying the supported image types.
Definition: ByteImage.h:86
static dc1394camera_t * m_cameras[MAX_CAMERAS]
GLenum GLenum GLenum input
Definition: glext.h:5307
ImageType type
The type of the image.
Definition: ByteImage.h:292
GLdouble GLdouble GLdouble r
Definition: glext.h:3227
GLenum GLint GLint y
Definition: glext.h:3125
const GLdouble * v
Definition: glext.h:3212
const ImageProcessor::BayerPatternType m_bayerPatternType
#define MAX_CAMERAS
GLuint GLenum GLenum transform
Definition: glext.h:5683
void SetGain(int nValue)
#define NUM_BUFFERS
void SleepThread(int nMS)
Definition: Threading.cpp:84
static CLinux1394Capture2 * m_pCameraOpener[MAX_CAMERAS]
#define MAX_S400_BANDWIDTH
bool Resize(const CByteImage *pInputImage, CByteImage *pOutputImage, const MyRegion *pROI=0, bool bInterpolation=true)
Resizes a CByteImage and writes the result to a CByteImage.
static int m_nOverallCameras
dc1394video_mode_t m_video_mode


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28