[GRASS-SVN] r70456 - grass-addons/grass7/imagery/i.superpixels.slic

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jan 30 00:29:13 PST 2017


Author: mmetz
Date: 2017-01-30 00:29:13 -0800 (Mon, 30 Jan 2017)
New Revision: 70456

Added:
   grass-addons/grass7/imagery/i.superpixels.slic/minsize.c
   grass-addons/grass7/imagery/i.superpixels.slic/pavl.c
   grass-addons/grass7/imagery/i.superpixels.slic/pavl.h
   grass-addons/grass7/imagery/i.superpixels.slic/rclist.c
   grass-addons/grass7/imagery/i.superpixels.slic/rclist.h
Modified:
   grass-addons/grass7/imagery/i.superpixels.slic/main.c
Log:
i.superpixels.slic: improve merging of small clumps

Modified: grass-addons/grass7/imagery/i.superpixels.slic/main.c
===================================================================
--- grass-addons/grass7/imagery/i.superpixels.slic/main.c	2017-01-29 19:50:33 UTC (rev 70455)
+++ grass-addons/grass7/imagery/i.superpixels.slic/main.c	2017-01-30 08:29:13 UTC (rev 70456)
@@ -39,6 +39,10 @@
 int SLIC_EnforceLabelConnectivity(int **labels, int ncols, int nrows,
 				   int **nlabels, int minsize);
 
+int merge_small_clumps(DCELL ***pdata, int nbands,
+                       int **klabels, int nlabels,
+                       int diag, int minsize);
+
 int main(int argc, char *argv[])
 {
     struct GModule *module;	/* GRASS module for parsing arguments */
@@ -65,8 +69,8 @@
     int step;
     int offset;
     DCELL ***pdata;
-    int **klabels, **nlabels, label_change;
-    double **distvec, **distspec;
+    int **klabels, **nlabels, schange;
+    double ***dists;
 
     double xerrperstrip, yerrperstrip;
     int xstrips, ystrips, xoff, yoff, xerr, yerr;
@@ -77,9 +81,8 @@
     int seedx, seedy;
 
     int *clustersize;
-    double *kseedsx, *kseedsy;
-    double **kseedsb;
-    double **sigmab, *sigmax, *sigmay;
+    double *kseedsx, *kseedsy, *sigmax, *sigmay;
+    DCELL **kseedsb, **sigmab;
     double *maxdistspeck, maxdistspec, maxdistspecprev;
 
     double invwt;
@@ -108,7 +111,7 @@
     opt_iteration->type = TYPE_INTEGER;
     opt_iteration->required = NO;
     opt_iteration->description = _("Maximum number of iterations");
-    opt_iteration->answer = "100";
+    opt_iteration->answer = "10";
 
     opt_super_pixels = G_define_option();
     opt_super_pixels->key = "k";
@@ -259,6 +262,7 @@
     G_debug(1, "numk = %d", numk);
 
     /* load input bands */
+    G_message(_("Loading input..."));
     pdata = G_malloc(sizeof(DCELL *) * nbands);
 
     ifd = G_malloc(sizeof(int *) * nbands);
@@ -300,6 +304,7 @@
 	    }
 	}
     }
+    G_percent(nrows, nrows, 2);
 
     for (b = 0; b < nbands; b++) {
 	Rast_close(ifd[b]);
@@ -309,10 +314,10 @@
     G_free(ibuf);
 
     /* allocate seed variables */
-    kseedsb = G_malloc(sizeof(double *) * numk);
+    kseedsb = G_malloc(sizeof(DCELL *) * numk);
     for (k = 0; k < numk; k++) {
-	kseedsb[k] = G_malloc(sizeof(double) * nbands);
-	memset(kseedsb[k], 0, sizeof(double) * nbands);
+	kseedsb[k] = G_malloc(sizeof(DCELL) * nbands);
+	memset(kseedsb[k], 0, sizeof(DCELL) * nbands);
     }
 
     kseedsx = G_malloc(sizeof(double) * numk);
@@ -321,6 +326,26 @@
     kseedsy = G_malloc(sizeof(double) * numk);
     memset(kseedsy, 0, sizeof(double) * numk);
 
+    clustersize = G_malloc(sizeof(int) * numk);
+    memset(clustersize, 0, sizeof(int) * numk);
+
+    sigmab = G_malloc(sizeof(DCELL *) * numk);
+    for (k = 0; k < numk; k++) {
+	sigmab[k] = G_malloc(sizeof(DCELL) * nbands);
+	memset(sigmab[k], 0, sizeof(DCELL) * nbands);
+    }
+
+    sigmax = G_malloc(sizeof(double) * numk);
+    memset(sigmax, 0, sizeof(double) * numk);
+
+    sigmay = G_malloc(sizeof(double) * numk);
+    memset(sigmay, 0, sizeof(double) * numk);
+
+    maxdistspeck = G_malloc(sizeof(double) * numk);
+    for (k = 0; k < numk; k++)
+	maxdistspeck[k] = 1;
+
+
     /* initial seed values */
     k = 0;
     for (y = 0; y < ystrips; y++) {
@@ -348,22 +373,6 @@
     if (k != numk)
 	G_warning(_("Initialized %d of %d seeds"), k, numk);
 
-    clustersize = G_malloc(sizeof(int) * numk);
-    memset(clustersize, 0, sizeof(int) * numk);
-
-    sigmab = G_malloc(sizeof(double *) * numk);
-    for (k = 0; k < numk; k++) {
-	sigmab[k] = G_malloc(sizeof(double) * nbands);
-	memset(sigmab[k], 0, sizeof(double) * nbands);
-    }
-
-    sigmax = G_malloc(sizeof(double) * numk);
-    memset(sigmax, 0, sizeof(double) * numk);
-
-    sigmay = G_malloc(sizeof(double) * numk);
-    memset(sigmay, 0, sizeof(double) * numk);
-
-
     /* allocate cell variables */
     klabels = G_malloc(sizeof(int *) * nrows);
 
@@ -373,35 +382,29 @@
 	    klabels[row][col] = -1;
     }
 
-    distvec = G_malloc(sizeof(double *) * nrows);
-    for (row = 0; row < nrows; row++)
-	distvec[row] = G_malloc(sizeof(double) * ncols);
-
-    distspec = G_malloc(sizeof(double *) * nrows);
+    dists = G_malloc(sizeof(double **) * nrows);
     for (row = 0; row < nrows; row++) {
-	distspec[row] = G_malloc(sizeof(double) * ncols);
-	for (col = 0; col < ncols; col++)
-	    distspec[row][col] = 0;
+	dists[row] = G_malloc(sizeof(double *) * ncols);
+	for (col = 0; col < ncols; col++) {
+	    dists[row][col] = G_malloc(sizeof(double) * 2);
+	}
     }
 
-    maxdistspeck = G_malloc(sizeof(double) * numk);
     maxdistspec = maxdistspecprev = 0;
 
-    for (k = 0; k < numk; k++)
-	maxdistspeck[k] = 1;
-
     /* magic factor */
     invwt = 0.1 * compactness / (offset * offset);
 
+    G_message(_("Performing k mean segmentation..."));
     for (itr = 0; itr < n_iterations; itr++) {
 	G_percent(itr, n_iterations, 2);
 
-	label_change = 0;
+	schange = 0;
 
 	for (row = 0; row < nrows; row++) {
 	    for (col = 0; col < ncols; col++) {
-		distvec[row][col] = 1E+9;
-		distspec[row][col] = 0;
+		dists[row][col][0] = 0;
+		dists[row][col][1] = 1E+9;
 	    }
 	}
 
@@ -432,11 +435,9 @@
 		    distsum = dist / maxdistspeck[k] + distxy * invwt;
 		    /* dist = sqrt(dist) + sqrt(distxy*invwt);  this is more exact */
 		    /*------------------------------------------------------------------------ */
-		    if (distsum < distvec[y][x]) {
-			distvec[y][x] = distsum;
-			distspec[y][x] = dist;
-			if (klabels[y][x] != k)
-			    label_change++;
+		    if (distsum < dists[y][x][1]) {
+			dists[y][x][0] = dist;
+			dists[y][x][1] = distsum;
 			klabels[y][x] = k;
 		    }
 
@@ -454,8 +455,8 @@
 		for (col = 0; col < ncols; col++) {
 		    k = klabels[row][col];
 		    if (k >= 0) {
-			if (maxdistspeck[k] < distspec[row][col])
-			    maxdistspeck[k] = distspec[row][col];
+			if (maxdistspeck[k] < dists[row][col][0])
+			    maxdistspeck[k] = dists[row][col][0];
 		    }
 		}
 	    }
@@ -467,14 +468,14 @@
 		for (col = 0; col < ncols; col++) {
 		    k = klabels[row][col];
 		    if (k >= 0) {
-			if (maxdistspec < distspec[row][col])
-			    maxdistspec = distspec[row][col];
+			if (maxdistspec < dists[row][col][0])
+			    maxdistspec = dists[row][col][0];
 		    }
 		}
 	    }
 	    for (k = 0; k < numk; k++)
 		maxdistspeck[k] = maxdistspec;
-	    G_debug(3, "maxdistspec = %.15g", maxdistspec);
+	    G_debug(1, "Largest spectral distance = %.15g", maxdistspec);
 	}
 
 	for (k = 0; k < numk; k++) {
@@ -493,42 +494,74 @@
 		    }
 		    sigmax[klabels[row][col]] += col;
 		    sigmay[klabels[row][col]] += row;
