don't borrow dx/dy from a point with none
[sparrow.git] / gamma.py
blob6f798406a93770f5a05cd8549b6bf39847fc5707
1 #!/usr/bin/python
4 def gamma_table(start=0, stop=256, gamma=0.45):
5 lut = []
6 spread = stop - start
7 for point in range(spread):
8 p1 = float(point) / spread
9 p1 = p1 ** gamma
10 out = int(p1 * spread) + start
11 if out >= stop:
12 out = stop - 1
13 lut.append(out)
15 if start > 0:
16 lut[0:0] = [lut[0]] * start
17 if stop < 256:
18 lut.extend([lut[-1]] * (256 - stop))
20 return lut
23 def srgb_to_linear_table():
24 lut = []
25 a = 0.055
26 for point in range(256):
27 p = point / 255.0
28 if p <= 0.4545:
29 p /= 12.92
30 else:
31 p = ((p + a) / (1 + a)) ** 2.4
32 lut.append(int(p * 255))
33 return lut
35 def linear_to_srgb_table():
36 lut = []
37 a = 0.055
38 for point in range(256):
39 p = point / 255.0
40 if p <= 0.0031308:
41 p *= 12.92
42 else:
43 p = (1 + a) * (p ** (1 / 2.4)) - a
44 lut.append(int(p * 255))
45 return lut
48 def cformat(table, name="SOME_TABLE", wrap=75, unused=True):
49 table = list(table)
50 outs = []
51 if unused:
52 outs.append('UNUSED')
54 outs.extend(("static guint8 %s [%s] = {" % (name, len(table)),
55 ' %s' % table[0]))
56 for v in table[1:]:
57 if len(outs[-1]) >= wrap:
58 outs[-1] += ','
59 outs.append(' %s' % v)
60 else:
61 outs[-1] += ', %s' % v
62 outs.append('};\n')
64 print '\n'.join(outs)
67 print "/* Gamma corrected lookup tables for inverting video */"
68 print "/* generated by '%s'. DO NOT EDIT */" % __file__
69 print '#include "gstsparrow.h"'
70 cformat(reversed(gamma_table(gamma=2.0)), "sparrow_rgb_gamma_full_range_REVERSE")
71 cformat(reversed(gamma_table(16, 236)), "sparrow_rgb_gamma_headroom_REVERSE")