[GRASS-SVN] r40083 - grass/trunk/lib/raster

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Dec 20 01:20:24 EST 2009


Author: glynn
Date: 2009-12-20 01:20:23 -0500 (Sun, 20 Dec 2009)
New Revision: 40083

Modified:
   grass/trunk/lib/raster/R.h
   grass/trunk/lib/raster/closecell.c
   grass/trunk/lib/raster/get_row.c
   grass/trunk/lib/raster/opencell.c
   grass/trunk/lib/raster/put_row.c
Log:
Keep null bitmap open


Modified: grass/trunk/lib/raster/R.h
===================================================================
--- grass/trunk/lib/raster/R.h	2009-12-19 21:51:38 UTC (rev 40082)
+++ grass/trunk/lib/raster/R.h	2009-12-20 06:20:23 UTC (rev 40083)
@@ -55,6 +55,8 @@
     int null_cur_row;		/* Current null row in memory   */
     int cur_nbytes;		/* nbytes per cell for current row */
     unsigned char *data;	/* Decompressed data buffer     */
+    int null_fd;		/* Null bitmap fd               */
+    unsigned char *null_bits;	/* Null bitmap buffer           */
     int nbytes;			/* bytes per cell               */
     RASTER_MAP_TYPE map_type;	/* type: int, float or double map */
     char *temp_name;		/* Temporary name for NEW files */
@@ -64,8 +66,6 @@
     char *mapset;		/* Mapset of open file          */
     int io_error;		/* io error warning given       */
     XDR xdrstream;		/* xdr stream for reading fp    */
-    unsigned char *NULL_ROWS[NULL_ROWS_INMEM];
-    int min_null_row;		/* Minimum row null row number in memory */
     struct Quant quant;
     struct GDAL_link *gdal;
 };

Modified: grass/trunk/lib/raster/closecell.c
===================================================================
--- grass/trunk/lib/raster/closecell.c	2009-12-19 21:51:38 UTC (rev 40082)
+++ grass/trunk/lib/raster/closecell.c	2009-12-20 06:20:23 UTC (rev 40083)
@@ -115,7 +115,6 @@
 static int close_old(int fd)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
-    int i;
 
     /* if R__.auto_mask was only allocated for reading map rows to create
        non-existant null rows, and not for actuall mask, free R__.mask_row 
@@ -127,8 +126,11 @@
     if (fcb->gdal)
 	Rast_close_gdal_link(fcb->gdal);
 
-    for (i = 0; i < NULL_ROWS_INMEM; i++)
-	G_free(fcb->NULL_ROWS[i]);
+    if (fcb->null_bits)
+	G_free(fcb->null_bits);
+    if (fcb->null_fd >= 0)
+	close(fcb->null_fd);
+    fcb->null_fd = -1;
 
     if (fcb->cellhd.compressed)
 	G_free(fcb->row_ptr);
@@ -315,12 +317,16 @@
     struct fileinfo *fcb = &R__.fileinfo[fd];
     int stat;
     char path[GPATH_MAX];
-    int row, i;
+    int row;
     const char *CELL_DIR;
 
     if (fcb->gdal)
 	return close_new_gdal(fd, ok);
 
+    if (fcb->null_fd >= 0)
+	close(fcb->null_fd);
+    fcb->null_fd = -1;
+
     if (ok) {
 	switch (fcb->open_mode) {
 	case OPEN_NEW_COMPRESSED:
@@ -347,35 +353,8 @@
 
 	if (fcb->null_cur_row > 0) {
 	    /* if temporary NULL file exists, write it into cell_misc/name/null */
