9 #define XDR_DOUBLE_LENGTH 8 10 #define XDR_DOUBLE_NOF_EXP_BYTES 2 11 #define XDR_FLOAT_LENGTH 4 12 #define XDR_FLOAT_NOF_EXP_BYTES 1 21 bit = 1 << (numBits - 1);
23 for (i = 0; i < numBits; i++) {
24 printf(
"%d", (*((
unsigned char *)c) & bit) != 0);
35 sign = *numPointer >> 7;
36 exponent = (*numPointer << 1) | (*(numPointer + 1) >> 7);
38 printf(
"%f: sign = ", *((
float *)numPointer));
42 printf(
" mantissa = ");
51 static unsigned char clearMask[9] =
52 { 255, 128, 192, 224, 240, 248, 252, 254, 255 };
56 #define ALL_NULL_CODE 2 57 #define ZERO_NULL_CODE 1 58 #define SOME_NULL_CODE 0 63 G_fpcompress_rearrangeEncodeFloats(
unsigned char *src,
int size,
64 int precision,
unsigned char *
dst,
65 int *length,
int *offsetMantissa)
67 unsigned int nNullBits, nBits;
68 register unsigned char *srcStop;
69 register unsigned char *cp0, *cp1, *cp2, *cp3, *nullBits;
70 unsigned char mask, isNull;
71 int gt8, gt16, srcIncrement, nofNull;
76 if ((precision >= 23) || (precision == -1)) {
81 while (srcStop != src) {
89 *offsetMantissa = size;
96 while (srcStop != (
unsigned char *)f)
99 if (nofNull == size) {
112 gt16 = precision > 16;
114 srcIncrement = 1 + (!gt8) + (!gt16);
120 cp0 = nullBits + size / 8 + ((size % 8) != 0);
123 cp3 = cp0 + size - nofNull;
124 cp2 = cp3 + size - nofNull;
125 cp1 = cp3 + (gt8 + gt16) * (size - nofNull);
127 mask = clearMask[precision];
128 nBits = nNullBits = 0;
130 while (srcStop != src) {
135 *nullBits |= ((
unsigned char)isNull << nNullBits++);
136 if (nNullBits == 8) {
142 *nullBits = (
unsigned char)isNull;
160 if (nBits && precision) {
161 *cp1 |= (
unsigned char)((
unsigned char)(*src & mask) >> nBits);
165 if (8 - nBits < precision) {
171 (
unsigned char)((
unsigned char)((*src & mask)) <<
173 nBits += precision - 8;
176 nBits = (nBits + precision) % 8;
182 *cp1 = (
unsigned char)(*src & mask);
184 nBits = (nBits + precision) % 8;
195 *length += size / 8 + ((size % 8) != 0);
198 *length += (gt8 + gt16 + (precision == 0) + 1) * (size - nofNull) +
199 ((precision * (size - nofNull)) / 8) +
200 (((precision * (size - nofNull)) % 8) != 0);
202 *offsetMantissa = size - nofNull;
208 G_fpcompress_rearrangeEncodeDoubles(
unsigned char *src,
int size,
209 int precision,
unsigned char *
dst,
210 int *length,
int *offsetMantissa)
212 unsigned int nNullBits, nBits;
213 unsigned char isNull;
214 register unsigned char *srcStop;
215 register unsigned char *cp0, *cp1, *cp2, *cp3;
216 register unsigned char *cp4, *cp5, *cp6, *cp7, *nullBits;
218 int gt8, gt16, gt24, gt32, gt40, gt48, srcIncrement, nofNull;
223 if ((precision >= 52) || (precision == -1)) {
233 while (srcStop != src) {
245 *offsetMantissa = size;
254 while (srcStop != (
unsigned char *)d)
257 if (nofNull == size) {
268 gt48 = precision > 48;
269 gt40 = precision > 40;
270 gt32 = precision > 32;
271 gt24 = precision > 24;
272 gt16 = precision > 16;
275 1 + (!gt8) + (!gt16) + (!gt24) + (!gt32) + (!gt40) + (!gt48);
281 cp0 = nullBits + size / 8 + ((size % 8) != 0);
284 cp7 = cp0 + size - nofNull;
285 cp6 = cp7 + size - nofNull;
286 cp5 = cp6 + size - nofNull;
287 cp4 = cp5 + size - nofNull;
288 cp3 = cp4 + size - nofNull;
289 cp2 = cp3 + size - nofNull;
290 cp1 = cp7 + (gt8 + gt16 + gt24 + gt32 + gt40 + gt48) * (size - nofNull);
292 mask = clearMask[precision];
293 nBits = nNullBits = 0;
295 while (srcStop != src) {
300 *nullBits |= ((
unsigned char)isNull << nNullBits++);
301 if (nNullBits == 8) {
307 *nullBits = (
unsigned char)isNull;
339 if (nBits && precision) {
340 *cp1 |= (
unsigned char)((
unsigned char)(*src & mask) >> nBits);
341 if (8 - nBits < precision) {
344 (
unsigned char)(((
unsigned char)(*src & mask)) <<
346 nBits += precision - 8;
349 nBits = (nBits + precision) % 8;
355 *cp1 = (
unsigned char)(*src & mask);
356 nBits = (nBits + precision) % 8;
367 *length += size / 8 + ((size % 8) != 0);
370 (1 + gt8 + gt16 + gt24 + gt32 + gt40 + gt48 +
372 0)) * (size - nofNull) + ((precision * (size - nofNull)) / 8) +
373 (((precision * (size - nofNull)) % 8) != 0);
376 *offsetMantissa = 2 * (size - nofNull);
378 *offsetMantissa = *length;
384 G_fpcompress_rearrangeDecodeFloats(
unsigned char *src,
int size,
385 int precision,
unsigned char *
dst)
387 unsigned int nNullBits, nBits;
388 register unsigned char *dstStop;
389 register unsigned char *cp0, *cp1, *cp2, *cp3, *nullBits;
390 unsigned char mask, isNull;
391 int gt8, gt16, dstIncrement, nofNull;
394 if ((precision != -1) && (precision <= 15)) {
397 while (dstStop != cp3) {
402 if (precision <= 7) {
405 while (dstStop != cp3) {
414 if ((precision >= 23) || (precision == -1)) {
419 while (dstStop != dst) {
431 while (dstStop != (
unsigned char *)f)
439 gt16 = precision > 16;
441 dstIncrement = 1 + (!gt8) + (!gt16);
450 fStop = (
float *)(src + size * XDR_FLOAT_LENGTH);
451 while (fStop != f++) {
452 nofNull += ((*nullBits & ((
unsigned char)1 << nNullBits++)) != 0);
453 if (nNullBits == 8) {
462 cp0 = nullBits + size / 8 + ((size % 8) != 0);
465 cp3 = cp0 + size - nofNull;
466 cp2 = cp3 + size - nofNull;
467 cp1 = cp3 + (gt8 + gt16) * (size - nofNull);
469 mask = clearMask[precision];
470 nBits = nNullBits = 0;
472 while (dstStop != dst) {
474 isNull = *nullBits & ((
unsigned char)1 << nNullBits++);
476 if (nNullBits == 8) {
494 if (nBits && precision) {
495 *dst = (
unsigned char)((*cp1 << nBits) & mask);
497 if (8 - nBits < precision) {
499 *dst |= (
unsigned char)((*cp1 >> (8 - nBits)) & mask);
500 nBits += precision - 8;
503 nBits = (nBits + precision) % 8;
509 *dst = (
unsigned char)(*cp1 & mask);
510 nBits = (nBits + precision) % 8;
522 G_fpcompress_rearrangeDecodeDoubles(
unsigned char *src,
int size,
523 int precision,
unsigned char *
dst)
525 unsigned int nNullBits, nBits;
526 register unsigned char *dstStop;
527 register unsigned char *cp0, *cp1, *cp2, *cp3;
528 register unsigned char *cp4, *cp5, *cp6, *cp7, *nullBits;
529 unsigned char mask, isNull;
530 int gt8, gt16, gt24, gt32, gt40, gt48, dstIncrement, offs, nofNull;
533 if ((precision != -1) && (precision <= 44)) {
534 for (offs = 7; offs >= (precision + 19) / 8; offs--) {
537 while (dstStop != cp7) {
546 if ((precision >= 52) || (precision == -1)) {
556 while (dstStop != dst) {
573 while (dstStop != (
unsigned char *)d)
581 gt48 = precision > 48;
582 gt40 = precision > 40;
583 gt32 = precision > 32;
584 gt24 = precision > 24;
585 gt16 = precision > 16;
589 1 + (!gt8) + (!gt16) + (!gt24) + (!gt32) + (!gt40) + (!gt48);
598 dStop = (
double *)(src + size * XDR_DOUBLE_LENGTH);
599 while (dStop != d++) {
600 nofNull += ((*nullBits & ((
unsigned char)1 << nNullBits++)) != 0);
601 if (nNullBits == 8) {
610 cp0 = nullBits + size / 8 + ((size % 8) != 0);
613 cp7 = cp0 + size - nofNull;
614 cp6 = cp7 + size - nofNull;
615 cp5 = cp6 + size - nofNull;
616 cp4 = cp5 + size - nofNull;
617 cp3 = cp4 + size - nofNull;
618 cp2 = cp3 + size - nofNull;
619 cp1 = cp7 + (gt8 + gt16 + gt24 + gt32 + gt40 + gt48) * (size - nofNull);
621 mask = clearMask[precision];
622 nBits = nNullBits = 0;
624 while (dstStop != dst) {
626 isNull = *nullBits & ((
unsigned char)1 << nNullBits++);
628 if (nNullBits == 8) {
662 if (nBits && precision) {
663 *dst = (
unsigned char)((*cp1 << nBits) & mask);
665 if (8 - nBits < precision) {
667 *dst |= (
unsigned char)((*cp1 >> (8 - nBits)) & mask);
668 nBits += precision - 8;
671 nBits = (nBits + precision) % 8;
677 *dst = (
unsigned char)(*cp1 & mask);
678 nBits = (nBits + precision) % 8;
691 char *compressBuf,
int isFloat)
698 G_fpcompress_rearrangeEncodeFloats((
unsigned char *)src, nofNum, precision,
699 (
unsigned char *)(compressBuf + 1),
700 &nBytes, &offsetMantissa);
702 G_fpcompress_rearrangeEncodeDoubles((
unsigned char *)src, nofNum, precision,
703 (
unsigned char *)(compressBuf + 1),
704 &nBytes, &offsetMantissa);
710 Rast3d_error(
"Rast3d_fpcompress_write_xdr_nums: write error");
721 int precision,
char *compressBuf,
int isFloat)
724 int lengthEncode, lengthDecode;
726 char *src, *
dest, *srcStop;
730 nofNum * nBytes + 1, 2);
733 Rast3d_error(
"Rast3d_fpcompress_read_xdr_nums: read error");
738 if (*compressBuf++ == 1) {
741 &lengthEncode, &lengthDecode);
745 if (status == nofNum * nBytes)
746 status -= lengthDecode - lengthEncode;
748 src = compressBuf + status - 1;
749 srcStop = compressBuf + lengthEncode - 1;
750 dest = compressBuf + (status - lengthEncode) + lengthDecode - 1;
751 while (src != srcStop)
755 srcStop = src + lengthDecode;
757 while (src != srcStop)
762 G_fpcompress_rearrangeDecodeFloats((
unsigned char *)compressBuf, nofNum, precision,
763 (
unsigned char *)dst);
765 G_fpcompress_rearrangeDecodeDoubles((
unsigned char *)compressBuf, nofNum, precision,
766 (
unsigned char *)dst);
void Rast3d_set_xdr_null_double(double *)
int G_write_compressed(int, unsigned char *, int, int)
void Rast3d_rle_decode(char *, char *, int, int, int *, int *)
#define XDR_DOUBLE_LENGTH
int Rast3d_fpcompress_read_xdr_nums(int fd, char *dst, int nofNum, int fileBytes, int precision, char *compressBuf, int isFloat)
int Rast3d_is_xdr_null_double(const double *)
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void Rast3d_error(const char *,...) __attribute__((format(printf
int Rast3d_fpcompress_write_xdr_nums(int fd, char *src, int nofNum, int precision, char *compressBuf, int isFloat)
void Rast3d_fpcompress_print_binary(char *c, int numBits)
void Rast3d_fpcompress_dissect_xdr_double(unsigned char *numPointer)
void Rast3d_set_xdr_null_float(float *)
dglInt32_t sign(dglInt32_t x)
int Rast3d_is_xdr_null_float(const float *)
int G_read_compressed(int, int, unsigned char *, int, int)