[GRASS-SVN] r59050 - grass/trunk/raster/r.li/r.li.padrange

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Feb 14 07:06:54 PST 2014


Author: mmetz
Date: 2014-02-14 07:06:54 -0800 (Fri, 14 Feb 2014)
New Revision: 59050

Modified:
   grass/trunk/raster/r.li/r.li.padrange/padrange.c
Log:
r.li.padrange: fix segfault

Modified: grass/trunk/raster/r.li/r.li.padrange/padrange.c
===================================================================
--- grass/trunk/raster/r.li/r.li.padrange/padrange.c	2014-02-14 15:06:14 UTC (rev 59049)
+++ grass/trunk/raster/r.li/r.li.padrange/padrange.c	2014-02-14 15:06:54 UTC (rev 59050)
@@ -60,20 +60,15 @@
 			       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;
@@ -84,7 +79,6 @@
 	    break;
 	}
     default:
-
 	{
 	    G_fatal_error("data type unknown");
 	    return RLI_ERRORE;
@@ -94,28 +88,25 @@
 	*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 */
     int area_max = 0, area_min = 0;
-
     long npatch = 0;
     long tot = 0;
     long zero = 0;
@@ -124,15 +115,11 @@
     long lastId = 0;
     long *mask_patch_sup;
     long *mask_patch_corr;
-
-
+    long *ltmp;
     double indice = 0;
     double rk = 0;
-
     avlID_tree albero = NULL;
-
     avlID_table *array = NULL;
-
     generic_cell gc;
 
     gc.t = CELL_TYPE;
@@ -162,28 +149,24 @@
 	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;
-    }
+    /*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;
 
-    Rast_set_c_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 */
-    {
+    /*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);
 	}
@@ -199,9 +182,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++) {
 	    corrCell = buf[i + ad->x];
 	    if (masked && mask_buf[i + ad->x] == 0) {
 		Rast_set_c_null_value(&corrCell, 1);
@@ -209,38 +191,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) {
@@ -249,10 +221,8 @@
 				}
 				npatch++;
 			    }
-
-			    else
+			    else {
 				/*tree not empty */
-			    {
 				ris = avlID_add(&albero, idCorr, totCorr);
 				switch (ris) {
 				case AVL_ERR:
@@ -283,13 +253,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) {
@@ -299,7 +267,6 @@
 			    npatch++;
 			}
 			else {	/*tree not null */
-
 			    ris = avlID_add(&albero, idCorr, totCorr);
 			    switch (ris) {
 			    case AVL_ERR:
@@ -333,7 +300,6 @@
 		    /*        ?
 		     *      1 1
 		     */
-
 		    if (corrCell == supCell) {	/*current cell and upper cell are equal */
 			/*        1
 			 *      1 1
@@ -342,7 +308,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) {
@@ -400,28 +365,15 @@
 		}
 	    }
 	    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);
@@ -456,7 +408,6 @@
 	    }
 	}
 
-
 	array = G_malloc(npatch * sizeof(avlID_tableRow));
 	if (array == NULL) {
 	    G_fatal_error("malloc array failed");
@@ -470,11 +421,8 @@
 	    return RLI_ERRORE;
 	}
 
-
 	for (i = 0; i < npatch; i++) {
-
 	    if (array[i]->tot != 0) {
-
 		if (array[i]->tot > area_max) {
 		    area_max = array[i]->tot;
 		}
@@ -484,8 +432,6 @@
 	    }
 	}
 
-
-
 	rk = area_max - area_min;
 
 	indice = rk;
@@ -495,29 +441,29 @@
     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);
     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 */
@@ -530,6 +476,7 @@
     long lastId = 0;
     long *mask_patch_sup;
     long *mask_patch_corr;
+    long *ltmp;
     double indice = 0;
     double rk = 0;
     avlID_tree albero = NULL;
@@ -559,25 +506,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 (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);
 	}
@@ -598,21 +545,14 @@
 
 	    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];
+		supCell = buf_sup[i + ad->x];
 
-		if (corrCell != precCell)
+		if (corrCell != precCell) {
 		    /*        ?
 		     *      1 2
 		     * */
-		{
 		    if (corrCell != supCell) {
-
 			/*        3
 			 *      1 2
 			 * */
@@ -623,11 +563,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) {
@@ -636,10 +574,8 @@
 				}
 				npatch++;
 			    }
-
-			    else
+			    else {
 				/*tree not empty */
-			    {
 				ris = avlID_add(&albero, idCorr, totCorr);
 				switch (ris) {
 				case AVL_ERR:
@@ -670,13 +606,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) {
@@ -686,7 +620,6 @@
 			    npatch++;
 			}
 			else {	/*tree not null */
-
 			    ris = avlID_add(&albero, idCorr, totCorr);
 			    switch (ris) {
 			    case AVL_ERR:
@@ -720,7 +653,6 @@
 		    /*        ?
 		     *      1 1
 		     */
-
 		    if (corrCell == supCell) {	/*current cell and upper cell are equal */
 			/*        1
 			 *      1 1
@@ -729,7 +661,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) {
@@ -789,26 +720,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);
@@ -843,7 +762,6 @@
 	    }
 	}
 
-
 	array = G_malloc(npatch * sizeof(avlID_tableRow));
 	if (array == NULL) {
 	    G_fatal_error("malloc array failed");
@@ -857,9 +775,7 @@
 	    return RLI_ERRORE;
 	}
 
-
 	for (i = 0; i < npatch; i++) {
-
 	    if (array[i]->tot != 0) {
 
 		if (array[i]->tot > area_max) {
@@ -871,8 +787,6 @@
 	    }
 	}
 
-
-
 	rk = area_max - area_min;
 
 	indice = rk;
@@ -882,25 +796,29 @@
     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 */
@@ -913,6 +831,7 @@
     long lastId = 0;
     long *mask_patch_sup;
     long *mask_patch_corr;
+    long *ltmp;
     double indice = 0;
     double rk = 0;
     avlID_tree albero = NULL;
@@ -942,26 +861,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);
 	}
@@ -974,8 +892,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 */
 	    corrCell = buf[i + ad->x];
 	    if (masked && mask_buf[i + ad->x] == 0) {
 		Rast_set_f_null_value(&corrCell, 1);
@@ -983,21 +901,14 @@
 
 	    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];
+		supCell = buf_sup[i + ad->x];
 
-		if (corrCell != precCell)
+		if (corrCell != precCell) {
 		    /*        ?
 		     *      1 2
 		     * */
-		{
 		    if (corrCell != supCell) {
-
 			/*        3
 			 *      1 2
 			 * */
@@ -1008,11 +919,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) {
@@ -1021,10 +930,8 @@
 				}
 				npatch++;
 			    }
-
-			    else
+			    else {
 				/*tree not empty */
-			    {
 				ris = avlID_add(&albero, idCorr, totCorr);
 				switch (ris) {
 				case AVL_ERR:
@@ -1055,13 +962,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) {
@@ -1071,7 +976,6 @@
 			    npatch++;
 			}
 			else {	/*tree not null */
-
 			    ris = avlID_add(&albero, idCorr, totCorr);
 			    switch (ris) {
 			    case AVL_ERR:
@@ -1105,7 +1009,6 @@
 		    /*        ?
 		     *      1 1
 		     */
-
 		    if (corrCell == supCell) {	/*current cell and upper cell are equal */
 			/*        1
 			 *      1 1
@@ -1114,7 +1017,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 +1076,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);
@@ -1228,7 +1118,6 @@
 	    }
 	}
 
-
 	array = G_malloc(npatch * sizeof(avlID_tableRow));
 	if (array == NULL) {
 	    G_fatal_error("malloc array failed");
@@ -1242,11 +1131,8 @@
 	    return RLI_ERRORE;
 	}
 
-
 	for (i = 0; i < npatch; i++) {
-
 	    if (array[i]->tot != 0) {
-
 		if (array[i]->tot > area_max) {
 		    area_max = array[i]->tot;
 		}
@@ -1256,8 +1142,6 @@
 	    }
 	}
 
-
-
 	rk = area_max - area_min;
 
 	indice = rk;
@@ -1267,10 +1151,14 @@
     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;
 



More information about the grass-commit mailing list