LUT#

using Itala;
using Itala.GenApi;

/// <summary>
/// "LUT" example shows how to configure the LUT functionality,
///  available as set of GenICam features. LUTs allow to map pixel values
///  of an image to new specific values.In the following example the LUT
///  functionality is demonstrated by configuring a LUT which inverts
///  the acquired image.
/// </summary>
internal class Program
{

  private static void LUT_Sample()
  {
    Console.WriteLine("######## LUT ########");
    ISystem system = SystemFactory.Create();
    List<DeviceInfo> deviceInfos = system.EnumerateDevices();

    if (deviceInfos.Count == 0)
      throw new ItalaRuntimeException("No devices found. Example canceled.");
    if (deviceInfos[0].AccessStatus != DeviceAccessStatus.AvailableReadWrite)
      throw new ItalaRuntimeException("Target device is unaccessible in RW mode. Example canceled.");
    IDevice device = system.CreateDevice(deviceInfos[0]);
    Console.WriteLine("First device initialized.");
    Console.WriteLine("Configuring the LUT...");

    // Sets the LUT mode. In this case, the LUT is applied to the image luminance.
    IEnumeration LUTSelector = device.GetNodeMap().GetNode<IEnumeration>("LUTSelector");
    if (!LUTSelector.IsWritable)
      throw new ItalaRuntimeException("Unable to select the LUT mode. Aborting.");
    LUTSelector.FromString("Luminance");

    // Every different luminance value of the image pixel is also an index of
    // the LUT. This way, it's possible to map every luminance value by iterating
    // through the indexes.
    IInteger LUTIndex = device.GetNodeMap().GetNode<IInteger>("LUTIndex");
    if (!LUTIndex.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the LUT index. Aborting.");
    Int64 maxIndex = LUTIndex.Max;
    Int64 indexInc = LUTIndex.Increment;

    // Gets the maximum and minimum luminance value allowed. 
    IInteger LUTValue = device.GetNodeMap().GetNode<IInteger>("LUTValue");
    if (!LUTValue.IsWritable)
      throw new ItalaRuntimeException("Unable to get the allowed LUT values. Aborting.");
    Int64 maxValue = LUTValue.Max;

    // Iterate through all indexes, i.e. through all possible luminance values and
    // set them their inverted counterpart (e.g. 242 becomes 255 - 242 = 13 in Mono8).
    // The increment is also defined by the LUTIndex node.
    for(Int64 i = 0; i < maxIndex; i += indexInc)
    {
      LUTIndex.Value = i;
      LUTValue.Value = maxValue - i;
      Console.WriteLine("\t" + i + " becomes "+ (maxValue - i));
    }
    Console.WriteLine();
    Console.WriteLine("LUTValue values set.");

    // Once all values are mapped, enable the LUT.
    IBoolean LUTEnable = device.GetNodeMap().GetNode<IBoolean>("LUTEnable");
    if (!LUTEnable.IsWritable)
      throw new ItalaRuntimeException("Unable to activate the LUT. Aborting.");
    LUTEnable.Value = true;

    Console.WriteLine("LUT functionality enabled.\n");

    device.Dispose();
    Console.WriteLine("Device instance disposed.");
    system.Dispose();
    Console.WriteLine("System instance disposed.");
  }

  private static void Main(string[] args)
  {
    try
    {
      LUT_Sample();
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.ToString());
    }
  }
}