-
-	    /*-------------------------------------------*/
-		    /* edgesum[klabels[ind]] += edgemag[ind];    */
-
-	    /*-------------------------------------------*/
 		    clustersize[klabels[row][col]] += 1;
 		}
 	    }
 	}
 
 	for (k = 0; k < numk; k++) {
+	    double newxy;
+	    int kchange = 0;
+
 	    if (clustersize[k] <= 0)
 		clustersize[k] = 1;
 
 	    for (b = 0; b < nbands; b++) {
-		kseedsb[k][b] = sigmab[k][b] / clustersize[k];
+		DCELL newb;
+
+		newb = sigmab[k][b] / clustersize[k];
+		
+		if (kseedsb[k][b] != newb)
+		    kchange = 1;
+
+		kseedsb[k][b] = newb;
 	    }
-	    kseedsx[k] = sigmax[k] / clustersize[k];
-	    kseedsy[k] = sigmay[k] / clustersize[k];
+	    newxy = sigmax[k] / clustersize[k];
+	    if (kseedsx[k] != newxy)
+		kchange = 1;
+	    kseedsx[k] = newxy;
+	    newxy = sigmay[k] / clustersize[k];
+	    if (kseedsy[k] != newxy)
+		kchange = 1;
+	    kseedsy[k] = newxy;
 
-      /*------------------------------------*/
-	    /* edgesum[k] *= inv[k];              */
-
-      /*------------------------------------*/
+	    if (kchange)
+		schange++;
 	}
-	if (label_change == 0)
+	/* SLIC (k mean) converges */
+	G_debug(1, "Number of changed seeds: %d", schange);
+	if (schange == 0)
 	    break;
-	G_debug(3, "Number of changed labels: %d", label_change);
+#if 0
 	if (!slic0 && maxdistspecprev == maxdistspec)
 	    break;
+#endif
     }
     G_percent(1, 1, 1);
 
     if (itr < n_iterations)
 	G_message(_("SLIC converged after %d iterations"), itr);
 
+    /* free */
+    for (row = 0; row < nrows; row++) {
+	for (col = 0; col < ncols; col++)
+	    G_free(dists[row][col]);
+	G_free(dists[row]);
+    }
+    G_free(dists);
+
+    for (k = 0; k < numk; k++) {
+	G_free(kseedsb[k]);
+	G_free(sigmab[k]);
+    }
+    G_free(kseedsb);
+    G_free(kseedsx);
+    G_free(kseedsy);
+    G_free(sigmab);
+    G_free(sigmax);
+    G_free(sigmay);
+    G_free(clustersize);
+
     nlabels = G_malloc(sizeof(int *) * nrows);
     for (row = 0; row < nrows; row++) {
 	nlabels[row] = G_malloc(sizeof(int) * ncols);
@@ -536,15 +569,20 @@
     }
 
     numlabels = SLIC_EnforceLabelConnectivity(klabels, ncols, nrows, 
-                                              nlabels, minsize);
-
+                                              nlabels, 0);
     for (row = 0; row < nrows; row++) {
 	for (col = 0; col < ncols; col++) {
 	    if (klabels[row][col] >= 0)
 		klabels[row][col] = nlabels[row][col];
 	}
     }
+    
+    G_free(nlabels);
 