-	    int null_fd;
-
-	    null_fd = Rast__open_null_write(fd);
-	    if (null_fd <= 0)
-		return -1;
-	    if (null_fd < 1)
-		return -1;
-
-	    /* first finish writing null file */
-	    /* write out the rows stored in memory */
-	    for (row = fcb->min_null_row; row < fcb->null_cur_row; row++)
-		Rast__write_null_bits(null_fd,
-				      fcb->NULL_ROWS[row - fcb->min_null_row],
-				      row, fcb->cellhd.cols, fd);
-
-	    /* write missing rows */
-	    if (fcb->null_cur_row < fcb->cellhd.rows) {
-		unsigned char *null_work_buf =
-		    Rast__allocate_null_bits(fcb->cellhd.cols);
-		Rast__init_null_bits(null_work_buf, fcb->cellhd.cols);
-		for (row = fcb->null_cur_row; row < fcb->cellhd.rows; row++)
-		    Rast__write_null_bits(null_fd, null_work_buf, row,
-					  fcb->cellhd.cols, fd);
-		G_free(null_work_buf);
-	    }
-	    close(null_fd);
-
 	    if (rename(fcb->null_temp_name, path)) {
-		G_warning(_("Unable to renae null file '%s'"),
+		G_warning(_("Unable to rename null file '%s'"),
 			  fcb->null_temp_name, path);
 		stat = -1;
 	    }
@@ -464,8 +443,7 @@
     G_free(fcb->name);
     G_free(fcb->mapset);
 
-    for (i = 0; i < NULL_ROWS_INMEM; i++)
-	G_free(fcb->NULL_ROWS[i]);
+    G_free(fcb->null_bits);
 
     if (fcb->map_type != CELL_TYPE)
 	Rast_quant_free(&fcb->quant);

Modified: grass/trunk/lib/raster/get_row.c
===================================================================
--- grass/trunk/lib/raster/get_row.c	2009-12-19 21:51:38 UTC (rev 40082)
+++ grass/trunk/lib/raster/get_row.c	2009-12-20 06:20:23 UTC (rev 40083)
@@ -25,8 +25,6 @@
 #include "../gis/G.h"
 #include "R.h"
 
-#define NULL_FILE   "null"
-
 static int embed_nulls(int, void *, int, RASTER_MAP_TYPE, int, int);
 
 static int compute_window_row(int fd, int row, int *cellRow)
@@ -901,44 +899,12 @@
     return Rast_get_row(fd, buf, row, DCELL_TYPE);
 }
 
