[GRASS-SVN] r59051 - grass/trunk/raster/r.li/r.li.padsd
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Feb 14 07:07:22 PST 2014
Author: mmetz
Date: 2014-02-14 07:07:22 -0800 (Fri, 14 Feb 2014)
New Revision: 59051
Modified:
grass/trunk/raster/r.li/r.li.padsd/padsd.c
Log:
r.li.padsd: fix segfault
Modified: grass/trunk/raster/r.li/r.li.padsd/padsd.c
===================================================================
--- grass/trunk/raster/r.li/r.li.padsd/padsd.c 2014-02-14 15:06:54 UTC (rev 59050)
+++ grass/trunk/raster/r.li/r.li.padsd/padsd.c 2014-02-14 15:07:22 UTC (rev 59051)
@@ -22,9 +22,11 @@
#include "../r.li.daemon/avlID.h"
#include "../r.li.daemon/GenericCell.h"
#include "../r.li.daemon/daemon.h"
+
int calculate(int fd, struct area_entry *ad, double *result);
int calculateD(int fd, struct area_entry *ad, double *result);
int calculateF(int fd, struct area_entry *ad, double *result);
+
int main(int argc, char *argv[])
{
struct Option *raster, *conf, *output;
@@ -57,32 +59,25 @@
int patchAreaDistributionSD(int fd, char **par, struct area_entry *ad, double *result)
{
double indice = 0;
- struct Cell_head hd;
int ris = RLI_OK;
- Rast_get_cellhd(ad->raster, "", &hd);
-
switch (ad->data_type) {
case CELL_TYPE:
-
{
ris = calculate(fd, ad, &indice);
break;
}
case DCELL_TYPE:
-
{
ris = calculateD(fd, ad, &indice);
break;
}
case FCELL_TYPE:
-
{
ris = calculateF(fd, ad, &indice);
break;
}
default:
-
{
G_fatal_error("data type unknown");
return RLI_ERRORE;
@@ -92,19 +87,20 @@
*result = -1;
return RLI_ERRORE;
}
+
*result = indice;
+
return RLI_OK;
}
int calculate(int fd, struct area_entry *ad, double *result)
{
- CELL *buf;
- CELL *buf_sup;
+ CELL *buf, *buf_sup, *buf_null;
CELL corrCell;
CELL precCell;
CELL supCell;
int i, j;
- int mask_fd = -1, *mask_buf;
+ int mask_fd = -1, *mask_buf = NULL;
int ris = 0;
int masked = FALSE;
int areaPatch = 0; /*if all cells are null areaPatch=0 */
@@ -118,6 +114,7 @@
long doppi = 0;
long *mask_patch_sup;
long *mask_patch_corr;
+ long *ltmp;
double indice = 0;
double somma = 0;
double area = 0;
@@ -150,25 +147,25 @@
G_fatal_error("malloc mask_patch_corr failed");
return RLI_ERRORE;
}
- buf_sup = Rast_allocate_c_buf();
- if (buf_sup == NULL) {
- G_fatal_error("malloc buf_sup failed");
+
+ buf_null = Rast_allocate_c_buf();
+ if (buf_null == NULL) {
+ G_fatal_error("malloc buf_null failed");
return RLI_ERRORE;
}
- buf = Rast_allocate_c_buf();
- if (buf == NULL) {
- G_fatal_error("malloc buf failed");
- return RLI_ERRORE;
- }
- Rast_set_c_null_value(buf_sup + ad->x, ad->cl); /*the first time buf_sup is all null */
+
+ /*the first time buf_sup is all null */
+ Rast_set_c_null_value(buf_null, Rast_window_cols());
+ buf_sup = buf_null;
+ buf = buf_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 */
- {
+ /*for each raster row */
+ for (j = 0; j < ad->rl; j++) {
if (j > 0) {
buf_sup = RLI_get_cell_raster_row(fd, j - 1 + ad->y, ad);
}
@@ -180,9 +177,8 @@
}
}
Rast_set_c_null_value(&precCell, 1);
- for (i = 0; i < ad->cl; i++)
- /* for each cell in the row */
- {
+ /* for each cell in the row */
+ for (i = 0; i < ad->cl; i++) {
area++;
corrCell = buf[i + ad->x];
if (masked && mask_buf[i + ad->x] == 0) {
@@ -191,38 +187,28 @@
}
if (!(Rast_is_null_value(&corrCell, gc.t))) {
areaPatch++;
- if (i > 0)
- precCell = buf[i - 1 + ad->x];
- if (j == 0)
- Rast_set_c_null_value(&supCell, 1);
- else
- supCell = buf_sup[i + ad->x];
+ supCell = buf_sup[i + ad->x];
- if (corrCell != precCell)
+ if (corrCell != precCell) {
/* ?
* 1 2
* */
- {
if (corrCell != supCell) {
/* 3
* 1 2
* */
/*new patch */
- if (idCorr == 0)
+ if (idCorr == 0) {
/*first found patch */
-
- {
lastId = 1;
idCorr = 1;
totCorr = 1;
mask_patch_corr[i] = idCorr;
}
-
- else
+ else {
/*not first patch */
/* put in the tree the previous value */
- {
if (albero == NULL) {
albero = avlID_make(idCorr, totCorr);
if (albero == NULL) {
@@ -231,31 +217,25 @@
}
npatch++;
}
-
- else
+ 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");
@@ -269,12 +249,10 @@
mask_patch_corr[i] = idCorr;
}
}
-
- else
+ else {
/* 2
* 1 2
* */
- {
if (albero == NULL) {
albero = avlID_make(idCorr, totCorr);
if (albero == NULL) {
@@ -284,7 +262,6 @@
npatch++;
}
else { /*tree not null */
-
ris = avlID_add(&albero, idCorr, totCorr);
switch (ris) {
case AVL_ERR:
@@ -318,7 +295,6 @@
/* ?
* 1 1
*/
-
if (corrCell == supCell) { /*current cell and upper cell are equal */
/* 1
* 1 1
@@ -327,7 +303,6 @@
long r = 0;
long del = mask_patch_sup[i];
-
r = avlID_sub(&albero, del); /*r=number of cell of patch removed */
if (r == 0) {
@@ -387,25 +362,14 @@
else { /*cell is null or is not to consider */
mask_patch_corr[i] = 0;
-
}
+ precCell = corrCell;
}
-
- {
- 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;
- }
- }
-
-
+ ltmp = mask_patch_sup;
+ mask_patch_sup = mask_patch_corr;
+ mask_patch_corr = ltmp;
}
-
if (areaPatch != 0) {
if (albero == NULL) {
albero = avlID_make(idCorr, totCorr);
@@ -415,29 +379,24 @@
}
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;
@@ -480,30 +439,32 @@
indice = sd;
G_free(array);
}
-
else
indice = (double)(-1);
- if (masked)
+
+ if (masked) {
+ close(mask_fd);
G_free(mask_buf);
+ }
G_free(mask_patch_sup);
+ G_free(mask_patch_corr);
+ G_free(buf_null);
+ avlID_destroy(albero);
+
*result = indice;
- G_free(buf_sup);
- G_free(buf);
return RLI_OK;
}
-
int calculateD(int fd, struct area_entry *ad, double *result)
{
- DCELL *buf;
- DCELL *buf_sup;
+ DCELL *buf, *buf_sup, *buf_null;
DCELL corrCell;
DCELL precCell;
DCELL supCell;
int i, j;
- int mask_fd = -1, *mask_buf;
+ int mask_fd = -1, *mask_buf = NULL;
int ris = 0;
int masked = FALSE;
int areaPatch = 0; /*if all cells are null areaPatch=0 */
@@ -517,6 +478,7 @@
long doppi = 0;
long *mask_patch_sup;
long *mask_patch_corr;
+ long *ltmp;
double indice = 0;
double somma = 0;
double area = 0;
@@ -549,25 +511,25 @@
G_fatal_error("malloc mask_patch_corr failed");
return RLI_ERRORE;
}
- buf_sup = Rast_allocate_d_buf();
- if (buf_sup == NULL) {
- G_fatal_error("malloc buf_sup failed");
+
+ buf_null = Rast_allocate_d_buf();
+ if (buf_null == NULL) {
+ G_fatal_error("malloc buf_null failed");
return RLI_ERRORE;
}
- buf = Rast_allocate_d_buf();
- if (buf == NULL) {
- G_fatal_error("malloc buf failed");
- return RLI_ERRORE;
- }
- Rast_set_d_null_value(buf_sup + ad->x, ad->cl); /*the first time buf_sup is all null */
+
+ /*the first time buf_sup is all null */
+ Rast_set_d_null_value(buf_null, Rast_window_cols());
+ buf_sup = buf_null;
+ buf = buf_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 */
- {
+ /*for each raster row */
+ for (j = 0; j < ad->rl; j++) {
if (j > 0) {
buf_sup = RLI_get_dcell_raster_row(fd, j - 1 + ad->y, ad);
}
@@ -579,9 +541,8 @@
}
}
Rast_set_d_null_value(&precCell, 1);
- for (i = 0; i < ad->cl; i++)
- /* for each dcell in the row */
- {
+ /* for each dcell in the row */
+ for (i = 0; i < ad->cl; i++) {
area++;
corrCell = buf[i + ad->x];
if (masked && mask_buf[i + ad->x] == 0) {
@@ -590,18 +551,13 @@
}
if (!(Rast_is_null_value(&corrCell, gc.t))) {
areaPatch++;
- if (i > 0)
- precCell = buf[i - 1 + ad->x];
- if (j == 0)
- Rast_set_d_null_value(&supCell, 1);
- else
- supCell = buf_sup[i + ad->x];
- if (corrCell != precCell)
+ supCell = buf_sup[i + ad->x];
+
+ if (corrCell != precCell) {
/* ?
* 1 2
* */
- {
if (corrCell != supCell) {
/* 3
* 1 2
@@ -613,11 +569,9 @@
totCorr = 1;
mask_patch_corr[i] = idCorr;
}
-
- else
+ else {
/*not first patch */
/* put in the tree the previous value */
- {
if (albero == NULL) {
albero = avlID_make(idCorr, totCorr);
if (albero == NULL) {
@@ -626,31 +580,25 @@
}
npatch++;
}
-
- else
+ 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");
@@ -664,13 +612,11 @@
mask_patch_corr[i] = idCorr;
}
}
-
- else
+ else {
/*current cell and upper cell are equal */
/* 2
* 1 2
* */
- {
if (albero == NULL) {
albero = avlID_make(idCorr, totCorr);
if (albero == NULL) {
@@ -714,7 +660,6 @@
/* ?
* 1 1
*/
-
if (corrCell == supCell) { /*current cell and upper cell are equal */
/* 1
* 1 1
@@ -723,7 +668,6 @@
long r = 0;
long del = mask_patch_sup[i];
-
r = avlID_sub(&albero, del); /*r=number of cell of patch removed */
if (r == 0) {
@@ -783,26 +727,14 @@
else { /*cell is null or is not to consider */
mask_patch_corr[i] = 0;
-
}
+ precCell = corrCell;
}
-
- {
- 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;
- }
- }
-
-
+ ltmp = mask_patch_sup;
+ mask_patch_sup = mask_patch_corr;
+ mask_patch_corr = ltmp;
}
-
-
if (areaPatch != 0) {
if (albero == NULL) {
albero = avlID_make(idCorr, totCorr);
@@ -812,29 +744,24 @@
}
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;
@@ -877,25 +804,31 @@
indice = sd;
G_free(array);
}
-
else
indice = (double)(-1);
- if (masked)
+
+ if (masked) {
+ close(mask_fd);
G_free(mask_buf);
+ }
G_free(mask_patch_sup);
+ G_free(mask_patch_corr);
+ G_free(buf_null);
+ avlID_destroy(albero);
+
*result = indice;
+
return RLI_OK;
}
int calculateF(int fd, struct area_entry *ad, double *result)
{
- FCELL *buf;
- FCELL *buf_sup;
+ FCELL *buf, *buf_sup, *buf_null;
FCELL corrCell;
FCELL precCell;
FCELL supCell;
int i, j;
- int mask_fd = -1, *mask_buf;
+ int mask_fd = -1, *mask_buf = NULL;
int ris = 0;
int masked = FALSE;
int areaPatch = 0; /*if all cells are null areaPatch=0 */
@@ -908,6 +841,7 @@
long doppi = 0;
long *mask_patch_sup;
long *mask_patch_corr;
+ long *ltmp;
long np = 0;
double indice = 0;
double somma = 0;
@@ -941,25 +875,25 @@
G_fatal_error("malloc mask_patch_corr failed");
return RLI_ERRORE;
}
- buf_sup = Rast_allocate_f_buf();
- if (buf_sup == NULL) {
- G_fatal_error("malloc buf_sup failed");
+
+ buf_null = Rast_allocate_f_buf();
+ if (buf_null == NULL) {
+ G_fatal_error("malloc buf_null failed");
return RLI_ERRORE;
}
- buf = Rast_allocate_f_buf();
- if (buf == NULL) {
- G_fatal_error("malloc buf failed");
- return RLI_ERRORE;
- }
- Rast_set_f_null_value(buf_sup + ad->x, ad->cl); /*the first time buf_sup is all null */
+
+ /*the first time buf_sup is all null */
+ Rast_set_f_null_value(buf_null, Rast_window_cols());
+ buf_sup = buf_null;
+ buf = buf_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 */
- {
+ /*for each raster row */
+ for (j = 0; j < ad->rl; j++) {
if (j > 0) {
buf_sup = RLI_get_fcell_raster_row(fd, j - 1 + ad->y, ad);
}
@@ -971,8 +905,8 @@
}
}
Rast_set_f_null_value(&precCell, 1);
+ /* for each fcell in the row */
for (i = 0; i < ad->cl; i++) {
- /* for each fcell in the row */
area++;
corrCell = buf[i + ad->x];
if (masked && mask_buf[i + ad->x] == 0) {
@@ -981,18 +915,13 @@
}
if (!(Rast_is_null_value(&corrCell, gc.t))) {
areaPatch++;
- if (i > 0)
- precCell = buf[i - 1 + ad->x];
- if (j == 0)
- Rast_set_f_null_value(&supCell, 1);
- else
- supCell = buf_sup[i + ad->x];
- if (corrCell != precCell)
+ supCell = buf_sup[i + ad->x];
+
+ if (corrCell != precCell) {
/* ?
* 1 2
* */
- {
if (corrCell != supCell) {
/* 3
* 1 2
@@ -1004,11 +933,9 @@
totCorr = 1;
mask_patch_corr[i] = idCorr;
}
-
- else
+ else {
/*not first patch */
/* put in the tree the previous value */
- {
if (albero == NULL) {
albero = avlID_make(idCorr, totCorr);
if (albero == NULL) {
@@ -1017,31 +944,25 @@
}
npatch++;
}
-
- else
+ 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");
@@ -1055,13 +976,11 @@
mask_patch_corr[i] = idCorr;
}
}
-
- else
+ else {
/*current cell and upper cell are equal */
/* 2
* 1 2
* */
- {
if (albero == NULL) {
albero = avlID_make(idCorr, totCorr);
if (albero == NULL) {
@@ -1105,7 +1024,6 @@
/* ?
* 1 1
*/
-
if (corrCell == supCell) { /*current cell and upper cell are equal */
/* 1
* 1 1
@@ -1114,7 +1032,6 @@
long r = 0;
long del = mask_patch_sup[i];
-
r = avlID_sub(&albero, del); /*r=number of cell of patch removed */
if (r == 0) {
@@ -1174,26 +1091,14 @@
else { /*cell is null or is not to consider */
mask_patch_corr[i] = 0;
-
}
+ precCell = corrCell;
}
-
- {
- 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;
- }
- }
-
-
+ ltmp = mask_patch_sup;
+ mask_patch_sup = mask_patch_corr;
+ mask_patch_corr = ltmp;
}
-
-
if (areaPatch != 0) {
if (albero == NULL) {
albero = avlID_make(idCorr, totCorr);
@@ -1203,29 +1108,24 @@
}
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;
@@ -1268,12 +1168,19 @@
indice = sd;
G_free(array);
}
-
else
indice = (double)(-1);
- if (masked)
+
+ if (masked) {
+ close(mask_fd);
G_free(mask_buf);
+ }
G_free(mask_patch_sup);
+ G_free(mask_patch_corr);
+ G_free(buf_null);
+ avlID_destroy(albero);
+
*result = indice;
+
return RLI_OK;
}
More information about the grass-commit
mailing list