GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
g3ddefaults.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <grass/G3d.h>
4 #include "G3d_intern.h"
5 
6 /*---------------------------------------------------------------------------*/
7 
8 #define G3D_NO_DEFAULT -10
9 
10 #define G3D_COMPRESSION_DEFAULT G3D_COMPRESSION
11 #define G3D_USE_LZW_DEFAULT G3D_NO_LZW
12 #define G3D_USE_RLE_DEFAULT G3D_USE_RLE
13 #define G3D_PRECISION_DEFAULT G3D_MAX_PRECISION
14 #define G3D_CACHE_SIZE_DEFAULT 1000
15 #define G3D_CACHE_SIZE_MAX_DEFAULT 2000000
16 #define G3D_FILE_TYPE_DEFAULT DCELL_TYPE
17 #define G3D_TILE_X_DEFAULT 8
18 #define G3D_TILE_Y_DEFAULT 8
19 #define G3D_TILE_Z_DEFAULT 8
20 #define G3D_ERROR_FUN_DEFAULT G3d_skipError
21 #define G3D_UNIT_DEFAULT "none"
22 
23 /*---------------------------------------------------------------------------*/
24 
25 #define G3D_COMPRESSION_ENV_VAR_YES "G3D_USE_COMPRESSION"
26 #define G3D_COMPRESSION_ENV_VAR_NO "G3D_NO_COMPRESSION"
27 
28 #define G3D_LZW_ENV_VAR_YES "G3D_USE_LZW"
29 #define G3D_LZW_ENV_VAR_NO "G3D_NO_LZW"
30 
31 #define G3D_RLE_ENV_VAR_YES "G3D_USE_RLE"
32 #define G3D_RLE_ENV_VAR_NO "G3D_NO_RLE"
33 
34 #define G3D_PRECISION_ENV_VAR "G3D_PRECISION"
35 #define G3D_PRECISION_ENV_VAR_MAX "G3D_MAX_PRECISION"
36 
37 #define G3D_CACHE_SIZE_ENV_VAR "G3D_DEFAULT_CACHE_SIZE"
38 #define G3D_CACHE_SIZE_MAX_ENV_VAR "G3D_MAX_CACHE_SIZE"
39 
40 #define G3D_FILE_FLOAT_ENV_VAR "G3D_WRITE_FLOAT"
41 #define G3D_FILE_DOUBLE_ENV_VAR "G3D_WRITE_DOUBLE"
42 
43 #define G3D_TILE_DIM_X_ENV_VAR "G3D_TILE_DIMENSION_X"
44 #define G3D_TILE_DIM_Y_ENV_VAR "G3D_TILE_DIMENSION_Y"
45 #define G3D_TILE_DIM_Z_ENV_VAR "G3D_TILE_DIMENSION_Z"
46 
47 #define G3D_FATAL_ERROR_ENV_VAR "G3D_USE_FATAL_ERROR"
48 #define G3D_PRINT_ERROR_ENV_VAR "G3D_USE_PRINT_ERROR"
49 
50 #define G3D_DEFAULT_WINDOW3D "G3D_DEFAULT_WINDOW3D"
51 
52 /*---------------------------------------------------------------------------*/
53 
62  { G3D_NO_DEFAULT, G3D_NO_DEFAULT, G3D_NO_DEFAULT };
63 void (*g3d_error_fun) (const char *) = NULL;
65 
66 /*---------------------------------------------------------------------------*/
67 
68 
84 void
85 G3d_setCompressionMode(int doCompress, int doLzw, int doRle, int precision)
86 {
87  if ((doCompress != G3D_NO_COMPRESSION) && (doCompress != G3D_COMPRESSION))
88  G3d_fatalError("G3d_setCompressionMode: wrong value for doCompress.");
89 
90  g3d_do_compression = doCompress;
91 
92  if (doCompress == G3D_NO_COMPRESSION)
93  return;
94 
95  if ((doLzw != G3D_NO_LZW) && (doLzw != G3D_USE_LZW))
96  G3d_fatalError("G3d_setCompressionMode: wrong value for doLzw.");
97 
98  if ((doRle != G3D_NO_RLE) && (doRle != G3D_USE_RLE))
99  G3d_fatalError("G3d_setCompressionMode: wrong value for doRle.");
100 
101  if (precision < -1)
102  G3d_fatalError("G3d_setCompressionMode: wrong value for precision.");
103 
104  g3d_do_lzw_compression = doLzw;
105  g3d_do_rle_compression = doRle;
106  g3d_precision = precision;
107 }
108 
109 /*---------------------------------------------------------------------------*/
110 
111 
124 void
125 G3d_getCompressionMode(int *doCompress, int *doLzw, int *doRle,
126  int *precision)
127 {
128  if (doCompress != NULL)
129  *doCompress = g3d_do_compression;
130  if (doLzw != NULL)
131  *doLzw = g3d_do_lzw_compression;
132  if (doRle != NULL)
133  *doRle = g3d_do_rle_compression;
134  if (precision != NULL)
135  *precision = g3d_precision;
136 }
137 
138 /*---------------------------------------------------------------------------*/
139 
140 
150 void G3d_setCacheSize(int nTiles)
151 {
152  if (nTiles < 0)
153  G3d_fatalError("G3d_setCacheSize: size out of range.");
154 
155  g3d_cache_default = nTiles;
156 }
157 
158 /*---------------------------------------------------------------------------*/
159 
160 
170 {
171  return g3d_cache_default;
172 }
173 
174 /*---------------------------------------------------------------------------*/
175 
176 
186 void G3d_setCacheLimit(int nBytes)
187 {
188  if (nBytes <= 0)
189  G3d_fatalError("G3d_setCacheLimit: size out of range.");
190 
191  g3d_cache_max = nBytes;
192 }
193 
194 /*---------------------------------------------------------------------------*/
195 
196 
207 {
208  return g3d_cache_max;
209 }
210 
211 /*---------------------------------------------------------------------------*/
212 
213 
224 {
225  if ((type != FCELL_TYPE) && (type != DCELL_TYPE))
226  G3d_fatalError("G3d_setFileTypeDefault: invalid type");
227 
229 }
230 
231 /*---------------------------------------------------------------------------*/
232 
233 
244 {
245  return g3d_file_type;
246 }
247 
248 /*---------------------------------------------------------------------------*/
249 
250 
262 void G3d_setTileDimension(int tileX, int tileY, int tileZ)
263 {
264  if ((g3d_tile_dimension[0] = tileX) <= 0)
266  ("G3d_setTileDimension: value for tile x environment variable out of range");
267 
268  if ((g3d_tile_dimension[1] = tileY) <= 0)
270  ("G3d_setTileDimension: value for tile y environment variable out of range");
271 
272  if ((g3d_tile_dimension[2] = tileZ) <= 0)
274  ("G3d_setTileDimension: value for tile z environment variable out of range");
275 }
276 
277 /*---------------------------------------------------------------------------*/
278 
279 
291 void G3d_getTileDimension(int *tileX, int *tileY, int *tileZ)
292 {
293  *tileX = g3d_tile_dimension[0];
294  *tileY = g3d_tile_dimension[1];
295  *tileZ = g3d_tile_dimension[2];
296 }
297 
298 /*---------------------------------------------------------------------------*/
299 
300 
310 void G3d_setErrorFun(void (*fun) (const char *))
311 {
312  g3d_error_fun = fun;
313 }
314 
315 /*---------------------------------------------------------------------------*/
316 
317 
327 void G3d_setUnit(const char *unit)
328 {
330  g3d_unit_default = G_store(unit);
331 }
332 
333 /*---------------------------------------------------------------------------*/
334 
335 
348 {
349  static int firstTime = 1;
350  const char *value, *windowName;
351  G3D_Region window;
352 
353  if (!firstTime)
354  return;
355  firstTime = 0;
356 
359  g3d_do_compression = G3D_COMPRESSION;
360  }
361  else {
363  g3d_do_compression = G3D_NO_COMPRESSION;
364  }
365  else {
367  }
368  }
369  }
370 
372  if (NULL != getenv(G3D_LZW_ENV_VAR_YES)) {
373  g3d_do_lzw_compression = G3D_USE_LZW;
374  }
375  else {
376  if (NULL != getenv(G3D_LZW_ENV_VAR_NO)) {
377  g3d_do_lzw_compression = G3D_NO_LZW;
378  }
379  else {
381  }
382  }
383  }
384 
386  if (NULL != getenv(G3D_RLE_ENV_VAR_YES)) {
387  g3d_do_rle_compression = G3D_USE_RLE;
388  }
389  else {
390  if (NULL != getenv(G3D_RLE_ENV_VAR_NO)) {
391  g3d_do_rle_compression = G3D_NO_RLE;
392  }
393  else {
395  }
396  }
397  }
398 
399  if (g3d_precision == G3D_NO_DEFAULT) {
401  g3d_precision = G3D_MAX_PRECISION;
402  }
403  else {
404  value = getenv(G3D_PRECISION_ENV_VAR);
405  if (value == NULL) {
407  }
408  else {
409  if (sscanf(value, "%d", &g3d_precision) != 1) {
411  ("G3d_initDefaults: precision environment variable has invalid value");
412  }
413  else {
414  if (g3d_precision < -1) {
416  ("G3d_initDefaults: value for cache environment variable out of range");
417  }
418  }
419  }
420  }
421  }
422 
423  if (g3d_file_type == G3D_NO_DEFAULT) {
425  g3d_file_type = FCELL_TYPE;
426  }
427  else {
429  g3d_file_type = DCELL_TYPE;
430  }
431  else {
433  }
434  }
435  }
436 
439 
440  if (value == NULL) {
442  }
443  else {
444  if (sscanf(value, "%d", &g3d_cache_default) != 1) {
446  ("G3d_initDefaults: cache environment variable has invalid value");
447  }
448  if (g3d_cache_default < 0) {
450  ("G3d_initDefaults: value for cache environment variable out of range");
451  }
452  }
453  }
454 
455  if (g3d_cache_max == G3D_NO_DEFAULT) {
457 
458  if (value == NULL) {
460  }
461  else {
462  if (sscanf(value, "%d", &g3d_cache_max) != 1) {
464  ("G3d_initDefaults: cache environment variable has invalid value");
465  }
466  if (g3d_cache_max < 0) {
468  ("G3d_initDefaults: value for cache environment variable out of range");
469  }
470  }
471  }
472 
475 
476  if (value == NULL) {
478  }
479  else {
480  if (sscanf(value, "%d", g3d_tile_dimension) != 1) {
482  ("G3d_initDefaults: tile dimension x environment variable has invalid value");
483  }
484  if (g3d_tile_dimension[0] <= 0) {
486  ("G3d_initDefaults: value for tile x environment variable out of range");
487  }
488  }
489 
491 
492  if (value == NULL) {
494  }
495  else {
496  if (sscanf(value, "%d", g3d_tile_dimension + 1) != 1) {
498  ("G3d_initDefaults: tile dimension y environment variable has invalid value");
499  }
500  if (g3d_tile_dimension[1] <= 0) {
502  ("G3d_initDefaults: value for tile y environment variable out of range");
503  }
504  }
505 
507 
508  if (value == NULL) {
510  }
511  else {
512  if (sscanf(value, "%d", g3d_tile_dimension + 2) != 1) {
514  ("G3d_initDefaults: tile dimension z environment variable has invalid value");
515  }
516  if (g3d_tile_dimension[2] <= 0) {
518  ("G3d_initDefaults: value for tile z environment variable out of range");
519  }
520  }
521  }
522 
523  if (g3d_error_fun == NULL) {
525 
526  if (value != NULL) {
528  }
529  else {
531 
532  if (value != NULL) {
534  }
535  else {
537  }
538  }
539  }
540 
541  if (g3d_unit_default == NULL)
543 
544  windowName = G3d_getWindowParams();
545  if (windowName == NULL) {
546  value = getenv(G3D_DEFAULT_WINDOW3D);
547  if (value != NULL)
548  if (*value != 0)
549  windowName = value;
550  }
551 
552  if (!G3d_readWindow(&window, windowName))
553  G3d_fatalError("G3d_initDefaults: Error reading window");
554  G3d_setWindow(&window);
555 
556 }