CapturePluginCmu.cpp
Go to the documentation of this file.
00001 /*
00002  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
00003  *
00004  * Copyright 2007-2012 VTT Technical Research Centre of Finland
00005  *
00006  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
00007  *          <http://www.vtt.fi/multimedia/alvar.html>
00008  *
00009  * ALVAR is free software; you can redistribute it and/or modify it under the
00010  * terms of the GNU Lesser General Public License as published by the Free
00011  * Software Foundation; either version 2.1 of the License, or (at your option)
00012  * any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful, but WITHOUT
00015  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00017  * for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with ALVAR; if not, see
00021  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
00022  */
00023 
00024 #include "CapturePluginCmu.h"
00025 
00026 #include <sstream>
00027 
00028 namespace alvar {
00029 namespace plugins {
00030 
00031 CaptureCmu::CaptureCmu(const CaptureDevice captureDevice)
00032     : Capture(captureDevice)
00033     , mCamera(new C1394Camera())
00034     , mChannels(-1)
00035     , mReturnFrame(NULL)
00036 {
00037 }
00038 
00039 CaptureCmu::~CaptureCmu()
00040 {
00041     stop();
00042     delete mCamera;
00043 }
00044 
00045 bool CaptureCmu::start()
00046 {
00047     if (isCapturing()) {
00048         return isCapturing();
00049     }
00050 
00051     if (mCamera->CheckLink() != CAM_SUCCESS) {
00052         return false;
00053     }
00054 
00055     int numberCameras = mCamera->GetNumberCameras();
00056     bool cameraSelected = false;
00057     for (int i = 0; i < numberCameras; i++) {
00058         mCamera->SelectCamera(i);
00059         LARGE_INTEGER uniqueId;
00060         mCamera->GetCameraUniqueID(&uniqueId);
00061         std::stringstream convert;
00062         convert << std::hex << uniqueId.HighPart << uniqueId.LowPart;
00063         if (captureDevice().id().compare(convert.str()) == 0) {
00064             cameraSelected = true;
00065             break;
00066         }
00067     }
00068 
00069     if (!cameraSelected) {
00070         return false;
00071     }
00072 
00073     if (mCamera->InitCamera(true) != CAM_SUCCESS) {
00074         return false;
00075     }
00076 
00077     // TODO: this needs to be parameterized somehow
00078     if (mCamera->HasVideoMode(2, 4)) { // 1600x1200rgb
00079         if (mCamera->SetVideoFormat(2) != CAM_SUCCESS) return false;
00080         if (mCamera->SetVideoMode(4) != CAM_SUCCESS) return false;
00081         mChannels = 3;
00082     }
00083     else if (mCamera->HasVideoMode(2, 1)) { // 1280x960rgb
00084         if (mCamera->SetVideoFormat(2) != CAM_SUCCESS) return false;
00085         if (mCamera->SetVideoMode(1) != CAM_SUCCESS) return false;
00086         mChannels = 3;
00087     }
00088     else if (mCamera->HasVideoMode(1, 4)) { // 1024x768rgb
00089         if (mCamera->SetVideoFormat(1) != CAM_SUCCESS) return false;
00090         if (mCamera->SetVideoMode(4) != CAM_SUCCESS) return false;
00091         mChannels = 3;
00092     }
00093     else if (mCamera->HasVideoMode(1, 1)) { // 800x600rgb
00094         if (mCamera->SetVideoFormat(1) != CAM_SUCCESS) return false;
00095         if (mCamera->SetVideoMode(1) != CAM_SUCCESS) return false;
00096         mChannels = 3;
00097     }
00098     else if (mCamera->HasVideoMode(0, 4)) { // 640x480rgb
00099         if (mCamera->SetVideoFormat(0) != CAM_SUCCESS) return false;
00100         if (mCamera->SetVideoMode(4) != CAM_SUCCESS) return false;
00101         mChannels = 3;
00102     /* // TODO: also support YUV422
00103     }
00104     else if (mCamera->HasVideoMode(2, 3)) { // 1600x1200yuv422
00105         if (mCamera->SetVideoFormat(2) != CAM_SUCCESS) return false;
00106         if (mCamera->SetVideoMode(3) != CAM_SUCCESS) return false;
00107         mChannels = 2;
00108     }
00109     else if (mCamera->HasVideoMode(2, 0)) { // 1280x960yuv422
00110         if (mCamera->SetVideoFormat(2) != CAM_SUCCESS) return false;
00111         if (mCamera->SetVideoMode(0) != CAM_SUCCESS) return false;
00112         mChannels = 2;
00113     }
00114     else if (mCamera->HasVideoMode(1, 3)) { // 1024x768yuv422
00115         if (mCamera->SetVideoFormat(1) != CAM_SUCCESS) return false;
00116         if (mCamera->SetVideoMode(3) != CAM_SUCCESS) return false;
00117         mChannels = 2;
00118     }
00119     else if (mCamera->HasVideoMode(1, 0)) { // 800x600yuv422
00120         if (mCamera->SetVideoFormat(1) != CAM_SUCCESS) return false;
00121         if (mCamera->SetVideoMode(0) != CAM_SUCCESS) return false;
00122         mChannels = 2;
00123     }
00124     else if (mCamera->HasVideoMode(0, 3)) { // 640x480yuv422
00125         if (mCamera->SetVideoFormat(0) != CAM_SUCCESS) return false;
00126         if (mCamera->SetVideoMode(3) != CAM_SUCCESS) return false;
00127         mChannels = 2;
00128     }
00129     else if (mCamera->HasVideoMode(0, 1)) { // 320x240yuv422
00130         if (mCamera->SetVideoFormat(0) != CAM_SUCCESS) return false;
00131         if (mCamera->SetVideoMode(1) != CAM_SUCCESS) return false;
00132         mChannels = 2;
00133     */
00134     }
00135     else if (mCamera->HasVideoMode(2, 5)) { // 1600x1200mono
00136         if (mCamera->SetVideoFormat(2) != CAM_SUCCESS) return false;
00137         if (mCamera->SetVideoMode(5) != CAM_SUCCESS) return false;
00138         mChannels = 1;
00139     }
00140     else if (mCamera->HasVideoMode(2, 2)) { // 1280x960mono
00141         if (mCamera->SetVideoFormat(2) != CAM_SUCCESS) return false;
00142         if (mCamera->SetVideoMode(2) != CAM_SUCCESS) return false;
00143         mChannels = 1;
00144     }
00145     else if (mCamera->HasVideoMode(1, 5)) { // 1024x768mono
00146         if (mCamera->SetVideoFormat(1) != CAM_SUCCESS) return false;
00147         if (mCamera->SetVideoMode(5) != CAM_SUCCESS) return false;
00148         mChannels = 1;
00149     }
00150     else if (mCamera->HasVideoMode(1, 2)) { // 800x600mono
00151         if (mCamera->SetVideoFormat(1) != CAM_SUCCESS) return false;
00152         if (mCamera->SetVideoMode(2) != CAM_SUCCESS) return false;
00153         mChannels = 1;
00154     }
00155     else if (mCamera->HasVideoMode(0, 5)) { // 640x480mono
00156         if (mCamera->SetVideoFormat(0) != CAM_SUCCESS) return false;
00157         if (mCamera->SetVideoMode(5) != CAM_SUCCESS) return false;
00158         mChannels = 1;
00159     }
00160     else {
00161         return false;
00162     }
00163 
00164     mCamera->GetVideoFrameDimensions(&mXResolution, &mYResolution);
00165     mReturnFrame = cvCreateImage(cvSize(mXResolution, mYResolution), IPL_DEPTH_8U, mChannels);
00166     mCamera->StartImageAcquisitionEx(6, 1, ACQ_START_VIDEO_STREAM);
00167     mIsCapturing = true;
00168 
00169     return isCapturing();
00170 }
00171 
00172 void CaptureCmu::stop()
00173 {
00174     if (isCapturing()) {
00175         mCamera->StopImageAcquisition();
00176         cvReleaseImage(&mReturnFrame);
00177         mIsCapturing = false;
00178     }
00179 }
00180 
00181 IplImage *CaptureCmu::captureImage()
00182 {
00183     if (!isCapturing()) {
00184         return NULL;
00185     }
00186 
00187     if (mCamera->AcquireImageEx(false, NULL) == CAM_SUCCESS) {
00188         unsigned long length = mReturnFrame->widthStep * mYResolution;
00189         memcpy(mReturnFrame->imageData, mCamera->GetRawData(&length), length);
00190     }
00191     return mReturnFrame;
00192 }
00193 
00194 bool CaptureCmu::showSettingsDialog()
00195 {
00196     CameraControlDialog(NULL, mCamera, true);
00197     return true;
00198 }
00199 
00200 std::string CaptureCmu::SerializeId()
00201 {
00202     return "CaptureCmu";
00203 }
00204 
00205 bool CaptureCmu::Serialize(Serialization *serialization)
00206 {
00207     if (!mCamera) {
00208         return false;
00209     }
00210 
00211         unsigned short value;
00212         if (serialization->IsInput()) {
00213                 if (!serialization->Serialize(value, "Gain")) return false;
00214                 mCamera->GetCameraControl(FEATURE_GAIN)->SetAutoMode(false);
00215                 mCamera->GetCameraControl(FEATURE_GAIN)->SetValue(value);
00216 
00217                 if (!serialization->Serialize(value, "AutoExposure")) return false;
00218                 mCamera->GetCameraControl(FEATURE_AUTO_EXPOSURE)->SetAutoMode(false);
00219                 mCamera->GetCameraControl(FEATURE_AUTO_EXPOSURE)->SetValue(value);
00220 
00221                 if (!serialization->Serialize(value, "Shutter")) return false;
00222                 mCamera->GetCameraControl(FEATURE_SHUTTER)->SetAutoMode(false);
00223                 mCamera->GetCameraControl(FEATURE_SHUTTER)->SetValue(value);
00224 
00225                 if (!serialization->Serialize(value, "Brightness")) return false;
00226                 mCamera->GetCameraControl(FEATURE_BRIGHTNESS)->SetAutoMode(false);
00227                 mCamera->GetCameraControl(FEATURE_BRIGHTNESS)->SetValue(value);
00228 
00229                 if (!serialization->Serialize(value, "Gamma")) return false;
00230                 mCamera->GetCameraControl(FEATURE_GAMMA)->SetAutoMode(false);
00231                 mCamera->GetCameraControl(FEATURE_GAMMA)->SetValue(value);
00232         } else {
00233                 mCamera->GetCameraControl(FEATURE_GAIN)->GetValue(&value);
00234                 if (!serialization->Serialize(value, "Gain")) return false;
00235 
00236                 mCamera->GetCameraControl(FEATURE_AUTO_EXPOSURE)->GetValue(&value);
00237                 if (!serialization->Serialize(value, "AutoExposure")) return false;
00238 
00239                 mCamera->GetCameraControl(FEATURE_SHUTTER)->GetValue(&value);
00240                 if (!serialization->Serialize(value, "Shutter")) return false;
00241 
00242                 mCamera->GetCameraControl(FEATURE_BRIGHTNESS)->GetValue(&value);
00243                 if (!serialization->Serialize(value, "Brightness")) return false;
00244 
00245                 mCamera->GetCameraControl(FEATURE_GAMMA)->GetValue(&value);
00246                 if (!serialization->Serialize(value, "Gamma")) return false;
00247         }
00248         return true;
00249 }
00250 
00251 CapturePluginCmu::CapturePluginCmu(const std::string &captureType)
00252     : CapturePlugin(captureType)
00253 {
00254 }
00255 
00256 CapturePluginCmu::~CapturePluginCmu()
00257 {
00258 }
00259 
00260 CapturePlugin::CaptureDeviceVector CapturePluginCmu::enumerateDevices()
00261 {
00262     CaptureDeviceVector devices;
00263 
00264     C1394Camera camera;
00265     if (camera.CheckLink() != CAM_SUCCESS) {
00266         return devices;
00267     }
00268 
00269     int numberCameras = camera.GetNumberCameras();
00270     for (int i = 0; i < numberCameras; i++) {
00271         camera.SelectCamera(i);
00272         LARGE_INTEGER uniqueId;
00273         camera.GetCameraUniqueID(&uniqueId);
00274         std::stringstream convert;
00275         convert << std::hex << uniqueId.HighPart << uniqueId.LowPart;
00276         std::stringstream description;
00277         char buffer[500];
00278         camera.GetCameraVendor(buffer, 500);
00279         description << buffer << " ";
00280         camera.GetCameraName(buffer, 500);
00281         description << buffer;
00282         CaptureDevice captureDevice(mCaptureType, convert.str(), description.str());
00283         devices.push_back(captureDevice);
00284     }
00285 
00286     return devices;
00287 }
00288 
00289 Capture *CapturePluginCmu::createCapture(const CaptureDevice captureDevice)
00290 {
00291     return new CaptureCmu(captureDevice);
00292 }
00293 
00294 void registerPlugin(const std::string &captureType, alvar::CapturePlugin *&capturePlugin)
00295 {
00296     capturePlugin = new CapturePluginCmu(captureType);
00297 }
00298 
00299 } // namespace plugins
00300 } // namespace alvar


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Thu Feb 16 2017 03:23:02