+    if (minsize > 1)
+	merge_small_clumps(pdata, nbands, klabels, numlabels, 0,
+	                   minsize);
+
     outfd = Rast_open_new(outname, CELL_TYPE);
     obuf = Rast_allocate_c_buf();
     for (row = 0; row < nrows; row++) {

Copied: grass-addons/grass7/imagery/i.superpixels.slic/minsize.c (from rev 70393, grass/trunk/raster/r.clump/minsize.c)
===================================================================
--- grass-addons/grass7/imagery/i.superpixels.slic/minsize.c	                        (rev 0)
+++ grass-addons/grass7/imagery/i.superpixels.slic/minsize.c	2017-01-30 08:29:13 UTC (rev 70456)
@@ -0,0 +1,407 @@
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+#include "pavl.h"
+#include "rclist.h"
+
+struct nbr_cnt {
+    int id;
+    int row, col;
+    int cnt;
+};
+
+static void avl_free_item(void *avl_item)
+{
+    G_free(avl_item);
+}
+
+static int cmp_nbrs(const void *a, const void *b)
+{
+    struct nbr_cnt *aa = (struct nbr_cnt *)a;
+    struct nbr_cnt *bb = (struct nbr_cnt *)b;
+
+    return (aa->id - bb->id);
+}
+
+static int cmp_rc(const void *first, const void *second)
+{
+    struct rc *a = (struct rc *)first, *b = (struct rc *)second;
+
+    if (a->row == b->row)
+	return (a->col - b->col);
+
+    return (a->row - b->row);
+}
+
+static int get_eight_neighbors(int row, int col, int nrows, int ncols,
+			       int neighbors[8][2])
+{
+    int rown, coln, n;
+    
+    n = -1;
+    /* previous row */
+    rown = row - 1;
+    if (rown >= 0) {
+	coln = col - 1;
+	if (coln >= 0) {
+	    n++;
+	    neighbors[n][0] = rown;
+	    neighbors[n][1] = coln;
+	}
+	n++;
+	neighbors[n][0] = rown;
+	neighbors[n][1] = col;
+	coln = col + 1;
+	if (coln < ncols) {
+	    n++;
+	    neighbors[n][0] = rown;
+	    neighbors[n][1] = coln;
+	}
+    }
+    
+    /* next row */
+    rown = row + 1;
+    if (rown < nrows) {
+	coln = col - 1;
+	if (coln >= 0) {
+	    n++;
+	    neighbors[n][0] = rown;
+	    neighbors[n][1] = coln;
+	}
+	n++;
+	neighbors[n][0] = rown;
+	neighbors[n][1] = col;
+	coln = col + 1;
+	if (coln < ncols) {
+	    n++;
+	    neighbors[n][0] = rown;
+	    neighbors[n][1] = coln;
+	}
+    }
+    
+    /* current row */
+    coln = col - 1;
+    if (coln >= 0) {
+	n++;
+	neighbors[n][0] = row;
+	neighbors[n][1] = coln;
+    }
+    coln = col + 1;
+    if (coln < ncols) {
+	n++;
+	neighbors[n][0] = row;
+	neighbors[n][1] = coln;
+    }
+    
+    return n;
+}
+
+static int get_four_neighbors(int row, int col, int nrows, int ncols,
+			       int neighbors[8][2])
+{
+    int rown, coln, n;
+    
+    n = -1;
+    /* previous row */
+    rown = row - 1;
+    if (rown >= 0) {
+	n++;
+	neighbors[n][0] = rown;
+	neighbors[n][1] = col;
+    }
+    
+    /* next row */
+    rown = row + 1;
+    if (rown < nrows) {
+	n++;
+	neighbors[n][0] = rown;
+	neighbors[n][1] = col;
+    }
+    
+    /* current row */
+    coln = col - 1;
+    if (coln >= 0) {
+	n++;
+	neighbors[n][0] = row;
+	neighbors[n][1] = coln;
+    }
+    coln = col + 1;
+    if (coln < ncols) {
+	n++;
+	neighbors[n][0] = row;
+	neighbors[n][1] = coln;
+    }
+    
+    return n;
+}
+
+static int (*get_neighbors)(int row, int col, int nrows, int ncols,
+			       int neighbors[8][2]);
+
+static int update_cid(int **klabels, int row, int col, int old_id, int new_id)
+{
+    int nrows, ncols, rown, coln, n;
+    int neighbors[8][2];
+    struct rc next;
+    struct rclist rilist;
+
+    if (klabels[row][col] != old_id) {
+	G_fatal_error(_("Wrong id %d for row %d, col %d"), 
+	              klabels[row][col], row, col);
+    }
+    klabels[row][col] = new_id;
+
+    nrows = Rast_window_rows();
+    ncols = Rast_window_cols();
+
+    /* breadth-first search */
+    next.row = row;
+    next.col = col;
+    rclist_init(&rilist);
+
+    do {
+
+	n = get_neighbors(next.row, next.col, nrows, ncols, neighbors);
+	do {
+	    rown = neighbors[n][0];
+	    coln = neighbors[n][1];
+
+	    if (klabels[rown][coln] == old_id) {
+		klabels[rown][coln] = new_id;
+		rclist_add(&rilist, rown, coln);
+	    }
+
+	} while (n--);    /* end do loop - next neighbor */
+    } while (rclist_drop(&rilist, &next));   /* while there are cells to check */
+
+    rclist_destroy(&rilist);
+
+    return 1;
+}
+
+static int find_best_neighbour(DCELL **clumpbsum, int nbands, int *clumpsize,
+                               int **klabels, int row, int col,
+			       int this_id, int *best_id)
+{
+    int rown, coln, n, count, b;
+    int nrows, ncols;
+    int neighbors[8][2];
+    struct rc next, ngbr_rc, *pngbr_rc;
+    struct rclist rilist;
+    int ngbr_id;
+    struct pavl_table *nbtree, *visited;
+    struct nbr_cnt Rk, *Rkp;
+    double sim, best_sim;
+
+    G_debug(3, "find_best_neighbour()");
+
+    nrows = Rast_window_rows();
+    ncols = Rast_window_cols();
+
+    nbtree = pavl_create(cmp_nbrs, NULL);
+    visited = pavl_create(cmp_rc, NULL);
+    ngbr_rc.row = row;
+    ngbr_rc.col = col;
+
+    pngbr_rc = G_malloc(sizeof(struct rc));
+    *pngbr_rc = ngbr_rc;
+    pavl_insert(visited, pngbr_rc);
+    pngbr_rc = NULL;
+
+    /* breadth-first search */
+    next.row = row;
+    next.col = col;
+    rclist_init(&rilist);
+    count = 1;
+    *best_id = -1;
+    best_sim = 2;
+    
+    do {
+	n = get_neighbors(next.row, next.col, nrows, ncols, neighbors);
+	do {
+	    rown = neighbors[n][0];
+	    coln = neighbors[n][1];
+
+	    if (klabels[rown][coln] < 0)
+		continue;
+
+	    ngbr_rc.row = rown;
+	    ngbr_rc.col = coln;
+
+	    if (pngbr_rc == NULL)
+		pngbr_rc = G_malloc(sizeof(struct rc));
+
+	    *pngbr_rc = ngbr_rc;
+
+	    if (pavl_insert(visited, pngbr_rc) == NULL) {
+		pngbr_rc = NULL;
+
+		/* get neighbor ID */
+		ngbr_id = klabels[rown][coln];
+		/* same neighbour */
+		if (ngbr_id == this_id) {
+		    count++;
+		    rclist_add(&rilist, rown, coln);
+		}
+		else if (ngbr_id >= 0) { /* different neighbour */
+
+		    /* find in neighbor tree */
+		    Rk.id = ngbr_id;
+		    if (pavl_find(nbtree, &Rk) == NULL) {
+			Rk.row = rown;
+			Rk.col = coln;
+			Rkp = G_malloc(sizeof(struct nbr_cnt));
+			*Rkp = Rk;
+			pavl_insert(nbtree, Rkp);
+			
+			sim = 0;
+			for (b = 0; b < nbands; b++) {
+			    sim += (clumpbsum[this_id][b] / clumpsize[this_id] -
+			            clumpbsum[ngbr_id][b] / clumpsize[ngbr_id]) *
+				    (clumpbsum[this_id][b] / clumpsize[this_id] -
+			            clumpbsum[ngbr_id][b] / clumpsize[ngbr_id]);
+			}
+			sim /= nbands;
+
+			if (best_sim > sim) {
+			    best_sim = sim;
+			    *best_id = ngbr_id;
+			}
+			else if (best_sim == sim &&
+			         clumpsize[*best_id] > clumpsize[ngbr_id]) {
+			    best_sim = sim;
+			    *best_id = ngbr_id;
+			}
+		    }
+		}
+	    }
+	} while (n--);    /* end do loop - next neighbor */
+    } while (rclist_drop(&rilist, &next));   /* while there are cells to check */
+
+    rclist_destroy(&rilist);
+    pavl_destroy(visited, avl_free_item);
+    pavl_destroy(nbtree, avl_free_item);
+
+    return (*best_id >= 0);
+}
+
+int merge_small_clumps(DCELL ***pdata, int nbands,
+                       int **klabels, int nlabels,
+                       int diag, int minsize)
+{
+    int row, col, nrows, ncols, i, b;
+    int this_id;
+    int best_id;
+    int reg_size;
+    int *clumpsize, n_clumps_new;
+    DCELL **clumpbsum;
+
+    /* merge with best (most similar) neighbour */
+
+    if (minsize < 2)
+	G_fatal_error(_("Minimum size must be larger than 1"));
+
+    nrows = Rast_window_rows();
+    ncols = Rast_window_cols();
+    
+    if (nlabels < 2) {
+	G_warning(_("Not enough clumps for merging"));
+
+	return nlabels;
+    }
+
+    if (diag)
+	get_neighbors = get_eight_neighbors;
+    else
+	get_neighbors = get_four_neighbors;
+
+    /* init clump sizes and band sums */
+    clumpsize = (int *) G_malloc(sizeof(int) * (nlabels));
+    clumpbsum = (DCELL **) G_malloc(sizeof(DCELL *) * (nlabels));
+    for (i = 0; i < nlabels; i++) {
+	clumpsize[i] = 0;
+	clumpbsum[i] = (DCELL *) G_malloc(sizeof(DCELL) * (nbands));
+	for (b = 0; b < nbands; b++)
+	    clumpbsum[i][b] = 0;
+    }
+
+    G_message(_("Merging clumps smaller than %d cells..."), minsize);
+
+    /* get clump sizes and band sums */
+    for (row = 0; row < nrows; row++) {
+	for (col = 0; col < ncols; col++) {
+	    this_id = klabels[row][col];
+	    if (this_id >= 0) {
+		clumpsize[this_id]++;
+		for (b = 0; b < nbands; b++)
+		    clumpbsum[this_id][b] += pdata[row][col][b];
+	    }
+	}
+    }
+
+    /* go through all cells */
+    G_percent_reset();
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 2);
+
+	for (col = 0; col < ncols; col++) {
+
+	    /* get clump id */
+	    this_id = klabels[row][col];
+	    if (this_id < 0)
+		continue;
+
+	    reg_size = clumpsize[this_id];
+
+	    best_id = 0;
+	    while (reg_size < minsize && best_id >= 0) {
+		best_id = -1;
+
+		find_best_neighbour(clumpbsum, nbands, clumpsize, klabels,
+				    row, col, this_id, &best_id);
+
+		if (best_id >= 0) {
+		    /* update cid */
+		    update_cid(klabels, row, col, this_id, best_id);
+		    /* mark as merged */
+		    for (b = 0; b < nbands; b++)
+			clumpbsum[best_id][b] += clumpbsum[this_id][b];
+		    clumpsize[best_id] += clumpsize[this_id];
+		    reg_size = clumpsize[best_id];
+		    clumpsize[this_id] = 0;
+		    this_id = best_id;
+		}
+	    }
+	}
+    }
+    G_percent(1, 1, 1);
+
+    n_clumps_new = 0;
+
+    /* clumpsize becomes new clump ID */
+    for (i = 0; i < nlabels; i++) {
+	if (clumpsize[i] > 0)
+	    clumpsize[i] = n_clumps_new++;
+    }
+
+    G_message(_("Renumbering remaining %d clumps..."), n_clumps_new);
+
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 4);
+
+	for (col = 0; col < ncols; col++) {
+
+	    this_id = klabels[row][col];
+	    if (this_id >= 0)
+		klabels[row][col] = clumpsize[this_id];
+	}
+    }
+    G_percent(1, 1, 1);
+    
+    for (i = 0; i < nlabels; i++)
+	G_free(clumpbsum[i]);
+    G_free(clumpbsum);
+    G_free(clumpsize);
+
+    return 1;
+}

