leutron.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <core/exception.h>
00025
00026 #include <cams/leutron.h>
00027 #include <fvutils/color/colorspaces.h>
00028
00029 #include <lvdef.h>
00030 #include <dsylib.h>
00031 #include <grabber.h>
00032 #include <cstdlib>
00033
00034 using namespace fawkes;
00035
00036 namespace firevision {
00037 #if 0
00038 }
00039 #endif
00040
00041
00042
00043
00044
00045
00046 LeutronCamera::LeutronCamera()
00047 {
00048 started = opened = false;
00049 autodetect = false;
00050
00051 cspace = YUV422_PACKED;
00052
00053 camera_name = "PAL_S_CCIR";
00054 }
00055
00056
00057
00058 LeutronCamera::~LeutronCamera()
00059 {
00060 }
00061
00062
00063 void
00064 LeutronCamera::open()
00065 {
00066 opened = false;
00067 camera_handle = HANDLE_INVALID;
00068
00069
00070 DsyInit();
00071
00072
00073 if (DsyRecheckConnector() != I_NoError) {
00074 throw Exception("LeutronCam: DsyRecheckConnector() failed");
00075 }
00076
00077 LvCameraConnDlgInfo *info;
00078 HANDLE conn_info_handle = NULL;
00079 HGRABBER grabber_handle = HANDLE_INVALID;
00080
00081 if (DsyDetectCamera(&conn_info_handle) > 0) {
00082
00083 info = (LvCameraConnDlgInfo *)GlobalLock(conn_info_handle);
00084 if (info) {
00085
00086 grabber = info[0].Grabber;
00087
00088 int camera_id = -1;
00089 if (autodetect) {
00090
00091 camera_id = info[0].CameraType;
00092 } else {
00093
00094 LvCameraInfo cam_info;
00095 for (int i = 0; DsyEnumCameraType(i, &cam_info) == I_NoError; ++i) {
00096 if ( strcmp(camera_name, cam_info.Name) == 0 ) {
00097
00098 camera_id = cam_info.Id;
00099 break;
00100 }
00101 }
00102 if (camera_id == -1) {
00103
00104 camera_id = info[0].CameraType;
00105 }
00106 }
00107
00108 if (grabber == NULL) {
00109 throw Exception("LeutronCam: grabber == NULL");
00110 }
00111 grabber_handle = info[0].hGrabber;
00112 if (grabber_handle == HANDLE_INVALID) {
00113 throw Exception("LeutronCam: grabber handle is invalid.");
00114 }
00115 if (info[0].hConn == HANDLE_INVALID) {
00116 throw Exception("LeutronCam: connection handle is invalid.");
00117 }
00118 camera_handle = grabber->ConnectCamera(camera_id,
00119 info[0].hConn,
00120 info[0].SyncNr);
00121 if ( camera_handle == HANDLE_INVALID ) {
00122 throw Exception("LeutronCam: Could not connect the camera");
00123 }
00124
00125 char tmp[128];
00126 camera = grabber->GetCameraPtr(camera_handle);
00127 camera->GetDescription(tmp, sizeof(tmp));
00128
00129
00130
00131 GlobalFree(conn_info_handle);
00132
00133 if (grabber->ActivateCamera( camera_handle ) != DSY_I_NoError) {
00134 throw Exception("LeutronCam: Could not activate camera");
00135 }
00136
00137 LvSourceInfo src_info;
00138 camera->GetSourceROI(&src_info);
00139 src_info.StartX = 0;
00140 src_info.StartY = 0;
00141
00142 if (camera->SetSourceROI( &src_info ) != DSY_I_NoError) {
00143
00144 }
00145
00146 width = src_info.MaxWidth;
00147 height = src_info.MaxHeight;
00148 scaled_width = width;
00149 scaled_height = height;
00150
00151
00152
00153
00154
00155
00156 if ( (scaled_width != width) || (scaled_height != height) ) {
00157
00158 scaled_buffer = (unsigned char*)malloc(colorspace_buffer_size(YUV422_PACKED, scaled_width, scaled_height));
00159 }
00160
00161 LvROI roi;
00162 grabber->GetConnectionInfo( camera_handle, &roi );
00163 roi.SetTargetBuffer( TgtBuffer_CPU );
00164 roi.SetDIBMode( TRUE );
00165 if (cspace != YUV422_PACKED) {
00166
00167 }
00168 roi.SetColorFormat( ColF_YUV_422 );
00169 roi.SetStartPosition( 0, 0 );
00170 roi.SetDimension( scaled_width, scaled_height );
00171 roi.SetMemoryWidth( width );
00172
00173
00174
00175
00176
00177
00178
00179
00180 if (grabber->ActivateROI(camera_handle, &roi) != DSY_I_NoError) {
00181 throw Exception("LeutronCam: Cannot activate ROI");
00182 }
00183
00184 camera->Live( SY_None );
00185
00186 } else {
00187 throw Exception("LeutronCam: Could not get lock on connection info.");
00188 }
00189 opened = true;
00190 } else {
00191 throw Exception("LeutronCam: Could not find any camera.");
00192 }
00193
00194 }
00195
00196
00197 void
00198 LeutronCamera::start()
00199 {
00200 if ( started ) return;
00201 if (!opened) {
00202 throw Exception("LeutronCam: Trying to start closed cam!");
00203 }
00204
00205 started = true;
00206 }
00207
00208
00209 void
00210 LeutronCamera::stop()
00211 {
00212 started = false;
00213 }
00214
00215 void
00216 LeutronCamera::print_info()
00217 {
00218 }
00219
00220 void
00221 LeutronCamera::capture()
00222 {
00223 }
00224
00225 void
00226 LeutronCamera::flush()
00227 {
00228 }
00229
00230 unsigned char*
00231 LeutronCamera::buffer()
00232 {
00233 LvROI roi;
00234 grabber->GetConnectionInfo(camera_handle, &roi);
00235
00236 if ( (scaled_width != width) || (scaled_height != height) ) {
00237 unsigned char *r, *buf;
00238 r = (unsigned char*)roi.MemoryInfo.BaseAddress+roi.StartAddress;
00239 buf = scaled_buffer;
00240 for (unsigned int i = 0; i < height; ++i) {
00241 memcpy(buf, r, roi.GetPixelIncrement() * scaled_width);
00242 buf += roi.GetPixelIncrement() * scaled_width;
00243 r += roi.GetLineIncrement();
00244 }
00245 return scaled_buffer;
00246 } else {
00247 return (unsigned char*) roi.MemoryInfo.BaseAddress+roi.StartAddress;
00248 }
00249
00250 }
00251
00252 unsigned int
00253 LeutronCamera::buffer_size()
00254 {
00255 return colorspace_buffer_size(YUV422_PACKED, 0, 0);
00256 }
00257
00258 void
00259 LeutronCamera::close()
00260 {
00261 if (opened) {
00262 if ( (scaled_width != width) || (scaled_height != height) ) {
00263 free(scaled_buffer);
00264 }
00265 }
00266
00267 DsyClose();
00268
00269 }
00270
00271 void
00272 LeutronCamera::dispose_buffer()
00273 {
00274 }
00275
00276 unsigned int
00277 LeutronCamera::pixel_width()
00278 {
00279 if (opened) {
00280 return scaled_width;
00281 } else {
00282 throw Exception("LeutronCam: Camera not opened");
00283 }
00284 }
00285
00286 unsigned int
00287 LeutronCamera::pixel_height()
00288 {
00289 if (opened) {
00290 return scaled_height;
00291 } else {
00292 throw Exception("LeutronCam: Camera not opened");
00293 }
00294 }
00295
00296
00297 colorspace_t
00298 LeutronCamera::colorspace()
00299 {
00300 return cspace;
00301 }
00302
00303
00304 bool
00305 LeutronCamera::ready()
00306 {
00307 return started;
00308 }
00309
00310
00311 void
00312 LeutronCamera::set_image_number(unsigned int n)
00313 {
00314 }
00315
00316 }