int gaussurf(char *out, /* Name of raster maps to be opened. */ double mean, double sigma /* Distribution parameters. */ ) { int nrows, ncols; /* Number of cell rows and columns */ DCELL *row_out; /* Buffer just large enough to hold one */ /* row of the raster map layer. */ int fd_out; /* File descriptor - used to identify */ /* open raster maps. */ struct History history; /* cmd line history metadata */ int row_count, col_count; /****** INITIALISE RANDOM NUMBER GENERATOR ******/ G_math_rand(-1 * getpid()); /****** OPEN CELL FILES AND GET CELL DETAILS ******/ fd_out = G_open_raster_new(out, DCELL_TYPE); nrows = G_window_rows(); ncols = G_window_cols(); row_out = G_allocate_d_raster_buf(); /****** PASS THROUGH EACH CELL ASSIGNING RANDOM VALUE ******/ for (row_count = 0; row_count < nrows; row_count++) { for (col_count = 0; col_count < ncols; col_count++) *(row_out + col_count) = (DCELL) (G_math_rand_gauss(2742, sigma) + mean); /* Write contents row by row */ G_put_d_raster_row(fd_out, (DCELL *) row_out); } /****** CLOSE THE CELL FILE ******/ G_close_cell(fd_out); G_short_history(out, "raster", &history); G_command_history(&history); G_write_history(out, &history); return 0; }
int main( int argc, char **argv ) { struct GModule *module; struct Option *info_opt, *rast_opt, *vect_opt, *coor_opt; struct Cell_head window; /* Initialize the GIS calls */ G_gisinit( argv[0] ); module = G_define_module(); module->description = ( "Get info about locations,mapsets,maps" ); info_opt = G_define_option(); info_opt->key = "info"; info_opt->type = TYPE_STRING; info_opt->description = "info key"; info_opt->options = "proj,window,query"; rast_opt = G_define_standard_option( G_OPT_R_INPUT ); rast_opt->key = "rast"; rast_opt->required = NO; vect_opt = G_define_standard_option( G_OPT_V_INPUT ); vect_opt->key = "vect"; vect_opt->required = NO; coor_opt = G_define_option(); coor_opt->key = "coor"; coor_opt->type = TYPE_DOUBLE; coor_opt->multiple = YES; if ( G_parser( argc, argv ) ) exit( EXIT_FAILURE ); if ( strcmp( "proj", info_opt->answer ) == 0 ) { G_get_window( &window ); /* code from g.proj */ if ( window.proj != PROJECTION_XY ) { struct Key_Value *projinfo, *projunits; char *wkt; projinfo = G_get_projinfo(); projunits = G_get_projunits(); wkt = GPJ_grass_to_wkt( projinfo, projunits, 0, 0 ); fprintf( stdout, "%s", wkt ); } } else if ( strcmp( "window", info_opt->answer ) == 0 ) { if ( rast_opt->answer ) { G_get_cellhd( rast_opt->answer, "", &window ); fprintf( stdout, "%f,%f,%f,%f", window.west, window.south, window.east, window.north ); } else if ( vect_opt->answer ) { G_fatal_error( "Not yet supported" ); } } else if ( strcmp( "query", info_opt->answer ) == 0 ) { double x, y; int row, col; x = atof( coor_opt->answers[0] ); y = atof( coor_opt->answers[1] ); if ( rast_opt->answer ) { int fd; RASTER_MAP_TYPE rast_type; DCELL *dcell; CELL *cell; G_get_cellhd( rast_opt->answer, "", &window ); G_set_window( &window ); fd = G_open_cell_old( rast_opt->answer, "" ); col = ( int ) G_easting_to_col( x, &window ); row = ( int ) G_northing_to_row( y, &window ); if ( col == window.cols ) col--; if ( row == window.rows ) row--; if ( col < 0 || col > window.cols || row < 0 || row > window.rows ) { fprintf( stdout, "value:null\n" ); } else { void *ptr; double val; #if defined(GRASS_VERSION_MAJOR) && defined(GRASS_VERSION_MINOR) && \ ( ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR > 2 ) || GRASS_VERSION_MAJOR > 6 ) rast_type = G_get_raster_map_type( fd ); #else rast_type = G_raster_map_type( rast_opt->answer, "" ); #endif cell = G_allocate_c_raster_buf(); dcell = G_allocate_d_raster_buf(); if ( rast_type == CELL_TYPE ) { if ( G_get_c_raster_row( fd, cell, row ) < 0 ) { G_fatal_error(( "Unable to read raster map <%s> row %d" ), rast_opt->answer, row ); } val = cell[col]; ptr = &( cell[col] ); } else { if ( G_get_d_raster_row( fd, dcell, row ) < 0 ) { G_fatal_error(( "Unable to read raster map <%s> row %d" ), rast_opt->answer, row ); } val = dcell[col]; ptr = &( dcell[col] ); } if ( G_is_null_value( ptr, rast_type ) ) { fprintf( stdout, "value:null\n" ); } else { fprintf( stdout, "value:%f\n", val ); } } G_close_cell( fd ); } else if ( vect_opt->answer ) { G_fatal_error( "Not yet supported" ); } } exit( EXIT_SUCCESS ); }
CPLErr GRASSRasterBand::IRasterIO ( GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize, void * pData, int nBufXSize, int nBufYSize, GDALDataType eBufType, int nPixelSpace, int nLineSpace ) { /* GRASS library does that, we have only calculate and reset the region in map units * and if the region has changed, reopen the raster */ /* Calculate the region */ struct Cell_head sWindow; struct Cell_head *psDsWindow; if ( ! this->valid ) return CE_Failure; psDsWindow = &(((GRASSDataset *)poDS)->sCellInfo); sWindow.north = psDsWindow->north - nYOff * psDsWindow->ns_res; sWindow.south = sWindow.north - nYSize * psDsWindow->ns_res; sWindow.west = psDsWindow->west + nXOff * psDsWindow->ew_res; sWindow.east = sWindow.west + nXSize * psDsWindow->ew_res; sWindow.proj = psDsWindow->proj; sWindow.zone = psDsWindow->zone; sWindow.cols = nBufXSize; sWindow.rows = nBufYSize; /* Reset resolution */ G_adjust_Cell_head ( &sWindow, 1, 1); if ( ResetReading ( &sWindow ) != CE_None ) { return CE_Failure; } /* Read Data */ CELL *cbuf = NULL; FCELL *fbuf = NULL; DCELL *dbuf = NULL; bool direct = false; /* Reset space if default (0) */ if ( nPixelSpace == 0 ) nPixelSpace = GDALGetDataTypeSize ( eBufType ) / 8; if ( nLineSpace == 0 ) nLineSpace = nBufXSize * nPixelSpace; if ( nGRSType == CELL_TYPE && ( !nativeNulls || eBufType != GDT_Int32 || sizeof(CELL) != 4 || nPixelSpace != sizeof(CELL) ) ) { cbuf = G_allocate_c_raster_buf(); } else if( nGRSType == FCELL_TYPE && ( eBufType != GDT_Float32 || nPixelSpace != sizeof(FCELL) ) ) { fbuf = G_allocate_f_raster_buf(); } else if( nGRSType == DCELL_TYPE && ( eBufType != GDT_Float64 || nPixelSpace != sizeof(DCELL) ) ) { dbuf = G_allocate_d_raster_buf(); } else { direct = true; } for ( int row = 0; row < nBufYSize; row++ ) { char *pnt = (char *)pData + row * nLineSpace; if ( nGRSType == CELL_TYPE ) { if ( direct ) { G_get_c_raster_row ( hCell, (CELL *) pnt, row ); } else { G_get_c_raster_row ( hCell, cbuf, row ); /* Reset NULLs */ for ( int col = 0; col < nBufXSize; col++ ) { if ( G_is_c_null_value(&(cbuf[col])) ) cbuf[col] = (CELL) dfNoData; } GDALCopyWords ( (void *) cbuf, GDT_Int32, sizeof(CELL), (void *) pnt, eBufType, nPixelSpace, nBufXSize ); } } else if( nGRSType == FCELL_TYPE ) { if ( direct ) { G_get_f_raster_row ( hCell, (FCELL *) pnt, row ); } else { G_get_f_raster_row ( hCell, fbuf, row ); GDALCopyWords ( (void *) fbuf, GDT_Float32, sizeof(FCELL), (void *) pnt, eBufType, nPixelSpace, nBufXSize ); } } else if( nGRSType == DCELL_TYPE ) { if ( direct ) { G_get_d_raster_row ( hCell, (DCELL *) pnt, row ); } else { G_get_d_raster_row ( hCell, dbuf, row ); GDALCopyWords ( (void *) dbuf, GDT_Float64, sizeof(DCELL), (void *) pnt, eBufType, nPixelSpace, nBufXSize ); } } } if ( cbuf ) G_free ( cbuf ); if ( fbuf ) G_free ( fbuf ); if ( dbuf ) G_free ( dbuf ); return CE_None; }
int main( int argc, char **argv ) { struct GModule *module; struct Option *info_opt, *rast_opt, *vect_opt, *coor_opt, *north_opt, *south_opt, *east_opt, *west_opt, *rows_opt, *cols_opt; struct Cell_head window; /* Initialize the GIS calls */ G_gisinit( argv[0] ); module = G_define_module(); module->description = ( "Get info about locations,mapsets,maps" ); info_opt = G_define_option(); info_opt->key = "info"; info_opt->type = TYPE_STRING; info_opt->description = "info key"; info_opt->options = "proj,window,size,query,info,colors,stats"; rast_opt = G_define_standard_option( G_OPT_R_INPUT ); rast_opt->key = "rast"; rast_opt->required = NO; vect_opt = G_define_standard_option( G_OPT_V_INPUT ); vect_opt->key = "vect"; vect_opt->required = NO; coor_opt = G_define_option(); coor_opt->key = "coor"; coor_opt->type = TYPE_DOUBLE; coor_opt->multiple = YES; north_opt = G_define_option(); north_opt->key = "north"; north_opt->type = TYPE_STRING; south_opt = G_define_option(); south_opt->key = "south"; south_opt->type = TYPE_STRING; east_opt = G_define_option(); east_opt->key = "east"; east_opt->type = TYPE_STRING; west_opt = G_define_option(); west_opt->key = "west"; west_opt->type = TYPE_STRING; rows_opt = G_define_option(); rows_opt->key = "rows"; rows_opt->type = TYPE_INTEGER; cols_opt = G_define_option(); cols_opt->key = "cols"; cols_opt->type = TYPE_INTEGER; if ( G_parser( argc, argv ) ) exit( EXIT_FAILURE ); if ( strcmp( "proj", info_opt->answer ) == 0 ) { G_get_window( &window ); /* code from g.proj */ if ( window.proj != PROJECTION_XY ) { struct Key_Value *projinfo, *projunits; char *wkt; projinfo = G_get_projinfo(); projunits = G_get_projunits(); wkt = GPJ_grass_to_wkt( projinfo, projunits, 0, 0 ); fprintf( stdout, "%s", wkt ); } } else if ( strcmp( "window", info_opt->answer ) == 0 ) { if ( rast_opt->answer ) { G_get_cellhd( rast_opt->answer, "", &window ); fprintf( stdout, "%f,%f,%f,%f", window.west, window.south, window.east, window.north ); } else if ( vect_opt->answer ) { G_fatal_error( "Not yet supported" ); } } // raster width and height else if ( strcmp( "size", info_opt->answer ) == 0 ) { if ( rast_opt->answer ) { G_get_cellhd( rast_opt->answer, "", &window ); fprintf( stdout, "%d,%d", window.cols, window.rows ); } else if ( vect_opt->answer ) { G_fatal_error( "Not yet supported" ); } } // raster informations else if ( strcmp( "info", info_opt->answer ) == 0 ) { struct FPRange range; double zmin, zmax; // Data type RASTER_MAP_TYPE raster_type = G_raster_map_type( rast_opt->answer, "" ); fprintf( stdout, "TYPE:%d\n", raster_type ); // Statistics if ( G_read_fp_range( rast_opt->answer, "", &range ) < 0 ) { G_fatal_error(( "Unable to read range file" ) ); } G_get_fp_range_min_max( &range, &zmin, &zmax ); fprintf( stdout, "MIN_VALUE:%.17e\n", zmin ); fprintf( stdout, "MAX_VALUE:%.17e\n", zmax ); } else if ( strcmp( "colors", info_opt->answer ) == 0 ) { // Color table struct Colors colors; int i, ccount; if ( G_read_colors( rast_opt->answer, "", &colors ) == 1 ) { //int maxcolor; //CELL min, max; //G_get_color_range ( &min, &max, &colors); ccount = G_colors_count( &colors ); for ( i = ccount - 1; i >= 0; i-- ) { DCELL val1, val2; unsigned char r1, g1, b1, r2, g2, b2; G_get_f_color_rule( &val1, &r1, &g1, &b1, &val2, &r2, &g2, &b2, &colors, i ); fprintf( stdout, "%.17e %.17e %d %d %d %d %d %d\n", val1, val2, r1, g1, b1, r2, g2, b2 ); } } } else if ( strcmp( "query", info_opt->answer ) == 0 ) { double x, y; int row, col; //x = atof( coor_opt->answers[0] ); //y = atof( coor_opt->answers[1] ); if ( rast_opt->answer ) { int fd; RASTER_MAP_TYPE rast_type; DCELL *dcell; CELL *cell; char buff[101]; G_get_cellhd( rast_opt->answer, "", &window ); G_set_window( &window ); fd = G_open_cell_old( rast_opt->answer, "" ); // wait for coors from stdin while ( fgets( buff, 100, stdin ) != 0 ) { if ( sscanf( buff, "%lf%lf", &x, &y ) != 2 ) { fprintf( stdout, "value:error\n" ); } else { col = ( int ) G_easting_to_col( x, &window ); row = ( int ) G_northing_to_row( y, &window ); if ( col == window.cols ) col--; if ( row == window.rows ) row--; if ( col < 0 || col > window.cols || row < 0 || row > window.rows ) { fprintf( stdout, "value:out\n" ); } else { void *ptr; double val; #if defined(GRASS_VERSION_MAJOR) && defined(GRASS_VERSION_MINOR) && \ ( ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR > 2 ) || GRASS_VERSION_MAJOR > 6 ) rast_type = G_get_raster_map_type( fd ); #else rast_type = G_raster_map_type( rast_opt->answer, "" ); #endif cell = G_allocate_c_raster_buf(); dcell = G_allocate_d_raster_buf(); if ( rast_type == CELL_TYPE ) { if ( G_get_c_raster_row( fd, cell, row ) < 0 ) { G_fatal_error(( "Unable to read raster map <%s> row %d" ), rast_opt->answer, row ); } val = cell[col]; ptr = &( cell[col] ); } else { if ( G_get_d_raster_row( fd, dcell, row ) < 0 ) { G_fatal_error(( "Unable to read raster map <%s> row %d" ), rast_opt->answer, row ); } val = dcell[col]; ptr = &( dcell[col] ); } if ( G_is_null_value( ptr, rast_type ) ) { fprintf( stdout, "value:null\n" ); } else { fprintf( stdout, "value:%f\n", val ); } } } fflush( stdout ); } G_close_cell( fd ); } else if ( vect_opt->answer ) { G_fatal_error( "Not yet supported" ); } } else if ( strcmp( "stats", info_opt->answer ) == 0 ) { if ( rast_opt->answer ) { int fd; RASTER_MAP_TYPE rast_type; DCELL *dcell; CELL *cell; int ncols, nrows; int row, col; void *ptr; double val; double min = DBL_MAX; double max = -DBL_MAX; double sum = 0; // sum of values int count = 0; // count of non null values double mean = 0; double squares_sum = 0; // sum of squares double stdev = 0; // standard deviation G_get_cellhd( rast_opt->answer, "", &window ); window.north = atof( north_opt->answer ); window.south = atof( south_opt->answer ); window.east = atof( east_opt->answer ); window.west = atof( west_opt->answer ); window.rows = ( int ) atoi( rows_opt->answer ); window.cols = ( int ) atoi( cols_opt->answer ); G_set_window( &window ); fd = G_open_cell_old( rast_opt->answer, "" ); ncols = G_window_cols(); nrows = G_window_rows(); #if defined(GRASS_VERSION_MAJOR) && defined(GRASS_VERSION_MINOR) && \ ( ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR > 2 ) || GRASS_VERSION_MAJOR > 6 ) rast_type = G_get_raster_map_type( fd ); #else rast_type = G_raster_map_type( rast_opt->answer, "" ); #endif cell = G_allocate_c_raster_buf(); dcell = G_allocate_d_raster_buf(); // Calc stats is very slow for large rasters -> prefer optimization for speed over // code length and readability (which is not currently true) for ( row = 0; row < nrows; row++ ) { if ( rast_type == CELL_TYPE ) { if ( G_get_c_raster_row( fd, cell, row ) < 0 ) { G_fatal_error(( "Unable to read raster map <%s> row %d" ), rast_opt->answer, row ); } } else { if ( G_get_d_raster_row( fd, dcell, row ) < 0 ) { G_fatal_error(( "Unable to read raster map <%s> row %d" ), rast_opt->answer, row ); } } for ( col = 0; col < ncols; col++ ) { if ( rast_type == CELL_TYPE ) { val = cell[col]; ptr = &( cell[col] ); } else { val = dcell[col]; ptr = &( dcell[col] ); } if ( ! G_is_null_value( ptr, rast_type ) ) { if ( val < min ) min = val; if ( val > max ) max = val; sum += val; count++; squares_sum += pow( val, 2 ); } } } mean = sum / count; squares_sum -= count * pow( mean, 2 ); stdev = sqrt( squares_sum / ( count - 1 ) ); fprintf( stdout, "MIN:%.17e\n", min ); fprintf( stdout, "MAX:%.17e\n", max ); fprintf( stdout, "SUM:%.17e\n", sum ); fprintf( stdout, "MEAN:%.17e\n", mean ); fprintf( stdout, "COUNT:%d\n", count ); fprintf( stdout, "STDEV:%.17e\n", stdev ); fprintf( stdout, "SQSUM:%.17e\n", squares_sum ); G_close_cell( fd ); } else if ( vect_opt->answer ) { G_fatal_error( "Not yet supported" ); } } exit( EXIT_SUCCESS ); }
int main(int argc, char *argv[]) { char *input; char *output; char *title; FILE *fd; int cf; struct Cell_head cellhd; CELL *cell; FCELL *fcell; DCELL *dcell; int row, col; int nrows, ncols; static int missingval; int rtype; double mult_fact; double x; struct GModule *module; struct History history; struct { struct Option *input, *output, *type, *title, *mult; } parm; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, import"); module->description = _("Converts an ESRI ARC/INFO ascii raster file (GRID) " "into a (binary) raster map layer."); parm.input = G_define_option(); parm.input->key = "input"; parm.input->type = TYPE_STRING; parm.input->required = YES; parm.input->description = _("ARC/INFO ASCII raster file (GRID) to be imported"); parm.input->gisprompt = "old_file,file,input"; parm.output = G_define_standard_option(G_OPT_R_OUTPUT); parm.type = G_define_option(); parm.type->key = "type"; parm.type->type = TYPE_STRING; parm.type->required = NO; parm.type->options = "CELL,FCELL,DCELL"; parm.type->answer = "FCELL"; parm.type->description = _("Storage type for resultant raster map"); parm.title = G_define_option(); parm.title->key = "title"; parm.title->key_desc = "\"phrase\""; parm.title->type = TYPE_STRING; parm.title->required = NO; parm.title->description = _("Title for resultant raster map"); parm.mult = G_define_option(); parm.mult->key = "mult"; parm.mult->type = TYPE_DOUBLE; parm.mult->answer = "1.0"; parm.mult->required = NO; parm.mult->description = _("Multiplier for ASCII data"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); input = parm.input->answer; output = parm.output->answer; if (title = parm.title->answer) G_strip(title); sscanf(parm.mult->answer, "%lf", &mult_fact); if (strcmp("CELL", parm.type->answer) == 0) rtype = CELL_TYPE; else if (strcmp("DCELL", parm.type->answer) == 0) rtype = DCELL_TYPE; else rtype = FCELL_TYPE; if (strcmp("-", input) == 0) { Tmp_file = G_tempfile(); if (NULL == (Tmp_fd = fopen(Tmp_file, "w+"))) G_fatal_error(_("Unable to open temporary file <%s>"), Tmp_file); unlink(Tmp_file); if (0 > file_cpy(stdin, Tmp_fd)) exit(EXIT_FAILURE); fd = Tmp_fd; } else fd = fopen(input, "r"); if (fd == NULL) G_fatal_error(_("Unable to open input file <%s>"), input); if (!gethead(fd, &cellhd, &missingval)) G_fatal_error(_("Can't get cell header")); nrows = cellhd.rows; ncols = cellhd.cols; if (G_set_window(&cellhd) < 0) G_fatal_error(_("Can't set window")); if (nrows != G_window_rows()) G_fatal_error(_("OOPS: rows changed from %d to %d"), nrows, G_window_rows()); if (ncols != G_window_cols()) G_fatal_error(_("OOPS: cols changed from %d to %d"), ncols, G_window_cols()); switch (rtype) { case CELL_TYPE: cell = G_allocate_c_raster_buf(); break; case FCELL_TYPE: fcell = G_allocate_f_raster_buf(); break; case DCELL_TYPE: dcell = G_allocate_d_raster_buf(); break; } cf = G_open_raster_new(output, rtype); if (cf < 0) G_fatal_error(_("Unable to create raster map <%s>"), output); for (row = 0; row < nrows; row++) { G_percent(row, nrows, 5); for (col = 0; col < ncols; col++) { if (fscanf(fd, "%lf", &x) != 1) { G_unopen_cell(cf); G_fatal_error(_("Data conversion failed at row %d, col %d"), row + 1, col + 1); } switch (rtype) { case CELL_TYPE: if ((int)x == missingval) G_set_c_null_value(cell + col, 1); else cell[col] = (CELL) x *mult_fact; break; case FCELL_TYPE: if ((int)x == missingval) G_set_f_null_value(fcell + col, 1); else fcell[col] = (FCELL) x *mult_fact; break; case DCELL_TYPE: if ((int)x == missingval) G_set_d_null_value(dcell + col, 1); else dcell[col] = (DCELL) x *mult_fact; break; } } switch (rtype) { case CELL_TYPE: G_put_c_raster_row(cf, cell); break; case FCELL_TYPE: G_put_f_raster_row(cf, fcell); break; case DCELL_TYPE: G_put_d_raster_row(cf, dcell); break; } } /* G_message(_("CREATING SUPPORT FILES FOR %s"), output); */ G_close_cell(cf); if (title) G_put_cell_title(output, title); G_short_history(output, "raster", &history); G_command_history(&history); G_write_history(output, &history); exit(EXIT_SUCCESS); }
int calculateD(int fd, area_des ad, double *result) { DCELL *buf; DCELL *buf_sup; DCELL corrCell; DCELL precCell; DCELL supCell; int i, j; int mask_fd = -1, *mask_buf; int ris = 0; int masked = FALSE; int areaPatch = 0; /*if all cells are null areaPatch=0 */ long npatch = 0; long tot = 0; long zero = 0; long totCorr = 0; long idCorr = 0; long lastId = 0; long doppi = 0; long np = 0; long *mask_patch_sup; long *mask_patch_corr; double indice = 0; double somma = 0; double area = 0; double mn = 0; double sd = 0; double cv = 0; avlID_tree albero = NULL; avlID_table *array = NULL; generic_cell gc; gc.t = DCELL_TYPE; /* open mask if needed */ if (ad->mask == 1) { if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0) return RLI_ERRORE; mask_buf = G_malloc(ad->cl * sizeof(int)); if (mask_buf == NULL) { G_fatal_error("malloc mask_buf failed"); return RLI_ERRORE; } masked = TRUE; } mask_patch_sup = G_malloc(ad->cl * sizeof(long)); if (mask_patch_sup == NULL) { G_fatal_error("malloc mask_patch_sup failed"); return RLI_ERRORE; } mask_patch_corr = G_malloc(ad->cl * sizeof(long)); if (mask_patch_corr == NULL) { G_fatal_error("malloc mask_patch_corr failed"); return RLI_ERRORE; } buf_sup = G_allocate_d_raster_buf(); if (buf_sup == NULL) { G_fatal_error("malloc buf_sup failed"); return RLI_ERRORE; } buf = G_allocate_d_raster_buf(); if (buf == NULL) { G_fatal_error("malloc buf failed"); return RLI_ERRORE; } G_set_d_null_value(buf_sup + ad->x, ad->cl); /*the first time buf_sup is all null */ for (i = 0; i < ad->cl; i++) { mask_patch_sup[i] = 0; mask_patch_corr[i] = 0; } for (j = 0; j < ad->rl; j++) /*for each raster row */ { if (j > 0) { buf_sup = RLI_get_dcell_raster_row(fd, j - 1 + ad->y, ad); } buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad); if (masked) { if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) { G_fatal_error("mask read failed"); return RLI_ERRORE; } } G_set_d_null_value(&precCell, 1); for (i = 0; i < ad->cl; i++) { /* for each dcell in the row */ area++; corrCell = buf[i + ad->x]; if (masked && mask_buf[i + ad->x] == 0) { G_set_d_null_value(&corrCell, 1); area--; } if (!(G_is_null_value(&corrCell, gc.t))) { areaPatch++; if (i > 0) precCell = buf[i - 1 + ad->x]; if (j == 0) G_set_d_null_value(&supCell, 1); else supCell = buf_sup[i + ad->x]; if (corrCell != precCell) /* ? * 1 2 * */ { if (corrCell != supCell) { /* 3 * 1 2 * */ /*new patch */ if (idCorr == 0) { /*first patch */ lastId = 1; idCorr = 1; totCorr = 1; mask_patch_corr[i] = idCorr; } else /*not first patch */ /* put in the tree the previous value */ { if (albero == NULL) { albero = avlID_make(idCorr, totCorr); if (albero == NULL) { G_fatal_error("avlID_make error"); return RLI_ERRORE; } npatch++; } else /*tree not empty */ { ris = avlID_add(&albero, idCorr, totCorr); switch (ris) { case AVL_ERR: { G_fatal_error("avlID_add error"); return RLI_ERRORE; } case AVL_ADD: { npatch++; break; } case AVL_PRES: { break; } default: { G_fatal_error ("avlID_add unknown error"); return RLI_ERRORE; } } } totCorr = 1; lastId++; idCorr = lastId; mask_patch_corr[i] = idCorr; } } else /*current cell and upper cell are equal */ /* 2 * 1 2 * */ { if (albero == NULL) { albero = avlID_make(idCorr, totCorr); if (albero == NULL) { G_fatal_error("avlID_make error"); return RLI_ERRORE; } npatch++; } else { /*tree not null */ ris = avlID_add(&albero, idCorr, totCorr); switch (ris) { case AVL_ERR: { G_fatal_error("avlID_add error"); return RLI_ERRORE; } case AVL_ADD: { npatch++; break; } case AVL_PRES: { break; } default: { G_fatal_error("avlID_add unknown error"); return RLI_ERRORE; } } } idCorr = mask_patch_sup[i]; mask_patch_corr[i] = idCorr; totCorr = 1; } } else { /*current cell and previuos cell are equal */ /* ? * 1 1 */ if (corrCell == supCell) { /*current cell and upper cell are equal */ /* 1 * 1 1 */ if (mask_patch_sup[i] != mask_patch_corr[i - 1]) { long r = 0; long del = mask_patch_sup[i]; r = avlID_sub(&albero, del); /*r=number of cell of patch removed */ if (r == 0) { G_fatal_error("avlID_sub error"); return RLI_ERRORE; } /*Remove one patch because it makes part of a patch already found */ ris = avlID_add(&albero, idCorr, r); switch (ris) { case AVL_ERR: { G_fatal_error("avlID_add error"); return RLI_ERRORE; } case AVL_ADD: { npatch++; break; } case AVL_PRES: { break; } default: { G_fatal_error("avlID_add unknown error"); return RLI_ERRORE; } } r = i; while (r < ad->cl) { if (mask_patch_sup[r] == del) { mask_patch_sup[r] = idCorr; } r++; } mask_patch_corr[i] = idCorr; } else { mask_patch_corr[i] = idCorr; } } else { /*current cell and upper cell are not equal */ /* 2 * 1 1 */ mask_patch_corr[i] = idCorr; } totCorr++; } } else { /*cell is null or is not to consider */ mask_patch_corr[i] = 0; } } { int ii; long c; for (ii = 0; ii < ad->cl; ii++) { c = mask_patch_corr[ii]; mask_patch_sup[ii] = c; mask_patch_corr[ii] = 0; } } } if (areaPatch != 0) { if (albero == NULL) { albero = avlID_make(idCorr, totCorr); if (albero == NULL) { G_fatal_error("avlID_make error"); return RLI_ERRORE; } npatch++; } else { ris = avlID_add(&albero, idCorr, totCorr); switch (ris) { case AVL_ERR: { G_fatal_error("avlID_add error"); return RLI_ERRORE; } case AVL_ADD: { npatch++; break; } case AVL_PRES: { break; } default: { G_fatal_error("avlID_add unknown error"); return RLI_ERRORE; } } } array = G_malloc(npatch * sizeof(avlID_tableRow)); if (array == NULL) { G_fatal_error("malloc array failed"); return RLI_ERRORE; } tot = avlID_to_array(albero, zero, array); if (tot != npatch) { G_warning ("avlID_to_array unaspected value. the result could be wrong"); return RLI_ERRORE; } for (i = 0; i < npatch; i++) { if (array[i]->tot == 0) { doppi++; } } np = npatch; npatch = npatch - doppi; mn = areaPatch / npatch; /* calculate summary */ for (i = 0; i < np; i++) { long areaPi = 0; double diff; if (array[i]->tot != 0) { ris = ris + array[i]->tot; areaPi = (double)array[i]->tot; diff = areaPi - mn; somma = somma + (diff * diff); } } sd = sqrt(somma / npatch); cv = sd * 100 / mn; indice = cv; G_free(array); } else indice = (double)(-1); if (masked) G_free(mask_buf); G_free(mask_patch_sup); *result = indice; return RLI_OK; }
int main(int argc, char *argv[]) { struct GModule *module; struct Option *rastin, *rastout, *method; struct History history; char title[64]; char buf_nsres[100], buf_ewres[100]; struct Colors colors; char *inmap; int infile, outfile; DCELL *outbuf; int row, col; struct Cell_head dst_w, src_w; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, resample"); module->description = _("Resamples raster map layers to a finer grid using interpolation."); rastin = G_define_standard_option(G_OPT_R_INPUT); rastout = G_define_standard_option(G_OPT_R_OUTPUT); method = G_define_option(); method->key = "method"; method->type = TYPE_STRING; method->required = NO; method->description = _("Interpolation method"); method->options = "nearest,bilinear,bicubic"; method->answer = "bilinear"; if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (G_strcasecmp(method->answer, "nearest") == 0) neighbors = 1; else if (G_strcasecmp(method->answer, "bilinear") == 0) neighbors = 2; else if (G_strcasecmp(method->answer, "bicubic") == 0) neighbors = 4; else G_fatal_error(_("Invalid method: %s"), method->answer); G_get_set_window(&dst_w); inmap = G_find_cell2(rastin->answer, ""); if (!inmap) G_fatal_error(_("Raster map <%s> not found"), rastin->answer); /* set window to old map */ G_get_cellhd(rastin->answer, inmap, &src_w); /* enlarge source window */ { double north = G_row_to_northing(0.5, &dst_w); double south = G_row_to_northing(dst_w.rows - 0.5, &dst_w); int r0 = (int)floor(G_northing_to_row(north, &src_w) - 0.5) - 1; int r1 = (int)floor(G_northing_to_row(south, &src_w) - 0.5) + 3; double west = G_col_to_easting(0.5, &dst_w); double east = G_col_to_easting(dst_w.cols - 0.5, &dst_w); int c0 = (int)floor(G_easting_to_col(west, &src_w) - 0.5) - 1; int c1 = (int)floor(G_easting_to_col(east, &src_w) - 0.5) + 3; src_w.south -= src_w.ns_res * (r1 - src_w.rows); src_w.north += src_w.ns_res * (-r0); src_w.west -= src_w.ew_res * (-c0); src_w.east += src_w.ew_res * (c1 - src_w.cols); src_w.rows = r1 - r0; src_w.cols = c1 - c0; } G_set_window(&src_w); /* allocate buffers for input rows */ for (row = 0; row < neighbors; row++) bufs[row] = G_allocate_d_raster_buf(); cur_row = -100; /* open old map */ infile = G_open_cell_old(rastin->answer, inmap); if (infile < 0) G_fatal_error(_("Unable to open raster map <%s>"), rastin->answer); /* reset window to current region */ G_set_window(&dst_w); outbuf = G_allocate_d_raster_buf(); /* open new map */ outfile = G_open_raster_new(rastout->answer, DCELL_TYPE); if (outfile < 0) G_fatal_error(_("Unable to create raster map <%s>"), rastout->answer); G_suppress_warnings(1); /* otherwise get complaints about window changes */ switch (neighbors) { case 1: /* nearest */ for (row = 0; row < dst_w.rows; row++) { double north = G_row_to_northing(row + 0.5, &dst_w); double maprow_f = G_northing_to_row(north, &src_w) - 0.5; int maprow0 = (int)floor(maprow_f + 0.5); G_percent(row, dst_w.rows, 2); G_set_window(&src_w); read_rows(infile, maprow0); for (col = 0; col < dst_w.cols; col++) { double east = G_col_to_easting(col + 0.5, &dst_w); double mapcol_f = G_easting_to_col(east, &src_w) - 0.5; int mapcol0 = (int)floor(mapcol_f + 0.5); double c = bufs[0][mapcol0]; if (G_is_d_null_value(&c)) { G_set_d_null_value(&outbuf[col], 1); } else { outbuf[col] = c; } } G_set_window(&dst_w); G_put_d_raster_row(outfile, outbuf); } break; case 2: /* bilinear */ for (row = 0; row < dst_w.rows; row++) { double north = G_row_to_northing(row + 0.5, &dst_w); double maprow_f = G_northing_to_row(north, &src_w) - 0.5; int maprow0 = (int)floor(maprow_f); double v = maprow_f - maprow0; G_percent(row, dst_w.rows, 2); G_set_window(&src_w); read_rows(infile, maprow0); for (col = 0; col < dst_w.cols; col++) { double east = G_col_to_easting(col + 0.5, &dst_w); double mapcol_f = G_easting_to_col(east, &src_w) - 0.5; int mapcol0 = (int)floor(mapcol_f); int mapcol1 = mapcol0 + 1; double u = mapcol_f - mapcol0; double c00 = bufs[0][mapcol0]; double c01 = bufs[0][mapcol1]; double c10 = bufs[1][mapcol0]; double c11 = bufs[1][mapcol1]; if (G_is_d_null_value(&c00) || G_is_d_null_value(&c01) || G_is_d_null_value(&c10) || G_is_d_null_value(&c11)) { G_set_d_null_value(&outbuf[col], 1); } else { outbuf[col] = G_interp_bilinear(u, v, c00, c01, c10, c11); } } G_set_window(&dst_w); G_put_d_raster_row(outfile, outbuf); } break; case 4: /* bicubic */ for (row = 0; row < dst_w.rows; row++) { double north = G_row_to_northing(row + 0.5, &dst_w); double maprow_f = G_northing_to_row(north, &src_w) - 0.5; int maprow1 = (int)floor(maprow_f); int maprow0 = maprow1 - 1; double v = maprow_f - maprow1; G_percent(row, dst_w.rows, 2); G_set_window(&src_w); read_rows(infile, maprow0); for (col = 0; col < dst_w.cols; col++) { double east = G_col_to_easting(col + 0.5, &dst_w); double mapcol_f = G_easting_to_col(east, &src_w) - 0.5; int mapcol1 = (int)floor(mapcol_f); int mapcol0 = mapcol1 - 1; int mapcol2 = mapcol1 + 1; int mapcol3 = mapcol1 + 2; double u = mapcol_f - mapcol1; double c00 = bufs[0][mapcol0]; double c01 = bufs[0][mapcol1]; double c02 = bufs[0][mapcol2]; double c03 = bufs[0][mapcol3]; double c10 = bufs[1][mapcol0]; double c11 = bufs[1][mapcol1]; double c12 = bufs[1][mapcol2]; double c13 = bufs[1][mapcol3]; double c20 = bufs[2][mapcol0]; double c21 = bufs[2][mapcol1]; double c22 = bufs[2][mapcol2]; double c23 = bufs[2][mapcol3]; double c30 = bufs[3][mapcol0]; double c31 = bufs[3][mapcol1]; double c32 = bufs[3][mapcol2]; double c33 = bufs[3][mapcol3]; if (G_is_d_null_value(&c00) || G_is_d_null_value(&c01) || G_is_d_null_value(&c02) || G_is_d_null_value(&c03) || G_is_d_null_value(&c10) || G_is_d_null_value(&c11) || G_is_d_null_value(&c12) || G_is_d_null_value(&c13) || G_is_d_null_value(&c20) || G_is_d_null_value(&c21) || G_is_d_null_value(&c22) || G_is_d_null_value(&c23) || G_is_d_null_value(&c30) || G_is_d_null_value(&c31) || G_is_d_null_value(&c32) || G_is_d_null_value(&c33)) { G_set_d_null_value(&outbuf[col], 1); } else { outbuf[col] = G_interp_bicubic(u, v, c00, c01, c02, c03, c10, c11, c12, c13, c20, c21, c22, c23, c30, c31, c32, c33); } } G_set_window(&dst_w); G_put_d_raster_row(outfile, outbuf); } break; } G_percent(dst_w.rows, dst_w.rows, 2); G_close_cell(infile); G_close_cell(outfile); /* record map metadata/history info */ sprintf(title, "Resample by %s interpolation", method->answer); G_put_cell_title(rastout->answer, title); G_short_history(rastout->answer, "raster", &history); strncpy(history.datsrc_1, rastin->answer, RECORD_LEN); history.datsrc_1[RECORD_LEN - 1] = '\0'; /* strncpy() doesn't null terminate if maxfill */ G_format_resolution(src_w.ns_res, buf_nsres, src_w.proj); G_format_resolution(src_w.ew_res, buf_ewres, src_w.proj); sprintf(history.datsrc_2, "Source map NS res: %s EW res: %s", buf_nsres, buf_ewres); G_command_history(&history); G_write_history(rastout->answer, &history); /* copy color table from source map */ if (G_read_colors(rastin->answer, inmap, &colors) < 0) G_fatal_error(_("Unable to read color table for %s"), rastin->answer); G_mark_colors_as_fp(&colors); if (G_write_colors(rastout->answer, G_mapset(), &colors) < 0) G_fatal_error(_("Unable to write color table for %s"), rastout->answer); return (EXIT_SUCCESS); }
/*! \brief Get categories/labels Formats label as in d.what.rast -> (catval) catlabel \param filename raster map name \param drow \param dcol \param catstr category string \return 1 on success \return 0 on failure */ int Gs_get_cat_label(const char *filename, int drow, int dcol, char *catstr) { struct Categories cats; const char *mapset; CELL *buf; DCELL *dbuf; RASTER_MAP_TYPE map_type; int fd; if ((mapset = G_find_cell2(filename, "")) == NULL) { G_warning(_("Raster map <%s> not found"), filename); return 0; } if (-1 != G_read_cats(filename, mapset, &cats)) { fd = G_open_cell_old(filename, mapset); map_type = G_get_raster_map_type(fd); if (map_type == CELL_TYPE) { buf = G_allocate_c_raster_buf(); if (G_get_c_raster_row(fd, buf, drow) < 0) { sprintf(catstr, "error"); } else if (G_is_c_null_value(&buf[dcol])) { sprintf(catstr, "(NULL) %s", G_get_c_raster_cat(&buf[dcol], &cats)); } else { sprintf(catstr, "(%d) %s", buf[dcol], G_get_c_raster_cat(&buf[dcol], &cats)); } G_free(buf); } else { /* fp map */ dbuf = G_allocate_d_raster_buf(); if (G_get_d_raster_row(fd, dbuf, drow) < 0) { sprintf(catstr, "error"); } else if (G_is_d_null_value(&dbuf[dcol])) { sprintf(catstr, "(NULL) %s", G_get_d_raster_cat(&dbuf[dcol], &cats)); } else { sprintf(catstr, "(%g) %s", dbuf[dcol], G_get_d_raster_cat(&dbuf[dcol], &cats)); } G_free(dbuf); } } else { strcpy(catstr, "no category label"); } /* TODO: may want to keep these around for multiple queries */ G_free_cats(&cats); G_close_cell(fd); return (1); }
void make_result_row ( int val, char *basename, char **hyps, int no_hyps, Sresult_struct *result_row, xmlDocPtr doc ) { int i; char* val_names[NUMVALS]={"bel","pl","doubt","common","bint","woc","maxbpa","minbpa", "maxsrc","minsrc"}; DCELL *v1; DCELL *v2; result_row->use = YES; /* need an array of DCELL rows to store bel, pl and other DST values */ if ( val == WOC ) { /* WOC (Weight of Conflict is always treated a bit differently, because we need this only once for all hypotheses in the FOD */ result_row->row[0] = (DCELL*) G_allocate_d_raster_buf (); } else { if (( val == MAXSRC) || ( val == MINSRC)) { for (i = 0; i < no_hyps; i++ ) { result_row->crow[i] = (CELL*) G_allocate_c_raster_buf (); } } else { for (i = 0; i < no_hyps; i++ ) { result_row->row[i] = (DCELL*) G_allocate_d_raster_buf (); } } } if ( val == WOC ) { result_row->filename = (char**) G_calloc ( sizeof (char*), 1); /* there is only one file for storing the WOC */ result_row->filename[0] = G_malloc ((unsigned) ((sizeof (char) * strlen (basename)) + (sizeof (char) * strlen (val_names[val])) + 2)); strcpy (result_row->filename[0],basename); strcat (result_row->filename[0],"."); strcat (result_row->filename[0],val_names[val]); } else { result_row->filename = (char**) G_calloc ( sizeof (char*), (unsigned) no_hyps); /* for all other metrics, we need one output file per hypothesis */ for (i=0; i<no_hyps;i++) { result_row->filename[i] = G_malloc ((unsigned)((sizeof (char) * strlen (basename)) + (sizeof (char) * strlen (hyps[i])) + (sizeof (char) * strlen (val_names[val])) + 3)); strcpy (result_row->filename[i],basename); strcat (result_row->filename[i],"."); strcat (result_row->filename[i],hyps[i]); strcat (result_row->filename[i],"."); strcat (result_row->filename[i],val_names[val]); G_strchg (result_row->filename[i], ',', '.'); } } /* allocate file descriptors */ if ( val == WOC ) { result_row->fd = (int*) G_calloc ( sizeof (int), 1); result_row->fd[0] = -1; } else { result_row->fd = (int*) G_calloc ( sizeof (int), (unsigned) no_hyps); for (i=0; i<no_hyps;i++) { result_row->fd[i] = -1; } } /* init color tables for output maps */ v1 = (DCELL*) G_malloc (sizeof (DCELL)); v2 = (DCELL*) G_malloc (sizeof (DCELL)); if ( val == WOC ) { result_row->colors = (struct Colors **) G_calloc ( sizeof (struct Colors*), 1); result_row->colors[0] = G_malloc ( sizeof (struct Colors)); G_init_colors (result_row->colors[0]); /* *v1 = (DCELL) WOC_MIN; *v2 = (DCELL) WOC_MAX; */ *v1 = 0; *v2 = 1.001; G_add_d_raster_color_rule (v1,0,0,0,v2,255,0,0, result_row->colors[0]); } if (( val == BINT ) || (val==MAXBPA) || (val==MINBPA) ){ result_row->colors = (struct Colors **) G_calloc ( sizeof (struct Colors*), (unsigned) no_hyps); for (i=0; i<no_hyps;i++) { result_row->colors[i] = G_malloc ( sizeof (struct Colors)); G_init_colors (result_row->colors[i]); *v1 = 0; *v2 = 1.001; G_add_d_raster_color_rule (v1,0,0,0,v2,255,0,0, result_row->colors[i]); } } if ((val == BEL) || (val==PL) || (val==DOUBT) || (val==COMMON )) { result_row->colors = (struct Colors **) G_calloc ( sizeof (struct Colors*), (unsigned) no_hyps); for (i=0; i<no_hyps;i++) { result_row->colors[i] = G_malloc ( sizeof (struct Colors)); G_init_colors (result_row->colors[i]); *v1 = 0; *v2 = 0.5; G_add_d_raster_color_rule (v1,36,216,72,v2,216,201,36, result_row->colors[i]); *v1 = 0.500001; *v2 = 1.001; G_add_d_raster_color_rule (v1,216,201,36,v2,216,36,39, result_row->colors[i]); /* *v1 = 0; *v2 = 0.333333; G_add_d_raster_color_rule (v1,36,216,072,v2,36,216,072, result_row->colors[i]); *v1 = 0.333334; *v2 = 0.666666; G_add_d_raster_color_rule (v1,216,201,36,v2,216,201,36, result_row->colors[i]); *v1 = 0.666667; *v2 = 1; G_add_d_raster_color_rule (v1,216,36,39,v2,216,36,39, result_row->colors[i]); */ } } /* allocate pointers into array of ordered hypotheses */ /* this is a look-up table for faster access to the 'real' */ /* index of a hypothesis in Theta */ { result_row->hyp_idx = (long*) G_calloc ( sizeof (int), (unsigned) no_hyps); for (i=0; i<no_hyps;i++) { result_row->hyp_idx[i] = find_hyp_idx( hyps[i], doc ); } } }
int main(int argc, char *argv[]) { struct Cell_head cellhd; struct Range range; char *name; /* input raster name */ char *result; /* output raster name */ char *mapset; /* mapset name */ DCELL *inrast; /* input buffer */ DCELL *outrast; /* output buffer */ int row,col; int infd, outfd; /* file descriptor */ int verbose; double *weights; /* array of weights */ DCELL **D_rows; DCELL *tmp; int nrows; int ncols; double min, max; /* raster map range */ RASTER_MAP_TYPE data_type; /* type of the map */ void *values; /* neighborhood values */ int n,i; /* number of neighborhood cells */ int size; /* matrix size */ double ssigma; /* sigma of the spatial part */ double csigma; /* sigma of the color part */ char title[1024]; /* map title */ struct GModule *module; /* GRASS module for parsing arguments */ struct { struct Option *input, *output; struct Option *sigma_s, *sigma_c, *size; struct Option *title; } parm; struct { struct Flag *quiet; struct Flag *print_sigmas; } flag; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name */ /* initialize module */ module = G_define_module(); module->description = ("Gaussian filter for raster maps."); /* Define the different options */ parm.input = G_define_option() ; parm.input->key = "input"; parm.input->type = TYPE_STRING; parm.input->required = YES; parm.input->description= ("Name of an input layer" ); parm.output = G_define_option() ; parm.output->key = "output"; parm.output->type = TYPE_STRING; parm.output->required = YES; parm.output->description= ("Name of an output layer"); parm.sigma_s = G_define_option() ; parm.sigma_s->key = "ssigma"; parm.sigma_s->type = TYPE_DOUBLE; parm.sigma_s->required = NO; parm.sigma_s->description= ("Sigma for space part of the filter\n\t(default: 0.465*((size-1)/2)"); parm.sigma_c = G_define_option() ; parm.sigma_c->key = "csigma"; parm.sigma_c->type = TYPE_DOUBLE; parm.sigma_c->required = NO; parm.sigma_c->description= ("Sigma for color part of the filter\n(default: 0.465*((color_range)/2)"); parm.size = G_define_option() ; parm.size->key = "size"; parm.size->type = TYPE_INTEGER; parm.size->required = YES; parm.size->description= ("Size of the matrix (odd number)"); flag.print_sigmas = G_define_flag() ; flag.print_sigmas->key = 's' ; flag.print_sigmas->description = "Print calculated values for sigmas" ; flag.quiet = G_define_flag() ; flag.quiet->key = 'q' ; flag.quiet->description = "Quiet" ; /* options and flags pareser */ if (G_parser(argc, argv)) exit (-1); /* stores options and flags to variables */ name = parm.input->answer; result = parm.output->answer; verbose = (! flag.quiet->answer); sscanf(parm.size->answer, "%d", &size); if (!parm.sigma_s->answer) ssigma = 0.465*((size-1)/2); else sscanf(parm.sigma_s->answer, "%lf", &ssigma); /* controlling the input values */ if (size%2 == 0) G_fatal_error("Size <%d> is not odd number", size); /* returs NULL if the map was not found in any mapset, * mapset name otherwise*/ mapset = G_find_cell2 (name, ""); if (mapset == NULL) G_fatal_error ("cell file [%s] not found", name); /* color sigma next */ if (!parm.sigma_c->answer) { if (G_read_range(name, mapset, &range) < 0) G_fatal_error("Could not read the raster map range"); /* for raster maps with range from 0-255 the result * should be around 60 */ min = (double)range.min; max = (double)range.max; csigma = 0.456*(max - min)/2; } else sscanf(parm.sigma_c->answer, "%lf", &csigma); /* print if appropriate */ if (flag.print_sigmas->answer) printf("Space sigma: %f\nColor sigma: %f\n", ssigma, csigma); if (G_legal_filename (result) < 0) G_fatal_error ("[%s] is an illegal name", result); /* count weights */ weights = (double *)malloc(size * size * sizeof(double)); /* stores values of gauss. bell into 'weigts'*/ count_weights(weights, size, ssigma); /* determine the inputmap type (CELL/FCELL/DCELL) */ data_type = G_raster_map_type(name, mapset); /* G_open_cell_old - returns file destriptor (>0) */ if ( (infd = G_open_cell_old (name, mapset)) < 0) G_fatal_error ("Cannot open cell file [%s]", name); /* controlling, if we can open input raster */ if (G_get_cellhd (name, mapset, &cellhd) < 0) G_fatal_error ("Cannot read file header of [%s]", name); /* Allocate input buffer */ inrast = G_allocate_raster_buf(data_type); /* Allocate output buffer, use input map data_type */ nrows = G_window_rows(); ncols = G_window_cols(); outrast = G_allocate_d_raster_buf(); /* Allocate values buffers */ values = (DCELL *) malloc(size * size * sizeof(DCELL)); /* allocating memory for rows */ D_rows = (DCELL **)malloc(size * sizeof(DCELL)); for (i = 0; i < size; i++) { D_rows[i] = G_allocate_raster_buf(DCELL_TYPE); } if (values == NULL) G_fatal_error("Cannot allocate memory"); /* controlling, if we can write the raster */ if ( (outfd = G_open_raster_new (result, data_type)) < 0) G_fatal_error ("Could not open <%s>",result); /* write first rows as NULL values */ for (row = 0; row < size/2; row++) { G_set_d_null_value(outrast, ncols); if (G_put_d_raster_row (outfd, outrast) < 0) G_fatal_error ("Cannot write to <%s>",result); } /* allocate first size/2 rows */ for (row = 0; row < size; row++) if (G_get_d_raster_row(infd, D_rows[row], row) < 0) G_fatal_error ("Could not open <%s>",result); /****************************************************************/ /* for each row inside the region */ for ( row = size/2; row < nrows - size/2; row++) { if (verbose) G_percent (row, nrows, 2); /* allocate new last row */ G_get_d_raster_row(infd, D_rows[size-1], row+(size/2)); /*process the data */ for (col=0; col < ncols; col++){ /* skip the outside columns */ if ( (col - size/2) < 0 || ncols <= (col + size/2)) { G_set_d_null_value(outrast, 1); } /* work only with columns, which are inside */ else { /* store values of the matrix into arry 'values', 'n' is * number of elements of the matrix */ n = D_gather(infd, values, D_rows, col, row,size); ((DCELL *)outrast)[col] = D_bilateral(values, ssigma, csigma, size, weights); } } /* for each column */ /* write raster row to output raster file */ G_put_d_raster_row (outfd, outrast); /* switch rows */ tmp = D_rows[0]; for (i = 0; i < size; i++){ D_rows[i] = D_rows[i + 1]; } D_rows[size-1] = tmp; } /* for each row */ /* write last rows as NULL values */ for (i = 0; i < size/2; i++) { G_set_d_null_value(outrast, ncols); G_put_d_raster_row (outfd, outrast); } /* memory cleaning */ G_free(outrast); G_free(inrast); G_free(values); for (i = 0; i < size; i++) { G_free(D_rows[i]); } free((void *) D_rows); /* closing rastr files */ G_close_cell (infd); G_close_cell (outfd); /* set the map title */ sprintf(title, "Bilateral filter of %s with %dx%d matrix: ssigma %.3f, csigma %.3f", name, size, size, ssigma, csigma ); G_put_cell_title (result, title ); return 0; }