Copied: grass-addons/grass7/imagery/i.superpixels.slic/pavl.c (from rev 70370, grass-addons/grass7/raster/r.resamp.tps/pavl.c)
===================================================================
--- grass-addons/grass7/imagery/i.superpixels.slic/pavl.c	                        (rev 0)
+++ grass-addons/grass7/imagery/i.superpixels.slic/pavl.c	2017-01-30 08:29:13 UTC (rev 70456)
@@ -0,0 +1,843 @@
+/* Produced by texiweb from libavl.w. */
+
+/* libavl - library for manipulation of binary trees.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
+   Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301 USA.
+ */
+
+/* Nov 2016, Markus Metz
+ * from libavl-2.0.3
+ * added safety checks and speed optimizations
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "pavl.h"
+
+/* Creates and returns a new table
+   with comparison function |compare| using parameter |param|
+   and memory allocator |allocator|.
+   Returns |NULL| if memory allocation failed. */
+struct pavl_table *pavl_create(pavl_comparison_func * compare,
+			       struct libavl_allocator *allocator)
+{
+    struct pavl_table *tree;
+
+    assert(compare != NULL);
+
+    if (allocator == NULL)
+	allocator = &pavl_allocator_default;
+
+    tree = allocator->libavl_malloc(sizeof *tree);
+    if (tree == NULL)
+	return NULL;
+
+    tree->pavl_root = NULL;
+    tree->pavl_compare = compare;
+    tree->pavl_alloc = allocator;
+    tree->pavl_count = 0;
+
+    return tree;
+}
+
+/* Search |tree| for an item matching |item|, and return it if found.
+   Otherwise return |NULL|. */
+void *pavl_find(const struct pavl_table *tree, const void *item)
+{
+    const struct pavl_node *p;
+
+    assert(tree != NULL && item != NULL);
+
+    p = tree->pavl_root;
+    while (p != NULL) {
+	int cmp = tree->pavl_compare(item, p->pavl_data);
+
+	if (cmp == 0)
+	    return p->pavl_data;
+
+	p = p->pavl_link[cmp > 0];
+    }
+
+    return NULL;
+}
+
+/* Inserts |item| into |tree| and returns a pointer to |item|'s address.
+   If a duplicate item is found in the tree,
+   returns a pointer to the duplicate without inserting |item|.
+   Returns |NULL| in case of memory allocation failure. */
+void **pavl_probe(struct pavl_table *tree, void *item)
+{
+    struct pavl_node *y;	/* Top node to update balance factor, and parent. */
+    struct pavl_node *p, *q;	/* Iterator, and parent. */
+    struct pavl_node *n;	/* Newly inserted node. */
+    struct pavl_node *w;	/* New root of rebalanced subtree. */
+    int dir;			/* Direction to descend. */
+
+    assert(tree != NULL && item != NULL);
+
+    y = p = tree->pavl_root;
+    q = NULL;
+    dir = 0;
+    while (p != NULL) {
+	int cmp = tree->pavl_compare(item, p->pavl_data);
+
+	if (cmp == 0)
+	    return &p->pavl_data;
+
+	dir = cmp > 0;
+
+	if (p->pavl_balance != 0)
+	    y = p;
+
+	q = p, p = p->pavl_link[dir];
+    }
+
+    n = tree->pavl_alloc->libavl_malloc(sizeof *p);
+    if (n == NULL)
+	return NULL;
+
+    tree->pavl_count++;
+    n->pavl_link[0] = n->pavl_link[1] = NULL;
+    n->pavl_parent = q;
+    n->pavl_data = item;
+    n->pavl_balance = 0;
+    if (q == NULL) {
+	tree->pavl_root = n;
+
+	return &n->pavl_data;
+    }
+    q->pavl_link[dir] = n;
+
+    p = n;
+    while (p != y) {
+	q = p->pavl_parent;
+	/*
+	   dir = q->pavl_link[0] != p;
+	   if (dir == 0)
+	   q->pavl_balance--;
+	   else
+	   q->pavl_balance++;
+	 */
+	if (q->pavl_link[0] != p)
+	    q->pavl_balance++;
+	else
+	    q->pavl_balance--;
+
+	p = q;
+    }
+
+    if (y->pavl_balance == -2) {
+	struct pavl_node *x = y->pavl_link[0];
+
+	if (x->pavl_balance == -1) {
+	    w = x;
+	    y->pavl_link[0] = x->pavl_link[1];
+	    x->pavl_link[1] = y;
+	    x->pavl_balance = y->pavl_balance = 0;
+	    x->pavl_parent = y->pavl_parent;
+	    y->pavl_parent = x;
+	    if (y->pavl_link[0] != NULL)
+		y->pavl_link[0]->pavl_parent = y;
+	}
+	else {
+	    assert(x->pavl_balance == +1);
+	    w = x->pavl_link[1];
+	    x->pavl_link[1] = w->pavl_link[0];
+	    w->pavl_link[0] = x;
+	    y->pavl_link[0] = w->pavl_link[1];
+	    w->pavl_link[1] = y;
+	    if (w->pavl_balance == -1)
+		x->pavl_balance = 0, y->pavl_balance = +1;
+	    else if (w->pavl_balance == 0)
+		x->pavl_balance = y->pavl_balance = 0;
+	    else		/* |w->pavl_balance == +1| */
+		x->pavl_balance = -1, y->pavl_balance = 0;
+	    w->pavl_balance = 0;
+	    w->pavl_parent = y->pavl_parent;
+	    x->pavl_parent = y->pavl_parent = w;
+	    if (x->pavl_link[1] != NULL)
+		x->pavl_link[1]->pavl_parent = x;
+	    if (y->pavl_link[0] != NULL)
+		y->pavl_link[0]->pavl_parent = y;
+	}
+    }
+    else if (y->pavl_balance == +2) {
+	struct pavl_node *x = y->pavl_link[1];
+
+	if (x->pavl_balance == +1) {
+	    w = x;
+	    y->pavl_link[1] = x->pavl_link[0];
+	    x->pavl_link[0] = y;
+	    x->pavl_balance = y->pavl_balance = 0;
+	    x->pavl_parent = y->pavl_parent;
+	    y->pavl_parent = x;
+	    if (y->pavl_link[1] != NULL)
+		y->pavl_link[1]->pavl_parent = y;
+	}
+	else {
+	    assert(x->pavl_balance == -1);
+	    w = x->pavl_link[0];
+	    x->pavl_link[0] = w->pavl_link[1];
+	    w->pavl_link[1] = x;
+	    y->pavl_link[1] = w->pavl_link[0];
+	    w->pavl_link[0] = y;
+	    if (w->pavl_balance == +1)
+		x->pavl_balance = 0, y->pavl_balance = -1;
+	    else if (w->pavl_balance == 0)
+		x->pavl_balance = y->pavl_balance = 0;
+	    else		/* |w->pavl_balance == -1| */
+		x->pavl_balance = +1, y->pavl_balance = 0;
+	    w->pavl_balance = 0;
+	    w->pavl_parent = y->pavl_parent;
+	    x->pavl_parent = y->pavl_parent = w;
+	    if (x->pavl_link[0] != NULL)
+		x->pavl_link[0]->pavl_parent = x;
+	    if (y->pavl_link[1] != NULL)
+		y->pavl_link[1]->pavl_parent = y;
+	}
+    }
+    else
+	return &n->pavl_data;
+
+    if (w->pavl_parent != NULL)
+	w->pavl_parent->pavl_link[y != w->pavl_parent->pavl_link[0]] = w;
+    else
+	tree->pavl_root = w;
+
+    return &n->pavl_data;
+}
+
+/* Inserts |item| into |table|.
+   Returns |NULL| if |item| was successfully inserted
+   or if a memory allocation error occurred.
+   Otherwise, returns the duplicate item. */
+void *pavl_insert(struct pavl_table *table, void *item)
+{
+    void **p = pavl_probe(table, item);
+
+    return p == NULL || *p == item ? NULL : *p;
+}
+
+/* Inserts |item| into |table|, replacing any duplicate item.
+   Returns |NULL| if |item| was inserted without replacing a duplicate,
+   or if a memory allocation error occurred.
+   Otherwise, returns the item that was replaced. */
+void *pavl_replace(struct pavl_table *table, void *item)
+{
+    void **p = pavl_probe(table, item);
+
+    if (p == NULL || *p == item)
+	return NULL;
+    else {
+	void *r = *p;
+
+	*p = item;
+
+	return r;
+    }
+}
+
+/* Deletes from |tree| and returns an item matching |item|.
+   Returns a null pointer if no matching item found. */
+void *pavl_delete(struct pavl_table *tree, const void *item)
+{
+    struct pavl_node *p;	/* Traverses tree to find node to delete. */
+    struct pavl_node *q;	/* Parent of |p|. */
+    int dir;			/* Side of |q| on which |p| is linked. */
+    int cmp;			/* Result of comparison between |item| and |p|. */
+
+    assert(tree != NULL && item != NULL);
+
+    p = tree->pavl_root;
+    dir = 0;
+    while (p != NULL) {
+	cmp = tree->pavl_compare(item, p->pavl_data);
+
+	if (cmp == 0)
+	    break;
+
+	dir = cmp > 0;
+	p = p->pavl_link[dir];
+    }
+    if (p == NULL)
+	return NULL;
+
+    item = p->pavl_data;
+
+    q = p->pavl_parent;
+    if (q == NULL) {
+	q = (struct pavl_node *)&tree->pavl_root;
+	dir = 0;
+    }
+
+    if (p->pavl_link[1] == NULL) {
+	q->pavl_link[dir] = p->pavl_link[0];
+	if (q->pavl_link[dir] != NULL)
+	    q->pavl_link[dir]->pavl_parent = p->pavl_parent;
+    }
+    else {
+	struct pavl_node *r = p->pavl_link[1];
+
+	if (r->pavl_link[0] == NULL) {
+	    r->pavl_link[0] = p->pavl_link[0];
+	    q->pavl_link[dir] = r;
+	    r->pavl_parent = p->pavl_parent;
+	    if (r->pavl_link[0] != NULL)
+		r->pavl_link[0]->pavl_parent = r;
+	    r->pavl_balance = p->pavl_balance;
+	    q = r;
+	    dir = 1;
+	}
+	else {
+	    struct pavl_node *s = r->pavl_link[0];
+
+	    while (s->pavl_link[0] != NULL)
+		s = s->pavl_link[0];
+	    r = s->pavl_parent;
+	    r->pavl_link[0] = s->pavl_link[1];
+	    s->pavl_link[0] = p->pavl_link[0];
+	    s->pavl_link[1] = p->pavl_link[1];
+	    q->pavl_link[dir] = s;
+	    if (s->pavl_link[0] != NULL)
+		s->pavl_link[0]->pavl_parent = s;
+	    s->pavl_link[1]->pavl_parent = s;
+	    s->pavl_parent = p->pavl_parent;
+	    if (r->pavl_link[0] != NULL)
+		r->pavl_link[0]->pavl_parent = r;
+	    s->pavl_balance = p->pavl_balance;
+	    q = r;
+	    dir = 0;
+	}
+    }
+    tree->pavl_alloc->libavl_free(p);
+
+    while (q != (struct pavl_node *)&tree->pavl_root) {
+	struct pavl_node *y = q;
+
+	if (y->pavl_parent != NULL)
+	    q = y->pavl_parent;
+	else
+	    q = (struct pavl_node *)&tree->pavl_root;
+
+	if (dir == 0) {
+	    dir = q->pavl_link[0] != y;
+	    y->pavl_balance++;
+	    if (y->pavl_balance == +1)
+		break;
+	    else if (y->pavl_balance == +2) {
+		struct pavl_node *x = y->pavl_link[1];
+
+		if (x->pavl_balance == -1) {
+		    struct pavl_node *w;
+
+		    assert(x->pavl_balance == -1);
+		    w = x->pavl_link[0];
+		    x->pavl_link[0] = w->pavl_link[1];
+		    w->pavl_link[1] = x;
+		    y->pavl_link[1] = w->pavl_link[0];
+		    w->pavl_link[0] = y;
+		    if (w->pavl_balance == +1)
+			x->pavl_balance = 0, y->pavl_balance = -1;
+		    else if (w->pavl_balance == 0)
+			x->pavl_balance = y->pavl_balance = 0;
+		    else	/* |w->pavl_balance == -1| */
+			x->pavl_balance = +1, y->pavl_balance = 0;
+		    w->pavl_balance = 0;
+		    w->pavl_parent = y->pavl_parent;
+		    x->pavl_parent = y->pavl_parent = w;
+		    if (x->pavl_link[0] != NULL)
+			x->pavl_link[0]->pavl_parent = x;
+		    if (y->pavl_link[1] != NULL)
+			y->pavl_link[1]->pavl_parent = y;
+		    q->pavl_link[dir] = w;
+		}
+		else {
+		    y->pavl_link[1] = x->pavl_link[0];
+		    x->pavl_link[0] = y;
+		    x->pavl_parent = y->pavl_parent;
+		    y->pavl_parent = x;
+		    if (y->pavl_link[1] != NULL)
+			y->pavl_link[1]->pavl_parent = y;
+		    q->pavl_link[dir] = x;
+		    if (x->pavl_balance == 0) {
+			x->pavl_balance = -1;
+			y->pavl_balance = +1;
+			break;
+		    }
+		    else {
+			x->pavl_balance = y->pavl_balance = 0;
+			y = x;
+		    }
+		}
+	    }
+	}
+	else {
+	    dir = q->pavl_link[0] != y;
+	    y->pavl_balance--;
+	    if (y->pavl_balance == -1)
+		break;
+	    else if (y->pavl_balance == -2) {
+		struct pavl_node *x = y->pavl_link[0];
+
+		if (x->pavl_balance == +1) {
+		    struct pavl_node *w;
+
+		    assert(x->pavl_balance == +1);
+		    w = x->pavl_link[1];
+		    x->pavl_link[1] = w->pavl_link[0];
+		    w->pavl_link[0] = x;
+		    y->pavl_link[0] = w->pavl_link[1];
+		    w->pavl_link[1] = y;
+		    if (w->pavl_balance == -1)
+			x->pavl_balance = 0, y->pavl_balance = +1;
+		    else if (w->pavl_balance == 0)
+			x->pavl_balance = y->pavl_balance = 0;
+		    else	/* |w->pavl_balance == +1| */
+			x->pavl_balance = -1, y->pavl_balance = 0;
+		    w->pavl_balance = 0;
+		    w->pavl_parent = y->pavl_parent;
+		    x->pavl_parent = y->pavl_parent = w;
+		    if (x->pavl_link[1] != NULL)
+			x->pavl_link[1]->pavl_parent = x;
+		    if (y->pavl_link[0] != NULL)
+			y->pavl_link[0]->pavl_parent = y;
+		    q->pavl_link[dir] = w;
+		}
+		else {
+		    y->pavl_link[0] = x->pavl_link[1];
+		    x->pavl_link[1] = y;
+		    x->pavl_parent = y->pavl_parent;
+		    y->pavl_parent = x;
+		    if (y->pavl_link[0] != NULL)
+			y->pavl_link[0]->pavl_parent = y;
+		    q->pavl_link[dir] = x;
+		    if (x->pavl_balance == 0) {
+			x->pavl_balance = +1;
+			y->pavl_balance = -1;
+			break;
+		    }
+		    else {
+			x->pavl_balance = y->pavl_balance = 0;
+			y = x;
+		    }
+		}
+	    }
+	}
+    }
+
+    tree->pavl_count--;
+    return (void *)item;
+}
+
+/* Initializes |trav| for use with |tree|
+   and selects the null node. */
+void pavl_t_init(struct pavl_traverser *trav, struct pavl_table *tree)
+{
+    trav->pavl_table = tree;
+    trav->pavl_node = NULL;
+}
+
+/* Initializes |trav| for |tree|.
+   Returns data item in |tree| with the least value,
+   or |NULL| if |tree| is empty. */
+void *pavl_t_first(struct pavl_traverser *trav, struct pavl_table *tree)
+{
+    assert(tree != NULL && trav != NULL);
+
+    trav->pavl_table = tree;
+    trav->pavl_node = tree->pavl_root;
+    if (trav->pavl_node != NULL) {
+	while (trav->pavl_node->pavl_link[0] != NULL)
+	    trav->pavl_node = trav->pavl_node->pavl_link[0];
+
+	return trav->pavl_node->pavl_data;
+    }
+    else
+	return NULL;
+}
+
+/* Initializes |trav| for |tree|.
+   Returns data item in |tree| with the greatest value,
+   or |NULL| if |tree| is empty. */
+void *pavl_t_last(struct pavl_traverser *trav, struct pavl_table *tree)
+{
+    assert(tree != NULL && trav != NULL);
+
+    trav->pavl_table = tree;
+    trav->pavl_node = tree->pavl_root;
+    if (trav->pavl_node != NULL) {
+	while (trav->pavl_node->pavl_link[1] != NULL)
+	    trav->pavl_node = trav->pavl_node->pavl_link[1];
+
+	return trav->pavl_node->pavl_data;
+    }
+    else
+	return NULL;
+}
+
+/* Searches for |item| in |tree|.
+   If found, initializes |trav| to the item found and returns the item
+   as well.
+   If there is no matching item, initializes |trav| to the null item
+   and returns |NULL|. */
+void *pavl_t_find(struct pavl_traverser *trav, struct pavl_table *tree,
+		  void *item)
+{
+    struct pavl_node *p;
+
+    assert(trav != NULL && tree != NULL && item != NULL);
+
+    trav->pavl_table = tree;
+    
+    p = tree->pavl_root;
+    while (p != NULL) {
+	int cmp = tree->pavl_compare(item, p->pavl_data);
+
+	if (cmp == 0) {
+	    trav->pavl_node = p;
+
+	    return p->pavl_data;
+	}
+
+	p = p->pavl_link[cmp > 0];
+    }
+
+    trav->pavl_node = NULL;
+
+    return NULL;
+}
+
+/* Attempts to insert |item| into |tree|.
+   If |item| is inserted successfully, it is returned and |trav| is
+   initialized to its location.
+   If a duplicate is found, it is returned and |trav| is initialized to
+   its location.  No replacement of the item occurs.
+   If a memory allocation failure occurs, |NULL| is returned and |trav|
+   is initialized to the null item. */
+void *pavl_t_insert(struct pavl_traverser *trav,
+		    struct pavl_table *tree, void *item)
+{
+    void **p;
+
+    assert(trav != NULL && tree != NULL && item != NULL);
+
+    p = pavl_probe(tree, item);
+    if (p != NULL) {
+	trav->pavl_table = tree;
+	trav->pavl_node = ((struct pavl_node *)((char *)p -
+						offsetof(struct pavl_node,
+							 pavl_data)));
+
+	return *p;
+    }
+    else {
+	pavl_t_init(trav, tree);
+
+	return NULL;
+    }
+}
+
+/* Initializes |trav| to have the same current node as |src|. */
+void *pavl_t_copy(struct pavl_traverser *trav,
+		  const struct pavl_traverser *src)
+{
+    assert(trav != NULL && src != NULL);
+
+    trav->pavl_table = src->pavl_table;
+    trav->pavl_node = src->pavl_node;
+
+    return trav->pavl_node != NULL ? trav->pavl_node->pavl_data : NULL;
+}
+
+/* Returns the next data item in inorder
+   within the tree being traversed with |trav|,
+   or if there are no more data items returns |NULL|. */
+void *pavl_t_next(struct pavl_traverser *trav)
+{
+    assert(trav != NULL);
+
+    if (trav->pavl_node == NULL)
+	return pavl_t_first(trav, trav->pavl_table);
+    else if (trav->pavl_node->pavl_link[1] == NULL) {
+	struct pavl_node *q, *p;	/* Current node and its child. */
+
+	for (p = trav->pavl_node, q = p->pavl_parent;;
+	     p = q, q = q->pavl_parent)
+	    if (q == NULL || p == q->pavl_link[0]) {
+		trav->pavl_node = q;
+
+		return trav->pavl_node != NULL ?
+		    trav->pavl_node->pavl_data : NULL;
+	    }
+    }
+    else {
+	trav->pavl_node = trav->pavl_node->pavl_link[1];
+	while (trav->pavl_node->pavl_link[0] != NULL)
+	    trav->pavl_node = trav->pavl_node->pavl_link[0];
+
+	return trav->pavl_node->pavl_data;
+    }
+}
+
+/* Returns the previous data item in inorder
+   within the tree being traversed with |trav|,
+   or if there are no more data items returns |NULL|. */
+void *pavl_t_prev(struct pavl_traverser *trav)
+{
+    assert(trav != NULL);
+
+    if (trav->pavl_node == NULL)
+	return pavl_t_last(trav, trav->pavl_table);
+    else if (trav->pavl_node->pavl_link[0] == NULL) {
+	struct pavl_node *q, *p;	/* Current node and its child. */
+
+	for (p = trav->pavl_node, q = p->pavl_parent;;
+	     p = q, q = q->pavl_parent)
+	    if (q == NULL || p == q->pavl_link[1]) {
+		trav->pavl_node = q;
+
+		return trav->pavl_node != NULL ?
+		    trav->pavl_node->pavl_data : NULL;
+	    }
+    }
+    else {
+	trav->pavl_node = trav->pavl_node->pavl_link[0];
+	while (trav->pavl_node->pavl_link[1] != NULL)
+	    trav->pavl_node = trav->pavl_node->pavl_link[1];
+
+	return trav->pavl_node->pavl_data;
+    }
+}
+
+/* Returns |trav|'s current item. */
+void *pavl_t_cur(struct pavl_traverser *trav)
+{
+    assert(trav != NULL);
+
+    return trav->pavl_node != NULL ? trav->pavl_node->pavl_data : NULL;
+}
+
+/* Replaces the current item in |trav| by |new| and returns the item replaced.
+   |trav| must not have the null item selected.
+   The new item must not upset the ordering of the tree. */
+void *pavl_t_replace(struct pavl_traverser *trav, void *new)
+{
+    void *old;
+
+    assert(trav != NULL && trav->pavl_node != NULL && new != NULL);
+    old = trav->pavl_node->pavl_data;
+    trav->pavl_node->pavl_data = new;
+
+    return old;
+}
+
+/* Destroys |new| with |pavl_destroy (new, destroy)|,
+   first initializing right links in |new| that have
+   not yet been initialized at time of call. */
+static void
+copy_error_recovery(struct pavl_node *q,
+		    struct pavl_table *new, pavl_item_func * destroy)
+{
+    assert(q != NULL && new != NULL);
+
+    for (;;) {
+	struct pavl_node *p = q;
+
+	q = q->pavl_parent;
+	if (q == NULL)
+	    break;
+
+	if (p == q->pavl_link[0])
+	    q->pavl_link[1] = NULL;
+    }
+
+    pavl_destroy(new, destroy);
+}
+
+/* Copies |org| to a newly created tree, which is returned.
+   If |copy != NULL|, each data item in |org| is first passed to |copy|,
+   and the return values are inserted into the tree;
+   |NULL| return values are taken as indications of failure.
+   On failure, destroys the partially created new tree,
+   applying |destroy|, if non-null, to each item in the new tree so far,
+   and returns |NULL|.
+   If |allocator != NULL|, it is used for allocation in the new tree.
+   Otherwise, the same allocator used for |org| is used. */
+struct pavl_table *pavl_copy(const struct pavl_table *org,
+			     pavl_copy_func * copy, pavl_item_func * destroy,
+			     struct libavl_allocator *allocator)
+{
+    struct pavl_table *new;
+    const struct pavl_node *x;
+    struct pavl_node *y;
+
+    assert(org != NULL);
+    new = pavl_create(org->pavl_compare,
+		      allocator != NULL ? allocator : org->pavl_alloc);
+    if (new == NULL)
+	return NULL;
+
+    new->pavl_count = org->pavl_count;
+    if (new->pavl_count == 0)
+	return new;
+
+    x = (const struct pavl_node *)&org->pavl_root;
+    y = (struct pavl_node *)&new->pavl_root;
+    while (x != NULL) {
+	while (x->pavl_link[0] != NULL) {
+	    y->pavl_link[0] =
+		new->pavl_alloc->libavl_malloc(sizeof *y->pavl_link[0]);
+	    if (y->pavl_link[0] == NULL) {
+		if (y != (struct pavl_node *)&new->pavl_root) {
+		    y->pavl_data = NULL;
+		    y->pavl_link[1] = NULL;
+		}
+
+		copy_error_recovery(y, new, destroy);
+
+		return NULL;
+	    }
+	    y->pavl_link[0]->pavl_parent = y;
+
+	    x = x->pavl_link[0];
+	    y = y->pavl_link[0];
+	}
+	y->pavl_link[0] = NULL;
+
+	for (;;) {
+	    y->pavl_balance = x->pavl_balance;
+	    if (copy == NULL)
+		y->pavl_data = x->pavl_data;
+	    else {
+		y->pavl_data = copy(x->pavl_data);
+		if (y->pavl_data == NULL) {
+		    y->pavl_link[1] = NULL;
+		    copy_error_recovery(y, new, destroy);
+
+		    return NULL;
+		}
+	    }
+
+	    if (x->pavl_link[1] != NULL) {
+		y->pavl_link[1] =
+		    new->pavl_alloc->libavl_malloc(sizeof *y->pavl_link[1]);
+		if (y->pavl_link[1] == NULL) {
+		    copy_error_recovery(y, new, destroy);
+
+		    return NULL;
+		}
+		y->pavl_link[1]->pavl_parent = y;
+
+		x = x->pavl_link[1];
+		y = y->pavl_link[1];
+		break;
+	    }
+	    else
+		y->pavl_link[1] = NULL;
+
+	    for (;;) {
+		const struct pavl_node *w = x;
+
+		x = x->pavl_parent;
+		if (x == NULL) {
+		    new->pavl_root->pavl_parent = NULL;
+		    return new;
+		}
+		y = y->pavl_parent;
+
+		if (w == x->pavl_link[0])
+		    break;
+	    }
+	}
+    }
+
+    return new;
+}
+
+/* Frees storage allocated for |tree|.
+   If |destroy != NULL|, applies it to each data item in inorder. */
+void pavl_destroy(struct pavl_table *tree, pavl_item_func * destroy)
+{
+    struct pavl_node *p, *q;
+
+    assert(tree != NULL);
+
+    p = tree->pavl_root;
+    while (p != NULL) {
+	if (p->pavl_link[0] == NULL) {
+	    q = p->pavl_link[1];
+	    if (destroy != NULL && p->pavl_data != NULL)
+		destroy(p->pavl_data);
+	    tree->pavl_alloc->libavl_free(p);
+	}
+	else {
+	    q = p->pavl_link[0];
+	    p->pavl_link[0] = q->pavl_link[1];
+	    q->pavl_link[1] = p;
+	}
+	p = q;
+    }
+
+    tree->pavl_alloc->libavl_free(tree);
+}
+
+/* Allocates |size| bytes of space using |malloc()|.
+   Returns a null pointer if allocation fails. */
+void *pavl_malloc(size_t size)
+{
+    if (size > 0)
+	return malloc(size);
+
+    return NULL;
+}
+
+/* Frees |block|. */
+void pavl_free(void *block)
+{
+    if (block)
+	free(block);
+}
+
+/* Default memory allocator that uses |malloc()| and |free()|. */
+struct libavl_allocator pavl_allocator_default = {
+    pavl_malloc,
+    pavl_free
+};
+
+#undef NDEBUG
+#include <assert.h>
+
+/* Asserts that |pavl_insert()| succeeds at inserting |item| into |table|. */
+void (pavl_assert_insert) (struct pavl_table * table, void *item)
+{
+    void **p = pavl_probe(table, item);
+
+    assert(p != NULL && *p == item);
+}
+
+/* Asserts that |pavl_delete()| really removes |item| from |table|,
+   and returns the removed item. */
+void *(pavl_assert_delete) (struct pavl_table * table, void *item)
+{
+    void *p = pavl_delete(table, item);
+
+    assert(p != NULL);
+
+    return p;
+}

