OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 from __future__ import print_function | |
5 from math import * | |
6 | |
7 | |
8 # From Barten SPIE 1989 | |
9 def contrast_sensitivity(cycles_per_degree, luminance): | |
10 a = 440.0 * pow(1.0 + 0.7 / luminance, -0.2) | |
11 b = 0.3 * pow(1 + 100.0 / luminance, 0.15) | |
12 return a * cycles_per_degree * exp(-b * cycles_per_degree) * sqrt(1.0 + 0.06 * exp(b * cycles_per_degree)) | |
13 | |
14 | |
15 # From Ward Larson Siggraph 1997 | |
16 def threshold_vs_intensity(adaptation_luminance): | |
17 log_lum = float('-inf') # Works in Python 2.6+ | |
18 try: | |
19 log_lum = log10(adaptation_luminance) | |
20 except ValueError: | |
21 pass | |
22 | |
23 x = 0.0 | |
24 | |
25 if log_lum < -3.94: | |
26 x = -2.86 | |
27 | |
28 elif log_lum < -1.44: | |
29 x = pow(0.405 * log_lum + 1.6, 2.18) - 2.86 | |
30 | |
31 elif log_lum < -0.0184: | |
32 x = log_lum - 0.395 | |
33 | |
34 elif log_lum < 1.9: | |
35 x = pow(0.249 * log_lum + 0.65, 2.7) - 0.72 | |
36 | |
37 else: | |
38 x = log_lum - 1.255 | |
39 | |
40 return pow(10.0, x) | |
41 | |
42 | |
43 # From Daly 1993 | |
44 def visual_mask(contrast): | |
45 x = pow(392.498 * contrast, 0.7) | |
46 x = pow(0.0153 * x, 4.0) | |
47 return pow(1.0 + x, 0.25) | |
48 | |
49 | |
50 # float gCubeRootTable[] | |
51 CUBE_ROOT_ACCESS_FUNCTION = ''' | |
52 float get_cube_root(float value) { | |
53 SkASSERT(value >= 0.0f); | |
54 SkASSERT(value * 1023.0f < 1024.0f); | |
55 return gCubeRootTable[(int)(value * 1023.0f)]; | |
56 } | |
57 ''' | |
58 def generate_cube_root_table(stream): | |
59 print('static float gCubeRootTable[] = { ', end='', file=stream) | |
djsollen
2013/07/16 13:55:11
put newline after {
| |
60 for i in range(1024): | |
61 print("%.10f" % pow(i / 1024.0, 1.0 / 3.0), end='f, ', file=stream) | |
djsollen
2013/07/16 13:55:11
if i % 6 newline?
| |
62 print('};', end='', file=stream) | |
63 print(CUBE_ROOT_ACCESS_FUNCTION, file=stream) | |
64 | |
65 | |
66 # float gGammaTable[] | |
67 GAMMA_ACCESS_FUNCTION = ''' | |
68 float get_gamma(unsigned char value) { | |
69 return gGammaTable[value]; | |
70 } | |
71 ''' | |
72 def generate_gamma_table(stream): | |
73 print('static float gGammaTable[] = { ', end='', file=stream) | |
74 for i in range(256): | |
75 print("%.10f" % pow(i / 255.0, 2.2), end='f, ', file=stream) | |
76 print('};', end='', file=stream) | |
77 print(GAMMA_ACCESS_FUNCTION, file=stream) | |
78 | |
79 | |
80 # float gTVITable[] | |
81 TVI_ACCESS_FUNCTION = ''' | |
82 float get_threshold_vs_intensity(float value) { | |
83 SkASSERT(value >= 0.0f); | |
84 SkASSERT(value < 100.0f); | |
85 return gTVITable[(int)(value * 100.0f)]; | |
86 } | |
87 ''' | |
88 def generate_tvi_table(stream): | |
89 print('static float gTVITable[] = { ', end='', file=stream) | |
90 for i in range(10000): | |
91 print("%.10f" % threshold_vs_intensity(i / 100.0), end='f, ', file=strea m) | |
92 print('};', end='', file=stream) | |
93 print(TVI_ACCESS_FUNCTION, file=stream) | |
94 | |
95 | |
96 # float gVisualMaskTable[] | |
97 VISUAL_MASK_DOMAIN = 4000 | |
98 VISUAL_MASK_ACCESS_FUNCTION = ''' | |
99 float get_visual_mask(float value) {{ | |
100 SkASSERT(value >= 0.0f); | |
101 SkASSERT(value < {}.0f); | |
102 return gVisualMaskTable[(int)value]; | |
103 }}''' | |
104 def generate_visual_mask_table(stream): | |
105 print('static float gVisualMaskTable[] = { ', end='', file=stream) | |
106 for i in range(VISUAL_MASK_DOMAIN): | |
107 print("%.10f" % visual_mask(i), end='f, ', file=stream) | |
108 print('};', end='', file=stream) | |
109 print(VISUAL_MASK_ACCESS_FUNCTION.format(VISUAL_MASK_DOMAIN), file=stream) | |
110 | |
111 | |
112 def generate_lookup_tables(stream): | |
113 print('namespace SkPMetricUtil {', file=stream) | |
114 generate_cube_root_table(stream) | |
115 generate_gamma_table(stream) | |
116 generate_tvi_table(stream) | |
117 generate_visual_mask_table(stream) | |
118 print('}', file=stream) | |
119 | |
120 | |
121 def main(): | |
122 pmetric_util_out = open('SkPMetricUtil_generated.h', 'wb') | |
123 generate_lookup_tables(pmetric_util_out) | |
124 pmetric_util_out.close() | |
125 | |
126 | |
127 if __name__ == '__main__': | |
128 main() | |
OLD | NEW |