hostsupport/hostopenvg/genheader/make_gamma_lut.py
branchbug235_bringup_0
changeset 53 c2ef9095503a
parent 20 d2d6724aef32
equal deleted inserted replaced
52:39e5f73667ba 53:c2ef9095503a
       
     1 #! /usr/bin/python
       
     2 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 #
       
     4 # Permission is hereby granted, free of charge, to any person obtaining a
       
     5 # copy of this software and /or associated documentation files
       
     6 # (the "Materials "), to deal in the Materials without restriction,
       
     7 # including without limitation the rights to use, copy, modify, merge,
       
     8 # publish, distribute, sublicense, and/or sell copies of the Materials,
       
     9 # and to permit persons to whom the Materials are furnished to do so,
       
    10 # subject to the following conditions:
       
    11 #
       
    12 # The above copyright notice and this permission notice shall be included
       
    13 # in all copies or substantial portions of the Materials.
       
    14 #
       
    15 # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    18 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
       
    19 # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    20 # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    21 # THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    22 
       
    23 # Extremely coarse lrgb<->srgb conversion table generation
       
    24 
       
    25 from c_utils import *
       
    26 
       
    27 GAMMA_BITS = 8
       
    28 
       
    29 def linear_to_gamma(f):
       
    30     if f <= 0.00304:
       
    31         return 12.92 * f
       
    32     return 1.0556*pow(f, 1/2.4) - 0.0556
       
    33 
       
    34 def gamma_to_linear(f):
       
    35     if f <= 0.03928:
       
    36         return f / 12.92
       
    37     return pow((f+0.0556)/1.0556, 2.4)
       
    38 
       
    39 def clamp(i, mn, mx):
       
    40     if i < mn:
       
    41         return mn
       
    42     if i > mx:
       
    43         return mx
       
    44     return i
       
    45 
       
    46 def icolortof(i):
       
    47     return i / 255.0
       
    48 
       
    49 def fcolortoi(f):
       
    50     return int(f * ((1<<GAMMA_BITS)-1) + 0.5)
       
    51 
       
    52 all_components = range(0,256)
       
    53 all_floats = map(icolortof, all_components)
       
    54 
       
    55 def make_color_list(conv_func):
       
    56     return map(fcolortoi, map(conv_func, all_floats))
       
    57 
       
    58 def make_lut_array(name, data):
       
    59     make_array("static const", "RIuint8", name, data, hex_formatter)  
       
    60 
       
    61 def print_lrgb_to_srgb_table():
       
    62     make_lut_array("sc_lRGB_to_sRGB", make_color_list(linear_to_gamma))
       
    63 
       
    64 def print_srgb_to_lrgb_table():
       
    65     make_lut_array("sc_sRGB_to_lRGB", make_color_list(gamma_to_linear))
       
    66 
       
    67 def print_lookups():
       
    68     begin_include_guard("_SFGAMMARCP_H_") 
       
    69     print ""
       
    70     make_include("riDefs.h")
       
    71     print ""
       
    72     begin_namespace("OpenVGRI")
       
    73     print_srgb_to_lrgb_table()
       
    74     print ""
       
    75     print_lrgb_to_srgb_table()
       
    76     end_namespace()
       
    77     end_include_guard()
       
    78 
       
    79 if __name__ == '__main__':
       
    80     print_lookups()
       
    81