-static int open_null_read(int fd)
+static int read_null_bits(int fd, int row)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
-    const char *name, *mapset, *dummy;
-    int null_fd;
-
-    if (fcb->null_file_exists == 0)
-	return -1;
-
-    if (fcb->reclass_flag) {
-	name = fcb->reclass.name;
-	mapset = fcb->reclass.mapset;
-    }
-    else {
-	name = fcb->name;
-	mapset = fcb->mapset;
-    }
-
-    dummy = G_find_file2_misc("cell_misc", NULL_FILE, name, mapset);
-
-    if (!dummy) {
-	/* G_warning("unable to find [%s]",path); */
-	fcb->null_file_exists = 0;
-	return -1;
-    }
-
-    null_fd = G_open_old_misc("cell_misc", NULL_FILE, name, mapset);
-    if (null_fd < 0)
-	return -1;
-
-    fcb->null_file_exists = 1;
-
-    return null_fd;
-}
-
-static int read_null_bits(int null_fd, unsigned char *flags, int row,
-			  int cols, int fd)
-{
+    int null_fd = fcb->null_fd;
+    unsigned char *flags = fcb->null_bits;
+    int cols = fcb->cellhd.cols;
     off_t offset;
     ssize_t size;
     int R;
@@ -952,7 +918,7 @@
 	return -1;
 
     size = Rast__null_bitstream_size(cols);
-    offset = (off_t) size *R;
+    offset = (off_t) size * R;
 
     if (lseek(null_fd, offset, SEEK_SET) < 0) {
 	G_warning(_("Error reading null row %d"), R);
@@ -970,92 +936,48 @@
 static void get_null_value_row_nomask(int fd, char *flags, int row)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
-    int i, j, null_fd;
+    int j;
 
     if (row > G__.window.rows || row < 0) {
 	G_warning(_("Reading raster map <%s@%s> request for row %d is outside region"),
 		  fcb->name, fcb->mapset, row);
+	for (j = 0; j < G__.window.cols; j++)
+	    flags[j] = 1;
+	return;
     }
 
-    if ((fcb->min_null_row > row) ||
-	(fcb->min_null_row + NULL_ROWS_INMEM - 1 < row))
-	/* the null row row is not in memory */
-    {
-	unsigned char *null_work_buf =
-	    G__alloca(Rast__null_bitstream_size(fcb->cellhd.cols));
+    if (row != fcb->null_cur_row) {
+	if (read_null_bits(fd, row) < 0) {
+	    fcb->null_cur_row = -1;
+	    if (fcb->map_type == CELL_TYPE) {
+		/* If can't read null row, assume  that all map 0's are nulls */
+		CELL *mask_buf = G__alloca(G__.window.cols * sizeof(CELL));
 
-	/* read in NULL_ROWS_INMEM rows from null file 
-	   so that the requested row is between fcb->min_null_row
-	   and fcb->min_null_row + NULL_ROWS_INMEM */
+		get_map_row_nomask(fd, mask_buf, row, CELL_TYPE);
+		for (j = 0; j < G__.window.cols; j++)
+		    flags[j] = (mask_buf[j] == 0);
 
-	fcb->min_null_row = (row / NULL_ROWS_INMEM) * NULL_ROWS_INMEM;
-
-	null_fd = open_null_read(fd);
-
-	for (i = 0; i < NULL_ROWS_INMEM; i++) {
-	    /* G__.window.rows doesn't have to be a multiple of NULL_ROWS_INMEM */
-	    if (i + fcb->min_null_row >= G__.window.rows)
-		break;
-
-	    if (read_null_bits(null_fd, null_work_buf,
-			       i + fcb->min_null_row, fcb->cellhd.cols,
-			       fd) < 0) {
-		if (fcb->map_type == CELL_TYPE) {
-		    /* If can't read null row, assume  that all map 0's are nulls */
-		    CELL *mask_buf =
-			G__alloca(G__.window.cols * sizeof(CELL));
-
-		    get_map_row_nomask(fd, mask_buf, i + fcb->min_null_row,
-				       CELL_TYPE);
-		    for (j = 0; j < G__.window.cols; j++)
-			flags[j] = (mask_buf[j] == 0);
-
-		    G__freea(mask_buf);
-		}
-		else {		/* fp map */
-
-		    /* if can't read null row, assume  that all data is valid */
-		    G_zero(flags, sizeof(char) * G__.window.cols);
-		    /* the flags row is ready now */
-		}
-	    }			/*if no null file */
-	    else {
-		/* copy null row to flags row translated by window column mapping */
-		/* the fcb->NULL_ROWS[row-fcb->min_null_row] has G__.window.cols bits, */
-		/* the null_work_buf has size fcb->cellhd.cols */
-		for (j = 0; j < G__.window.cols; j++) {
-		    if (!fcb->col_map[j])
-			flags[j] = 1;
-		    else
-			flags[j] = Rast__check_null_bit(null_work_buf,
-							fcb->col_map[j] - 1,
-							fcb->cellhd.cols);
-		}
+		G__freea(mask_buf);
 	    }
-	    /* remember the null row for i for the future reference */
+	    else {		/* fp map */
+		/* if can't read null row, assume  that all data is valid */
+		G_zero(flags, sizeof(char) * G__.window.cols);
+		/* the flags row is ready now */
+	    }
+	}			/*if no null file */
+	else
+	    fcb->null_cur_row = row;
+    }
 
-	    /*bf-We should take of the size - or we get 
-	       zeros running on their own after flags convertions -A.Sh. */
-	    fcb->NULL_ROWS[i] = G_realloc(fcb->NULL_ROWS[i],
-					  Rast__null_bitstream_size(G__.window.cols) +1);
-	    if (fcb->NULL_ROWS[i] == NULL)
-		G_fatal_error("get_null_value_row_nomask: %s",
-			      _("Unable to realloc buffer"));
-
-	    Rast__convert_01_flags(flags, fcb->NULL_ROWS[i], G__.window.cols);
-
-	}			/* for loop */
-
-	if (null_fd > 0)
-	    close(null_fd);
-
-	G__freea(null_work_buf);
-    }				/* row is not in memory */
-
-    /* copy null file data translated by column mapping to user null row */
-    /* the user requested flags row is of size G__.window.cols */
-    Rast__convert_flags_01(flags, fcb->NULL_ROWS[row - fcb->min_null_row],
-			   G__.window.cols);
+    /* copy null row to flags row translated by window column mapping */
+    for (j = 0; j < G__.window.cols; j++) {
+	if (!fcb->col_map[j])
+	    flags[j] = 1;
+	else
+	    flags[j] = Rast__check_null_bit(fcb->null_bits,
+					    fcb->col_map[j] - 1,
+					    fcb->cellhd.cols);
+    }
 }
 
 /*--------------------------------------------------------------------------*/

Modified: grass/trunk/lib/raster/opencell.c
===================================================================
--- grass/trunk/lib/raster/opencell.c	2009-12-19 21:51:38 UTC (rev 40082)
+++ grass/trunk/lib/raster/opencell.c	2009-12-20 06:20:23 UTC (rev 40083)
@@ -28,6 +28,7 @@
 #include "../gis/G.h"
 #include "R.h"
 #define FORMAT_FILE "f_format"
+#define NULL_FILE   "null"
 
 static struct fileinfo *new_fileinfo(int fd)
 {
@@ -55,7 +56,6 @@
     return &R__.fileinfo[fd];
 }
 
-
 /*!
  * \brief Open raster file
  *
@@ -162,7 +162,7 @@
     struct Cell_head cellhd;
     int CELL_nbytes = 0;	/* bytes per cell in CELL map */
     int INTERN_SIZE;
-    int reclass_flag, i;
+    int reclass_flag;
     int MAP_NBYTES;
     RASTER_MAP_TYPE MAP_TYPE;
     struct Reclass reclass;
@@ -286,10 +286,9 @@
     G_copy((char *)&fcb->cellhd, (char *)&cellhd, sizeof(cellhd));
 
     /* allocate null bitstream buffers for reading null rows */
-    for (i = 0; i < NULL_ROWS_INMEM; i++)
-	fcb->NULL_ROWS[i] = Rast__allocate_null_bits(G__.window.cols);
-    /* initialize : no NULL rows in memory */
-    fcb->min_null_row = (-1) * NULL_ROWS_INMEM;
+    fcb->null_fd = -1;
+    fcb->null_cur_row = -1;
+    fcb->null_bits = Rast__allocate_null_bits(cellhd.cols);
 
     /* mark closed */
     fcb->open_mode = -1;
@@ -300,8 +299,6 @@
 
     /* mark no data row in memory  */
     fcb->cur_row = -1;
-    /* fcb->null_cur_row is not used for reading, only for writing */
-    fcb->null_cur_row = -1;
 
     /* if reclass, copy reclass structure */
     if ((fcb->reclass_flag = reclass_flag))
@@ -341,8 +338,18 @@
     fcb->io_error = 0;
     fcb->map_type = MAP_TYPE;
     fcb->nbytes = MAP_NBYTES;
-    fcb->null_file_exists = -1;
 
+    if (!gdal) {
+	if (!G_find_file2_misc("cell_misc", NULL_FILE, r_name, r_mapset)) {
+	    /* G_warning("unable to find [%s]",path); */
+	    fcb->null_file_exists = 0;
+	}
+	else {
+	    fcb->null_fd = G_open_old_misc("cell_misc", NULL_FILE, r_name, r_mapset);
+	    fcb->null_file_exists = fcb->null_fd >= 0;
+	}
+    }
+
     if (fcb->map_type != CELL_TYPE)
 	xdrmem_create(&fcb->xdrstream, (caddr_t) fcb->data,
 		      (u_int) (fcb->nbytes * fcb->cellhd.cols), XDR_DECODE);
@@ -493,7 +500,6 @@
 {
     int fd;
     struct fileinfo *fcb;
-    int i;
 
     /* dummy descriptor to reserve the fileinfo slot */
     fd = open("/dev/null", O_RDONLY);
@@ -525,9 +531,8 @@
     fcb->temp_name = NULL;
     fcb->null_temp_name = NULL;
     fcb->null_cur_row = 0;
-    fcb->min_null_row = 0;
-    for (i = 0; i < NULL_ROWS_INMEM; i++)
-	fcb->NULL_ROWS[i] = NULL;
+    fcb->null_bits = NULL;
+    fcb->null_fd = -1;
 
     if (fcb->map_type != CELL_TYPE)
 	Rast_quant_init(&(fcb->quant));
@@ -557,7 +562,7 @@
 {
     char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
     struct fileinfo *fcb;
-    int i, null_fd, fd;
+    int fd;
     char *tempname;
     char *map;
     char *mapset;
@@ -671,8 +676,8 @@
 
     /* open a null tempfile name */
     tempname = G_tempfile();
-    null_fd = creat(tempname, 0666);
-    if (null_fd < 0) {
+    fcb->null_fd = creat(tempname, 0666);
+    if (fcb->null_fd < 0) {
 	G_warning(_("G__open_raster_new(): no temp files available"));
 	G_free(tempname);
 	G_free(fcb->name);
@@ -683,15 +688,12 @@
     }
 
     fcb->null_temp_name = tempname;
-    close(null_fd);
 
     /* next row to be written (in order) is zero */
     fcb->null_cur_row = 0;
 
-    /* allocate null bitstream buffers for writing */
-    for (i = 0; i < NULL_ROWS_INMEM; i++)
-	fcb->NULL_ROWS[i] = Rast__allocate_null_bits(fcb->cellhd.cols);
-    fcb->min_null_row = (-1) * NULL_ROWS_INMEM;
+    /* allocate null bitstream buffer for writing */
+    fcb->null_bits = Rast__allocate_null_bits(fcb->cellhd.cols);
 
     /* init cell stats */
     /* now works only for int maps */

Modified: grass/trunk/lib/raster/put_row.c
===================================================================
--- grass/trunk/lib/raster/put_row.c	2009-12-19 21:51:38 UTC (rev 40082)
+++ grass/trunk/lib/raster/put_row.c	2009-12-20 06:20:23 UTC (rev 40083)
@@ -602,42 +602,17 @@
 static int put_null_data(int fd, const char *flags, int row)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
-    int null_fd, i;
 
-    if (fcb->min_null_row + NULL_ROWS_INMEM <= row) {
-	/* the row is out of the range of rows stored in memory */
-	/* write out all the rows kept in memory, and initialize memory
-	   for keeping new NULL_ROWS_INMEM rows */
+    if (fcb->null_fd < 0)
+	return -1;
 
-	if (fcb->min_null_row >= 0) {
-	    null_fd = Rast__open_null_write(fd);
-	    if (null_fd < 0)
-		return -1;
-
-	    for (i = 0; i < NULL_ROWS_INMEM; i++) {
-		/* fcb->cellhd.rows doesn't have to be a miultiple of NULL_ROWS_INMEM */
-		if (i + fcb->min_null_row >= fcb->cellhd.rows)
-		    break;
-
-		if (Rast__write_null_bits(null_fd, fcb->NULL_ROWS[i],
-					  i + fcb->min_null_row,
-					  fcb->cellhd.cols, fd) < 0)
-		    return -1;
-
-	    }			/* done writing out memory rows */
-	    if (null_fd >= 0)
-		close(null_fd);
-	}
-
-	/* now initialize memory to store new NULL_ROWS_INMEM rows */
-	fcb->min_null_row = fcb->min_null_row + NULL_ROWS_INMEM;
-	/* init memory to store next NULL_ROWS_INMEM rows */
-    }
-
-    /* remember the null row for i for the future writing */
-    Rast__convert_01_flags(flags, fcb->NULL_ROWS[row - fcb->min_null_row],
+    Rast__convert_01_flags(flags, fcb->null_bits,
 			   fcb->cellhd.cols);
 
+    if (Rast__write_null_bits(fcb->null_fd, fcb->null_bits, row,
+			      fcb->cellhd.cols, fd) < 0)
+	return -1;
+
     return 1;
 }
 



More information about the grass-commit mailing list