Quellcode durchsuchen

libraster: update for ZSTD, use new G_compress_bound()

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@71891 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz vor 7 Jahren
Ursprung
Commit
fb033976af
2 geänderte Dateien mit 29 neuen und 16 gelöschten Zeilen
  1. 16 11
      lib/raster/init.c
  2. 13 5
      lib/raster/put_row.c

+ 16 - 11
lib/raster/init.c

@@ -93,32 +93,37 @@ static int init(void)
 
     R__.nbytes = sizeof(CELL);
 
-    zlib = getenv("GRASS_INT_ZLIB");
-    R__.compression_type = (!zlib || atoi(zlib)) ? 2 : 1;
+    R__.compression_type = G_default_compressor();
 
     cname = getenv("GRASS_COMPRESSOR");
     /* 1: RLE
      * 2: ZLIB (DEFLATE)
      * 3: LZ4
-     * 4: BZIP2 */
-    if (cname) {
+     * 4: BZIP2
+     * 5: ZSTD */
+    if (cname && *cname) {
 	/* ask gislib */
 	R__.compression_type = G_compressor_number(cname);
 	if (R__.compression_type < 1) {
 	    if (R__.compression_type < 0) {
-		G_warning(_("Unknown compression method <%s>, using default ZLIB"),
-		    cname);
+		G_warning(_("Unknown compression method <%s>, using default %s"),
+		    cname, G_compressor_name(G_default_compressor()));
 	    }
 	    if (R__.compression_type == 0) {
-		G_warning(_("No compression is not supported for GRASS raster maps, using default ZLIB"));
+		G_warning(_("No compression is not supported for GRASS raster maps, using default %s"),
+		          G_compressor_name(G_default_compressor()));
 	    }
-	    R__.compression_type = 2; /* default to ZLIB */
+	    /* use default */
+	    R__.compression_type = G_default_compressor();
 	}
 	if (G_check_compressor(R__.compression_type) != 1) {
-	    G_warning(_("This GRASS version does not support %s compression, using default ZLIB"),
-		cname);
-	    R__.compression_type = 2; /* default to ZLIB */
+	    G_warning(_("This GRASS version does not support %s compression, using default %s"),
+		cname, G_compressor_name(G_default_compressor()));
+	    /* use default */
+	    R__.compression_type = G_default_compressor();
 	}
+	G_debug(1, "Using %s compression",
+	           G_compressor_name(R__.compression_type));
     }
 
     nulls = getenv("GRASS_COMPRESS_NULLS");

+ 13 - 5
lib/raster/put_row.c

@@ -354,7 +354,7 @@ static void put_data(int fd, char *null_buf, const CELL * cell,
     if (compressed) {
 	int nbytes = count_bytes(wk, n, len);
 	unsigned char *compressed_buf;
-	int total;
+	int total, cmax;
 
 	if (fcb->nbytes < nbytes)
 	    fcb->nbytes = nbytes;
@@ -364,7 +364,12 @@ static void put_data(int fd, char *null_buf, const CELL * cell,
 	    trim_bytes(wk, n, len, len - nbytes);
 
 	total = nbytes * n;
-	compressed_buf = G_malloc(total + 1);
+	/* get upper bound of compressed size */
+	if (fcb->cellhd.compressed == 1)
+	    cmax = total;
+	else
+	    cmax = G_compress_bound(total, fcb->cellhd.compressed);
+	compressed_buf = G_malloc(cmax + 1);
 
 	compressed_buf[0] = work_buf[0] = nbytes;
 
@@ -372,7 +377,7 @@ static void put_data(int fd, char *null_buf, const CELL * cell,
 	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,
+	    nwrite = G_compress(work_buf + 1, total, compressed_buf + 1, cmax,
 	                        fcb->cellhd.compressed);
 	}
 
@@ -500,13 +505,16 @@ static void write_null_bits_compressed(const unsigned char *flags,
     struct fileinfo *fcb = &R__.fileinfo[fd];
     unsigned char *compressed_buf;
     ssize_t nwrite;
+    size_t cmax;
 
     fcb->null_row_ptr[row] = lseek(fcb->null_fd, 0L, SEEK_CUR);
 
-    compressed_buf = G_malloc(size + 1);
+    /* get upper bound of compressed size */
+    cmax = G_compress_bound(size, 3);
+    compressed_buf = G_malloc(cmax);
 
     /* compress null bits file with LZ4, see lib/gis/compress.h */
-    nwrite = G_lz4_compress(flags, size, compressed_buf, size);
+    nwrite = G_compress(flags, size, compressed_buf, cmax, 3);
 
     if (nwrite > 0 && nwrite < size) {
 	if (write(fcb->null_fd, compressed_buf, nwrite) != nwrite)