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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mfe committed Dec 11, 2013
2 parents 1b90bd9 + c934b3e commit 36da0c8
Show file tree
Hide file tree
Showing 9 changed files with 754 additions and 76 deletions.
3 changes: 3 additions & 0 deletions lutLab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Available scripts :

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

- **rgb_to_xyz_matrix**: generate RGB colorspace to XYZ conversion matrix


Supported input LUT formats : 3dl, csp, cub, cube, hdl, look, mga/m3d, spid1d, spi3d, spimtx, vf.
See [OpenColorIO FAQ](http://opencolorio.org/FAQ.html) for more informations.
Expand All @@ -33,6 +35,7 @@ Command line usage
See command line help :
`lut_to_lut.py -h`
`ext_1d_lut.py -h`
`rgb_to_xyz_matrix.py -h`

Screenshots
-----------
Expand Down
8 changes: 6 additions & 2 deletions lutLab/lut_to_lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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, write_3d_cube_lut
from utils.lut_utils import get_default_out_path
from utils.lut_utils import get_default_out_path, write_3d_json_file
from PyOpenColorIO.Constants import (
INTERP_LINEAR, INTERP_TETRAHEDRAL
)
Expand Down Expand Up @@ -49,6 +49,10 @@ def lut_to_lut(inlutfile, outlutfile=None, type='1D_CUBE',
ext = ".csp"
write_function = write_2d_csp_lut
interp = INTERP_LINEAR
elif type == '3D_JSON':
ext = ".json"
write_function = write_3d_json_file
interp = INTERP_TETRAHEDRAL
else:
raise LutToLutException("Unsupported export format!")
if not outlutfile:
Expand Down Expand Up @@ -97,7 +101,7 @@ def __get_options():
parser.add_argument("-t", "--out-type",
help=("Output LUT type."),
type=str,
choices=['1D_CSP', '1D_CUBE', '3D_CUBE'],
choices=['1D_CSP', '1D_CUBE', '3D_CUBE', '3D_JSON'],
default='1D_CUBE')
# out lut size
parser.add_argument("-os", "--out-lut-size", help=(
Expand Down
175 changes: 175 additions & 0 deletions lutLab/rgb_to_xyz_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
""" Display RGB colorspaces to XYZ conversion matrices and their inverses
.. moduleauthor:: `Marie FETIVEAU <github.com/mfe>`_
"""
from utils.colors_helper import xy_to_XYZ
from utils.colorspaces import COLORSPACES
from utils.private_colorspaces import PRIVATE_COLORSPACES
import numpy
import argparse


class RGBToXYZMatrixException(Exception):
pass


def get_primaries_matrix(xy_red, xy_green, xy_blue):
"""Return primaries XYZ matrix form xy coords
Args:
xy_red (float, float): red primary coords
xy_green (float, float): green primary coords
xy_blue (float, float): blue primary coords
Returns:
.numpy.matrix (3x3)
"""
XYZ_red = xy_to_XYZ(xy_red)
XYZ_green = xy_to_XYZ(xy_green)
XYZ_blue = xy_to_XYZ(xy_blue)
primaries_matrix = numpy.matrix(
[
[XYZ_red[0], XYZ_green[0], XYZ_blue[0]],
[XYZ_red[1], XYZ_green[1], XYZ_blue[1]],
[XYZ_red[2], XYZ_green[2], XYZ_blue[2]],
])
return primaries_matrix


def get_white_matrix(xy_white):
"""Return white point XYZ matrix form xy coords
Args:
xy_white (float, float): white point coords
Returns:
.numpy.matrix (3x1)
"""
XYZ_white = xy_to_XYZ(xy_white)
white_matrix = numpy.matrix(
[
[XYZ_white[0]],
[XYZ_white[1]],
[XYZ_white[2]],
])
return white_matrix


def get_RGB_to_XYZ_matrix(xy_red, xy_green, xy_blue, xy_white):
"""Compute RGB to XYZ matrix
See Bruce Lindbloom page :
http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
Args:
xy_red (float, float): red primary coords
xy_green (float, float): green primary coords
xy_blue (float, float): blue primary coords
xy_white (float, float): white point coords
Returns:
.numpy.matrix (3x3)
"""
primaries_matrix = get_primaries_matrix(xy_red, xy_green, xy_blue)
white_matrix = get_white_matrix(xy_white)
s = primaries_matrix ** -1 * white_matrix
s_r, s_g, s_b = s.item(0, 0), s.item(1, 0), s.item(2, 0)
RGB_to_XYZ = numpy.matrix([
[s_r * primaries_matrix.item(0, 0), s_g * primaries_matrix.item(0, 1),
s_b * primaries_matrix.item(0, 2)],
[s_r * primaries_matrix.item(1, 0), s_g * primaries_matrix.item(1, 1),
s_b * primaries_matrix.item(1, 2)],
[s_r * primaries_matrix.item(2, 0), s_g * primaries_matrix.item(2, 1),
s_b * primaries_matrix.item(2, 2)]])
return RGB_to_XYZ


def matrix_to_string(matrix, extra=""):
return ("{0:.10f} {1:.10f} {2:.10f} {9}\n"
"{3:.10f} {4:.10f} {5:.10f} {10}\n"
"{6:.10f} {7:.10f} {8:.10f} {11} \n").format(matrix.item(0, 0),
matrix.item(0, 1),
matrix.item(0, 2),
matrix.item(1, 0),
matrix.item(1, 1),
matrix.item(1, 2),
matrix.item(2, 0),
matrix.item(2, 1),
matrix.item(2, 2),
extra,
extra,
extra)


def display_matrix(colorspace, format):
"""Display RGB to XYZ matrix corresponding to colorspace and formatting
as format
Args:
colorspace (str): input colorspace. For now, REC709 is the only option.
format (str): output format. simple, matrix, spimtx.
"""
try:
colorspace_obj = COLORSPACES[colorspace]
except KeyError:
colorspace_obj = PRIVATE_COLORSPACES[colorspace]
matrix = get_RGB_to_XYZ_matrix(colorspace_obj.get_red_primaries(),
colorspace_obj.get_green_primaries(),
colorspace_obj.get_blue_primaries(),
colorspace_obj.get_white_point())
if format == 'simple':
matrix_dump = matrix_to_string(matrix)
inv_matrix_dump = matrix_to_string(matrix.I)
elif format == 'spimtx':
matrix_dump = matrix_to_string(matrix, "0")
inv_matrix_dump = matrix_to_string(matrix.I, "0")
else:
matrix_dump = "{0}".format(matrix)
inv_matrix_dump = "{0}".format(matrix.I)
print "{0} to XYZ matrix ({1} output):\n".format(colorspace, format)
print matrix_dump
print "XYZ to {0} matrix ({1} output):\n".format(colorspace, format)
print inv_matrix_dump


def __get_options():
""" Return rgb_to_xyz option parser
Returns:
.argparse.ArgumentParser.args
"""
## Define parser
description = 'Print RGB -> XYZ matrix'
parser = argparse.ArgumentParser(description=description)
# RGB colorspace
parser.add_argument("-c", "--colorspace",
help=("Input RGB Colorspace."),
type=str,
choices= sorted(COLORSPACES.keys() +
PRIVATE_COLORSPACES.keys()),
default='REC709')
# Output format
parser.add_argument("-f", "--format",
help=("Output formatting."),
type=str,
choices=['matrix', 'spimtx', 'simple'],
default='matrix')
return parser.parse_args()


if __name__ == '__main__':
""" Command line interface
"""
args = __get_options()
display_matrix(args.colorspace, args.format)
Loading

0 comments on commit 36da0c8

Please sign in to comment.