Copied: grass-addons/grass7/imagery/i.superpixels.slic/pavl.h (from rev 70370, grass-addons/grass7/raster/r.resamp.tps/pavl.h)
===================================================================
--- grass-addons/grass7/imagery/i.superpixels.slic/pavl.h	                        (rev 0)
+++ grass-addons/grass7/imagery/i.superpixels.slic/pavl.h	2017-01-30 08:29:13 UTC (rev 70456)
@@ -0,0 +1,106 @@
+/* Produced by texiweb from libavl.w. */
+
+/* libavl - library for manipulation of binary trees.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
+   Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301 USA.
+ */
+
+#ifndef PAVL_H
+#define PAVL_H 1
+
+#include <stddef.h>
+
+/* Function types. */
+typedef int pavl_comparison_func(const void *pavl_a, const void *pavl_b);
+typedef void pavl_item_func(void *pavl_item);
+typedef void *pavl_copy_func(void *pavl_item);
+
+#ifndef LIBAVL_ALLOCATOR
+#define LIBAVL_ALLOCATOR
+/* Memory allocator. */
+struct libavl_allocator
+{
+    void *(*libavl_malloc) (size_t libavl_size);
+    void (*libavl_free) (void *libavl_block);
+};
+#endif
+
+/* Default memory allocator. */
+extern struct libavl_allocator pavl_allocator_default;
+void *pavl_malloc(size_t);
+void pavl_free(void *);
+
+/* Maximum PAVL height, unused. */
+#ifndef PAVL_MAX_HEIGHT
+#define PAVL_MAX_HEIGHT 32
+#endif
+
+/* Tree data structure. */
+struct pavl_table
+{
+    struct pavl_node *pavl_root;	/* Tree's root. */
+    pavl_comparison_func *pavl_compare;	/* Comparison function. */
+    struct libavl_allocator *pavl_alloc;	/* Memory allocator. */
+    size_t pavl_count;		/* Number of items in tree. */
+};
+
+/* An PAVL tree node. */
+struct pavl_node
+{
+    struct pavl_node *pavl_link[2];	/* Subtrees. */
+    struct pavl_node *pavl_parent;	/* Parent node. */
+    void *pavl_data;		/* Pointer to data. */
+    signed char pavl_balance;	/* Balance factor. */
+};
+
+/* PAVL traverser structure. */
+struct pavl_traverser
+{
+    struct pavl_table *pavl_table;	/* Tree being traversed. */
+    struct pavl_node *pavl_node;	/* Current node in tree. */
+};
+
+/* Table functions. */
+struct pavl_table *pavl_create(pavl_comparison_func *,
+			       struct libavl_allocator *);
+struct pavl_table *pavl_copy(const struct pavl_table *, pavl_copy_func *,
+			     pavl_item_func *, struct libavl_allocator *);
+void pavl_destroy(struct pavl_table *, pavl_item_func *);
+void **pavl_probe(struct pavl_table *, void *);
+void *pavl_insert(struct pavl_table *, void *);
+void *pavl_replace(struct pavl_table *, void *);
+void *pavl_delete(struct pavl_table *, const void *);
+void *pavl_find(const struct pavl_table *, const void *);
+void pavl_assert_insert(struct pavl_table *, void *);
+void *pavl_assert_delete(struct pavl_table *, void *);
+
+#define pavl_count(table) ((size_t) (table)->pavl_count)
+
+/* Table traverser functions. */
+void pavl_t_init(struct pavl_traverser *, struct pavl_table *);
+void *pavl_t_first(struct pavl_traverser *, struct pavl_table *);
+void *pavl_t_last(struct pavl_traverser *, struct pavl_table *);
+void *pavl_t_find(struct pavl_traverser *, struct pavl_table *, void *);
+void *pavl_t_insert(struct pavl_traverser *, struct pavl_table *, void *);
+void *pavl_t_copy(struct pavl_traverser *, const struct pavl_traverser *);
+void *pavl_t_next(struct pavl_traverser *);
+void *pavl_t_prev(struct pavl_traverser *);
+void *pavl_t_cur(struct pavl_traverser *);
+void *pavl_t_replace(struct pavl_traverser *, void *);
+
+#endif /* pavl.h */

