Roles now have some assignments
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / generate_lut.py
index 3ff606d..107698b 100755 (executable)
@@ -6,6 +6,8 @@ Defines objects to generate various kind of 1d, 2d and 3d LUTs in various file
 formats.
 """
 
+from __future__ import division
+
 import array
 import os
 import sys
@@ -36,8 +38,8 @@ __all__ = ['generate_1d_LUT_image',
 
 def generate_1d_LUT_image(ramp_1d_path,
                           resolution=1024,
-                          min_value=0.0,
-                          max_value=1.0):
+                          min_value=0,
+                          max_value=1):
     """
     Object description.
 
@@ -76,7 +78,13 @@ def generate_1d_LUT_image(ramp_1d_path,
     ramp.close()
 
 
-def write_SPI_1d(filename, from_min, from_max, data, entries, channels):
+def write_SPI_1d(filename, 
+                 from_min, 
+                 from_max, 
+                 data, 
+                 entries, 
+                 channels, 
+                 components=3):
     """
     Object description.
 
@@ -94,15 +102,19 @@ def write_SPI_1d(filename, from_min, from_max, data, entries, channels):
          Return value description.
     """
 
+    # May want to use fewer components than there are channels in the data
+    # Most commonly used for single channel LUTs
+    components = min(3, components, channels)
+
     with open(filename, 'w') as fp:
         fp.write('Version 1\n')
         fp.write('From %f %f\n' % (from_min, from_max))
         fp.write('Length %d\n' % entries)
-        fp.write('Components %d\n' % (min(3, channels)))
+        fp.write('Components %d\n' % components)
         fp.write('{\n')
         for i in range(0, entries):
             entry = ''
-            for j in range(0, min(3, channels)):
+            for j in range(0, components):
                 entry = '%s %s' % (entry, data[i * channels + j])
             fp.write('        %s\n' % entry)
         fp.write('}\n')
@@ -110,8 +122,9 @@ def write_SPI_1d(filename, from_min, from_max, data, entries, channels):
 
 def generate_1d_LUT_from_image(ramp_1d_path,
                                output_path=None,
-                               min_value=0.0,
-                               max_value=1.0):
+                               min_value=0,
+                               max_value=1,
+                               channels=3):
     """
     Object description.
 
@@ -131,16 +144,17 @@ def generate_1d_LUT_from_image(ramp_1d_path,
 
     ramp = oiio.ImageInput.open(ramp_1d_path)
 
-    spec = ramp.spec()
-    width = spec.width
-    channels = spec.nchannels
+    ramp_spec = ramp.spec()
+    ramp_width = ramp_spec.width
+    ramp_channels = ramp_spec.nchannels
 
     # Forcibly read data as float, the Python API doesn't handle half-float
     # well yet.
     type = oiio.FLOAT
-    data = ramp.read_image(type)
+    ramp_data = ramp.read_image(type)
 
-    write_SPI_1d(output_path, min_value, max_value, data, width, channels)
+    write_SPI_1d(output_path, min_value, max_value, 
+      ramp_data, ramp_width, ramp_channels, channels)
 
 
 def generate_3d_LUT_image(ramp_3d_path, resolution=32):
@@ -206,11 +220,11 @@ def generate_3d_LUT_from_image(ramp_3d_path, output_path=None, resolution=32):
 
 def apply_CTL_to_image(input_image,
                        output_image,
-                       ctl_paths=[],
-                       input_scale=1.0,
-                       output_scale=1.0,
-                       global_params={},
-                       aces_CTL_directory=None):
+                       ctl_paths=None,
+                       input_scale=1,
+                       output_scale=1,
+                       global_params=None,
+                       aces_ctl_directory=None):
     """
     Object description.
 
