[GRASS-SVN] r71951 - grass-addons/grass7/imagery/i.superpixels.slic
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Dec 18 14:19:38 PST 2017
Author: mmetz
Date: 2017-12-18 14:19:38 -0800 (Mon, 18 Dec 2017)
New Revision: 71951
Modified:
grass-addons/grass7/imagery/i.superpixels.slic/cache.c
grass-addons/grass7/imagery/i.superpixels.slic/cache.h
grass-addons/grass7/imagery/i.superpixels.slic/main.c
Log:
i.superpixels.slic: generalize cache interface
Modified: grass-addons/grass7/imagery/i.superpixels.slic/cache.c
===================================================================
--- grass-addons/grass7/imagery/i.superpixels.slic/cache.c 2017-12-18 22:18:56 UTC (rev 71950)
+++ grass-addons/grass7/imagery/i.superpixels.slic/cache.c 2017-12-18 22:19:38 UTC (rev 71951)
@@ -1,5 +1,4 @@
#include <string.h>
-#include <grass/raster.h>
#include <grass/segment.h>
#include <grass/glocale.h>
#include "cache.h"
@@ -29,15 +28,22 @@
return p;
}
-int cache_create(struct cache *c, int nrows, int ncols, int seg_size,
- int use_seg, int nbytes, int nseg)
+int cache_create(struct cache *c, int nrows, int ncols, int srows,
+ int scols, int nbytes, int nseg)
{
+ int nseg_total;
+
c->n = nbytes;
c->rows = nrows;
c->cols = ncols;
- if (use_seg) {
- if (Segment_open(&c->s, G_tempfile(), nrows, ncols, seg_size, seg_size,
+ nseg_total = ((nrows + srows - 1) / srows) *
+ ((ncols + scols - 1) / scols);
+
+ if (nseg < nseg_total) {
+ G_verbose_message("Using disk cache");
+
+ if (Segment_open(&c->s, G_tempfile(), nrows, ncols, srows, scols,
nbytes, nseg) != 1)
G_fatal_error("Unable to create temporary file");
@@ -46,6 +52,8 @@
c->put = cache_put_s;
}
else {
+ G_verbose_message("Using memory cache");
+
c->r = G_malloc(sizeof(char) * c->rows * c->cols * c->n);
c->get = cache_get_r;
c->put = cache_put_r;
Modified: grass-addons/grass7/imagery/i.superpixels.slic/cache.h
===================================================================
--- grass-addons/grass7/imagery/i.superpixels.slic/cache.h 2017-12-18 22:18:56 UTC (rev 71950)
+++ grass-addons/grass7/imagery/i.superpixels.slic/cache.h 2017-12-18 22:19:38 UTC (rev 71951)
@@ -10,8 +10,8 @@
void *(* put)(struct cache *c, void *p, int row, int col);
};
-int cache_create(struct cache *c, int nrows, int ncols, int seg_size,
- int use_seg, int nbytes, int nseg);
+int cache_create(struct cache *c, int nrows, int ncols, int srows,
+ int scols, int nbytes, int nseg);
int cache_destroy(struct cache *c);
void *cache_get(struct cache *c, void *p, int row, int col);
void *cache_put(struct cache *c, void *p, int row, int col);
Modified: grass-addons/grass7/imagery/i.superpixels.slic/main.c
===================================================================
--- grass-addons/grass7/imagery/i.superpixels.slic/main.c 2017-12-18 22:18:56 UTC (rev 71950)
+++ grass-addons/grass7/imagery/i.superpixels.slic/main.c 2017-12-18 22:19:38 UTC (rev 71951)
@@ -63,8 +63,8 @@
struct Colors colors;
struct History hist;
- int seg_size, nseg, nsegc, nseg_total, use_seg;
- double segs_mb, k_mb, all_mb;
+ int seg_size, nseg;
+ double segs_mb, k_mb;
int n_iterations, n_super_pixels, numk, numlabels, slic0;
int nrows, ncols, row, col, b, k;
@@ -319,18 +319,7 @@
G_fatal_error(_("Not enough memory, increase %s option"), opt_mem->answer);
G_debug(1, "MB for temporary data: %g", segs_mb);
-
- all_mb = (sizeof(DCELL) * nbands + sizeof(double) * 2 + sizeof(int)) *
- ((double) nrows / 1024.) * ((double) ncols / 1024.);
- G_debug(1, "MB for all in RAM: %g", all_mb);
-
- use_seg = 0;
- /* TODO: k_mb + all_mb is smaller than the actual memory consumption
- * when using all-in-ram mode */
- if (segs_mb < k_mb + all_mb)
- use_seg = 1;
-
segs_mb -= k_mb;
seg_size = 64;
@@ -338,31 +327,15 @@
(seg_size * seg_size * (sizeof(DCELL) * nbands +
sizeof(double) * 2 + sizeof(int)));
- nsegc = ncols / seg_size;
- if (ncols % seg_size)
- nsegc++;
- nseg_total = nsegc * ((nrows + seg_size - 1) / seg_size);
-
- if (!use_seg) {
- G_message(_("Cache data in memory mode"));
- }
- else {
- G_message(_("Cache data on disk mode"));
- if (nseg > nseg_total)
- nseg = nseg_total;
- G_verbose_message(_("Number of segments in memory: %d of %d"), nseg, nseg_total);
-
- }
-
- if (cache_create(&bands_seg, nrows, ncols, seg_size, use_seg,
+ if (cache_create(&bands_seg, nrows, ncols, seg_size, seg_size,
sizeof(DCELL) * nbands, nseg) != 1)
G_fatal_error("Unable to create grid cache");
- if (cache_create(&dist_seg, nrows, ncols, seg_size, use_seg,
+ if (cache_create(&dist_seg, nrows, ncols, seg_size, seg_size,
sizeof(double) * 2, nseg) != 1)
G_fatal_error("Unable to create grid cache");
- if (cache_create(&k_seg, nrows, ncols, seg_size, use_seg,
+ if (cache_create(&k_seg, nrows, ncols, seg_size, seg_size,
sizeof(int), nseg) != 1)
G_fatal_error("Unable to create grid cache");
@@ -682,7 +655,7 @@
G_free(clustersize);
cache_destroy(&dist_seg);
- cache_create(&nk_seg, nrows, ncols, seg_size, use_seg,
+ cache_create(&nk_seg, nrows, ncols, seg_size, seg_size,
sizeof(int), nseg);
numlabels = SLIC_EnforceLabelConnectivity(&k_seg, ncols, nrows,
More information about the grass-commit
mailing list