2 # -*- coding: utf-8 -*-
5 build instructions for osx for needed packages.
8 brew install -vd opencolorio --with-python
11 brew tap homebrew/science
14 brew install -vd libRaw
15 brew install -vd OpenCV
17 brew install -vd openimageio --with-python
23 # this time, 'ociolutimage' will build because openimageio is installed
24 brew uninstall -vd opencolorio
25 brew install -vd opencolorio --with-python
32 import OpenImageIO as oiio
33 from aces_ocio.process import Process
37 # Functions used to generate LUTs using CTL transforms
39 def generate1dLUTImage(ramp1dPath, resolution=1024, minValue=0.0, maxValue=1.0):
40 #print( "Generate 1d LUT image - %s" % ramp1dPath)
43 format = os.path.splitext(ramp1dPath)[1]
44 ramp = oiio.ImageOutput.create(ramp1dPath)
47 spec = oiio.ImageSpec()
48 spec.set_format( oiio.FLOAT )
49 #spec.format.basetype = oiio.FLOAT
50 spec.width = resolution
54 ramp.open (ramp1dPath, spec, oiio.Create)
56 data = array.array("f", "\0" * spec.width * spec.height * spec.nchannels * 4)
57 for i in range(resolution):
58 value = float(i)/(resolution-1) * (maxValue - minValue) + minValue
59 data[i*spec.nchannels +0] = value
60 data[i*spec.nchannels +1] = value
61 data[i*spec.nchannels +2] = value
63 ramp.write_image(spec.format, data)
66 # Credit to Alex Fry for the original single channel version of the spi1d writer
67 def writeSPI1D(filename, fromMin, fromMax, data, entries, channels):
68 f = file(filename,'w')
69 f.write("Version 1\n")
70 f.write("From %f %f\n" % (fromMin, fromMax))
71 f.write("Length %d\n" % entries)
72 f.write("Components %d\n" % (min(3, channels)) )
74 for i in range(0, entries):
76 for j in range(0, min(3, channels)):
77 entry = "%s %s" % (entry, data[i*channels + j])
78 f.write(" %s\n" % entry)
82 def generate1dLUTFromImage(ramp1dPath, outputPath=None, minValue=0.0, maxValue=1.0):
83 if outputPath == None:
84 outputPath = ramp1dPath + ".spi1d"
87 ramp = oiio.ImageInput.open( ramp1dPath )
91 type = spec.format.basetype
94 channels = spec.nchannels
97 # Force data to be read as float. The Python API doesn't handle half-floats well yet.
99 data = ramp.read_image(type)
101 writeSPI1D(outputPath, minValue, maxValue, data, width, channels)
103 def generate3dLUTImage(ramp3dPath, resolution=32):
104 args = ["--generate", "--cubesize", str(resolution), "--maxwidth", str(resolution*resolution), "--output", ramp3dPath]
105 lutExtract = Process(description="generate a 3d LUT image", cmd="ociolutimage", args=args)
108 def generate3dLUTFromImage(ramp3dPath, outputPath=None, resolution=32):
109 if outputPath == None:
110 outputPath = ramp3dPath + ".spi3d"
112 args = ["--extract", "--cubesize", str(resolution), "--maxwidth", str(resolution*resolution), "--input", ramp3dPath, "--output", outputPath]
113 lutExtract = Process(description="extract a 3d LUT", cmd="ociolutimage", args=args)
116 def applyCTLToImage(inputImage,
122 acesCTLReleaseDir=None):
123 if len(ctlPaths) > 0:
125 if acesCTLReleaseDir != None:
126 if os.path.split(acesCTLReleaseDir)[1] != "utilities":
127 ctlModulePath = "%s/utilities" % acesCTLReleaseDir
129 ctlModulePath = acesCTLReleaseDir
130 ctlenv['CTL_MODULE_PATH'] = ctlModulePath
134 args += ['-ctl', ctl]
136 #args += ["-verbose"]
137 args += ["-input_scale", str(inputScale)]
138 args += ["-output_scale", str(outputScale)]
139 args += ["-global_param1", "aIn", "1.0"]
140 for key, value in globalParams.iteritems():
141 args += ["-global_param1", key, str(value)]
143 args += [outputImage]
145 #print( "args : %s" % args )
147 ctlp = Process(description="a ctlrender process", cmd="ctlrender", args=args, env=ctlenv )
151 def convertBitDepth(inputImage, outputImage, depth):
152 args = [inputImage, "-d", depth, "-o", outputImage]
153 convert = Process(description="convert image bit depth", cmd="oiiotool", args=args)
156 def generate1dLUTFromCTL(lutPath,
159 identityLutBitDepth='half',
164 acesCTLReleaseDir=None,
170 lutPathBase = os.path.splitext(lutPath)[0]
172 identityLUTImageFloat = lutPathBase + ".float.tiff"
173 generate1dLUTImage(identityLUTImageFloat, lutResolution, minValue, maxValue)
175 if identityLutBitDepth != 'half':
176 identityLUTImage = lutPathBase + ".uint16.tiff"
177 convertBitDepth(identityLUTImageFloat, identityLUTImage, identityLutBitDepth)
179 identityLUTImage = identityLUTImageFloat
181 transformedLUTImage = lutPathBase + ".transformed.exr"
182 applyCTLToImage(identityLUTImage, transformedLUTImage, ctlPaths, inputScale, outputScale, globalParams, acesCTLReleaseDir)
184 generate1dLUTFromImage(transformedLUTImage, lutPath, minValue, maxValue)
187 os.remove(identityLUTImage)
188 if identityLUTImage != identityLUTImageFloat:
189 os.remove(identityLUTImageFloat)
190 os.remove(transformedLUTImage)
192 def correctLUTImage(transformedLUTImage, correctedLUTImage, lutResolution):
194 transformed = oiio.ImageInput.open( transformedLUTImage )
197 transformedSpec = transformed.spec()
198 type = transformedSpec.format.basetype
199 width = transformedSpec.width
200 height = transformedSpec.height
201 channels = transformedSpec.nchannels
204 if width != lutResolution * lutResolution or height != lutResolution:
205 print( "Correcting image as resolution is off. Found %d x %d. Expected %d x %d" % (width, height, lutResolution * lutResolution, lutResolution) )
206 print( "Generating %s" % correctedLUTImage)
209 # We're going to generate a new correct image
212 # Get the source data
213 # Force data to be read as float. The Python API doesn't handle half-floats well yet.
215 sourceData = transformed.read_image(type)
217 format = os.path.splitext(correctedLUTImage)[1]
218 correct = oiio.ImageOutput.create(correctedLUTImage)
221 correctSpec = oiio.ImageSpec()
222 correctSpec.set_format( oiio.FLOAT )
223 correctSpec.width = height
224 correctSpec.height = width
225 correctSpec.nchannels = channels
227 correct.open (correctedLUTImage, correctSpec, oiio.Create)
229 destData = array.array("f", "\0" * correctSpec.width * correctSpec.height * correctSpec.nchannels * 4)
230 for j in range(0, correctSpec.height):
231 for i in range(0, correctSpec.width):
232 for c in range(0, correctSpec.nchannels):
234 destData[correctSpec.nchannels*correctSpec.width*j + correctSpec.nchannels*i + c] = sourceData[correctSpec.nchannels*correctSpec.width*j + correctSpec.nchannels*i + c]
236 correct.write_image(correctSpec.format, destData)
239 #shutil.copy(transformedLUTImage, correctedLUTImage)
240 correctedLUTImage = transformedLUTImage
244 return correctedLUTImage
246 def generate3dLUTFromCTL(lutPath,
249 identityLutBitDepth='half',
254 acesCTLReleaseDir=None):
258 lutPathBase = os.path.splitext(lutPath)[0]
260 identityLUTImageFloat = lutPathBase + ".float.tiff"
261 generate3dLUTImage(identityLUTImageFloat, lutResolution)
264 if identityLutBitDepth != 'half':
265 identityLUTImage = lutPathBase + "." + identityLutBitDepth + ".tiff"
266 convertBitDepth(identityLUTImageFloat, identityLUTImage, identityLutBitDepth)
268 identityLUTImage = identityLUTImageFloat
270 transformedLUTImage = lutPathBase + ".transformed.exr"
271 applyCTLToImage(identityLUTImage, transformedLUTImage, ctlPaths, inputScale, outputScale, globalParams, acesCTLReleaseDir)
273 correctedLUTImage = lutPathBase + ".correct.exr"
274 correctedLUTImage = correctLUTImage(transformedLUTImage, correctedLUTImage, lutResolution)
276 generate3dLUTFromImage(correctedLUTImage, lutPath, lutResolution)
279 os.remove(identityLUTImage)
280 if identityLUTImage != identityLUTImageFloat:
281 os.remove(identityLUTImageFloat)
282 os.remove(transformedLUTImage)
283 if correctedLUTImage != transformedLUTImage:
284 os.remove(correctedLUTImage)
285 #os.remove(correctedLUTImage)
290 p = optparse.OptionParser(description='A utility to generate LUTs from CTL',
293 usage='%prog [options]')
295 p.add_option('--lut', '-l', type="string", default="")
296 p.add_option('--ctl', '-c', type="string", action="append")
297 p.add_option('--lutResolution1d', '', type="int", default=1024)
298 p.add_option('--lutResolution3d', '', type="int", default=33)
299 p.add_option('--ctlReleasePath', '-r', type="string", default="")
300 p.add_option('--bitDepth', '-b', type="string", default="float")
301 p.add_option('--keepTempImages', '', action="store_true")
302 p.add_option('--minValue', '', type="float", default=0.0)
303 p.add_option('--maxValue', '', type="float", default=1.0)
304 p.add_option('--inputScale', '', type="float", default=1.0)
305 p.add_option('--outputScale', '', type="float", default=1.0)
306 p.add_option('--ctlRenderParam', '-p', type="string", nargs=2, action="append")
308 p.add_option('--generate1d', '', action="store_true")
309 p.add_option('--generate3d', '', action="store_true")
311 options, arguments = p.parse_args()
318 lutResolution1d = options.lutResolution1d
319 lutResolution3d = options.lutResolution3d
320 minValue = options.minValue
321 maxValue = options.maxValue
322 inputScale = options.inputScale
323 outputScale = options.outputScale
324 ctlReleasePath = options.ctlReleasePath
325 generate1d = options.generate1d == True
326 generate3d = options.generate3d == True
327 bitDepth = options.bitDepth
328 cleanup = not options.keepTempImages
331 if options.ctlRenderParam != None:
332 for param in options.ctlRenderParam:
333 params[param[0]] = float(param[1])
336 argsStart = sys.argv.index('--') + 1
337 args = sys.argv[argsStart:]
339 argsStart = len(sys.argv)+1
342 #print( "command line : \n%s\n" % " ".join(sys.argv) )
348 print( "1D LUT generation options")
350 print( "3D LUT generation options")
352 print( "lut : %s" % lut )
353 print( "ctls : %s" % ctls )
354 print( "lut res 1d : %s" % lutResolution1d )
355 print( "lut res 3d : %s" % lutResolution3d )
356 print( "min value : %s" % minValue )
357 print( "max value : %s" % maxValue )
358 print( "input scale : %s" % inputScale )
359 print( "output scale : %s" % outputScale )
360 print( "ctl render params : %s" % params )
361 print( "ctl release path : %s" % ctlReleasePath )
362 print( "bit depth of input : %s" % bitDepth )
363 print( "cleanup temp images : %s" % cleanup)
366 generate1dLUTFromCTL( lut,
379 generate3dLUTFromCTL( lut,
389 print( "\n\nNo LUT generated. You must choose either 1D or 3D LUT generation\n\n")
392 if __name__ == '__main__':