contrib/brl/vvid/cmu_1394_camera.cxx
Go to the documentation of this file.
00001 #include "cmu_1394_camera.h"
00002 //:
00003 // \file
00004 #include <vpro/vpro_capture_process.h>
00005 
00006 cmu_1394_camera::cmu_1394_camera()
00007   : cmu_1394_camera_params()
00008 {
00009  int node = 0;
00010  if (!init(node))
00011    link_failed_ = true;
00012    running_ = false;
00013    image_valid_ = false;
00014    current_ = -1;
00015 }
00016 
00017 cmu_1394_camera::~cmu_1394_camera()
00018 {}
00019 
00020 cmu_1394_camera::cmu_1394_camera(int node, const cmu_1394_camera_params& cp)
00021   : cmu_1394_camera_params(cp)
00022 {
00023   if (!init(node))
00024     link_failed_ = true;
00025 
00026   //redundant?
00027   this->update_settings();
00028   running_ = false;
00029   image_valid_ = false;
00030    current_ = -1;
00031 }
00032 
00033 void cmu_1394_camera::print_control(C1394CameraControl const& c)
00034 {
00035   vcl_cout << "Feature Information\n";
00036 
00037   if (c.m_present)
00038     vcl_cout << "Feature Present true\n";
00039   else
00040     vcl_cout << "Feature Present false\n";
00041 
00042   if (c.m_onePush)
00043     vcl_cout << "One Push true\n";
00044   else
00045     vcl_cout << "One Push false\n";
00046 
00047   if (c.m_readout)
00048     vcl_cout << "Readout true\n";
00049   else
00050     vcl_cout << "Readout false\n";
00051 
00052   if (c.m_onoff)
00053     vcl_cout << "On-Off  true\n";
00054   else
00055     vcl_cout << "On-Off false\n";
00056 
00057   if (c.m_auto)
00058     vcl_cout << "Auto  true\n";
00059   else
00060     vcl_cout << "Auto false\n";
00061 
00062   if (c.m_manual)
00063     vcl_cout << "Manual  true\n";
00064   else
00065     vcl_cout << "Manual false\n";
00066 
00067   vcl_cout <<"Min Value " << c.m_min
00068            <<"Max Value " << c.m_max;
00069 }
00070 
00071 void cmu_1394_camera::print_status(C1394CameraControl const& c)
00072 {
00073   vcl_cout << " Status\n";
00074 
00075   if (c.m_statusOnePush)
00076     vcl_cout << "OnePush  true | ";
00077   else
00078     vcl_cout << "OnePush false | ";
00079 
00080   if (c.m_statusOnOff)
00081     vcl_cout << "On-Off  true | ";
00082   else
00083     vcl_cout << "On-Off false | ";
00084 
00085   if (c.m_statusAutoManual)
00086     vcl_cout << "AutoMan  true\n";
00087   else
00088     vcl_cout << "AutoMan false\n";
00089 
00090   vcl_cout << "Value 1 " << c.m_value1 << '\n'
00091            << "Value 2 " << c.m_value2 << '\n';
00092 }
00093 
00094 //----------------------------------------------------------------
00095 // : update video configuration to current setting
00096 //
00097 void cmu_1394_camera::update_video_configuration()
00098 {
00099   if (current_<0||!format_.size())
00100     return;
00101   video_format_ = format_[current_];
00102   video_mode_ = mode_[current_];
00103   frame_rate_ = rate_[current_];
00104 }
00105 
00106 //----------------------------------------------------------------------
00107 //: is the camera resolution, frame rate and color sampling setting valid?
00108 //  if so set the current index to point to the setting
00109 //  if the suggested configuration is invalid then keep the original
00110 //  setting.  If this is the first validation then set the index to 0,
00111 //  i.e., the first valid configuration
00112 //
00113 void cmu_1394_camera::validate_default_configuration()
00114 {
00115   int n = format_.size();
00116   if (!n)
00117   {
00118     vcl_cout << "In cmu_1394_camera::validate_configuration() -"
00119              << " no capabilities (shouldn't happen)\n";
00120     return;
00121   }
00122   bool valid = false;
00123   for (int i = 0; i<n&&!valid; i++)
00124   {
00125     if (video_format_==format_[i]&&
00126         video_mode_ == mode_[i]&&
00127         frame_rate_ == rate_[i])
00128     {
00129       current_ = i;
00130       valid = true;
00131     }
00132   }
00133   if (valid)
00134     return;
00135   if (current_<0)
00136     current_ = 0;
00137 }
00138 
00139 void cmu_1394_camera::update_settings()
00140 {
00141   if (!camera_present_)
00142     return;
00143   this->update_video_configuration();
00144   cmu_1394_camera_params::constrain();
00145   //Shutter control
00146   if (!auto_exposure_)
00147   {
00148     C1394Camera::m_controlAutoExposure.TurnOn(false);
00149     C1394Camera::m_controlShutter.TurnOn(true);
00150   }
00151   else
00152   {
00153     C1394Camera::m_controlAutoExposure.TurnOn(true);
00154     C1394Camera::m_controlShutter.TurnOn(false);
00155   }
00156   if (!autowhitebalance_)
00157   {
00158     C1394Camera::m_controlWhiteBalance.SetAutoMode(false);
00159     C1394Camera::SetWhiteBalance(whitebalanceU_, whitebalanceV_);
00160   }
00161   else
00162   {
00163     C1394Camera::m_controlWhiteBalance.SetAutoMode(true);
00164   }
00165   if (onepushWBbalance_)
00166   {
00167     C1394Camera::m_controlWhiteBalance.SetAutoMode(false);
00168     C1394Camera::m_controlWhiteBalance.SetOnePush();
00169   }
00170   if (!auto_gain_)
00171   {
00172     C1394Camera::m_controlGain.SetAutoMode(false);
00173     C1394Camera::m_controlGain.TurnOn(true);
00174   }
00175   C1394Camera::SetVideoFormat(video_format_);
00176   C1394Camera::SetVideoMode(video_mode_);
00177   C1394Camera::SetVideoFrameRate(frame_rate_);
00178   C1394Camera::SetShutter(shutter_);
00179   C1394Camera::SetBrightness(brightness_);
00180   if (auto_exposure_)
00181     C1394Camera::SetAutoExposure(exposure_);
00182   if (!auto_gain_)
00183     C1394Camera::SetGain(gain_);
00184 }
00185 
00186 //-----------------------------------------------------------------
00187 //: find the standard 1394 capabilities of this camera
00188 //
00189 void cmu_1394_camera::init_capabilities()
00190 {
00191   format_.clear();
00192   mode_.clear();
00193   rate_.clear();
00194   capability_desc_.clear();
00195   for (int i = 0; i<3; i++)
00196     for (int j = 0; j<6; j++)
00197       for (int k = 0; k<6; k++)
00198         if ((*this).m_videoFlags[i][j][k])
00199         {
00200           vcl_string temp = this->video_configuration(i,j);
00201           temp += " Fr/Sec(";
00202           temp += this->frame_rate(k);
00203           temp += ")";
00204           capability_desc_.push_back(temp);
00205           format_.push_back(i);
00206           mode_.push_back(j);
00207           rate_.push_back(k);
00208         }
00209 }
00210 
00211 //-----------------------------------------------------------------
00212 //: Establish the control variables for the camera
00213 //
00214 void cmu_1394_camera::init_control()
00215 {
00216   //get the values from the camera
00217   C1394Camera::InquireControlRegisters();
00218   //Electronic shutter control
00219   manual_shutter_control_ = C1394Camera::m_controlShutter.m_present;
00220   min_shutter_ = C1394Camera::m_controlShutter.m_min;
00221   max_shutter_ = C1394Camera::m_controlShutter.m_max;
00222   shutter_ = int((max_shutter_-min_shutter_)/2.0) + min_shutter_;
00223   //Gain control
00224   min_gain_ = C1394Camera::m_controlGain.m_min;
00225   max_gain_ = C1394Camera::m_controlGain.m_max;
00226   gain_ = int((max_gain_-min_gain_)/2.0) + min_gain_;
00227 
00228   //Brightness control (make this minimum by default since we don't usually
00229   // want an offset);
00230   min_brightness_ = C1394Camera::m_controlBrightness.m_min;
00231   max_brightness_ = C1394Camera::m_controlBrightness.m_max;
00232   brightness_ = min_brightness_;
00233 
00234   min_WB_=C1394Camera::m_controlWhiteBalance.m_min;
00235   max_WB_=C1394Camera::m_controlWhiteBalance.m_max;
00236   //Auto exposure
00237   auto_exposure_control_ = C1394Camera::m_controlAutoExposure.m_present;
00238   min_exposure_ = C1394Camera::m_controlAutoExposure.m_min;
00239   max_exposure_ = C1394Camera::m_controlAutoExposure.m_max;
00240   exposure_ = int((max_exposure_-min_exposure_)/2.0) + min_exposure_;
00241 }
00242 
00243 bool cmu_1394_camera::init(int node)
00244 {
00245   if (C1394Camera::CheckLink())
00246   {
00247     vcl_cout << "In cmu_1394_camera::init() - link bad\n";
00248     camera_present_ = false;
00249     return false;
00250   }
00251   C1394Camera::SelectCamera(node);
00252   C1394Camera::InitCamera();
00253   this->init_capabilities();
00254   this->init_control();
00255   this->validate_default_configuration();
00256   this->update_settings();
00257   camera_present_ = true;
00258   image_valid_ = false;
00259   vp_ = 0;
00260   file_capture_ = false;
00261   capture_ = false;
00262   return true;
00263 }
00264 
00265 bool cmu_1394_camera::start()
00266 {
00267   if (!camera_present_)
00268   {
00269     vcl_cout << "In cmu_1394_camera::start() -- no camera link\n";
00270     return false;
00271   }
00272 
00273  if (!running_)
00274  {
00275    if (capture_)
00276    {
00277      if (C1394Camera::StartImageCapture())
00278      {
00279        vcl_cout<< "In cmu_1394_camera::start() - Problem Starting Capture\n";
00280        return false;
00281      }
00282    }
00283    else
00284     if (C1394Camera::StartImageAcquisition())
00285     {
00286       vcl_cout<< "In cmu_1394_camera::start() - Problem Starting Aquisition\n";
00287       return false;
00288     }
00289    if (this->get_frame())
00290      image_valid_ = true;
00291    running_ = true;
00292    return true;
00293  }
00294  return true;
00295 }
00296 
00297 void cmu_1394_camera::stop()
00298 {
00299   if (!camera_present_)
00300   {
00301     vcl_cout << "In cmu_1394_camera::stop() -- no camera link\n";
00302     return;
00303   }
00304 
00305   if (!running_)
00306   {
00307     vcl_cout << "In cmu_1394_camera::stop() -- already stopped\n";
00308     return;
00309   }
00310   if (capture_)
00311     C1394Camera::StopImageCapture();
00312   else
00313     C1394Camera::StopImageAcquisition();
00314   running_ = false;
00315   image_valid_ = false;
00316 }
00317 
00318 bool cmu_1394_camera::get_frame()
00319 {
00320   if (!camera_present_)
00321   {
00322     vcl_cout << "In cmu_1394_camera::get_frame() -- no camera link\n";
00323     return false;
00324   }
00325   if (running_)
00326   {
00327     if (capture_)
00328       C1394Camera::CaptureImage();
00329     else
00330       C1394Camera::AcquireImage();
00331     return true;
00332   }
00333   else
00334     return false;
00335 }
00336 
00337 void cmu_1394_camera::start_capture(vcl_string const & video_file_name)
00338 {
00339   vp_ = new vpro_capture_process(video_file_name);
00340   file_capture_ = true;
00341 }
00342 
00343 bool
00344 cmu_1394_camera::get_monochrome_image(vil1_memory_image_of<unsigned char>& im,
00345                                       int pixel_sample_interval, bool reread)
00346 {
00347   int xsize = C1394Camera::m_width/pixel_sample_interval,
00348       ysize = C1394Camera::m_height/pixel_sample_interval;
00349 
00350   //potentially reread the frame
00351   if (reread||!image_valid_)
00352     image_valid_ = get_frame();
00353 
00354   if (image_valid_&&running_)
00355   {
00356     im.resize(xsize, ysize);
00357     unsigned char *p;
00358     int size = C1394Camera::m_width*C1394Camera::m_height;
00359     p = im.get_buffer();
00360     int offset = 0, yoffset, ooffset, oyoffset =0;
00361     for (int y=0, oy=0; y<C1394Camera::m_height;
00362          y+=pixel_sample_interval, oy++)
00363     {
00364       yoffset = C1394Camera::m_width*y;
00365       oyoffset = xsize*oy;
00366       for (int x = 0, ox=0; x<C1394Camera::m_width;
00367            x+=pixel_sample_interval, ox++)
00368       {
00369         offset = x+yoffset;
00370         ooffset = ox+ oyoffset;
00371         *(p + ooffset) = *(C1394Camera::m_pData + offset);
00372       }
00373     }
00374     return true;
00375   }
00376   else {
00377     vcl_cout << " cmu_1394_camera:get_monocrome_image -> couldn't get frame\n";
00378     return false;
00379   }
00380 }
00381 
00382 bool cmu_1394_camera::
00383 get_rgb_image(vil1_memory_image_of< vil1_rgb<unsigned char> >& im,
00384               int pixel_sample_interval, bool reread)
00385 {
00386   int xsize = C1394Camera::m_width/pixel_sample_interval,
00387     ysize = C1394Camera::m_height/pixel_sample_interval;
00388   vil1_memory_image_of< vil1_rgb<unsigned char> > temp;
00389 
00390   //potentially reread the frame
00391   if (reread||!image_valid_)
00392     image_valid_ = get_frame();
00393 
00394   if (image_valid_&&running_)
00395   {
00396     temp.resize(C1394Camera::m_width, C1394Camera::m_height);
00397     C1394Camera::getRGB((unsigned char*)temp.get_buffer());
00398     //cache the frame for live video capture
00399     if (file_capture_)
00400     {
00401       vp_->clear_input();
00402       vp_->add_input_image(temp);
00403       if (!vp_->execute())
00404       {
00405         vcl_cout << "In cmu_1394_camera::get_rbg_image(..) - capture failed\n";
00406         file_capture_ = false;
00407       }
00408     }
00409     im.resize(xsize, ysize);
00410     for (int y=0, yi=0; y<C1394Camera::m_height; y+=pixel_sample_interval, yi++)
00411         for (int x=0, xi=0; x<C1394Camera::m_width;  x+=pixel_sample_interval, xi++)
00412         im(xi, yi) = temp(x,y);
00413     return true;
00414   }
00415   else {
00416     vcl_cout << " cmu_1394_camera:get_rgb_image -> couldn't get frame\n";
00417     return false;
00418   }
00419 }
00420 
00421 bool cmu_1394_camera::stop_capture()
00422 {
00423   if (!file_capture_||!vp_)
00424     return false;
00425   file_capture_ = false;
00426   return vp_->finish();
00427 }
00428 
00429 vcl_ostream& operator << (vcl_ostream& os, const cmu_1394_camera& cp)
00430 {
00431   os << "camera_vendor: " << (char*)cp.m_nameVendor << '\n'
00432      << "camera_model: " << (char*)cp.m_nameModel << '\n'
00433      << "width: " << cp.m_width << '\n'
00434      << "height " << cp.m_height << '\n'
00435      << "supported capabilities\n";
00436   for (vcl_vector<vcl_string>::const_iterator cit = cp.capability_desc_.begin();
00437        cit != cp.capability_desc_.end(); cit++)
00438     os << "  " << (*cit) << '\n';
00439   os << "link_checked: " << cp.m_linkChecked << '\n'
00440      << "camera_initialized: " << cp.m_cameraInitialized << '\n';
00441   return os;
00442 }