#!/usr/bin/python


def gamma_table(start=0, stop=256, gamma=0.45):
    lut = []
    spread = stop - start
    for point in range(spread):
        p1 = float(point) / spread
        p1 = p1 ** gamma
        out = int(p1 * spread) + start
        if out >= stop:
            out = stop - 1
        lut.append(out)

    if start > 0:
        lut[0:0] = [lut[0]] * start
    if stop < 256:
        lut.extend([lut[-1]] * (256 - stop))

    return lut



def cformat(table, name="SOME_TABLE", wrap=75):
    table = list(table)
    outs = ["static guint8 %s [%s] = {" % (name, len(table)),
            '    %s' % table[0]]
    for v in table[1:]:
        if len(outs[-1]) >= wrap:
            outs[-1] += ','
            outs.append('    %s' % v)
        else:
            outs[-1] += ', %s' % v
    outs.append('};\n')

    print '\n'.join(outs)


cformat(reversed(gamma_table(gamma=0.7)), "sparrow_rgb_gamma_full_range_REVERSE")
cformat(reversed(gamma_table(16, 236)), "sparrow_rgb_gamma_headroom_REVERSE")

@@ -443,16 +443,549 @@
 n lut%0A%0A%0A
+def srgb_to_linear_table():%0A    lut = %5B%5D%0A    a = 0.055%0A    for point in range(256):%0A        p = point / 255.0%0A        if p %3C= 0.4545:%0A            p /= 12.92%0A        else:%0A            p = ((p + a) / (1 + a)) ** 2.4%0A        lut.append(int(p * 255))%0A    return lut%0A%0Adef linear_to_srgb_table():%0A    lut = %5B%5D%0A    a = 0.055%0A    for point in range(256):%0A        p = point / 255.0%0A        if p %3C= 0.0031308:%0A            p *= 12.92%0A        else:%0A            p = (1 + a) * (p ** (1 / 2.4)) - a%0A        lut.append(int(p * 255))%0A    return lut%0A%0A
 %0Adef cfo

add srrgb table generators