Skip to content
This repository has been archived by the owner on Nov 22, 2019. It is now read-only.

Commit

Permalink
Merge branch 'feature/add_3Dcube_export' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mfe committed Oct 18, 2013
2 parents a6d4965 + 3398196 commit 7b59ae1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lutLab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ It uses [OpenColorIO](http://opencolorio.org/) to read and process input LUTs.

Available scripts :

- **lut_to_lut**: convert a lut into another format. For now, only conversion to 1D/2D cube or 1D/2D csp is supported
- **lut_to_lut**: convert a lut into another format. For now, only conversion to 1D/2D/3D cube or 1D/2D csp is supported

- **ext_1d_lut**: extract the tone mapping curve of a 3D LUT using a bicubic interpolation (or not)

Expand Down
56 changes: 37 additions & 19 deletions lutLab/lut_to_lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
import argparse
from utils.ocio_helper import OCIO_LUTS_FORMATS, create_ocio_processor
from utils.csp_helper import write_2d_csp_lut
from utils.cube_helper import write_2d_cube_lut
from utils.cube_helper import write_2d_cube_lut, write_3d_cube_lut
from utils.lut_utils import get_default_out_path
from PyOpenColorIO.Constants import (
INTERP_LINEAR, INTERP_TETRAHEDRAL
)


class LutToLutException(Exception):
pass


def lut_to_lut(inlutfile, outlutfile=None, type='1D_CUBE',
lutsize=16):
lutsize=16, cubesize=17):
"""Extract the tone mapping curve of a 3D LUT
Args:
Expand All @@ -27,38 +30,48 @@ def lut_to_lut(inlutfile, outlutfile=None, type='1D_CUBE',
outlutfile (str): the output 1D LUT. If not define, LUT is written in
the input LUT directory and post-fixed with "_export"
type (str): specify output LUT format. For now only 2D csp and 2D cube
are available.
type (str): specify output LUT format. For now only 2D/3D csp and 2D
cube are available.
lutsize (int): out LUT bit precision. Ex : 16 (bits)
lutsize (int): out LUT bit precision for 1D. Ex : 16 (bits)
"""
samples_count = pow(2, lutsize)
if type == '1D_CUBE':
ext = ".cube"
write_function = write_2d_cube_lut
interp = INTERP_LINEAR
elif type == '3D_CUBE':
ext = ".cube"
write_function = write_3d_cube_lut
interp = INTERP_TETRAHEDRAL
elif type == '1D_CSP':
ext = ".csp"
write_function = write_2d_csp_lut
interp = INTERP_LINEAR
else:
raise LutToLutException("Unsupported export format!")
if not outlutfile:
outlutfile = get_default_out_path(inlutfile, ext)
processor = create_ocio_processor(inlutfile)
# init vars
processor = create_ocio_processor(inlutfile, interpolation=interp)
# init vars
max_value = samples_count - 1.0
red_values = []
green_values = []
blue_values = []
# process color values
for n in range(0, samples_count):
x = n/max_value
res = processor.applyRGB([x, x, x])
red_values.append(res[0])
green_values.append(res[1])
blue_values.append(res[2])
# write
write_function(outlutfile, red_values, green_values, blue_values)
if "1D" in type:
# process color values
for n in range(0, samples_count):
x = n/max_value
res = processor.applyRGB([x, x, x])
red_values.append(res[0])
green_values.append(res[1])
blue_values.append(res[2])
# write
write_function(outlutfile, red_values, green_values, blue_values)
elif "3D" in type:
# write
write_function(outlutfile, cubesize, processor)


def __get_options():
Expand All @@ -83,16 +96,21 @@ def __get_options():
parser.add_argument("-t", "--out-type",
help=("Output LUT type."),
type=str,
choices=['1D_CSP', '1D_CUBE'], default='1D_CUBE')
choices=['1D_CSP', '1D_CUBE', '3D_CUBE'],
default='1D_CUBE')
# out lut size
parser.add_argument("-os", "--outlut-size", help=(
parser.add_argument("-os", "--out-lut-size", help=(
"Output lut bit precision. Ex : 10, 16, 32."
), default=16, type=int)
# out cube size
parser.add_argument("-ocs", "--out-cube-size", help=(
"Output cube size (3D only). Ex : 17, 32."
), default=17, type=int)
return parser.parse_args()

if __name__ == '__main__':
""" Command line interface
"""
args = __get_options()
lut_to_lut(args.inlutfile, args.outlutfile, args.out_type,
args.outlut_size)
args.out_lut_size, args.out_cube_size)
44 changes: 44 additions & 0 deletions utils/cube_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,49 @@ def write_1d_cube_lut(filename, values, comment=None, title="Iridas LUT"):
comment (str): an optionnal comment
title (str): title of the LUT
"""
write_2d_cube_lut(filename, values, comment, title)


def write_3d_cube_lut(filename, cubesize, processor,
comment=None, title="Cube LUT"):
"""Write a 3D Cube LUT
Args:
filename (str): out LUT path
cubesize (int): cube size. Ex: 17, 32...
processor (PyOpenColorIO.config.Processor): an OpenColorIO processor
Kwargs:
comment (str): an optionnal comment
title (str): title of the LUT
"""
f = open(filename, 'w+')
# comment
if comment:
f.write("#{0}\n".format(comment))
# title
f.write("TITLE {0}\n\n".format(title))
# lut size
f.write("{0} {1}\n\n".format(CUBE_3D, cubesize))
input_range = range(0, cubesize)
max_value = cubesize - 1.0
# process color values
for b in input_range:
for g in input_range:
for r in input_range:
# get a value between [0..1]
norm_r = r/max_value
norm_g = g/max_value
norm_b = b/max_value
# apply correction via OCIO
res = processor.applyRGB([norm_r, norm_g, norm_b])
f.write("{0:.6f} {1:.6f} {2:.6f}\n".format(res[0],
res[1],
res[2]))
f.close()

0 comments on commit 7b59ae1

Please sign in to comment.