[GRASS-SVN] r67215 - in grass/trunk/lib: raster raster3d

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Dec 18 00:37:56 PST 2015


Author: mmetz
Date: 2015-12-18 00:37:56 -0800 (Fri, 18 Dec 2015)
New Revision: 67215

Modified:
   grass/trunk/lib/raster/init.c
   grass/trunk/lib/raster/open.c
   grass/trunk/lib/raster/put_row.c
   grass/trunk/lib/raster3d/fpcompress.c
Log:
rasterlib, raster3dlib: use new compression fns

Modified: grass/trunk/lib/raster/init.c
===================================================================
--- grass/trunk/lib/raster/init.c	2015-12-18 08:30:15 UTC (rev 67214)
+++ grass/trunk/lib/raster/init.c	2015-12-18 08:37:56 UTC (rev 67215)
@@ -77,7 +77,7 @@
 
 static int init(void)
 {
-    char *zlib, *nulls;
+    char *zlib, *nulls, *ctype;
 
     Rast__init_window();
 
@@ -96,6 +96,18 @@
     zlib = getenv("GRASS_INT_ZLIB");
     R__.compression_type = (!zlib || atoi(zlib)) ? 2 : 1;
 
+    ctype = getenv("GRASS_COMPRESSOR");
+    /* 1: RLE
+     * 2: ZLIB (DEFLATE)
+     * 3: LZ4
+     * 4: BZIP2 */
+    if (ctype) {
+	/* ask gislib */
+	R__.compression_type = G_get_compressor(ctype);
+	if (R__.compression_type < 1)
+	    R__.compression_type = 2; /* default to ZLIB */
+    }
+
     nulls = getenv("GRASS_COMPRESS_NULLS");
     R__.compress_nulls = (nulls && atoi(nulls)) ? 1 : 0;
 

Modified: grass/trunk/lib/raster/open.c
===================================================================
--- grass/trunk/lib/raster/open.c	2015-12-18 08:30:15 UTC (rev 67214)
+++ grass/trunk/lib/raster/open.c	2015-12-18 08:37:56 UTC (rev 67215)
@@ -217,6 +217,21 @@
 			  r_name, r_mapset);
     }
 
+    /* compressor */
+    if (MAP_TYPE != CELL_TYPE) {
+	/* fp maps do not use RLE */
+	/* previously, compressed simply meant yes (ZLIB) or no
+	 * now compressed encodes compressor type
+	 * 0: not compressed
+	 * 1, 2: ZLIB
+	 * 3: LZ4
+	 * 4: BZIP2
+	 * etc */
+	if (cellhd.compressed == 1)
+	    cellhd.compressed = 2;
+    }
+    /* TODO: test if compressor type is supported */
+
     if (cellhd.proj != R__.rd_window.proj)
 	G_fatal_error(_("Raster map <%s> is in different projection than current region. "
 			"Found <%s>, should be <%s>."),
@@ -634,6 +649,8 @@
      */
     fcb->cellhd = R__.wr_window;
 
+    /* TODO: test if compressor type is supported */
+
     if (open_mode == OPEN_NEW_COMPRESSED && fcb->map_type == CELL_TYPE) {
 	fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
 	G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
@@ -657,6 +674,11 @@
 	    Rast_quant_init(&(fcb->quant));
 	}
     }
+    if (open_mode == OPEN_NEW_COMPRESSED && fcb->map_type != CELL_TYPE &&
+        fcb->cellhd.compressed == 1) {
+	/* fp maps do not use RLE */
+	fcb->cellhd.compressed = 2;
+    }
 
     /* save name and mapset, and tempfile name */
     fcb->name = map;

Modified: grass/trunk/lib/raster/put_row.c
===================================================================
--- grass/trunk/lib/raster/put_row.c	2015-12-18 08:30:15 UTC (rev 67214)
+++ grass/trunk/lib/raster/put_row.c	2015-12-18 08:37:56 UTC (rev 67215)
@@ -122,12 +122,12 @@
 		      row, fcb->name, strerror(errno));
 }
 
-static void write_data_compressed(int fd, int row, unsigned char *buf, int n)
+static void write_data_compressed(int fd, int row, unsigned char *buf, int n, int compressor)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
     int nwrite = fcb->nbytes * n;
 
