From fd70d0ef2e9602c5dd65730f000f5ca3ba7e083e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sergi=20Hern=C3=A0ndez=20Juan?= <shernand@iri.upc.edu>
Date: Fri, 11 Nov 2011 13:43:17 +0000
Subject: [PATCH] Added a function to check whether a given feature is present
 in the camera or not. Implemented the get_white_balance_value() function.

---
 src/firewirecamera.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++
 src/firewirecamera.h   |  1 +
 2 files changed, 76 insertions(+)

diff --git a/src/firewirecamera.cpp b/src/firewirecamera.cpp
index 7ee5e62..82691e8 100644
--- a/src/firewirecamera.cpp
+++ b/src/firewirecamera.cpp
@@ -945,6 +945,32 @@ void CFirewireCamera::set_ISO_speed(float speed)
   DEBUG_INFO("ok\n");
 }
 
+bool CFirewireCamera::is_feature_present(dc1394feature_t feature_id)
+{
+  dc1394error_t error;
+  dc1394bool_t present;
+
+  DEBUG_INFO("Checking if the feature is present ... ");
+  error=dc1394_feature_is_present(this->camera_handle,feature_id,&present);
+  if ( error != DC1394_SUCCESS )
+  {
+    DEBUG_INFO("failed\n");
+    /* handle the error */
+    throw CFirewireInternalException(_HERE_,error);
+  }
+  DEBUG_INFO("ok\n");
+  if ( present == DC1394_TRUE)
+  {
+    DEBUG_INFO("\tFeature is present.\n");
+    return true;
+  }
+  else
+  {
+    DEBUG_INFO("\tFeature is not present.\n");
+    return false;
+  }
+}
+
 void CFirewireCamera::enable_feature(dc1394feature_t feature_id)
 {
   dc1394error_t error;
@@ -1371,7 +1397,56 @@ void CFirewireCamera::set_white_balance_value(unsigned int u_b_value, unsigned i
 
 void CFirewireCamera::get_white_balance_value(unsigned int *u_b_value, unsigned int *v_r_value)
 {
+  dc1394error_t error;
+  dc1394bool_t present;
+  dc1394bool_t readable;
 
+  DEBUG_INFO("Checking if the feature is present ... ");
+  error=dc1394_feature_is_present(this->camera_handle,DC1394_FEATURE_WHITE_BALANCE,&present);
+  if ( error != DC1394_SUCCESS )
+  {
+    DEBUG_INFO("failed\n");
+    /* handle the error */
+    throw CFirewireInternalException(_HERE_,error);
+  }
+  DEBUG_INFO("ok\n");
+  if ( present == DC1394_TRUE)
+  {
+    DEBUG_INFO("\tFeature is present.\n");
+    DEBUG_INFO("Checking the reading status of the feature ... ");
+    error=dc1394_feature_is_readable(this->camera_handle,DC1394_FEATURE_WHITE_BALANCE,&readable);
+    if ( error != DC1394_SUCCESS )
+    {
+      DEBUG_INFO("failed\n");
+      /* handle the error */
+      throw CFirewireInternalException(_HERE_,error);
+    }
+    DEBUG_INFO("ok\n");
+    if ( readable == DC1394_TRUE)
+    {
+      DEBUG_INFO("Reading the current value ... ");
+      error=dc1394_feature_whitebalance_get_value(this->camera_handle,u_b_value,v_r_value);
+      if ( error != DC1394_SUCCESS )
+      {
+        DEBUG_INFO("failed\n");
+        /* handle the error */
+        throw CFirewireInternalException(_HERE_,error);
+      }
+      DEBUG_INFO("ok\n");
+    }
+    else
+    {
+      DEBUG_INFO("Feature is not readable.\n");
+      /* handle exception */
+      throw CFirewireFeatureException(_HERE_,dc1394_feature_get_string(DC1394_FEATURE_WHITE_BALANCE),"Is not readable.\n");
+    }
+  }
+  else
+  {
+    DEBUG_INFO("\tFeature is not present.\n");
+    /* handle exception */
+    throw CFirewireFeatureException(_HERE_,dc1394_feature_get_string(DC1394_FEATURE_WHITE_BALANCE),"Is not present.\n");
+  }
 }
 
 void CFirewireCamera::reset(void)
diff --git a/src/firewirecamera.h b/src/firewirecamera.h
index a58234a..3204265 100644
--- a/src/firewirecamera.h
+++ b/src/firewirecamera.h
@@ -979,6 +979,7 @@ class CFirewireCamera
     //@}
     /** @name Functions to handle the camera features */
     //@{
+    bool is_feature_present(dc1394feature_t feature_id);
     void enable_feature(dc1394feature_t feature_id);
     void disable_feature(dc1394feature_t feature_id);
     bool is_feature_enabled(dc1394feature_t feature_id);
-- 
GitLab