utilities.c
来自「CA仿真模型中SLEUTH模型」· C语言 代码 · 共 993 行 · 第 1/2 页
C
993 行
#define UTILITIES_MODULE#include <stdlib.h>#include <assert.h>#include <string.h>#include <ctype.h>#include <math.h>#include <errno.h>#include "coeff_obj.h"#include "scenario_obj.h"#include "igrid_obj.h"#include "landclass_obj.h"#include "globals.h"#include "utilities.h"#include "random.h"#include "input.h"#include "ugm_macros.h"#include "memory_obj.h"#include "proc_obj.h"#include "color_obj.h"#include "gdif_obj.h"/*****************************************************************************\********************************************************************************* **** SCCS ID **** *********************************************************************************\*****************************************************************************/char utilities_c_sccs_id[] = "@(#)utilities.c 1.479 12/4/00";/*************************************************************************************************************************************************************** FUNCTION NAME: util_copy_grid** PURPOSE: copy grid from source to target** AUTHOR: Keith Clarke** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500** CREATION DATE: 11/11/1999** DESCRIPTION:*****/void util_copy_grid (GRID_P source, /* IN */ GRID_P target) /* OUT */{ char func[] = "util_copy_grid"; int total_pixels; FUNC_INIT; assert (source != NULL); assert (target != NULL); total_pixels = mem_GetTotalPixels (); assert (total_pixels > 0); memcpy (target, source, sizeof (PIXEL) * total_pixels); FUNC_END;}/*************************************************************************************************************************************************************** FUNCTION NAME: util_init_grid** PURPOSE: initialize a grid with value** AUTHOR: Keith Clarke** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500** CREATION DATE: 11/11/1999** DESCRIPTION:*****/void util_init_grid (GRID_P gif, /* OUT */ PIXEL value /* IN */ ) /* IN */{ char func[] = "util_init_grid"; int i; int total_pixels; FUNC_INIT; total_pixels = mem_GetTotalPixels (); assert (gif != NULL); assert (total_pixels > 0); for (i = 0; i < total_pixels; i++) { gif[i] = value; } FUNC_END;}/*************************************************************************************************************************************************************** FUNCTION NAME: util_condition_gif** PURPOSE: set the pixels in target based on values in source grid** AUTHOR: Keith Clarke** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500** CREATION DATE: 11/11/1999** DESCRIPTION:*****/void util_condition_gif (int num_pixels, /* IN */ GRID_P source, /* IN */ int option, /* IN */ int cmp_value, /* IN */ GRID_P target, /* OUT */ int set_value) /* IN */{ char func[] = "util_condition_gif"; int i; FUNC_INIT; assert (num_pixels > 0); assert (source != NULL); assert (target != NULL); switch (option) { case LT: for (i = 0; i < num_pixels; i++) { if (source[i] < cmp_value) { target[i] = set_value; } } break; case LE: for (i = 0; i < num_pixels; i++) { if (source[i] <= cmp_value) { target[i] = set_value; } } break; case EQ: for (i = 0; i < num_pixels; i++) { if (source[i] == cmp_value) { target[i] = set_value; } } break; case NE: for (i = 0; i < num_pixels; i++) { if (source[i] != cmp_value) { target[i] = set_value; } } break; case GE: for (i = 0; i < num_pixels; i++) { if (source[i] >= cmp_value) { target[i] = set_value; } } break; case GT: for (i = 0; i < num_pixels; i++) { if (source[i] > cmp_value) { target[i] = set_value; } } break; default: sprintf (msg_buf, "Unknown option = %d", option); LOG_ERROR (msg_buf); EXIT (1); } FUNC_END;}/*************************************************************************************************************************************************************** FUNCTION NAME: util_img_intersection** PURPOSE: count the # of similar pixels in two given grids** AUTHOR: Keith Clarke** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500** CREATION DATE: 11/11/1999** DESCRIPTION:*****/int util_img_intersection (int num_pixels, /* IN */ GRID_P ptr1, /* IN */ GRID_P ptr2) /* IN */{ char func[] = "util_image_intersection"; int i; int count = 0; FUNC_INIT; assert (num_pixels > 0); assert (ptr1 != NULL); assert (ptr2 != NULL); for (i = 0; i < num_pixels; i++) { if (ptr1[i] == ptr2[i]) { count++; } } FUNC_END; return count;}/*************************************************************************************************************************************************************** FUNCTION NAME: util_count_pixels** PURPOSE: count pixels meeting option & value conditionals** AUTHOR: Keith Clarke** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500** CREATION DATE: 11/11/1999** DESCRIPTION:*****/int util_count_pixels (int num_pixels, /* IN */ GRID_P pixels, /* IN */ int option, /* IN */ int value) /* IN */{ char func[] = "util_count_pixels"; int i; int count = 0; FUNC_INIT; assert (num_pixels > 0); assert (pixels != NULL); switch (option) { case LT: for (i = 0; i < num_pixels; i++) { if (pixels[i] < value) { count++; } } break; case LE: for (i = 0; i < num_pixels; i++) { if (pixels[i] <= value) { count++; } } break; case EQ: for (i = 0; i < num_pixels; i++) { if (pixels[i] == value) { count++; } } break; case NE: for (i = 0; i < num_pixels; i++) { if (pixels[i] != value) { count++; } } break; case GE: for (i = 0; i < num_pixels; i++) { if (pixels[i] >= value) { count++; } } break; case GT: for (i = 0; i < num_pixels; i++) { if (pixels[i] > value) { count++; } } break; default: sprintf (msg_buf, "Unknown option = %d", option); LOG_ERROR (msg_buf); EXIT (1); } FUNC_END; return (count);}/*************************************************************************************************************************************************************** FUNCTION NAME: util_merge_background** PURPOSE: merge the background image with the foreground image** AUTHOR: Keith Clarke** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500** CREATION DATE: 11/11/1999** DESCRIPTION:*****/void util_merge_background (GRID_P foreground_gif, /* IN */ GRID_P background_gif, /* IN */ GRID_P merged_gif) /* OUT */{ char func[] = "util_merge_background"; int i; int fore_pixel; int back_pixel; int merge_pixel; int total_pixels; FUNC_INIT; assert (foreground_gif != NULL); assert (background_gif != NULL); assert (merged_gif != NULL); total_pixels = mem_GetTotalPixels (); assert (total_pixels > 0); for (i = 0; i < total_pixels; i++) { fore_pixel = foreground_gif[i]; back_pixel = background_gif[i]; if (fore_pixel < 50) { if (back_pixel > 11) { merge_pixel = back_pixel; } else { merge_pixel = 12; } } else { if (fore_pixel == 100) { merge_pixel = 11; } else { merge_pixel = ((fore_pixel - 50) / 5) + 1; } } merged_gif[i] = merge_pixel; } FUNC_END;}/*************************************************************************************************************************************************************** FUNCTION NAME: util_trim** PURPOSE: trim string from left and right** AUTHOR: Keith Clarke** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500** CREATION DATE: 11/11/1999** DESCRIPTION:*****/int util_trim (char s[]) /* IN/OUT */{ int n; char *temp; char func[] = "util_trim"; FUNC_INIT; if (s != NULL) { temp = s; for (n = strlen (s) - 1; n >= 0; n--) { if ((s[n] != ' ') && (s[n] != '\t' && s[n] != '\n')) break; } s[n + 1] = '\0'; for (n = 0; n < strlen (s); n++) { if ((s[n] != ' ') && (s[n] != '\t' && s[n] != '\n')) { temp = &s[n]; break; } } if (temp != s) { for (n = 0; n <= strlen (temp); n++) { s[n] = temp[n]; } } } FUNC_END; return strlen (s);}/*************************************************************************************************************************************************************** FUNCTION NAME: util_get_neighbor** PURPOSE: return a randomly selected neighbor** AUTHOR: Keith Clarke** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500** CREATION DATE: 11/11/1999** DESCRIPTION:*****/void util_get_neighbor (int i_in, /* IN */ int j_in, /* IN */ int *i_out, /* OUT */ int *j_out) /* OUT */{ char func[] = "util_get_neighbor"; int random_int; int i_adj; int j_adj; int row[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; int col[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; FUNC_INIT; assert (i_out != NULL); assert (j_out != NULL); /* * * -------------------------------------- * |0 |7 |6 | * | (-1,-1) | (-1, 0) | (-1, 1) | * | | | | * -------------------------------------- * |1 | |5 | * | ( 0,-1) |(i_in,j_in) | ( 0, 1) | * | | | | * -------------------------------------- * |2 |3 |4 | * | ( 1,-1) | ( 1, 0) | ( 1, 1) | * | | | | * -------------------------------------- * * * row[] and col[] contain offsets from (i_in,j_in) * * i_out - row coord. of (i_in,j_in) randomly selected neighbor * j_out - col coord. of (i_in,j_in) randomly selected neighbor * */ random_int = RANDOM_INT (8); i_adj = row[random_int]; j_adj = col[random_int]; (*i_out) = i_in + i_adj; (*j_out) = j_in + j_adj; FUNC_END;}/*************************************************************************************************************************************************************** FUNCTION NAME: util_count_neighbors** PURPOSE: count the neighbors meeting certain criteria** AUTHOR: Keith Clarke** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500** CREATION DATE: 11/11/1999** DESCRIPTION:*****/int util_count_neighbors (GRID_P grid, /* IN */ int i, /* IN */ int j, /* IN */ int option, /* IN */ PIXEL value) /* IN */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?