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 /** @class LeutronCamera <cams/leutron.h>
00037  * Cameras accessed through Leutron framegrabber.
00038  */
00039 
00040 /** Constructor. */
00041 LeutronCamera::LeutronCamera()
00042 {
00043   started = opened = false;
00044   autodetect = false;
00045
00046   cspace = YUV422_PACKED;
00047
00048   camera_name = "PAL_S_CCIR";
00049 }
00050
00051 
00052 /** Destructor. */
00053 LeutronCamera::~LeutronCamera()
00054 {
00055 }
00056
00057
00058 void
00059 LeutronCamera::open()
00060 {
00061   opened = false;
00062   camera_handle = HANDLE_INVALID;
00063
00064   // cout << "LeutronCam: Calling DsyInit(). This may take a while..." << std::flush;
00065   DsyInit();
00066   // cout << "done" << endl;
00067
00068   if (DsyRecheckConnector() != I_NoError) {
00069     throw Exception("LeutronCam: DsyRecheckConnector() failed");
00070   }
00071
00072   LvCameraConnDlgInfo *info;
00073   HANDLE conn_info_handle = NULL;
00074   HGRABBER grabber_handle = HANDLE_INVALID;
00075
00076   if (DsyDetectCamera(&conn_info_handle) > 0) {
00077     // We found at least one camera
00078     info = (LvCameraConnDlgInfo *)GlobalLock(conn_info_handle);
00079     if (info) {
00080
00081       grabber = info[0].Grabber;
00082
00083       int camera_id = -1;
00084       if (autodetect) {
00085         // Take the first one detected
00086         camera_id = info[0].CameraType;
00087       } else {
00088         // Find given camera
00089         LvCameraInfo cam_info;
00090         for (int i = 0; DsyEnumCameraType(i, &cam_info) == I_NoError; ++i) {
00091           if ( strcmp(camera_name, cam_info.Name) == 0 ) {
00092             // We found the camera
00093             camera_id = cam_info.Id;
00094             break;
00095           }
00096         }
00097         if (camera_id == -1) {
00098           // throw Exception("LeutronCam: Could not find the requested camera. Trying default.");
00099           camera_id = info[0].CameraType;
00100         }
00101       }
00102
00103       if (grabber == NULL) {
00104         throw Exception("LeutronCam: grabber == NULL");
00105       }
00106       grabber_handle = info[0].hGrabber;
00107       if (grabber_handle == HANDLE_INVALID) {
00108         throw Exception("LeutronCam: grabber handle is invalid.");
00109       }
00110       if (info[0].hConn == HANDLE_INVALID) {
00111         throw Exception("LeutronCam: connection handle is invalid.");
00112       }
00113       camera_handle = grabber->ConnectCamera(camera_id,
00114                                              info[0].hConn,
00115                                              info[0].SyncNr);
00116       if ( camera_handle == HANDLE_INVALID ) {
00117         throw Exception("LeutronCam: Could not connect the camera");
00118       }
00119
00120       char tmp[128];
00121       camera = grabber->GetCameraPtr(camera_handle);
00122       camera->GetDescription(tmp, sizeof(tmp));
00123       // cout << "LeutronCam: Camera '" << tmp << "' is connected to '" << grabber->GetName() << "'" << endl;
00124
00125       // No effect: GlobalUnlock(conn_info_handle);
00126       GlobalFree(conn_info_handle);
00127
00128       if (grabber->ActivateCamera( camera_handle ) != DSY_I_NoError) {
00129         throw Exception("LeutronCam: Could not activate camera");
00130       }
00131
00132       LvSourceInfo src_info;
00133       camera->GetSourceROI(&src_info);
00134       src_info.StartX = 0;
00135       src_info.StartY = 0;
00136
00137       if (camera->SetSourceROI( &src_info ) != DSY_I_NoError) {
00138         // cout << "LeutronCam: WARNING. Cannot set source info ROI" << endl;
00139       }
00140
00141       width  = src_info.MaxWidth;
00142       height = src_info.MaxHeight;
00143       scaled_width = width;
00144       scaled_height = height;
00145       /*
00146       cout << "LeutronCam:  Width:        " << width << "   Height:        " << height << endl;
00147       cout << "LeutronCam:  Scaled Width: " << scaled_width
00148            << "   Scaled Height: " << scaled_height << endl;
00149       */
00150
00151       if ( (scaled_width != width) || (scaled_height != height) ) {
00152         // scaled image
00153         scaled_buffer = (unsigned char*)malloc(colorspace_buffer_size(YUV422_PACKED, scaled_width, scaled_height));
00154       }
00155
00156       LvROI  roi;
00157       grabber->GetConnectionInfo( camera_handle, &roi );
00158       roi.SetTargetBuffer( TgtBuffer_CPU );
00159       roi.SetDIBMode( TRUE );
00160       if (cspace != YUV422_PACKED) {
00161         // cout << "LeutronCam: WARNING! Cannot capture in requested color space, defaulting to packed  YUV422" << endl;
00162       }
00163       roi.SetColorFormat( ColF_YUV_422 );
00164       roi.SetStartPosition( 0, 0 );
00165       roi.SetDimension( scaled_width, scaled_height );
00166       roi.SetMemoryWidth( width );
00167
00168       /*
00169       cout << "LeutronCam(Memory Info): PixelIncrement: " << roi.GetPixelIncrement() << endl
00170            << "LeutronCam(Memory Info): LineIncrement:  " << roi.GetLineIncrement()  << endl;
00171       printf( "LeutronCam(Memory Info): BaseAddress:    %x\n", (unsigned int)roi.MemoryInfo.BaseAddress);
00172       printf( "LeutronCam(Memory Info): StartAddress    %x\n", (unsigned int)roi.StartAddress);
00173       */
00174
00175       if (grabber->ActivateROI(camera_handle, &roi) != DSY_I_NoError) {
00176         throw Exception("LeutronCam: Cannot activate ROI");
00177       }
00178
00179       camera->Live( SY_None );
00180
00181     } else {
00182       throw Exception("LeutronCam: Could not get lock on connection info.");
00183     }
00184     opened = true;
00185   } else {
00186     throw Exception("LeutronCam: Could not find any camera.");
00187   }
00188
00189 }
00190
00191
00192 void
00193 LeutronCamera::start()
00194 {
00195   if ( started ) return;
00196   if (!opened) {
00197     throw Exception("LeutronCam: Trying to start closed cam!");
00198   }
00199
00200   started = true;
00201 }
00202
00203
00204 void
00205 LeutronCamera::stop()
00206 {
00207   started = false;
00208 }
00209
00210 void
00211 LeutronCamera::print_info()
00212 {
00213 }
00214
00215 void
00216 LeutronCamera::capture()
00217 {
00218 }
00219
00220 void
00221 LeutronCamera::flush()
00222 {
00223 }
00224
00225 unsigned char*
00226 LeutronCamera::buffer()
00227 {
00228   LvROI roi;
00229   grabber->GetConnectionInfo(camera_handle, &roi);
00230
00231   if ( (scaled_width != width) || (scaled_height != height) ) {
00232     unsigned char *r, *buf;
00233     r   = (unsigned char*)roi.MemoryInfo.BaseAddress+roi.StartAddress;
00234     buf = scaled_buffer;
00235     for (unsigned int i = 0; i < height; ++i) {
00236       memcpy(buf, r, roi.GetPixelIncrement() * scaled_width);
00237       buf += roi.GetPixelIncrement() * scaled_width;
00238       r   += roi.GetLineIncrement();
00239     }
00240     return scaled_buffer;
00241   } else {
00242     return (unsigned char*) roi.MemoryInfo.BaseAddress+roi.StartAddress;
00243   }
00244
00245 }
00246
00247 unsigned int
00248 LeutronCamera::buffer_size()
00249 {
00250   return colorspace_buffer_size(YUV422_PACKED, 0, 0);
00251 }
00252
00253 void
00254 LeutronCamera::close()
00255 {
00256   if (opened) {
00257     if ( (scaled_width != width) || (scaled_height != height) ) {
00258       free(scaled_buffer);
00259     }
00260   }
00261   //cout << "LeutronCam: Calling DsyClose().." << std::flush;
00262   DsyClose();
00263   //cout << "done" << endl;
00264 }
00265
00266 void
00267 LeutronCamera::dispose_buffer()
00268 {
00269 }
00270
00271 unsigned int
00272 LeutronCamera::pixel_width()
00273 {
00274   if (opened) {
00275     return scaled_width;
00276   } else {
00277     throw Exception("LeutronCam: Camera not opened");
00278   }
00279 }
00280
00281 unsigned int
00282 LeutronCamera::pixel_height()
00283 {
00284   if (opened) {
00285     return scaled_height;
00286   } else {
00287     throw Exception("LeutronCam: Camera not opened");
00288   }
00289 }
00290
00291
00292 colorspace_t
00293 LeutronCamera::colorspace()
00294 {
00295   return cspace;
00296 }
00297
00298
00299 bool
00300 LeutronCamera::ready()
00301 {
00302   return started;
00303 }
00304
00305
00306 void
00307 LeutronCamera::set_image_number(unsigned int n)
00308 {
00309 }