Fawkes API  Fawkes Development Version
leutron.cpp
1 
2 /***************************************************************************
3  * leutron.cpp - Leutron camera
4  *
5  * Generated: Thu Mar 24 22:36:05 2005
6  * Copyright 2005 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <core/exception.h>
25 
26 #include <fvcams/leutron.h>
27 #include <fvutils/color/colorspaces.h>
28 
29 #include <lvdef.h>
30 #include <dsylib.h>
31 #include <grabber.h>
32 #include <cstdlib>
33 
34 using namespace fawkes;
35 
36 namespace firevision {
37 #if 0 /* just to make Emacs auto-indent happy */
38 }
39 #endif
40 
41 /** @class LeutronCamera <fvcams/leutron.h>
42  * Cameras accessed through Leutron framegrabber.
43  */
44 
45 /** Constructor. */
46 LeutronCamera::LeutronCamera()
47 {
48  started = opened = false;
49  autodetect = false;
50 
51  cspace = YUV422_PACKED;
52 
53  camera_name = "PAL_S_CCIR";
54 }
55 
56 
57 /** Destructor. */
58 LeutronCamera::~LeutronCamera()
59 {
60 }
61 
62 
63 void
64 LeutronCamera::open()
65 {
66  opened = false;
67  camera_handle = HANDLE_INVALID;
68 
69  // cout << "LeutronCam: Calling DsyInit(). This may take a while..." << std::flush;
70  DsyInit();
71  // cout << "done" << endl;
72 
73  if (DsyRecheckConnector() != I_NoError) {
74  throw Exception("LeutronCam: DsyRecheckConnector() failed");
75  }
76 
77  LvCameraConnDlgInfo *info;
78  HANDLE conn_info_handle = NULL;
79  HGRABBER grabber_handle = HANDLE_INVALID;
80 
81  if (DsyDetectCamera(&conn_info_handle) > 0) {
82  // We found at least one camera
83  info = (LvCameraConnDlgInfo *)GlobalLock(conn_info_handle);
84  if (info) {
85 
86  grabber = info[0].Grabber;
87 
88  int camera_id = -1;
89  if (autodetect) {
90  // Take the first one detected
91  camera_id = info[0].CameraType;
92  } else {
93  // Find given camera
94  LvCameraInfo cam_info;
95  for (int i = 0; DsyEnumCameraType(i, &cam_info) == I_NoError; ++i) {
96  if ( strcmp(camera_name, cam_info.Name) == 0 ) {
97  // We found the camera
98  camera_id = cam_info.Id;
99  break;
100  }
101  }
102  if (camera_id == -1) {
103  // throw Exception("LeutronCam: Could not find the requested camera. Trying default.");
104  camera_id = info[0].CameraType;
105  }
106  }
107 
108  if (grabber == NULL) {
109  throw Exception("LeutronCam: grabber == NULL");
110  }
111  grabber_handle = info[0].hGrabber;
112  if (grabber_handle == HANDLE_INVALID) {
113  throw Exception("LeutronCam: grabber handle is invalid.");
114  }
115  if (info[0].hConn == HANDLE_INVALID) {
116  throw Exception("LeutronCam: connection handle is invalid.");
117  }
118  camera_handle = grabber->ConnectCamera(camera_id,
119  info[0].hConn,
120  info[0].SyncNr);
121  if ( camera_handle == HANDLE_INVALID ) {
122  throw Exception("LeutronCam: Could not connect the camera");
123  }
124 
125  char tmp[128];
126  camera = grabber->GetCameraPtr(camera_handle);
127  camera->GetDescription(tmp, sizeof(tmp));
128  // cout << "LeutronCam: Camera '" << tmp << "' is connected to '" << grabber->GetName() << "'" << endl;
129 
130  // No effect: GlobalUnlock(conn_info_handle);
131  GlobalFree(conn_info_handle);
132 
133  if (grabber->ActivateCamera( camera_handle ) != DSY_I_NoError) {
134  throw Exception("LeutronCam: Could not activate camera");
135  }
136 
137  LvSourceInfo src_info;
138  camera->GetSourceROI(&src_info);
139  src_info.StartX = 0;
140  src_info.StartY = 0;
141 
142  if (camera->SetSourceROI( &src_info ) != DSY_I_NoError) {
143  // cout << "LeutronCam: WARNING. Cannot set source info ROI" << endl;
144  }
145 
146  width = src_info.MaxWidth;
147  height = src_info.MaxHeight;
148  scaled_width = width;
149  scaled_height = height;
150  /*
151  cout << "LeutronCam: Width: " << width << " Height: " << height << endl;
152  cout << "LeutronCam: Scaled Width: " << scaled_width
153  << " Scaled Height: " << scaled_height << endl;
154  */
155 
156  if ( (scaled_width != width) || (scaled_height != height) ) {
157  // scaled image
158  scaled_buffer = (unsigned char*)malloc(colorspace_buffer_size(YUV422_PACKED, scaled_width, scaled_height));
159  }
160 
161  LvROI roi;
162  grabber->GetConnectionInfo( camera_handle, &roi );
163  roi.SetTargetBuffer( TgtBuffer_CPU );
164  roi.SetDIBMode( TRUE );
165  if (cspace != YUV422_PACKED) {
166  // cout << "LeutronCam: WARNING! Cannot capture in requested color space, defaulting to packed YUV422" << endl;
167  }
168  roi.SetColorFormat( ColF_YUV_422 );
169  roi.SetStartPosition( 0, 0 );
170  roi.SetDimension( scaled_width, scaled_height );
171  roi.SetMemoryWidth( width );
172 
173  /*
174  cout << "LeutronCam(Memory Info): PixelIncrement: " << roi.GetPixelIncrement() << endl
175  << "LeutronCam(Memory Info): LineIncrement: " << roi.GetLineIncrement() << endl;
176  printf( "LeutronCam(Memory Info): BaseAddress: %x\n", (unsigned int)roi.MemoryInfo.BaseAddress);
177  printf( "LeutronCam(Memory Info): StartAddress %x\n", (unsigned int)roi.StartAddress);
178  */
179 
180  if (grabber->ActivateROI(camera_handle, &roi) != DSY_I_NoError) {
181  throw Exception("LeutronCam: Cannot activate ROI");
182  }
183 
184  camera->Live( SY_None );
185 
186  } else {
187  throw Exception("LeutronCam: Could not get lock on connection info.");
188  }
189  opened = true;
190  } else {
191  throw Exception("LeutronCam: Could not find any camera.");
192  }
193 
194 }
195 
196 
197 void
198 LeutronCamera::start()
199 {
200  if ( started ) return;
201  if (!opened) {
202  throw Exception("LeutronCam: Trying to start closed cam!");
203  }
204 
205  started = true;
206 }
207 
208 
209 void
210 LeutronCamera::stop()
211 {
212  started = false;
213 }
214 
215 void
216 LeutronCamera::print_info()
217 {
218 }
219 
220 void
221 LeutronCamera::capture()
222 {
223 }
224 
225 void
226 LeutronCamera::flush()
227 {
228 }
229 
230 unsigned char*
231 LeutronCamera::buffer()
232 {
233  LvROI roi;
234  grabber->GetConnectionInfo(camera_handle, &roi);
235 
236  if ( (scaled_width != width) || (scaled_height != height) ) {
237  unsigned char *r, *buf;
238  r = (unsigned char*)roi.MemoryInfo.BaseAddress+roi.StartAddress;
239  buf = scaled_buffer;
240  for (unsigned int i = 0; i < height; ++i) {
241  memcpy(buf, r, roi.GetPixelIncrement() * scaled_width);
242  buf += roi.GetPixelIncrement() * scaled_width;
243  r += roi.GetLineIncrement();
244  }
245  return scaled_buffer;
246  } else {
247  return (unsigned char*) roi.MemoryInfo.BaseAddress+roi.StartAddress;
248  }
249 
250 }
251 
252 unsigned int
253 LeutronCamera::buffer_size()
254 {
255  return colorspace_buffer_size(YUV422_PACKED, 0, 0);
256 }
257 
258 void
259 LeutronCamera::close()
260 {
261  if (opened) {
262  if ( (scaled_width != width) || (scaled_height != height) ) {
263  free(scaled_buffer);
264  }
265  }
266  //cout << "LeutronCam: Calling DsyClose().." << std::flush;
267  DsyClose();
268  //cout << "done" << endl;
269 }
270 
271 void
272 LeutronCamera::dispose_buffer()
273 {
274 }
275 
276 unsigned int
277 LeutronCamera::pixel_width()
278 {
279  if (opened) {
280  return scaled_width;
281  } else {
282  throw Exception("LeutronCam: Camera not opened");
283  }
284 }
285 
286 unsigned int
287 LeutronCamera::pixel_height()
288 {
289  if (opened) {
290  return scaled_height;
291  } else {
292  throw Exception("LeutronCam: Camera not opened");
293  }
294 }
295 
296 
297 colorspace_t
298 LeutronCamera::colorspace()
299 {
300  return cspace;
301 }
302 
303 
304 bool
305 LeutronCamera::ready()
306 {
307  return started;
308 }
309 
310 
311 void
312 LeutronCamera::set_image_number(unsigned int n)
313 {
314 }
315 
316 } // end namespace firevision
Fawkes library namespace.
Base class for exceptions in Fawkes.
Definition: exception.h:36