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 #include <fvcams/leutron.h>
26 #include <fvutils/color/colorspaces.h>
27 
28 #include <cstdlib>
29 #include <dsylib.h>
30 #include <grabber.h>
31 #include <lvdef.h>
32 
33 using namespace fawkes;
34 
35 namespace firevision {
36 
37 /** @class LeutronCamera <fvcams/leutron.h>
38  * Cameras accessed through Leutron framegrabber.
39  */
40 
41 /** Constructor. */
42 LeutronCamera::LeutronCamera()
43 {
44  started = opened = false;
45  autodetect = false;
46 
47  cspace = YUV422_PACKED;
48 
49  camera_name = "PAL_S_CCIR";
50 }
51 
52 /** Destructor. */
53 LeutronCamera::~LeutronCamera()
54 {
55 }
56 
57 void
58 LeutronCamera::open()
59 {
60  opened = false;
61  camera_handle = HANDLE_INVALID;
62 
63  // cout << "LeutronCam: Calling DsyInit(). This may take a while..." << std::flush;
64  DsyInit();
65  // cout << "done" << endl;
66 
67  if (DsyRecheckConnector() != I_NoError) {
68  throw Exception("LeutronCam: DsyRecheckConnector() failed");
69  }
70 
71  LvCameraConnDlgInfo *info;
72  HANDLE conn_info_handle = NULL;
73  HGRABBER grabber_handle = HANDLE_INVALID;
74 
75  if (DsyDetectCamera(&conn_info_handle) > 0) {
76  // We found at least one camera
77  info = (LvCameraConnDlgInfo *)GlobalLock(conn_info_handle);
78  if (info) {
79  grabber = info[0].Grabber;
80 
81  int camera_id = -1;
82  if (autodetect) {
83  // Take the first one detected
84  camera_id = info[0].CameraType;
85  } else {
86  // Find given camera
87  LvCameraInfo cam_info;
88  for (int i = 0; DsyEnumCameraType(i, &cam_info) == I_NoError; ++i) {
89  if (strcmp(camera_name, cam_info.Name) == 0) {
90  // We found the camera
91  camera_id = cam_info.Id;
92  break;
93  }
94  }
95  if (camera_id == -1) {
96  // throw Exception("LeutronCam: Could not find the requested camera. Trying default.");
97  camera_id = info[0].CameraType;
98  }
99  }
100 
101  if (grabber == NULL) {
102  throw Exception("LeutronCam: grabber == NULL");
103  }
104  grabber_handle = info[0].hGrabber;
105  if (grabber_handle == HANDLE_INVALID) {
106  throw Exception("LeutronCam: grabber handle is invalid.");
107  }
108  if (info[0].hConn == HANDLE_INVALID) {
109  throw Exception("LeutronCam: connection handle is invalid.");
110  }
111  camera_handle = grabber->ConnectCamera(camera_id, info[0].hConn, info[0].SyncNr);
112  if (camera_handle == HANDLE_INVALID) {
113  throw Exception("LeutronCam: Could not connect the camera");
114  }
115 
116  char tmp[128];
117  camera = grabber->GetCameraPtr(camera_handle);
118  camera->GetDescription(tmp, sizeof(tmp));
119  // cout << "LeutronCam: Camera '" << tmp << "' is connected to '" << grabber->GetName() << "'" << endl;
120 
121  // No effect: GlobalUnlock(conn_info_handle);
122  GlobalFree(conn_info_handle);
123 
124  if (grabber->ActivateCamera(camera_handle) != DSY_I_NoError) {
125  throw Exception("LeutronCam: Could not activate camera");
126  }
127 
128  LvSourceInfo src_info;
129  camera->GetSourceROI(&src_info);
130  src_info.StartX = 0;
131  src_info.StartY = 0;
132 
133  if (camera->SetSourceROI(&src_info) != DSY_I_NoError) {
134  // cout << "LeutronCam: WARNING. Cannot set source info ROI" << endl;
135  }
136 
137  width = src_info.MaxWidth;
138  height = src_info.MaxHeight;
139  scaled_width = width;
140  scaled_height = height;
141  /*
142  cout << "LeutronCam: Width: " << width << " Height: " << height << endl;
143  cout << "LeutronCam: Scaled Width: " << scaled_width
144  << " Scaled Height: " << scaled_height << endl;
145  */
146 
147  if ((scaled_width != width) || (scaled_height != height)) {
148  // scaled image
149  scaled_buffer = (unsigned char *)malloc(
150  colorspace_buffer_size(YUV422_PACKED, scaled_width, scaled_height));
151  }
152 
153  LvROI roi;
154  grabber->GetConnectionInfo(camera_handle, &roi);
155  roi.SetTargetBuffer(TgtBuffer_CPU);
156  roi.SetDIBMode(TRUE);
157  if (cspace != YUV422_PACKED) {
158  // cout << "LeutronCam: WARNING! Cannot capture in requested color space, defaulting to packed YUV422" << endl;
159  }
160  roi.SetColorFormat(ColF_YUV_422);
161  roi.SetStartPosition(0, 0);
162  roi.SetDimension(scaled_width, scaled_height);
163  roi.SetMemoryWidth(width);
164 
165  /*
166  cout << "LeutronCam(Memory Info): PixelIncrement: " << roi.GetPixelIncrement() << endl
167  << "LeutronCam(Memory Info): LineIncrement: " << roi.GetLineIncrement() << endl;
168  printf( "LeutronCam(Memory Info): BaseAddress: %x\n", (unsigned int)roi.MemoryInfo.BaseAddress);
169  printf( "LeutronCam(Memory Info): StartAddress %x\n", (unsigned int)roi.StartAddress);
170  */
171 
172  if (grabber->ActivateROI(camera_handle, &roi) != DSY_I_NoError) {
173  throw Exception("LeutronCam: Cannot activate ROI");
174  }
175 
176  camera->Live(SY_None);
177 
178  } else {
179  throw Exception("LeutronCam: Could not get lock on connection info.");
180  }
181  opened = true;
182  } else {
183  throw Exception("LeutronCam: Could not find any camera.");
184  }
185 }
186 
187 void
188 LeutronCamera::start()
189 {
190  if (started)
191  return;
192  if (!opened) {
193  throw Exception("LeutronCam: Trying to start closed cam!");
194  }
195 
196  started = true;
197 }
198 
199 void
200 LeutronCamera::stop()
201 {
202  started = false;
203 }
204 
205 void
206 LeutronCamera::print_info()
207 {
208 }
209 
210 void
211 LeutronCamera::capture()
212 {
213 }
214 
215 void
216 LeutronCamera::flush()
217 {
218 }
219 
220 unsigned char *
221 LeutronCamera::buffer()
222 {
223  LvROI roi;
224  grabber->GetConnectionInfo(camera_handle, &roi);
225 
226  if ((scaled_width != width) || (scaled_height != height)) {
227  unsigned char *r, *buf;
228  r = (unsigned char *)roi.MemoryInfo.BaseAddress + roi.StartAddress;
229  buf = scaled_buffer;
230  for (unsigned int i = 0; i < height; ++i) {
231  memcpy(buf, r, roi.GetPixelIncrement() * scaled_width);
232  buf += roi.GetPixelIncrement() * scaled_width;
233  r += roi.GetLineIncrement();
234  }
235  return scaled_buffer;
236  } else {
237  return (unsigned char *)roi.MemoryInfo.BaseAddress + roi.StartAddress;
238  }
239 }
240 
241 unsigned int
242 LeutronCamera::buffer_size()
243 {
244  return colorspace_buffer_size(YUV422_PACKED, 0, 0);
245 }
246 
247 void
248 LeutronCamera::close()
249 {
250  if (opened) {
251  if ((scaled_width != width) || (scaled_height != height)) {
252  free(scaled_buffer);
253  }
254  }
255  //cout << "LeutronCam: Calling DsyClose().." << std::flush;
256  DsyClose();
257  //cout << "done" << endl;
258 }
259 
260 void
261 LeutronCamera::dispose_buffer()
262 {
263 }
264 
265 unsigned int
266 LeutronCamera::pixel_width()
267 {
268  if (opened) {
269  return scaled_width;
270  } else {
271  throw Exception("LeutronCam: Camera not opened");
272  }
273 }
274 
275 unsigned int
276 LeutronCamera::pixel_height()
277 {
278  if (opened) {
279  return scaled_height;
280  } else {
281  throw Exception("LeutronCam: Camera not opened");
282  }
283 }
284 
285 colorspace_t
286 LeutronCamera::colorspace()
287 {
288  return cspace;
289 }
290 
291 bool
292 LeutronCamera::ready()
293 {
294  return started;
295 }
296 
297 void
298 LeutronCamera::set_image_number(unsigned int n)
299 {
300 }
301 
302 } // end namespace firevision
fawkes
fawkes::Exception
Definition: exception.h:41