-    if (G_zlib_write(fcb->data_fd, buf, nwrite) < 0)
+    if (G_write_compressed(fcb->data_fd, buf, nwrite, compressor) < 0)
 	G_fatal_error(_("Error writing compressed FP data for row %d of <%s>: %s"),
 		      row, fcb->name, strerror(errno));
 }
@@ -205,7 +205,7 @@
 	convert_double(work_buf, size, null_buf, rast, row, n);
 
     if (compressed)
-	write_data_compressed(fd, row, work_buf, n);
+	write_data_compressed(fd, row, work_buf, n, fcb->cellhd.compressed);
     else
 	write_data(fd, row, work_buf, n);
 
@@ -322,15 +322,6 @@
 	n -= count;
     }
 
-    return nwrite;
-}
-
-static int zlib_compress(unsigned char *dst, unsigned char *src, int n,
-			 int nbytes)
-{
-    int total = nbytes * n;
-    int nwrite = G_zlib_compress(src, total, dst, total);
-
     return (nwrite >= total) ? 0 : nwrite;
 }
 
@@ -338,7 +329,7 @@
 		     int row, int n, int zeros_r_nulls)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
-    int compressed = fcb->cellhd.compressed;
+    int compressed = (fcb->open_mode == OPEN_NEW_COMPRESSED);
     int len = compressed ? sizeof(CELL) : fcb->nbytes;
     unsigned char *work_buf, *wk;
     ssize_t nwrite;
@@ -379,10 +370,16 @@
 	compressed_buf[0] = work_buf[0] = nbytes;
 
 	/* then compress the data */
-	nwrite = compressed == 1
-	    ? rle_compress(compressed_buf + 1, work_buf + 1, n, nbytes)
-	    : zlib_compress(compressed_buf + 1, work_buf + 1, n, nbytes);
+	if (fcb->cellhd.compressed == 1)
+	    nwrite = rle_compress(compressed_buf + 1, work_buf + 1, n, nbytes);
+	else {
+	    nwrite = G_compress(work_buf + 1, total, compressed_buf + 1, total,
+	                        fcb->cellhd.compressed);
+	}
 
+	if (nwrite >= total)
+	    nwrite = 0;
+
 	if (nwrite > 0) {
 	    nwrite++;
 
@@ -508,9 +505,9 @@
 
     compressed_buf = G_alloca(size + 1);
 
-    nwrite = G_zlib_compress(flags, size, compressed_buf, size);
+    nwrite = G_compress(flags, size, compressed_buf, size, fcb->cellhd.compressed);
 
-    if (nwrite < size) {
+    if (nwrite > 0 && nwrite < size) {
 	if (write(fcb->null_fd, compressed_buf, nwrite) != nwrite)
 	    G_fatal_error(_("Error writing compressed null data for row %d of <%s>"),
 			  row, fcb->name);

Modified: grass/trunk/lib/raster3d/fpcompress.c
===================================================================
--- grass/trunk/lib/raster3d/fpcompress.c	2015-12-18 08:30:15 UTC (rev 67214)
+++ grass/trunk/lib/raster3d/fpcompress.c	2015-12-18 08:37:56 UTC (rev 67215)
@@ -704,7 +704,7 @@
 					    &nBytes, &offsetMantissa);
 
 	*compressBuf = 0;
-	status = G_zlib_write(fd, (unsigned char *)compressBuf, nBytes + 1);
+	status = G_write_compressed(fd, (unsigned char *)compressBuf, nBytes + 1, 2);
 
     if (status < 0) {
 	Rast3d_error("Rast3d_fpcompress_write_xdr_nums: write error");
@@ -726,8 +726,8 @@
     char *src, *dest, *srcStop;
     nBytes = (isFloat ? XDR_FLOAT_LENGTH : XDR_DOUBLE_LENGTH);
 
-    status = G_zlib_read(fd, fileBytes, (unsigned char *)compressBuf,
-			 nofNum * nBytes + 1);
+    status = G_read_compressed(fd, fileBytes, (unsigned char *)compressBuf,
+			 nofNum * nBytes + 1, 2);
 
     if (status < 0) {
 	Rast3d_error("Rast3d_fpcompress_read_xdr_nums: read error");



More information about the grass-commit mailing list