-
Notifications
You must be signed in to change notification settings - Fork 11
/
hald_to_cube.py
68 lines (54 loc) · 1.91 KB
/
hald_to_cube.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
from __future__ import print_function, division
import math
import sys
from optparse import OptionParser
from PIL import Image
def main():
opt_parser = OptionParser(usage='%prog [options] input.png output.cube')
opts, args = opt_parser.parse_args()
if len(args) != 2:
opt_parser.print_usage()
exit(1)
in_ = Image.open(args[0])
w, h = in_.size
if w != h:
print('HALD input is not square.', file=sys.stderr)
exit(2)
steps = int(round(math.pow(w, 1/3)))
if steps ** 3 != w:
print('HALD input size is invalid: %d is not a cube.' % w, file=sys.stderr)
print('%d steps -> %d values' % (steps, steps**6), file=sys.stderr)
# Assume that we are going from 8 bits to 10.
out = open(args[1], 'w')
#out.write('#Created by: Adobe Photoshop CS6\n')
#out.write('#Copyright: Copyright 2012 Adobe Systems Inc.\n')
#out.write('TITLE "Testing"\n')
#out.write('\n')
#out.write('#LUT size\n')
out.write('LUT_3D_SIZE %d\n' % (steps ** 2))
#out.write('\n')
#out.write('#data domain\n')
out.write('DOMAIN_MIN 0.0 0.0 0.0\n')
out.write('DOMAIN_MAX 1.0 1.0 1.0\n')
#out.write('\n')
#out.write('#LUT data points\n')
if False:
steps1 = steps + 1
steps3 = steps ** 2 * (steps + 1)
steps5 = steps ** 4 * (steps + 1)
data = list(in_.getdata())
def lookup(ri, gi, bi):
return data[
ri * steps1 + gi * steps3 + bi * steps5
]
for bi in xrange(steps):
for gi in xrange(steps):
for ri in xrange(steps):
r, g, b = lookup(ri, gi, bi)[:3]
out.write('%f %f %f\n' % (r / 255.0, g / 255.0, b / 255.0))
else:
for pixel in in_.getdata():
r, g, b = pixel[:3]
out.write('%f %f %f\n' % (r / 255.0, g / 255.0, b / 255.0))
if __name__ == '__main__':
main()