GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <stdlib.h> 00002 #include <stdio.h> 00003 #include <grass/G3d.h> 00004 #include "G3d_intern.h" 00005 00006 /*---------------------------------------------------------------------------*/ 00007 00008 #define G3D_NO_DEFAULT -10 00009 00010 #define G3D_COMPRESSION_DEFAULT G3D_COMPRESSION 00011 #define G3D_USE_LZW_DEFAULT G3D_NO_LZW 00012 #define G3D_USE_RLE_DEFAULT G3D_USE_RLE 00013 #define G3D_PRECISION_DEFAULT G3D_MAX_PRECISION 00014 #define G3D_CACHE_SIZE_DEFAULT 1000 00015 #define G3D_CACHE_SIZE_MAX_DEFAULT 2000000 00016 #define G3D_FILE_TYPE_DEFAULT DCELL_TYPE 00017 #define G3D_TILE_X_DEFAULT 8 00018 #define G3D_TILE_Y_DEFAULT 8 00019 #define G3D_TILE_Z_DEFAULT 8 00020 #define G3D_ERROR_FUN_DEFAULT G3d_skipError 00021 #define G3D_UNIT_DEFAULT "none" 00022 00023 /*---------------------------------------------------------------------------*/ 00024 00025 #define G3D_COMPRESSION_ENV_VAR_YES "G3D_USE_COMPRESSION" 00026 #define G3D_COMPRESSION_ENV_VAR_NO "G3D_NO_COMPRESSION" 00027 00028 #define G3D_LZW_ENV_VAR_YES "G3D_USE_LZW" 00029 #define G3D_LZW_ENV_VAR_NO "G3D_NO_LZW" 00030 00031 #define G3D_RLE_ENV_VAR_YES "G3D_USE_RLE" 00032 #define G3D_RLE_ENV_VAR_NO "G3D_NO_RLE" 00033 00034 #define G3D_PRECISION_ENV_VAR "G3D_PRECISION" 00035 #define G3D_PRECISION_ENV_VAR_MAX "G3D_MAX_PRECISION" 00036 00037 #define G3D_CACHE_SIZE_ENV_VAR "G3D_DEFAULT_CACHE_SIZE" 00038 #define G3D_CACHE_SIZE_MAX_ENV_VAR "G3D_MAX_CACHE_SIZE" 00039 00040 #define G3D_FILE_FLOAT_ENV_VAR "G3D_WRITE_FLOAT" 00041 #define G3D_FILE_DOUBLE_ENV_VAR "G3D_WRITE_DOUBLE" 00042 00043 #define G3D_TILE_DIM_X_ENV_VAR "G3D_TILE_DIMENSION_X" 00044 #define G3D_TILE_DIM_Y_ENV_VAR "G3D_TILE_DIMENSION_Y" 00045 #define G3D_TILE_DIM_Z_ENV_VAR "G3D_TILE_DIMENSION_Z" 00046 00047 #define G3D_FATAL_ERROR_ENV_VAR "G3D_USE_FATAL_ERROR" 00048 #define G3D_PRINT_ERROR_ENV_VAR "G3D_USE_PRINT_ERROR" 00049 00050 #define G3D_DEFAULT_WINDOW3D "G3D_DEFAULT_WINDOW3D" 00051 00052 /*---------------------------------------------------------------------------*/ 00053 00054 int g3d_do_compression = G3D_NO_DEFAULT; 00055 int g3d_do_lzw_compression = G3D_NO_DEFAULT; 00056 int g3d_do_rle_compression = G3D_NO_DEFAULT; 00057 int g3d_precision = G3D_NO_DEFAULT; 00058 int g3d_cache_default = G3D_NO_DEFAULT; 00059 int g3d_cache_max = G3D_NO_DEFAULT; 00060 int g3d_file_type = G3D_NO_DEFAULT; 00061 int g3d_tile_dimension[3] = 00062 { G3D_NO_DEFAULT, G3D_NO_DEFAULT, G3D_NO_DEFAULT }; 00063 void (*g3d_error_fun) (const char *) = NULL; 00064 char *g3d_unit_default = NULL; 00065 00066 /*---------------------------------------------------------------------------*/ 00067 00068 00084 void 00085 G3d_setCompressionMode(int doCompress, int doLzw, int doRle, int precision) 00086 { 00087 if ((doCompress != G3D_NO_COMPRESSION) && (doCompress != G3D_COMPRESSION)) 00088 G3d_fatalError("G3d_setCompressionMode: wrong value for doCompress."); 00089 00090 g3d_do_compression = doCompress; 00091 00092 if (doCompress == G3D_NO_COMPRESSION) 00093 return; 00094 00095 if ((doLzw != G3D_NO_LZW) && (doLzw != G3D_USE_LZW)) 00096 G3d_fatalError("G3d_setCompressionMode: wrong value for doLzw."); 00097 00098 if ((doRle != G3D_NO_RLE) && (doRle != G3D_USE_RLE)) 00099 G3d_fatalError("G3d_setCompressionMode: wrong value for doRle."); 00100 00101 if (precision < -1) 00102 G3d_fatalError("G3d_setCompressionMode: wrong value for precision."); 00103 00104 g3d_do_lzw_compression = doLzw; 00105 g3d_do_rle_compression = doRle; 00106 g3d_precision = precision; 00107 } 00108 00109 /*---------------------------------------------------------------------------*/ 00110 00111 00124 void 00125 G3d_getCompressionMode(int *doCompress, int *doLzw, int *doRle, 00126 int *precision) 00127 { 00128 if (doCompress != NULL) 00129 *doCompress = g3d_do_compression; 00130 if (doLzw != NULL) 00131 *doLzw = g3d_do_lzw_compression; 00132 if (doRle != NULL) 00133 *doRle = g3d_do_rle_compression; 00134 if (precision != NULL) 00135 *precision = g3d_precision; 00136 } 00137 00138 /*---------------------------------------------------------------------------*/ 00139 00140 00150 void G3d_setCacheSize(int nTiles) 00151 { 00152 if (nTiles < 0) 00153 G3d_fatalError("G3d_setCacheSize: size out of range."); 00154 00155 g3d_cache_default = nTiles; 00156 } 00157 00158 /*---------------------------------------------------------------------------*/ 00159 00160 00169 int G3d_getCacheSize() 00170 { 00171 return g3d_cache_default; 00172 } 00173 00174 /*---------------------------------------------------------------------------*/ 00175 00176 00186 void G3d_setCacheLimit(int nBytes) 00187 { 00188 if (nBytes <= 0) 00189 G3d_fatalError("G3d_setCacheLimit: size out of range."); 00190 00191 g3d_cache_max = nBytes; 00192 } 00193 00194 /*---------------------------------------------------------------------------*/ 00195 00196 00206 int G3d_getCacheLimit() 00207 { 00208 return g3d_cache_max; 00209 } 00210 00211 /*---------------------------------------------------------------------------*/ 00212 00213 00223 void G3d_setFileType(int type) 00224 { 00225 if ((type != FCELL_TYPE) && (type != DCELL_TYPE)) 00226 G3d_fatalError("G3d_setFileTypeDefault: invalid type"); 00227 00228 g3d_file_type = type; 00229 } 00230 00231 /*---------------------------------------------------------------------------*/ 00232 00233 00243 int G3d_getFileType() 00244 { 00245 return g3d_file_type; 00246 } 00247 00248 /*---------------------------------------------------------------------------*/ 00249 00250 00262 void G3d_setTileDimension(int tileX, int tileY, int tileZ) 00263 { 00264 if ((g3d_tile_dimension[0] = tileX) <= 0) 00265 G3d_fatalError 00266 ("G3d_setTileDimension: value for tile x environment variable out of range"); 00267 00268 if ((g3d_tile_dimension[1] = tileY) <= 0) 00269 G3d_fatalError 00270 ("G3d_setTileDimension: value for tile y environment variable out of range"); 00271 00272 if ((g3d_tile_dimension[2] = tileZ) <= 0) 00273 G3d_fatalError 00274 ("G3d_setTileDimension: value for tile z environment variable out of range"); 00275 } 00276 00277 /*---------------------------------------------------------------------------*/ 00278 00279 00291 void G3d_getTileDimension(int *tileX, int *tileY, int *tileZ) 00292 { 00293 *tileX = g3d_tile_dimension[0]; 00294 *tileY = g3d_tile_dimension[1]; 00295 *tileZ = g3d_tile_dimension[2]; 00296 } 00297 00298 /*---------------------------------------------------------------------------*/ 00299 00300 00310 void G3d_setErrorFun(void (*fun) (const char *)) 00311 { 00312 g3d_error_fun = fun; 00313 } 00314 00315 /*---------------------------------------------------------------------------*/ 00316 00317 00327 void G3d_setUnit(const char *unit) 00328 { 00329 G3d_free(g3d_unit_default); 00330 g3d_unit_default = G_store(unit); 00331 } 00332 00333 /*---------------------------------------------------------------------------*/ 00334 00335 00347 void G3d_initDefaults(void) 00348 { 00349 static int firstTime = 1; 00350 const char *value, *windowName; 00351 G3D_Region window; 00352 00353 if (!firstTime) 00354 return; 00355 firstTime = 0; 00356 00357 if (g3d_do_compression == G3D_NO_DEFAULT) { 00358 if (NULL != getenv(G3D_COMPRESSION_ENV_VAR_YES)) { 00359 g3d_do_compression = G3D_COMPRESSION; 00360 } 00361 else { 00362 if (NULL != getenv(G3D_COMPRESSION_ENV_VAR_NO)) { 00363 g3d_do_compression = G3D_NO_COMPRESSION; 00364 } 00365 else { 00366 g3d_do_compression = G3D_COMPRESSION_DEFAULT; 00367 } 00368 } 00369 } 00370 00371 if (g3d_do_lzw_compression == G3D_NO_DEFAULT) { 00372 if (NULL != getenv(G3D_LZW_ENV_VAR_YES)) { 00373 g3d_do_lzw_compression = G3D_USE_LZW; 00374 } 00375 else { 00376 if (NULL != getenv(G3D_LZW_ENV_VAR_NO)) { 00377 g3d_do_lzw_compression = G3D_NO_LZW; 00378 } 00379 else { 00380 g3d_do_lzw_compression = G3D_USE_LZW_DEFAULT; 00381 } 00382 } 00383 } 00384 00385 if (g3d_do_rle_compression == G3D_NO_DEFAULT) { 00386 if (NULL != getenv(G3D_RLE_ENV_VAR_YES)) { 00387 g3d_do_rle_compression = G3D_USE_RLE; 00388 } 00389 else { 00390 if (NULL != getenv(G3D_RLE_ENV_VAR_NO)) { 00391 g3d_do_rle_compression = G3D_NO_RLE; 00392 } 00393 else { 00394 g3d_do_rle_compression = G3D_USE_RLE_DEFAULT; 00395 } 00396 } 00397 } 00398 00399 if (g3d_precision == G3D_NO_DEFAULT) { 00400 if (NULL != getenv(G3D_PRECISION_ENV_VAR_MAX)) { 00401 g3d_precision = G3D_MAX_PRECISION; 00402 } 00403 else { 00404 value = getenv(G3D_PRECISION_ENV_VAR); 00405 if (value == NULL) { 00406 g3d_precision = G3D_PRECISION_DEFAULT; 00407 } 00408 else { 00409 if (sscanf(value, "%d", &g3d_precision) != 1) { 00410 G3d_fatalError 00411 ("G3d_initDefaults: precision environment variable has invalid value"); 00412 } 00413 else { 00414 if (g3d_precision < -1) { 00415 G3d_fatalError 00416 ("G3d_initDefaults: value for cache environment variable out of range"); 00417 } 00418 } 00419 } 00420 } 00421 } 00422 00423 if (g3d_file_type == G3D_NO_DEFAULT) { 00424 if (NULL != getenv(G3D_FILE_FLOAT_ENV_VAR)) { 00425 g3d_file_type = FCELL_TYPE; 00426 } 00427 else { 00428 if (NULL != getenv(G3D_FILE_DOUBLE_ENV_VAR)) { 00429 g3d_file_type = DCELL_TYPE; 00430 } 00431 else { 00432 g3d_file_type = G3D_FILE_TYPE_DEFAULT; 00433 } 00434 } 00435 } 00436 00437 if (g3d_cache_default == G3D_NO_DEFAULT) { 00438 value = getenv(G3D_CACHE_SIZE_ENV_VAR); 00439 00440 if (value == NULL) { 00441 g3d_cache_default = G3D_CACHE_SIZE_DEFAULT; 00442 } 00443 else { 00444 if (sscanf(value, "%d", &g3d_cache_default) != 1) { 00445 G3d_fatalError 00446 ("G3d_initDefaults: cache environment variable has invalid value"); 00447 } 00448 if (g3d_cache_default < 0) { 00449 G3d_fatalError 00450 ("G3d_initDefaults: value for cache environment variable out of range"); 00451 } 00452 } 00453 } 00454 00455 if (g3d_cache_max == G3D_NO_DEFAULT) { 00456 value = getenv(G3D_CACHE_SIZE_MAX_ENV_VAR); 00457 00458 if (value == NULL) { 00459 g3d_cache_max = G3D_CACHE_SIZE_MAX_DEFAULT; 00460 } 00461 else { 00462 if (sscanf(value, "%d", &g3d_cache_max) != 1) { 00463 G3d_fatalError 00464 ("G3d_initDefaults: cache environment variable has invalid value"); 00465 } 00466 if (g3d_cache_max < 0) { 00467 G3d_fatalError 00468 ("G3d_initDefaults: value for cache environment variable out of range"); 00469 } 00470 } 00471 } 00472 00473 if (g3d_tile_dimension[0] == G3D_NO_DEFAULT) { 00474 value = getenv(G3D_TILE_DIM_X_ENV_VAR); 00475 00476 if (value == NULL) { 00477 g3d_tile_dimension[0] = G3D_TILE_X_DEFAULT; 00478 } 00479 else { 00480 if (sscanf(value, "%d", g3d_tile_dimension) != 1) { 00481 G3d_fatalError 00482 ("G3d_initDefaults: tile dimension x environment variable has invalid value"); 00483 } 00484 if (g3d_tile_dimension[0] <= 0) { 00485 G3d_fatalError 00486 ("G3d_initDefaults: value for tile x environment variable out of range"); 00487 } 00488 } 00489 00490 value = getenv(G3D_TILE_DIM_Y_ENV_VAR); 00491 00492 if (value == NULL) { 00493 g3d_tile_dimension[1] = G3D_TILE_Y_DEFAULT; 00494 } 00495 else { 00496 if (sscanf(value, "%d", g3d_tile_dimension + 1) != 1) { 00497 G3d_fatalError 00498 ("G3d_initDefaults: tile dimension y environment variable has invalid value"); 00499 } 00500 if (g3d_tile_dimension[1] <= 0) { 00501 G3d_fatalError 00502 ("G3d_initDefaults: value for tile y environment variable out of range"); 00503 } 00504 } 00505 00506 value = getenv(G3D_TILE_DIM_Z_ENV_VAR); 00507 00508 if (value == NULL) { 00509 g3d_tile_dimension[2] = G3D_TILE_Z_DEFAULT; 00510 } 00511 else { 00512 if (sscanf(value, "%d", g3d_tile_dimension + 2) != 1) { 00513 G3d_fatalError 00514 ("G3d_initDefaults: tile dimension z environment variable has invalid value"); 00515 } 00516 if (g3d_tile_dimension[2] <= 0) { 00517 G3d_fatalError 00518 ("G3d_initDefaults: value for tile z environment variable out of range"); 00519 } 00520 } 00521 } 00522 00523 if (g3d_error_fun == NULL) { 00524 value = getenv(G3D_FATAL_ERROR_ENV_VAR); 00525 00526 if (value != NULL) { 00527 g3d_error_fun = G3d_fatalError_noargs; 00528 } 00529 else { 00530 value = getenv(G3D_PRINT_ERROR_ENV_VAR); 00531 00532 if (value != NULL) { 00533 g3d_error_fun = G3d_printError; 00534 } 00535 else { 00536 g3d_error_fun = G3D_ERROR_FUN_DEFAULT; 00537 } 00538 } 00539 } 00540 00541 if (g3d_unit_default == NULL) 00542 g3d_unit_default = G_store(G3D_UNIT_DEFAULT); 00543 00544 windowName = G3d_getWindowParams(); 00545 if (windowName == NULL) { 00546 value = getenv(G3D_DEFAULT_WINDOW3D); 00547 if (value != NULL) 00548 if (*value != 0) 00549 windowName = value; 00550 } 00551 00552 if (!G3d_readWindow(&window, windowName)) 00553 G3d_fatalError("G3d_initDefaults: Error reading window"); 00554 G3d_setWindow(&window); 00555 00556 }