|
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 from c_utils import * |
|
24 |
|
25 # \todo Might require a more accurate table for lookups. |
|
26 |
|
27 RCP_BITS = 8 |
|
28 RCP_DATATYPE_BITS = RCP_BITS + 8 |
|
29 |
|
30 def tofx(f): |
|
31 return int(f * (1<<RCP_BITS) + 0.5) |
|
32 |
|
33 def alpha_to_uintrcp(alpha): |
|
34 if alpha == 0: |
|
35 rf = 255 # Max out |
|
36 else: |
|
37 rf = 255.0/alpha |
|
38 i = tofx(rf) |
|
39 assert(i < (1<<RCP_DATATYPE_BITS)) |
|
40 return i |
|
41 |
|
42 def test_error(): |
|
43 max_err = 0 |
|
44 for alpha in range(1, 256): |
|
45 for color in range(0, 256): |
|
46 if color > alpha: |
|
47 # Undefined result -> ignore test. |
|
48 continue |
|
49 fa = alpha / 255.0 |
|
50 fc = color / 255.0 |
|
51 ex = fc / fa |
|
52 exi = int(ex * 255.0 + 0.5) |
|
53 ia = alpha_to_uintrcp(alpha) |
|
54 m = (ia * color) >> RCP_BITS |
|
55 e = abs(exi - m) |
|
56 if e > max_err: |
|
57 print ia * color |
|
58 print alpha, color, exi, m, fa, fc, ex, ia / float(1<<RCP_BITS), ia |
|
59 max_err = e |
|
60 print max_err |
|
61 return max_err |
|
62 |
|
63 if __name__ == '__main__': |
|
64 vals = [] |
|
65 for alpha in range(0, 256): |
|
66 i = alpha_to_uintrcp(alpha) |
|
67 vals += [i] |
|
68 |
|
69 begin_include_guard("_SFALPHARCP_H_") |
|
70 print "" |
|
71 make_include("riDefs.h") |
|
72 print "" |
|
73 begin_namespace("OpenVGRI") |
|
74 make_constant("ALPHA_RCP_BITS", RCP_BITS) |
|
75 make_array("static const", "RIuint16", "sc_alphaRcp", vals, hex_formatter) |
|
76 end_namespace() |
|
77 end_include_guard() |
|
78 |