Fawkes API  Fawkes Development Version
leutron.cpp
00001 
00002 /***************************************************************************
00003  *  leutron.cpp - Leutron camera
00004  *
00005  *  Generated: Thu Mar 24 22:36:05 2005
00006  *  Copyright  2005  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
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 /* just to make Emacs auto-indent happy */
00038 }
00039 #endif
00040 
00041 /** @class LeutronCamera <cams/leutron.h>
00042  * Cameras accessed through Leutron framegrabber.
00043  */
00044 
00045 /** Constructor. */
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 /** Destructor. */
00058 LeutronCamera::~LeutronCamera()
00059 {
00060 }
00061 
00062 
00063 void
00064 LeutronCamera::open()
00065 {
00066   opened = false;
00067   camera_handle = HANDLE_INVALID;
00068 
00069   // cout << "LeutronCam: Calling DsyInit(). This may take a while..." << std::flush;
00070   DsyInit();
00071   // cout << "done" << endl;
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     // We found at least one camera
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         // Take the first one detected
00091         camera_id = info[0].CameraType;
00092       } else {
00093         // Find given camera
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             // We found the camera
00098             camera_id = cam_info.Id;
00099             break;
00100           }
00101         }
00102         if (camera_id == -1) {
00103           // throw Exception("LeutronCam: Could not find the requested camera. Trying default.");
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       // cout << "LeutronCam: Camera '" << tmp << "' is connected to '" << grabber->GetName() << "'" << endl;
00129 
00130       // No effect: GlobalUnlock(conn_info_handle);
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         // cout << "LeutronCam: WARNING. Cannot set source info ROI" << endl;
00144       }
00145 
00146       width  = src_info.MaxWidth;
00147       height = src_info.MaxHeight;
00148       scaled_width = width;
00149       scaled_height = height;
00150       /*
00151       cout << "LeutronCam:  Width:        " << width << "   Height:        " << height << endl;
00152       cout << "LeutronCam:  Scaled Width: " << scaled_width
00153            << "   Scaled Height: " << scaled_height << endl;
00154       */
00155 
00156       if ( (scaled_width != width) || (scaled_height != height) ) {
00157         // scaled image
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         // cout << "LeutronCam: WARNING! Cannot capture in requested color space, defaulting to packed  YUV422" << endl;
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       cout << "LeutronCam(Memory Info): PixelIncrement: " << roi.GetPixelIncrement() << endl
00175            << "LeutronCam(Memory Info): LineIncrement:  " << roi.GetLineIncrement()  << endl;
00176       printf( "LeutronCam(Memory Info): BaseAddress:    %x\n", (unsigned int)roi.MemoryInfo.BaseAddress);
00177       printf( "LeutronCam(Memory Info): StartAddress    %x\n", (unsigned int)roi.StartAddress);
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   //cout << "LeutronCam: Calling DsyClose().." << std::flush;
00267   DsyClose();
00268   //cout << "done" << endl;
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 } // end namespace firevision