//======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
using System.Runtime.InteropServices;
///
/// Stores the camera settings (brightness/contrast, gain/exposure, etc.) and interfaces with the ZED
/// when they need to be loaded or changed.
/// Created by ZEDCamera and referenced by ZEDCameraSettingsEditor.
///
/// The actual settings themselves are stored in an instance of CameraSettings, for easier manipulation.
/// But this class provides accessors for each value within it.
///
public class ZEDCameraSettings
{
#region DLL Calls
const string nameDll = "sl_unitywrapper";
[DllImport(nameDll, EntryPoint = "dllz_set_camera_settings")]
private static extern void dllz_set_camera_settings(int id, int mode, int value, int usedefault);
[DllImport(nameDll, EntryPoint = "dllz_get_camera_settings")]
private static extern int dllz_get_camera_settings(int id, int mode);
#endregion
///
/// Container for ZED camera settings, with constructors for easily creating default or specific values
/// or making duplicate instances.
///
public class CameraSettings
{
///
/// Holds an int for each setting, with indexes corresponding to sl.CAMERA_SETTINGS.
///
public int[] settings = new int[System.Enum.GetNames(typeof(sl.CAMERA_SETTINGS)).Length];
///
/// Constructor. Call without arguments to set all values to default.
///
/// Camera's brightness setting.
/// Camera's contrast setting.
/// Camera's hue setting.
/// Camera's saturation setting.
/// Camera's white balance setting. -1 means automatic.
/// Camera's gain setting. -1 means automatic.
/// Camera's exposure setting. -1 means automatic.
public CameraSettings(int brightness = 4, int contrast = 4, int hue = 0, int saturation = 4, int whiteBalance = -1, int gain = -1, int exposure = -1,int ledStatus = 1)
{
settings = new int[System.Enum.GetNames(typeof(sl.CAMERA_SETTINGS)).Length];
settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS] = brightness;
settings[(int)sl.CAMERA_SETTINGS.CONTRAST] = contrast;
settings[(int)sl.CAMERA_SETTINGS.SATURATION] = saturation;
settings[(int)sl.CAMERA_SETTINGS.HUE] = hue;
settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE] = whiteBalance;
settings[(int)sl.CAMERA_SETTINGS.GAIN] = gain;
settings[(int)sl.CAMERA_SETTINGS.EXPOSURE] = exposure;
settings[(int)sl.CAMERA_SETTINGS.LED_STATUS] = ledStatus;
}
///
/// Constructor. Sets settings to match another CameraSettings passed in the argument.
///
///
public CameraSettings(CameraSettings other)
{
settings = new int[System.Enum.GetNames(typeof(sl.CAMERA_SETTINGS)).Length];
settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS] = other.settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS];
settings[(int)sl.CAMERA_SETTINGS.CONTRAST] = other.settings[(int)sl.CAMERA_SETTINGS.CONTRAST];
settings[(int)sl.CAMERA_SETTINGS.SATURATION] = other.settings[(int)sl.CAMERA_SETTINGS.SATURATION];
settings[(int)sl.CAMERA_SETTINGS.HUE] = other.settings[(int)sl.CAMERA_SETTINGS.HUE];
settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE] = other.settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE];
settings[(int)sl.CAMERA_SETTINGS.GAIN] = other.settings[(int)sl.CAMERA_SETTINGS.GAIN];
settings[(int)sl.CAMERA_SETTINGS.EXPOSURE] = other.settings[(int)sl.CAMERA_SETTINGS.EXPOSURE];
settings[(int)sl.CAMERA_SETTINGS.LED_STATUS] = other.settings[(int)sl.CAMERA_SETTINGS.LED_STATUS];
}
///
/// Returns a new instance of CameraSettings with the same settings as the instance this function was called with.
///
/// New instance of CameraSettings.
public CameraSettings Clone()
{
return new CameraSettings(this);
}
///
/// ZED camera's brightness setting.
///
public int Brightness
{
get
{
return settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS];
}
set
{
settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS] = value;
}
}
///
/// ZED camera's saturation setting.
///
public int Saturation
{
get
{
return settings[(int)sl.CAMERA_SETTINGS.SATURATION];
}
set
{
settings[(int)sl.CAMERA_SETTINGS.SATURATION] = value;
}
}
///
/// ZED camera's hue setting.
///
public int Hue
{
get
{
return settings[(int)sl.CAMERA_SETTINGS.HUE];
}
set
{
settings[(int)sl.CAMERA_SETTINGS.HUE] = value;
}
}
///
/// ZED camera's contrast setting.
///
public int Contrast
{
get
{
return settings[(int)sl.CAMERA_SETTINGS.CONTRAST];
}
set
{
settings[(int)sl.CAMERA_SETTINGS.CONTRAST] = value;
}
}
///
/// ZED camera's gain setting. -1 means automatic.
///
public int Gain
{
get
{
return settings[(int)sl.CAMERA_SETTINGS.GAIN];
}
set
{
settings[(int)sl.CAMERA_SETTINGS.GAIN] = value;
}
}
///
/// ZED camera's exposure setting. -1 means automatic.
///
public int Exposure
{
get
{
return settings[(int)sl.CAMERA_SETTINGS.EXPOSURE];
}
set
{
settings[(int)sl.CAMERA_SETTINGS.EXPOSURE] = value;
}
}
///
/// ZED camera's white balance setting. -1 means automatic.
///
public int WhiteBalance
{
get
{
return settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE];
}
set
{
settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE] = value;
}
}
///
/// ZED camera's LED status
///
public int LEDStatus
{
get
{
return settings[(int)sl.CAMERA_SETTINGS.LED_STATUS];
}
set
{
settings[(int)sl.CAMERA_SETTINGS.LED_STATUS] = value;
}
}
}
///
/// Reference to the settings container object.
///
private CameraSettings settings_;
///
/// Reference to the settings container object.
///
public CameraSettings Settings
{
get { return settings_.Clone(); }
}
///
/// Whether exposure is set to automatic.
///
public bool auto = true;
///
/// Whether white balance is set to automatic.
///
public bool whiteBalanceAuto = true;
///
/// Constructor. Creates a new instance of CameraSettings to contain all settings values.
///
public ZEDCameraSettings()
{
settings_ = new CameraSettings();
}
///
/// Applies all settings from the container to the actual ZED camera.
///
/// Current instance of the ZEDCamera wrapper.
public void SetSettings(sl.ZEDCamera zedCamera)
{
if (zedCamera != null)
{
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS, settings_.Brightness, false);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST, settings_.Contrast, false);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.HUE, settings_.Hue, false);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SATURATION, settings_.Saturation, false);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, settings_.Gain, false);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, settings_.Exposure, false);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS, settings_.LEDStatus, false);
if (settings_.WhiteBalance != -1)
{
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, settings_.WhiteBalance, false);
}
}
}
///
/// Applies all settings from the container to the actual ZED camera.
///
/// Current instance of the ZEDCamera wrapper.
public void ResetCameraSettings(sl.ZEDCamera zedCamera)
{
if (zedCamera != null)
{
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS, 0, true);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST, 0 , true);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.HUE, 0 , true);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SATURATION, 0 , true);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, 0, true);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, 0, true);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, 0, true);
zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS, 1, true);
}
}
///
/// Loads camera settings from a file, and sets them to the container and camera.
/// File is loaded from the root project folder (one above Assets).
///
///
///
public void LoadCameraSettings(sl.ZEDCamera zedCamera, string path = "ZED_Settings.conf")
{
string[] lines = null;
try
{
lines = System.IO.File.ReadAllLines(path);
}
catch (System.Exception)
{
}
if (lines == null) return;
foreach (string line in lines)
{
string[] splittedLine = line.Split('=');
if (splittedLine.Length == 2)
{
string key = splittedLine[0];
string field = splittedLine[1];
if (key == "brightness")
{
settings_.Brightness = int.Parse(field);
}
else if (key == "contrast")
{
settings_.Contrast = int.Parse(field);
}
else if (key == "hue")
{
settings_.Hue = int.Parse(field);
}
else if (key == "saturation")
{
settings_.Saturation = int.Parse(field);
}
else if (key == "whiteBalance")
{
settings_.WhiteBalance = int.Parse(field);
}
else if (key == "gain")
{
settings_.Gain = int.Parse(field);
}
else if (key == "exposure")
{
settings_.Exposure = int.Parse(field);
}
else if (key == "LED")
{
settings_.LEDStatus = int.Parse(field);
}
}
}
SetSettings(zedCamera);
auto = (settings_.Exposure == -1);
whiteBalanceAuto = (settings_.WhiteBalance == -1);
}
///
/// Retrieves current settings from the ZED camera.
///
///
public void RetrieveSettingsCamera(sl.ZEDCamera zedCamera)
{
if (zedCamera != null)
{
settings_.Brightness = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS);
settings_.Contrast = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST);
settings_.Hue = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.HUE);
settings_.Saturation = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.SATURATION);
settings_.Gain = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.GAIN);
settings_.Exposure = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE);
settings_.WhiteBalance = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE);
settings_.LEDStatus = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS);
}
}
///
/// Applies an individual setting to the ZED camera.
///
/// Setting to be changed (brightness, contrast, gain, exposure, etc.)
/// New value for the setting.
/// If true, ignores the value and applies the default setting.
public void SetCameraSettings(int cid, sl.CAMERA_SETTINGS settings, int value, bool usedefault = false)
{
settings_.settings[(int)settings] = !usedefault && value != -1 ? value : -1;
dllz_set_camera_settings(cid, (int)settings, value, System.Convert.ToInt32(usedefault));
}
///
/// Gets the value from an individual ZED camera setting (brightness, contrast, gain, exposure, etc.)
///
/// Setting to be retrieved.
/// Current value.
public int GetCameraSettings(int cid, sl.CAMERA_SETTINGS settings)
{
settings_.settings[(int)settings] = dllz_get_camera_settings(cid, (int)settings);
return settings_.settings[(int)settings];
}
///
/// Saves all camera settings into a file into the specified path/name.
///
/// Path and filename to save the file (ex. /Assets/ZED_Settings.conf)
public void SaveCameraSettings(string path)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
{
file.WriteLine("brightness=" + settings_.Brightness.ToString());
file.WriteLine("contrast=" + settings_.Contrast.ToString());
file.WriteLine("hue=" + settings_.Hue.ToString());
file.WriteLine("saturation=" + settings_.Saturation.ToString());
file.WriteLine("whiteBalance=" + settings_.WhiteBalance.ToString());
file.WriteLine("gain=" + settings_.Gain.ToString());
file.WriteLine("exposure=" + settings_.Exposure.ToString());
file.WriteLine("LED=" + settings_.LEDStatus.ToString());
file.Close();
}
}
}