-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcms2js.py
179 lines (165 loc) · 6.25 KB
/
cms2js.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
##########################################################
# cms2js 0.3 #
# #
# ColorLUT converter from the Nucoda CMS format to #
# JavaScript multi-array for integration in HTML #
# applications providing 3D visualization of the #
# WebGL-based RGB cube representing the LUT. #
# #
# Copyright (C) 2014 Walter Arrighetti, Ph.D. #
# All Rights Reserved. #
##########################################################
VERSION = "0.3"
UseShaper = False
Path = {
"LUTs":"D:\\RD\\JScubes\\",
"web templates":"D:\\RD\\JScubes\\",
"JS templates":"D:\\RD\\JScubes\\",
"HTML output":"D:\\git\\",
"JS output":"D:\\git\\"
}
Template = {
"HTML":"template.html",
"JS":"template.js"
}
NL = {'HTML':'\r\n','JS':'\n'}
Placeholder = {
"HTML":["<!--###############PLACEHOLDER1###############-->"],
"JS":["/*###############PLACEHOLDER1###############*/"]
}
print "CMS2JS %s - ColorLUT converter from Nucoda CMS to JavaScript format"%VERSION
print "Copyright (C) 2014 Walter Arrighetti, Ph.D."
print "All Rights Reserved.\n"
import fnmatch
import sys
import re
import os
args, shaper = [], False
if len(sys.argv)==2 and os.path.isfile(sys.argv[1]): args.append(sys.argv[1])
elif len(sys.argv)==2 and os.path.isdir(sys.argv[1]):
print "Reading directory \"%s\" for Nucoda CMS ColorLUTs..."%sys.argv[1],
for dirpath,dirnames,filenames in os.walk(sys.argv[1]):
print filenames
for name in filenames:
if fnmatch.fnmatch(name.lower(),"*.cms"): args.append(os.path.join(dirpath,name))
print '\n',
else:
print "Syntax: cms2js [sourceLUT.cms | path-for-CMS-LUTs]"
sys.exit(1)
if not args:
print " * WARNING!: No Nucoda CMS ColorLUTs found to convert."
sys.exit(5)
for arg in args:
inputLUT = arg
RefName = os.path.splitext(os.path.split(inputLUT)[1])[0]
HTMLplaceholder = os.path.join(Path["web templates"],Template["HTML"])
JSplaceholder = os.path.join(Path["JS templates"],Template["JS"])
if not os.path.isfile(JSplaceholder):
print " * ERROR!: Unable to find in \"%s\" the main JavaScript template \"%s\" !"%os.path.split(JSplaceholder)
if not os.path.isfile(HTMLplaceholder):
print " * ERROR!: Unable to find in \"%s\" the main HTML template \"%s\" !"%os.path.split(HTMLplaceholder)
if not os.path.isfile(inputLUT):
inputLUT = os.path.join(Path["LUTs"],os.path.split(sys.argv[1])[1])
if not os.path.isfile(inputLUT):
print " * ERROR!: ColorLUT \"%s\" not found!"%inputLUT
try:
inLUT = open(inputLUT,"r")
lines = inLUT.readlines()
inLUT.close()
except:
print " * ERROR!: Unable to read ColorLUT \"%s\" !"%inputLUT
for n in range(len(lines)):
line = lines[n].strip()
if line.startswith("LUT_1D_SIZE"):
size = int(re.match("LUT_1D_SIZE\s+(?P<size>\d+)",line).group("size"))
if size: shaper = True
if line.startswith("LUT_3D_SIZE"):
side = int(re.match("LUT_3D_SIZE\s+(?P<size>\d+)",line).group("size"))
startLUT = n+2
break
if not side:
print " * ERROR!: Invalid CMS LUT header for \"%s\""%inputLUT
sys.exit(2)
if shaper:
print "Parsing shaper LUT from \"%s\"....."%os.path.split(inputLUT)[1],
shaperLUT = []
for n in range(startLUT,len(lines)):
if lines[n]=="":
startLUT = n+1
break
line = re.match("(?P<red>\d+\.\d{4,7})\s+(?P<green>\d+\.\d{4,7})\s+(?P<blue>\d+\.\d{4,7})",lines[n].strip())
if not line or len(line.groups())!=3: continue
shaperLUT.append(tuple(map(float,line.groups())))
if len(shaperLUT) >= size:
startLUT = n+1
break
print '\n',
if len(shaperLUT) != size:
print " * ERROR!: Invalid size for the shaper LUT!"
sys.exit(3)
LUT = []
for n in range(startLUT,len(lines)):
line = re.match("(?P<red>\d+\.\d{4,7})\s+(?P<green>\d+\.\d{4,7})\s+(?P<blue>\d+\.\d{4,7})",lines[n].strip())
if line:
startLUT = n
break
print "Parsing RGB cube from \"%s\"....."%os.path.split(inputLUT)[1],
for i in range(side):
LUT.append([])
for j in range(side):
LUT[i].append([])
for k in range(side):
LUT[i][j].append([])
line = lines[startLUT + (side**2)*i + side*j + k].strip()
LUTpoint = re.match("(?P<red>\d+\.\d{4,7})\s+(?P<green>\d+\.\d{4,7})\s+(?P<blue>\d+\.\d{4,7})",line)
LUT[i][j][k] = tuple(map(float,LUTpoint.groups()))
print '\n',
LUTjs = "\tvar LUT=["; shaperjs = "\tvar shaper=["
print "Converting ColorLUT \"%s\" into JavaScript....."%RefName,
for i in range(side):
LUTjs += "["; shaperjs += "["
for j in range(side):
LUTjs += "["; shaperjs += "["
for k in range(side):
shaperjs += "[%.6f,%.6f,%.6f],"%(float(i)/side,float(j)/side,float(k)/side)
LUTjs += "[%.6f,%.6f,%.6f],"%LUT[i][j][k]
LUTjs += "],"; shaperjs += "],"
LUTjs += "],"; shaperjs += "],"
LUTjs += "];"; shaperjs += "];"
print '\n',
outputLUT = os.path.join(Path["JS output"],RefName+".html")
outputJS = os.path.join(Path["JS output"],RefName+"_RGBcube.js")
HTMLoutput = os.path.join(Path["HTML output"],)
JSoutput = os.path.join(Path["JS output"],"")
try: PHhtml = open(HTMLplaceholder,"r")
except: print " * ERROR!: Unable to read HTML placeholder \"%s\" !"%HTMLplaceholder
try: PHjs = open(JSplaceholder,"r")
except: print " * ERROR!: Unable to read JS placeholder \"%s\" !"%JSplaceholder
try:
outJS = open(outputJS,"wb")
outLUT = open(outputLUT,"wb")
except:
print " * ERROR!: Unable to write final HTML+JavaScrt files."
sys.exit(4)
for line in PHhtml:
if Placeholder["HTML"][0] in line:
start = line.find(Placeholder["HTML"][0])
stop = start + len(Placeholder["HTML"][0])
outLUT.write( line[:start] + os.path.relpath(outputJS,Path["HTML output"]) + line[stop:].strip("\n\r") + NL['HTML'] )
else: outLUT.write(line.strip("\n\r")+NL['HTML'])
PHhtml.close()
outLUT.close()
print "Writing JavaScript LUT \"%s\"....."%os.path.split(outputJS)[1],
for line in PHjs:
if Placeholder["JS"][0] in line:
start = line.find(Placeholder["JS"][0])
stop = start + len(Placeholder["JS"][0])
outJS.write( line[:start]+NL['JS'] )
if UseShaper: outJS.write( shaperjs + NL['JS'] )
outJS.write( LUTjs + NL['JS'] )
outJS.write( line[stop:].strip("\n\r") + NL['JS'] )
else: outJS.write(line.strip("\n\r")+NL['JS'])
print '\n',
PHjs.close()
outJS.close()