@@ -225,13 +239,18 @@ def apply_CTL_to_image(input_image,
          Return value description.
     """
 
+    if ctl_paths is None:
+        ctl_paths = []
+    if global_params is None:
+        global_params = {}
+
     if len(ctl_paths) > 0:
         ctlenv = os.environ
-        if aces_CTL_directory is not None:
-            if os.path.split(aces_CTL_directory)[1] != 'utilities':
-                ctl_module_path = os.path.join(aces_CTL_directory, 'utilities')
+        if aces_ctl_directory is not None:
+            if os.path.split(aces_ctl_directory)[1] != 'utilities':
+                ctl_module_path = os.path.join(aces_ctl_directory, 'utilities')
             else:
-                ctl_module_path = aces_CTL_directory
+                ctl_module_path = aces_ctl_directory
             ctlenv['CTL_MODULE_PATH'] = ctl_module_path
 
         args = []
@@ -283,13 +302,14 @@ def generate_1d_LUT_from_CTL(lut_path,
                              ctl_paths,
                              lut_resolution=1024,
                              identity_LUT_bit_depth='half',
-                             input_scale=1.0,
-                             output_scale=1.0,
-                             global_params={},
+                             input_scale=1,
+                             output_scale=1,
+                             global_params=None,
                              cleanup=True,
-                             aces_CTL_directory=None,
-                             min_value=0.0,
-                             max_value=1.0):
+                             aces_ctl_directory=None,
+                             min_value=0,
+                             max_value=1,
+                             channels=3):
     """
     Object description.
 
@@ -304,6 +324,9 @@ def generate_1d_LUT_from_CTL(lut_path,
          Return value description.
     """
 
+    if global_params is None:
+        global_params = {}
+
     lut_path_base = os.path.splitext(lut_path)[0]
 
     identity_LUT_image_float = '%s.%s.%s' % (lut_path_base, 'float', 'tiff')
@@ -312,7 +335,7 @@ def generate_1d_LUT_from_CTL(lut_path,
                           min_value,
                           max_value)
 
-    if identity_LUT_bit_depth != 'half':
+    if identity_LUT_bit_depth not in ['half', 'float']:
         identity_LUT_image = '%s.%s.%s' % (lut_path_base, 'uint16', 'tiff')
         convert_bit_depth(identity_LUT_image_float,
                           identity_LUT_image,
@@ -327,12 +350,13 @@ def generate_1d_LUT_from_CTL(lut_path,
                        input_scale,
                        output_scale,
                        global_params,
-                       aces_CTL_directory)
+                       aces_ctl_directory)
 
     generate_1d_LUT_from_image(transformed_LUT_image,
                                lut_path,
                                min_value,
-                               max_value)
+                               max_value,
+                               channels)
 
     if cleanup:
         os.remove(identity_LUT_image)
@@ -418,11 +442,11 @@ def generate_3d_LUT_from_CTL(lut_path,
                              ctl_paths,
                              lut_resolution=64,
                              identity_LUT_bit_depth='half',
-                             input_scale=1.0,
-                             output_scale=1.0,
-                             global_params={},
+                             input_scale=1,
+                             output_scale=1,
+                             global_params=None,
                              cleanup=True,
-                             aces_CTL_directory=None):
+                             aces_ctl_directory=None):
     """
     Object description.
 
@@ -437,12 +461,15 @@ def generate_3d_LUT_from_CTL(lut_path,
          Return value description.
     """
 
+    if global_params is None:
+        global_params = {}
+
     lut_path_base = os.path.splitext(lut_path)[0]
 
     identity_LUT_image_float = '%s.%s.%s' % (lut_path_base, 'float', 'tiff')
     generate_3d_LUT_image(identity_LUT_image_float, lut_resolution)
 
-    if identity_LUT_bit_depth != 'half':
+    if identity_LUT_bit_depth not in ['half', 'float']:
         identity_LUT_image = '%s.%s.%s' % (lut_path_base,
                                            identity_LUT_bit_depth,
                                            'tiff')
@@ -459,7 +486,7 @@ def generate_3d_LUT_from_CTL(lut_path,
                        input_scale,
                        output_scale,
                        global_params,
-                       aces_CTL_directory)
+                       aces_ctl_directory)
 
     corrected_LUT_image = '%s.%s.%s' % (lut_path_base, 'correct', 'exr')
     corrected_LUT_image = correct_LUT_image(transformed_LUT_image,
@@ -507,10 +534,10 @@ def main():
     p.add_option('--ctlReleasePath', '-r', type='string', default='')
     p.add_option('--bitDepth', '-b', type='string', default='float')
     p.add_option('--keepTempImages', '', action='store_true')
-    p.add_option('--minValue', '', type='float', default=0.0)
-    p.add_option('--maxValue', '', type='float', default=1.0)
-    p.add_option('--inputScale', '', type='float', default=1.0)
-    p.add_option('--outputScale', '', type='float', default=1.0)
+    p.add_option('--minValue', '', type='float', default=0)
+    p.add_option('--maxValue', '', type='float', default=1)
+    p.add_option('--inputScale', '', type='float', default=1)
+    p.add_option('--outputScale', '', type='float', default=1)
     p.add_option('--ctlRenderParam', '-p', type='string', nargs=2,
                  action='append')
 
@@ -521,8 +548,8 @@ def main():
 
     lut = options.lut
     ctls = options.ctl
-    lut_resolution_1d = options.lut_resolution_1d
-    lut_resolution_3d = options.lut_resolution_3d
+    lut_resolution_1d = options.lutResolution1d
+    lut_resolution_3d = options.lutResolution3d
     min_value = options.minValue
     max_value = options.maxValue
     input_scale = options.inputScale