From cf75fa3c1f03d2336017dd86bbaac45fc056811b Mon Sep 17 00:00:00 2001 From: Ed J Date: Tue, 22 Oct 2024 17:03:39 +0000 Subject: [PATCH] switch rcomp error API to more like CFITSIO - #496 --- Libtmp/Compression/compression.pd | 10 ++++----- Libtmp/Compression/ricecomp.c | 36 +++++++++++++++++++------------ Libtmp/Compression/ricecomp.h | 2 +- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/Libtmp/Compression/compression.pd b/Libtmp/Compression/compression.pd index 413fa144a..a82d41f3e 100644 --- a/Libtmp/Compression/compression.pd +++ b/Libtmp/Compression/compression.pd @@ -78,7 +78,7 @@ EOD pp_def( "rice_compress", HandleBad => 0, - Pars => 'in(n); [o]out(m=CALC(ceil($SIZE(n) * 1.01))); int[o]len()', + Pars => 'in(n); [o]out(m=CALC(ceil($SIZE(n) * 1.01))); indx[o]len()', OtherPars => "int blocksize", # in OtherPars to avoid autopromotion GenericTypes =>['B','S','US','L'], Doc => <<'EOD', @@ -149,17 +149,15 @@ sub PDL::rice_compress { EOD CHeader => qq{#include "ricecomp.h"\n}, Code => <<'EOD', -int len; -char *err = rcomp( $P(in), +char *err = "error message not updated"; +$len() = rcomp(&err, $P(in), sizeof($GENERIC(in)), $SIZE(n), (unsigned char *)($P(out)), $SIZE(m) * sizeof($GENERIC(out)), - $COMP(blocksize), - &len + $COMP(blocksize) ); if (err) $CROAK("%s", err); -$len() = len; EOD ); diff --git a/Libtmp/Compression/ricecomp.c b/Libtmp/Compression/ricecomp.c index 41d86b399..45088336d 100644 --- a/Libtmp/Compression/ricecomp.c +++ b/Libtmp/Compression/ricecomp.c @@ -109,13 +109,14 @@ static int output_nbits(Buffer *buffer, int bits, int n); * */ -char *rcomp(void *a_v, /* input array */ +int rcomp(char **ret, + void *a_v, /* input array */ int bsize, /* sample size (in bytes) */ int nx, /* number of input pixels */ unsigned char *c, /* output buffer */ int clen, /* max length of output */ - int nblock, /* coding block size */ - int *ret) /* pointer to bytes */ + int nblock /* coding block size */ + ) { Buffer bufmem, *buffer = &bufmem; int *a = (int *)a_v; @@ -129,7 +130,7 @@ unsigned int *diff; // Blocksize is picked so that boundaries lie on 64-bit word edges for all data types if(nblock & 0x7 ) - return "rcomp: nblock must be divisible by 8"; + { *ret = "rcomp: nblock must be divisible by 8"; return -1; } /* Magic numbers from fits_rcomp in CFITSIO; these have to match the ones in * rdecomp, below @@ -148,7 +149,7 @@ unsigned int *diff; fsmax = 25; break; default: - return "rcomp: bsize must be 1, 2, or 4 bytes"; + { *ret = "rcomp: bsize must be 1, 2, or 4 bytes"; return -1; } } bbits = 1<current > buffer->end) { free(diff); - return "buffer overrun (4)"; + *ret = "buffer overrun (4)"; + return -1; } buffer->bitbuffer = lbitbuffer; buffer->bits_to_go = lbits_to_go; @@ -337,8 +345,8 @@ unsigned int *diff; /* * return number of bytes used */ - *ret = buffer->current - buffer->start; - return NULL; + *ret = NULL; + return buffer->current - buffer->start; } /*---------------------------------------------------------------------------*/ diff --git a/Libtmp/Compression/ricecomp.h b/Libtmp/Compression/ricecomp.h index 7cd98c215..4f5165189 100644 --- a/Libtmp/Compression/ricecomp.h +++ b/Libtmp/Compression/ricecomp.h @@ -1,2 +1,2 @@ char *rdecomp(unsigned char *c, int clen, void *array, int bsize, int nx, int nblock); -char *rcomp(void *a_v, int bsize, int nx, unsigned char *c, int clen, int nblock, int *ret); +int rcomp(char **ret, void *a_v, int bsize, int nx, unsigned char *c, int clen, int nblock);