LUT#

from itala import itala

system = itala.create_system()
devices_info = system.enumerate_devices(500)
if(len(devices_info) == 0):
    print("No devices found. Example canceled.")
    exit(1)
if(devices_info[0].access_status != itala.DeviceAccessStatus_AvailableReadWrite):
    print("Target device is unaccessible in RW mode. Example cancelled.")
    exit(1)
device = system.create_device(devices_info[0])
print("First device initialized.")
nodemap = device.node_map
print("Configuring the LUT..")

# Sets the LUT mode. In this case, the LUT is applied to the image luminance.
lut_selector_node = nodemap.LUTSelector
if(not itala.is_writable(lut_selector_node)):
    print("Unable to select the LUT mode. Aborting.")
    exit(1)
lut_selector_node.from_string("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.
lut_index_node = nodemap.LUTIndex
if(not itala.is_writable(lut_index_node)):
    print("Unable to configure the LUT index. Aborting.")
    exit(1)
max_index = lut_index_node.max
index_inc = lut_index_node.inc

# Gets the maximum and minimum luminance value allowed.
lut_value_node = nodemap.LUTValue
if(not itala.is_writable(lut_value_node)):
    print("Unable to get the allowed LUT values. Aborting.")
    exit(1)
max_value = lut_value_node.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 i in range(0, max_index, index_inc):
    lut_index_node.value = i
    lut_value_node.value = (max_value - i)
    print("\t" + str(i) + " becomes " + str(max_value - i))

print("LUT values set.")

# Once all values are mapped, enable the LUT.
lut_enable_node = nodemap.LUTEnable
if(not itala.is_writable(lut_enable_node)):
    print("Unable to activate the LUT. Aborting.")
    exit(1)
lut_enable_node.value = True

print("LUT functionality enabled.")

device.dispose()
print("Device instance disposed.")
system.dispose()
print("System instance disposed.")