Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: experimental/skpdiff/generate_pmetric_tables.py

Issue 19256002: add table pregeneration script for pmetric (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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)
60 for i in range(1024):
61 if i % 6 == 0:
62 print('\n ', end='', file=stream)
63 print("%.10f" % pow(i / 1024.0, 1.0 / 3.0), end='f,', file=stream)
64 print('\n};', end='', file=stream)
65 print(CUBE_ROOT_ACCESS_FUNCTION, file=stream)
66
67
68 # float gGammaTable[]
69 GAMMA_ACCESS_FUNCTION = '''
70 float get_gamma(unsigned char value) {
71 return gGammaTable[value];
72 }
73 '''
74 def generate_gamma_table(stream):
75 print('static float gGammaTable[] = {', end='', file=stream)
76 for i in range(256):
77 if i % 6 == 0:
78 print('\n ', end='', file=stream)
79 print("%.10f" % pow(i / 255.0, 2.2), end='f,', file=stream)
80 print('\n};', end='', file=stream)
81 print(GAMMA_ACCESS_FUNCTION, file=stream)
82
83
84 # float gTVITable[]
85 TVI_ACCESS_FUNCTION = '''
86 float get_threshold_vs_intensity(float value) {
87 SkASSERT(value >= 0.0f);
88 SkASSERT(value < 100.0f);
89 return gTVITable[(int)(value * 100.0f)];
90 }
91 '''
92 def generate_tvi_table(stream):
93 print('static float gTVITable[] = {', end='', file=stream)
94 for i in range(10000):
95 if i % 6 == 0:
96 print('\n ', end='', file=stream)
97 print("%.10f" % threshold_vs_intensity(i / 100.0), end='f,', file=stream )
98 print('\n};', end='', file=stream)
99 print(TVI_ACCESS_FUNCTION, file=stream)
100
101
102 # float gVisualMaskTable[]
103 VISUAL_MASK_DOMAIN = 4000
104 VISUAL_MASK_ACCESS_FUNCTION = '''
105 float get_visual_mask(float value) {{
106 SkASSERT(value >= 0.0f);
107 SkASSERT(value < {}.0f);
108 return gVisualMaskTable[(int)value];
109 }}'''
110 def generate_visual_mask_table(stream):
111 print('static float gVisualMaskTable[] = {', end='', file=stream)
112 for i in range(VISUAL_MASK_DOMAIN):
113 if i % 6 == 0:
114 print('\n ', end='', file=stream)
115 print("%.10f" % visual_mask(i), end='f,', file=stream)
116 print('\n};', end='', file=stream)
117 print(VISUAL_MASK_ACCESS_FUNCTION.format(VISUAL_MASK_DOMAIN), file=stream)
118
119
120 def generate_lookup_tables(stream):
121 print('namespace SkPMetricUtil {', file=stream)
122 generate_cube_root_table(stream)
123 generate_gamma_table(stream)
124 generate_tvi_table(stream)
125 generate_visual_mask_table(stream)
126 print('}', file=stream)
127
128
129 def main():
130 pmetric_util_out = open('SkPMetricUtil_generated.h', 'wb')
131 generate_lookup_tables(pmetric_util_out)
132 pmetric_util_out.close()
133
134
135 if __name__ == '__main__':
136 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698