Skip to content

Commit

Permalink
switch rcomp error API to more like CFITSIO - #496
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Oct 22, 2024
1 parent 180a836 commit cf75fa3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
10 changes: 4 additions & 6 deletions Libtmp/Compression/compression.pd
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
);

Expand Down
36 changes: 22 additions & 14 deletions Libtmp/Compression/ricecomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<<fsbits;
Expand All @@ -168,7 +169,8 @@ unsigned int *diff;
*/
diff = (unsigned int *) malloc(nblock*sizeof(unsigned int));
if (diff == (unsigned int *) NULL) {
return "rcomp: insufficient memory (allocating nblock ints for internal buffer)";
*ret = "rcomp: insufficient memory (allocating nblock ints for internal buffer)";
return -1;
}
/*
* Code in blocks of nblock pixels
Expand All @@ -186,7 +188,8 @@ unsigned int *diff;
}
if (output_nbits(buffer, a0, bsize * 8)) {
free(diff);
return "buffer overrun (1)";
*ret = "buffer overrun (1)";
return -1;
}
}

Expand Down Expand Up @@ -256,12 +259,14 @@ unsigned int *diff;
*/
if (output_nbits(buffer, fsmax+1, fsbits) ) {
free(diff);
return "buffer overrun (2)";
*ret = "buffer overrun (2)";
return -1;
}
for (j=0; j<thisblock; j++) {
if (output_nbits(buffer, diff[j], bbits) ) {
free(diff);
return "buffer overrun (3)";
*ret = "buffer overrun (3)";
return -1;
}
}
} else if (fs == 0 && pixelsum == 0) {
Expand All @@ -272,13 +277,15 @@ unsigned int *diff;
*/
if (output_nbits(buffer, 0, fsbits) ) {
free(diff);
return NULL;
*ret = "buffer overrun (6)";
return -1;
}
} else {
/* normal case: not either very high or very low entropy */
if (output_nbits(buffer, fs+1, fsbits) ) {
free(diff);
return NULL;
*ret = "buffer overrun (5)";
return -1;
}
fsmask = (1<<fs) - 1;
/*
Expand Down Expand Up @@ -326,7 +333,8 @@ unsigned int *diff;
/* check if overflowed output buffer */
if (buffer->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;
Expand All @@ -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;
}

/*---------------------------------------------------------------------------*/
Expand Down
2 changes: 1 addition & 1 deletion Libtmp/Compression/ricecomp.h
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit cf75fa3

Please sign in to comment.