-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfakerconfig.h
347 lines (293 loc) · 6.74 KB
/
fakerconfig.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Copyright (C)2004 Landmark Graphics
* Copyright (C)2005-2008 Sun Microsystems, Inc.
*
* This library is free software and may be redistributed and/or modified under
* the terms of the wxWindows Library License, Version 3 or (at your option)
* any later version. The full license is in the LICENSE.txt file included
* with this distribution.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* wxWindows Library License for more details.
*/
#ifndef __FAKER_CONFIG_H
#define __FAKER_CONFIG_H
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "rr.h"
#include "rrlog.h"
#include <stdio.h>
#define MAXSTR 256
class Config
{
public:
Config() {_set=false; _env=NULL;}
~Config() {if(_env) {free(_env); _env=NULL;}}
bool isset(void) {return _set;}
char *envget(const char *envvar)
{
char *temp=NULL;
if((temp=getenv(envvar))!=NULL && strlen(temp)>0 && newenv(temp))
return temp;
else return NULL;
}
protected:
bool newenv(char *env)
{
if(_env)
{
if(strlen(env)==strlen(_env) && !strncmp(env, _env, strlen(env)))
return false;
free(_env); _env=NULL;
}
_env=strdup(env);
return true;
}
bool _set;
char *_env;
};
class ConfigBool : public Config
{
public:
ConfigBool() {_b=false;}
ConfigBool& operator= (bool b) {set(b); return *this;}
bool operator= (ConfigBool &cb) {return cb._b;}
operator bool() const {return _b;}
void get(const char *envvar)
{
char *temp=NULL;
if((temp=envget(envvar))!=NULL)
{
if(!strncmp(temp, "1", 1)) set(true);
else if(!strncmp(temp, "0", 1)) set(false);
}
}
void set(bool b)
{
_b=b; _set=true;
}
private:
bool _b;
};
class ConfigDouble : public Config
{
public:
ConfigDouble() {_d=0.; _usebounds=false; _min=_max=0.;}
ConfigDouble& operator= (double d) {set(d); return *this;}
double operator= (ConfigDouble &cd) {return cd._d;}
operator double() const {return _d;}
void setbounds(double min, double max)
{
_min=min; _max=max; _usebounds=true;
}
void get(const char *envvar)
{
char *temp=NULL;
if((temp=envget(envvar))!=NULL)
{
char *t=NULL; double dtemp=strtod(temp, &t);
if(t && t!=temp) set(dtemp);
}
}
void set(double d)
{
if((d>=_min && d<=_max) || !_usebounds)
{
_d=d; _set=true;
}
}
protected:
double _d, _min, _max;
bool _usebounds;
};
class ConfigInt : public Config
{
public:
ConfigInt() {_i=0; _usebounds=false; _min=_max=0; _pow2=false;}
ConfigInt& operator= (int i) {set(i); return *this;}
int operator= (ConfigInt &ci) {return ci._i;}
operator int() const {return _i;}
void setbounds(int min, int max)
{
_min=min; _max=max; _usebounds=true;
}
void setpow2(void) {_pow2=true;}
void get(const char *envvar)
{
char *temp=NULL;
if((temp=envget(envvar))!=NULL)
{
char *t=NULL; int itemp=strtol(temp, &t, 10);
if(t && t!=temp) set(itemp);
}
}
void set(int i)
{
if((!_usebounds || (i>=_min && i<=_max))
&& (!_pow2 || isPow2(i)))
{
_i=i; _set=true;
}
}
protected:
int _i, _min, _max;
bool _usebounds, _pow2;
};
class ConfigString : public Config
{
public:
ConfigString() {_s=NULL;}
ConfigString& operator= (char *s) {set(s); return *this;}
char* operator= (ConfigString &cs) {return cs._s;}
~ConfigString() {if(_s) free(_s);}
operator char*() const {return _s;}
void get(const char *envvar)
{
char *temp=NULL;
if((temp=envget(envvar))!=NULL)
{
for(int i=0; i<(int)strlen(temp); i++)
if(temp[i]!=' ' && temp[i]!='\t')
{
temp=&temp[i]; break;
}
if(strlen(temp)>0)
{
for(int i=0; i<(int)strlen(temp); i++)
if(temp[i]==' ' || temp[i]=='\t')
{temp[i]='\0'; break;}
set(temp);
}
}
}
void set(char *s)
{
if(_s) free(_s);
if(s && strlen(s)>0) {_s=strdup(s); _set=true;} else {_s=NULL; _set=false;}
}
private:
char *_s;
};
class ConfigGamma : public ConfigDouble
{
public:
ConfigGamma& operator= (double d) {set(d); return *this;}
double get(const char *envvar)
{
char *temp=NULL;
if((temp=envget(envvar))!=NULL)
{
if(!strcmp(temp, "1"))
{
set(2.22);
}
else if(!strcmp(temp, "0"))
{
set(1.0);
}
else
{
char *t=NULL; double dtemp=strtod(temp, &t);
if(t && t!=temp) set(dtemp);
}
}
return _d;
}
unsigned char _lut[256];
unsigned short _lut16[65536];
private:
void set(double d)
{
if(d!=_d) {_d=d; buildlut();}
_set=true;
}
void buildlut(void)
{
if(_d!=0.0 && _d!=1.0 && _d!=-1.0)
{
for(int i=0; i<256; i++)
{
double g=_d>0.0? 1.0/_d : -_d;
_lut[i]=(unsigned char)(255.*pow((double)i/255., g)+0.5);
}
for(int i=0; i<65536; i++)
{
double g=_d>0.0? 1.0/_d : -_d;
_lut16[i]=(unsigned short)(255.*pow((double)(i/256)/255., g)+0.5)<<8;
_lut16[i]|=(unsigned short)(255.*pow((double)(i%256)/255., g)+0.5);
}
}
}
};
class FakerConfig
{
public:
FakerConfig(void)
{
// Defaults
fps.setbounds(0.0, 1000000.0);
gamma=1.0;
serial=true;
spoil=true;
stereo.setbounds(0, RR_STEREOOPT-1);
stereo=RRSTEREO_REDCYAN;
reloadenv();
}
void reloadenv(void)
{
char *env;
// Fetch values from environment
autotest.get("VGL_AUTOTEST");
fps.get("VGL_FPS");
gamma.get("VGL_GAMMA");
log.get("VGL_LOG");
serial.get("VGL_SERIAL");
spoil.get("VGL_SPOIL");
if((env=stereo.envget("VGL_STEREO"))!=NULL)
{
if(!strnicmp(env, "L", 1)) stereo=RRSTEREO_LEYE;
else if(!strnicmp(env, "RC", 2)) stereo=RRSTEREO_REDCYAN;
else if(!strnicmp(env, "R", 1)) stereo=RRSTEREO_REYE;
else
{
char *t=NULL; int itemp=strtol(env, &t, 10);
if(t && t!=env) stereo=itemp;
}
}
trace.get("VGL_TRACE");
verbose.get("VGL_VERBOSE");
}
#define prconfint_s(i) rrout.println(#i" = %d (set = %d)", (int)i, i.isset());
#define prconfstr_s(s) rrout.println(#s" = \"%s\" (set = %d)", (char *)s? (char *)s:"NULL", s.isset());
#define prconfdbl_s(d) rrout.println(#d" = %f (set = %d)", (double)d, d.isset());
ConfigBool autotest;
ConfigDouble fps;
ConfigGamma gamma;
ConfigString log;
ConfigBool serial;
ConfigBool spoil;
ConfigInt stereo;
ConfigBool trace;
ConfigBool verbose;
void print(void)
{
prconfint_s(autotest)
prconfdbl_s(fps)
prconfdbl_s(gamma)
prconfstr_s(log)
prconfint_s(serial)
prconfint_s(spoil)
prconfint_s(stereo)
prconfint_s(trace)
prconfint_s(verbose)
}
};
#ifdef __FAKERCONFIG_STATICDEF__
FakerConfig fconfig;
#else
extern FakerConfig fconfig;
#endif
#endif