Unicap1394Capture.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: Unicap1394Capture.cpp
37 // Author: Pedram Azad
38 // Date: 2005
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include "Unicap1394Capture.h"
47 
48 #include "Image/ByteImage.h"
49 
50 #include <stdio.h>
51 #include <string.h>
52 #include <string>
53 
54 
55 // ****************************************************************************
56 // Defines
57 // ****************************************************************************
58 
59 #define UYVY 0x59565955 /* UYVY (packed, 16 bits) */
60 #define FOURCC(a,b,c,d) (unsigned int)((((unsigned int)d)<<24)+(((unsigned int)c)<<16)+(((unsigned int)b)<<8)+a)
61 
62 
63 
64 // ****************************************************************************
65 // Constructor / Destructor
66 // ****************************************************************************
67 
68 CUnicap1394Capture::CUnicap1394Capture(int nCameras, VideoMode mode) : m_nCameras(nCameras), m_mode(mode)
69 {
70  source = 4;
71  width = 0;
72  height = 0;
73  norm = 0;
74 
75  for (int i = 0; i < UNICAP_MAX_CAMERAS; i++)
76  handle[i] = 0;
77 }
78 
80 {
81  CloseCamera();
82 }
83 
84 
85 // ****************************************************************************
86 // Methods
87 // ****************************************************************************
88 
90 {
91  unicap_property_t property;
92  unicap_format_t format;
93  unicap_format_t format_spec;
94 
95  int nCamerasFound = 0;
96 
97 
98  for (int i = 0; i < 10; i++)
99  {
100  if (SUCCESS(unicap_enumerate_devices(0, &device[i], i)))
101  {
102  printf("info: camera found at index %i\n", i);
103  printf("info: using device for cam: %s\n", device[i].identifier);
104  nCamerasFound++;
105  }
106 
107  if (!SUCCESS(unicap_open(&handle[i], &device[i])))
108  {
109  printf("error2\n");
110  return false;
111  }
112 
113  unicap_void_property( &property );
114  strcpy( property.identifier, "video norm" );
115  unicap_get_property( handle[i], &property );
116  strcpy( property.menu_item, property.menu.menu_items[this->norm] );
117  if(!SUCCESS(unicap_set_property(handle[i], &property)))
118  {
119  printf("error3\n");
120  return false;
121  }
122 
123  unicap_void_property( &property );
124  strcpy( property.identifier, "source" );
125  unicap_get_property( handle[i], &property );
126  strcpy( property.menu_item, property.menu.menu_items[this->source] );
127  if(!SUCCESS(unicap_set_property(handle[i], &property)))
128  {
129  printf("error4\n");
130  return false;
131  }
132 
133  if(!SUCCESS(unicap_lock_stream(handle[i])))
134  {
135  printf("error5\n");
136  return false;
137  }
138 
139  unicap_status_t status=0;
140  unicap_void_format( &format_spec );
141 
142  format_spec.fourcc = FOURCC('U','Y','V','Y');
143  int num= - 1;
144  status = unicap_enumerate_formats(handle[i], &format_spec, &format, 0 );
145  if(!SUCCESS(status))
146  {
147  printf("error: failed get video format\n");
148  return false;
149  }
150 
151  for (int j = 0; j < format.size_count; j++)
152  {
153  printf("format %i: width/height = %i/%i\n", j, format.sizes[j].width, format.sizes[j].height);
154 
155  switch (m_mode)
156  {
157  case e320x240:
158  if (format.sizes[j].width == 320 && format.sizes[j].height == 240)
159  {
160  num = j;
161  width = 320;
162  height = 240;
163  }
164  break;
165 
166  case e640x480:
167  if (format.sizes[j].width == 640 && format.sizes[j].height == 480)
168  {
169  num = j;
170  width = 640;
171  height = 480;
172  }
173  break;
174 
175  case e768x576:
176  if (format.sizes[j].width == 768 && format.sizes[j].height == 576)
177  {
178  num = j;
179  width = 768;
180  height = 576;
181  }
182  break;
183  }
184  }
185 
186  if (num < 0)
187  {
188  printf("error: could not found matching mode\n");
189  return false;
190  }
191 
192  printf("info: took format %i with resolution %i/%i\n", num, width, height);
193 
194  format.size.width = format.sizes[num].width;
195  format.size.height = format.sizes[num].height;
196 
197  if(!SUCCESS( unicap_set_format(handle[i], &format ) ) )
198  {
199  printf("error: Failed to set video format\n" );
200  return false;
201  }
202  if( !SUCCESS( unicap_get_format(handle[i], &format ) ) )
203  {
204  printf("error: Failed to get video format\n" );
205  return false;
206  }
207  memset(&buffer[i], 0x0, sizeof(unicap_data_buffer_t));
208  buffer[i].format = format;
209  buffer[i].buffer_size = format.buffer_size;
210  buffer[i].type = format.buffer_type;
211 
212  printf("info: %s->Buffer size: %d\n",device[i].identifier,format.buffer_size);
213 
214  if (buffer[i].type == UNICAP_BUFFER_TYPE_USER)
215  buffer[i].data = new unsigned char[buffer[i].buffer_size];
216 
217  status = unicap_start_capture(handle[i]);
218  if(!SUCCESS(status))
219  {
220  printf("error: failed to start capture on device: %s (Error Code : %x)\n", device[i].identifier,status );
221  return false;
222  }
223 
224  printf("\n");
225 
226  if (nCamerasFound == m_nCameras)
227  break;
228  }
229 
230  if (nCamerasFound < 0 || (m_nCameras != -1 && nCamerasFound != m_nCameras))
231  return false;
232 
233  return true;
234 }
235 
237 {
238  for (int i = 0; i < UNICAP_MAX_CAMERAS; i++)
239  {
240  if (handle[i])
241  {
242  if (!unicap_stop_capture(handle[i]))
243  printf("error: close\n");
244 
245  unicap_unlock_stream(handle[i]);
246 
247  if(!SUCCESS(unicap_close(handle[i])))
248  {
249  printf("error: destructor\n");
250  }
251 
252  handle[i] = 0;
253  }
254  }
255 
256 }
257 
258 void CUnicap1394Capture::Convert(const unsigned char *input, unsigned char *output)
259 {
260  const int nBytes = width * height * 2;
261 
262  float u,y1,v,y2,r1,g1,b1,r2,g2,b2;
263 
264  for(unsigned int i = 0,j=0; i<nBytes;i+=4,j+=6)
265  {
266  u = input[i];
267  y1 =input[i+1];
268  v = input[i+2];
269  y2 =input[i+3];
270 
271  u -= 128; //Werte zwischen -+0,5
272  v -= 128; //Werte zwischen -+0,5
273  y1 -= 16;
274  y2 -= 16;
275 
276  r1 = 1.164f * y1 + 1.596f * v;
277  g1 = 1.164f * y1 - 0.392f * u - 0.813f * v;
278  b1 = 1.164f * y1 + 2.017f * u;
279 
280  r2 = 1.164f * y2 + 1.596f * v;
281  g2 = 1.164f * y2 - 0.392f * u - 0.813f * v;
282  b2 = 1.164f * y2 + 2.017f * u;
283 
284 
285  if (r1 < 0) r1 = 0;
286  if (g1 < 0) g1 = 0;
287  if (b1 < 0) b1 = 0;
288  if (r1 > 255) r1 = 255;
289  if (g1 > 255) g1 = 255;
290  if (b1 > 255) b1 = 255;
291 
292  if (r2 < 0) r2 = 0;
293  if (g2 < 0) g2 = 0;
294  if (b2 < 0) b2 = 0;
295  if (r2 > 255) r2 = 255;
296  if (g2 > 255) g2 = 255;
297  if (b2 > 255) b2 = 255;
298 
299  output[j] = (unsigned char)r1;
300  output[j+1]= (unsigned char)g1;
301  output[j+2]= (unsigned char)b1;
302  output[j+3]= (unsigned char)r2;
303  output[j+4]= (unsigned char)g2;
304  output[j+5]= (unsigned char)b2;
305  }
306 }
307 
309 {
310 unicap_data_buffer_t *returned_buffer[10] = { 0 };
311  for (int i = 0; i < m_nCameras; i++)
312  {
313 
314  unicap_status_t status = unicap_queue_buffer(handle[i], &buffer[i]);
315  if(!SUCCESS(status))
316  {
317  printf("error: failed to queue a buffer on device: %s (Error Code : %x)\n", device[i].identifier,status);
318  return false;
319  }
320 
321  status = unicap_wait_buffer(handle[i], &returned_buffer[i]);
322 
323  if (!SUCCESS(status))
324  {
325  printf("error: %x\n", status);
326  printf("error: failed to wait for buffer on device: %s (Error Code : %x)\n", device[i].identifier,status );
327  }
328 
329  Convert(returned_buffer[i]->data, ppImages[i]->pixels);
330  }
331 
332  /*// alternative Conversion from YUV422 (YCbCr422) to RGB with IPP library
333  IppiSize size;
334  size.width=format.size.width;
335  size.height=format.size.height;
336  unsigned char *rgb_buffer = new unsigned char [format.size.width*format.size.height*6];
337  ippiCbYCr422ToRGB_8u_C2C3R((Ipp8u*)returned_buffer->data,size.width*2,(Ipp8u*)rgb_buffer,size.width*3,size);*/
338 
339  return true;
340 }
341 
342 
343 /*
344 void CUnicap1394Capture::setContrast(unsigned int contrast)
345 {
346  unicap_void_property( &property );
347  strcpy( property.identifier, "contrast" );
348  unicap_get_property( handle, &property );
349  sprintf(property.menu_item,"%d",contrast);
350  if( !SUCCESS( unicap_set_property( handle, &property ) ) )
351  {
352  BGTHROWC(bg::error,"Failed to set contrast!" );
353  exit(1);
354  }
355 
356 }
357 
358 unsigned int CUnicap1394Capture::getContrast()
359 {
360  unicap_void_property( &property );
361  strcpy( property.identifier, "contrast" );
362  unicap_get_property( handle, &property );
363  bg::string contrast(property.menu.menu_items[0]);
364  return contrast.toInt();
365 }
366 
367 
368 void CUnicap1394Capture::setBrightness(unsigned int brightness)
369 {
370  unicap_void_property( &property );
371  strcpy( property.identifier, "brightness" );
372  unicap_get_property( handle, &property );
373  sprintf(property.menu_item, "%d",brightness);
374  if( !SUCCESS( unicap_set_property( handle, &property ) ) )
375  {
376  BGTHROWC(bg::error,"Failed to set brightness!" );
377  exit(1);
378  }
379 }
380 
381 unsigned int CUnicap1394Capture::getBrightness()
382 {
383  unicap_void_property( &property );
384  strcpy( property.identifier, "brightness" );
385  unicap_get_property( handle, &property );
386  bg::string brightn(property.menu.menu_items[0]);
387  return brightn.toInt();
388 }
389 */
unicap_device_t device[UNICAP_MAX_CAMERAS]
unicap_handle_t handle[UNICAP_MAX_CAMERAS]
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
GLuint GLuint num
Definition: glext.h:5894
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3121
#define FOURCC(a, b, c, d)
CUnicap1394Capture(int nCameras, VideoMode mode)
#define UNICAP_MAX_CAMERAS
void Convert(const unsigned char *input, unsigned char *output)
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3129
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: glext.h:3154
GLint mode
Definition: glext.h:4669
GLenum GLsizei width
Definition: glext.h:3122
GLenum GLsizei GLsizei height
Definition: glext.h:3132
GLenum GLenum GLenum input
Definition: glext.h:5307
bool CaptureImage(CByteImage **ppImages)
GLsizei GLsizei GLchar * source
Definition: glext.h:3517
const GLdouble * v
Definition: glext.h:3212
GLenum GLsizei GLenum format
Definition: glext.h:3122
GLuint buffer
Definition: glext.h:3384


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