script to make gamma look up tables
[sparrow.git] / src / gamma.py
blobceb3d4e212bacbe7022db2d954d10d7d9708ab4e
1 #!/usr/bin/python
4 def gamma_table(table, gamma=0.45):
5 for point in table:
6 p1 = float(point) / spread
7 p1 = p1 ** gamma
8 out = int(p1 * spread) + start
9 if out >= stop:
10 out = stop - 1
11 lut.append(out)
13 if start > 0:
14 lut[0:0] = [lut[0]] * start
15 if stop < 256:
16 lut.extend([lut[-1]] * (256 - stop))
18 return lut
22 def input_table(start=0, stop=256):
23 lut = [0] * start
24 spread = stop - start
25 lut.extend(range(spread))
26 lut.extend([spread - 1] * (256 - stop))
27 return lut
29 def cformat(table, name="SOME_TABLE", wrap=75):
30 table = list(table)
31 outs = ["static guint8 %s [%s] = {" % (name, len(table)),
32 ' %s' % table[0]]
33 for v in table[1:]:
34 if len(outs[-1]) >= wrap:
35 outs[-1] += ','
36 outs.append(' %s' % v)
37 else:
38 outs[-1] += ', %s' % v
39 outs.append('};\n')
41 print '\n'.join(outs)
44 inputs_full = input_table()
45 inputs_headroom = input_table(16, 236)
47 cformat(reversed(gamma_table(inputs_full)), "sparrow_rgb_gamma_full_range_REVERSE")
48 cformat(reversed(gamma_table(inputs_headroom)), "sparrow_rgb_gamma_headroom_REVERSE")
50 cformat(reversed(gamma_table(inputs_full)), "sparrow_rgb_gamma_full_range_REVERSE")
51 cformat(reversed(gamma_table(inputs_headroom)), "sparrow_rgb_gamma_headroom_REVERSE")