Copied: grass-addons/grass7/imagery/i.superpixels.slic/rclist.c (from rev 70370, grass-addons/grass7/raster/r.resamp.tps/rclist.c)
===================================================================
--- grass-addons/grass7/imagery/i.superpixels.slic/rclist.c	                        (rev 0)
+++ grass-addons/grass7/imagery/i.superpixels.slic/rclist.c	2017-01-30 08:29:13 UTC (rev 70456)
@@ -0,0 +1,68 @@
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "rclist.h"
+
+void rclist_init(struct rclist *list)
+{
+    list->head = list->tail = NULL;
+    
+    return;
+}
+
+void rclist_add(struct rclist *list, int row, int col)
+{
+    struct rc *new = G_malloc(sizeof(struct rc));
+
+    if (!new)
+	G_fatal_error(_("rclist out of memory"));
+
+    new->next = NULL;
+    new->row = row;
+    new->col = col;
+    
+    if (list->head) {
+	list->head->next = new;
+	list->head = new;
+    }
+    else {
+	list->head = list->tail = new;
+    }
+    
+    return;
+}
+
+/* return 1 if an element was dropped
+ * return 0 if list is empty
+ */
+int rclist_drop(struct rclist *list, struct rc *rc)
+{
+    if (list->tail) {
+	struct rc *next = list->tail->next;
+
+	rc->row = list->tail->row;
+	rc->col = list->tail->col;
+	G_free(list->tail);
+	list->tail = next;
+	if (!list->tail)
+	    list->head = NULL;
+
+	return 1;
+    }
+
+    return 0;
+}
+
+void rclist_destroy(struct rclist *list)
+{
+    struct rc *next = list->tail;
+    
+    while (next) {
+	next = next->next;
+	G_free(list->tail);
+	list->tail = next;
+    }
+    list->head = NULL;
+    
+    return;
+}
+

Copied: grass-addons/grass7/imagery/i.superpixels.slic/rclist.h (from rev 70370, grass-addons/grass7/raster/r.resamp.tps/rclist.h)
===================================================================
--- grass-addons/grass7/imagery/i.superpixels.slic/rclist.h	                        (rev 0)
+++ grass-addons/grass7/imagery/i.superpixels.slic/rclist.h	2017-01-30 08:29:13 UTC (rev 70456)
@@ -0,0 +1,20 @@
+/* row/col list */
+
+struct rc
+{
+    struct rc *next;
+    int row;
+    int col;
+};
+
+struct rclist
+{
+    struct rc *tail, *head;
+};
+
+/* rclist.c */
+void rclist_init(struct rclist *);
+void rclist_add(struct rclist *, int, int);
+int rclist_drop(struct rclist *, struct rc *);
+void rclist_destroy(struct rclist *);
+



More information about the grass-commit mailing list