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
27 bit = 1 << (numBits - 1);
29 for (i = 0; i < numBits; i++) {
30 printf(
"%d", (*((
unsigned char *)c) & bit) != 0);
41 sign = *numPointer >> 7;
42 exponent = (*numPointer << 1) | (*(numPointer + 1) >> 7);
44 printf(
"%f: sign = ", *((
float *)numPointer));
48 printf(
" mantissa = ");
57 static unsigned char clearMask[9] =
58 { 255, 128, 192, 224, 240, 248, 252, 254, 255 };
62 #define ALL_NULL_CODE 2
63 #define ZERO_NULL_CODE 1
64 #define SOME_NULL_CODE 0
69 G_fpcompress_rearrangeEncodeFloats(
unsigned char *src,
int size,
70 int precision,
unsigned char *dst,
71 int *length,
int *offsetMantissa)
73 unsigned int nNullBits, nBits;
74 register unsigned char *srcStop;
75 register unsigned char *cp0, *cp1, *cp2, *cp3, *nullBits;
76 unsigned char mask, isNull;
77 int gt8, gt16, srcIncrement, nofNull;
82 if ((precision >= 23) || (precision == -1)) {
87 while (srcStop != src) {
95 *offsetMantissa =
size;
102 while (srcStop != (
unsigned char *)f)
105 if (nofNull == size) {
118 gt16 = precision > 16;
120 srcIncrement = 1 + (!gt8) + (!gt16);
126 cp0 = nullBits + size / 8 + ((size % 8) != 0);
129 cp3 = cp0 + size - nofNull;
130 cp2 = cp3 + size - nofNull;
131 cp1 = cp3 + (gt8 + gt16) * (size - nofNull);
133 mask = clearMask[precision];
134 nBits = nNullBits = 0;
136 while (srcStop != src) {
141 *nullBits |= ((
unsigned char)isNull << nNullBits++);
142 if (nNullBits == 8) {
148 *nullBits = (
unsigned char)isNull;
166 if (nBits && precision) {
167 *cp1 |= (
unsigned char)((
unsigned char)(*src & mask) >> nBits);
171 if (8 - nBits < precision) {
177 (
unsigned char)((
unsigned char)((*src & mask)) <<
179 nBits += precision - 8;
182 nBits = (nBits + precision) % 8;
188 *cp1 = (
unsigned char)(*src & mask);
190 nBits = (nBits + precision) % 8;
201 *length += size / 8 + ((size % 8) != 0);
204 *length += (gt8 + gt16 + (precision == 0) + 1) * (size - nofNull) +
205 ((precision * (size - nofNull)) / 8) +
206 (((precision * (size - nofNull)) % 8) != 0);
208 *offsetMantissa = size - nofNull;
214 G_fpcompress_rearrangeEncodeDoubles(
unsigned char *src,
int size,
215 int precision,
unsigned char *dst,
216 int *length,
int *offsetMantissa)
218 unsigned int nNullBits, nBits;
219 unsigned char isNull;
220 register unsigned char *srcStop;
221 register unsigned char *cp0, *cp1, *cp2, *cp3;
222 register unsigned char *cp4, *cp5, *cp6, *cp7, *nullBits;
224 int gt8, gt16, gt24, gt32, gt40, gt48, srcIncrement, nofNull;
229 if ((precision >= 52) || (precision == -1)) {
239 while (srcStop != src) {
251 *offsetMantissa =
size;
260 while (srcStop != (
unsigned char *)d)
263 if (nofNull == size) {
274 gt48 = precision > 48;
275 gt40 = precision > 40;
276 gt32 = precision > 32;
277 gt24 = precision > 24;
278 gt16 = precision > 16;
281 1 + (!gt8) + (!gt16) + (!gt24) + (!gt32) + (!gt40) + (!gt48);
287 cp0 = nullBits + size / 8 + ((size % 8) != 0);
290 cp7 = cp0 + size - nofNull;
291 cp6 = cp7 + size - nofNull;
292 cp5 = cp6 + size - nofNull;
293 cp4 = cp5 + size - nofNull;
294 cp3 = cp4 + size - nofNull;
295 cp2 = cp3 + size - nofNull;
296 cp1 = cp7 + (gt8 + gt16 + gt24 + gt32 + gt40 + gt48) * (size - nofNull);
298 mask = clearMask[precision];
299 nBits = nNullBits = 0;
301 while (srcStop != src) {
306 *nullBits |= ((
unsigned char)isNull << nNullBits++);
307 if (nNullBits == 8) {
313 *nullBits = (
unsigned char)isNull;
345 if (nBits && precision) {
346 *cp1 |= (
unsigned char)((
unsigned char)(*src & mask) >> nBits);
347 if (8 - nBits < precision) {
350 (
unsigned char)(((
unsigned char)(*src & mask)) <<
352 nBits += precision - 8;
355 nBits = (nBits + precision) % 8;
361 *cp1 = (
unsigned char)(*src & mask);
362 nBits = (nBits + precision) % 8;
373 *length += size / 8 + ((size % 8) != 0);
376 (1 + gt8 + gt16 + gt24 + gt32 + gt40 + gt48 +
378 0)) * (size - nofNull) + ((precision * (size - nofNull)) / 8) +
379 (((precision * (size - nofNull)) % 8) != 0);
382 *offsetMantissa = 2 * (size - nofNull);
384 *offsetMantissa = *length;
390 G_fpcompress_rearrangeDecodeFloats(
unsigned char *src,
int size,
391 int precision,
unsigned char *dst)
393 unsigned int nNullBits, nBits;
394 register unsigned char *dstStop;
395 register unsigned char *cp0, *cp1, *cp2, *cp3, *nullBits;
396 unsigned char mask, isNull;
397 int gt8, gt16, dstIncrement, nofNull;
400 if ((precision != -1) && (precision <= 15)) {
403 while (dstStop != cp3) {
408 if (precision <= 7) {
411 while (dstStop != cp3) {
420 if ((precision >= 23) || (precision == -1)) {
425 while (dstStop != dst) {
437 while (dstStop != (
unsigned char *)f)
445 gt16 = precision > 16;
447 dstIncrement = 1 + (!gt8) + (!gt16);
456 fStop = (
float *)(src + size * XDR_FLOAT_LENGTH);
457 while (fStop != f++) {
458 nofNull += ((*nullBits & ((
unsigned char)1 << nNullBits++)) != 0);
459 if (nNullBits == 8) {
468 cp0 = nullBits + size / 8 + ((size % 8) != 0);
471 cp3 = cp0 + size - nofNull;
472 cp2 = cp3 + size - nofNull;
473 cp1 = cp3 + (gt8 + gt16) * (size - nofNull);
475 mask = clearMask[precision];
476 nBits = nNullBits = 0;
478 while (dstStop != dst) {
480 isNull = *nullBits & ((
unsigned char)1 << nNullBits++);
482 if (nNullBits == 8) {
500 if (nBits && precision) {
501 *dst = (
unsigned char)((*cp1 << nBits) & mask);
503 if (8 - nBits < precision) {
505 *dst |= (
unsigned char)((*cp1 >> (8 - nBits)) & mask);
506 nBits += precision - 8;
509 nBits = (nBits + precision) % 8;
515 *dst = (
unsigned char)(*cp1 & mask);
516 nBits = (nBits + precision) % 8;
528 G_fpcompress_rearrangeDecodeDoubles(
unsigned char *src,
int size,
529 int precision,
unsigned char *dst)
531 unsigned int nNullBits, nBits;
532 register unsigned char *dstStop;
533 register unsigned char *cp0, *cp1, *cp2, *cp3;
534 register unsigned char *cp4, *cp5, *cp6, *cp7, *nullBits;
535 unsigned char mask, isNull;
536 int gt8, gt16, gt24, gt32, gt40, gt48, dstIncrement, offs, nofNull;
539 if ((precision != -1) && (precision <= 44)) {
540 for (offs = 7; offs >= (precision + 19) / 8; offs--) {
542 dstStop = dst + XDR_DOUBLE_LENGTH * size + offs;
543 while (dstStop != cp7) {
552 if ((precision >= 52) || (precision == -1)) {
562 while (dstStop != dst) {
579 while (dstStop != (
unsigned char *)d)
587 gt48 = precision > 48;
588 gt40 = precision > 40;
589 gt32 = precision > 32;
590 gt24 = precision > 24;
591 gt16 = precision > 16;
595 1 + (!gt8) + (!gt16) + (!gt24) + (!gt32) + (!gt40) + (!gt48);
604 dStop = (
double *)(src + size * XDR_DOUBLE_LENGTH);
605 while (dStop != d++) {
606 nofNull += ((*nullBits & ((
unsigned char)1 << nNullBits++)) != 0);
607 if (nNullBits == 8) {
616 cp0 = nullBits + size / 8 + ((size % 8) != 0);
619 cp7 = cp0 + size - nofNull;
620 cp6 = cp7 + size - nofNull;
621 cp5 = cp6 + size - nofNull;
622 cp4 = cp5 + size - nofNull;
623 cp3 = cp4 + size - nofNull;
624 cp2 = cp3 + size - nofNull;
625 cp1 = cp7 + (gt8 + gt16 + gt24 + gt32 + gt40 + gt48) * (size - nofNull);
627 mask = clearMask[precision];
628 nBits = nNullBits = 0;
630 while (dstStop != dst) {
632 isNull = *nullBits & ((
unsigned char)1 << nNullBits++);
634 if (nNullBits == 8) {
668 if (nBits && precision) {
669 *dst = (
unsigned char)((*cp1 << nBits) & mask);
671 if (8 - nBits < precision) {
673 *dst |= (
unsigned char)((*cp1 >> (8 - nBits)) & mask);
674 nBits += precision - 8;
677 nBits = (nBits + precision) % 8;
683 *dst = (
unsigned char)(*cp1 & mask);
684 nBits = (nBits + precision) % 8;
699 char *compressBuf,
int isFloat,
int useRle,
707 int status, rleLength, nBytes, offsetMantissa;
711 G_fpcompress_rearrangeEncodeFloats(src, nofNum, precision,
713 &nBytes, &offsetMantissa);
715 G_fpcompress_rearrangeEncodeDoubles(src, nofNum, precision,
717 &nBytes, &offsetMantissa);
719 #ifdef USE_LZW_COMPRESSION
723 if (useRle == G3D_USE_RLE)
726 if ((useRle == G3D_USE_RLE) && (rleLength < offsetMantissa)) {
729 srcStop = src + rleLength;
730 dst = compressBuf + 1 + offsetMantissa - rleLength;
731 while (src != srcStop)
734 *(compressBuf + offsetMantissa - rleLength) = 1;
736 if (useLzw == G3D_USE_LZW)
737 #ifdef USE_LZW_COMPRESSION
738 status = G_lzw_write(fd, compressBuf + offsetMantissa - rleLength,
739 nBytes - offsetMantissa + rleLength + 1);
741 status = G_zlib_write(fd,
742 (
unsigned char *)(compressBuf +
745 nBytes - offsetMantissa + rleLength + 1);
748 #ifdef USE_LZW_COMPRESSION
750 G_lzw_write_noCompress(fd,
751 compressBuf + offsetMantissa -
753 nBytes - offsetMantissa + rleLength +
756 status = G_zlib_write_noCompress(fd,
757 (
unsigned char *)(compressBuf +
760 nBytes - offsetMantissa +
767 if (useLzw == G3D_USE_LZW)
768 #ifdef USE_LZW_COMPRESSION
769 status = G_lzw_write(fd, compressBuf, nBytes + 1);
772 G_zlib_write(fd, (
unsigned char *)compressBuf, nBytes + 1);
775 #ifdef USE_LZW_COMPRESSION
776 status = G_lzw_write_noCompress(fd, compressBuf, nBytes + 1);
779 G_zlib_write_noCompress(fd, (
unsigned char *)compressBuf,
785 G3d_error(
"G_fpcompress_writeXdrNums: write error");
796 char *compressBuf,
int useRle,
int useLzw)
801 (
"G_fpcompress_writeXdrFloats: error in G_fpcompress_writeXdrNums");
812 char *compressBuf,
int useRle,
int useLzw)
817 (
"G_fpcompress_writeXdrDouble: error in G_fpcompress_writeXdrNums");
828 int precision,
char *compressBuf,
int isFloat)
830 int status, lengthEncode, lengthDecode;
832 char *src, *dest, *srcStop;
836 #ifdef USE_LZW_COMPRESSION
837 status = G_lzw_read2(fd, compressBuf, nofNum * nBytes + 1, fileBytes);
839 status = G_zlib_read(fd, fileBytes, (
unsigned char *)compressBuf,
840 nofNum * nBytes + 1);
843 G3d_error(
"G_fpcompress_readXdrNums: read error");
847 if (*compressBuf++ == 1) {
850 &lengthEncode, &lengthDecode);
851 if (*dst == ALL_NULL_CODE)
854 if (status == nofNum * nBytes)
855 status -= lengthDecode - lengthEncode;
857 src = compressBuf + status - 1;
858 srcStop = compressBuf + lengthEncode - 1;
859 dest = compressBuf + (status - lengthEncode) + lengthDecode - 1;
860 while (src != srcStop)
864 srcStop = src + lengthDecode;
866 while (src != srcStop)
871 G_fpcompress_rearrangeDecodeFloats(compressBuf, nofNum, precision,
874 G_fpcompress_rearrangeDecodeDoubles(compressBuf, nofNum, precision,
884 int precision,
char *compressBuf)
889 (
"G_fpcompress_readXdrFloats: error in G_fpcompress_readXdrNums");
900 int precision,
char *compressBuf)
905 (
"G_fpcompress_readXdrDouble: error in G_fpcompress_readXdrNums");
int G_fpcompress_readXdrDoubles(int fd, char *dst, int nofNum, int fileBytes, int precision, char *compressBuf)
int G_fpcompress_readXdrNums(int fd, char *dst, int nofNum, int fileBytes, int precision, char *compressBuf, int isFloat)
#define XDR_DOUBLE_LENGTH
void G3d_error(const char *msg,...)
int G_rle_count_only(char *src, int nofElts, int eltLength)
void G_fpcompress_printBinary(char *c, int numBits)
void G3d_setXdrNullDouble(double *d)
int G_fpcompress_writeXdrFloats(int fd, char *src, int nofNum, int precision, char *compressBuf, int useRle, int useLzw)
int G_fpcompress_readXdrFloats(int fd, char *dst, int nofNum, int fileBytes, int precision, char *compressBuf)
void G_rle_encode(char *src, char *dst, int nofElts, int eltLength)
int G3d_isXdrNullFloat(const float *f)
int G_fpcompress_writeXdrDouble(int fd, char *src, int nofNum, int precision, char *compressBuf, int useRle, int useLzw)
int G3d_isXdrNullDouble(const double *d)
void G_rle_decode(char *src, char *dst, int nofElts, int eltLength, int *lengthEncode, int *lengthDecode)
int G_fpcompress_writeXdrNums(int fd, char *src, int nofNum, int precision, char *compressBuf, int isFloat, int useRle, int useLzw)
void G_fpcompress_dissectXdrDouble(unsigned char *numPointer)
dglInt32_t sign(dglInt32_t x)
void G3d_setXdrNullFloat(float *f)
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...