GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
fpcompress.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include "G3d_intern.h"
6 
7 /*--------------------------------------------------------------------------*/
8 
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
13 
14 /*************
15  * Only needed for transition */
16 /* #define USE_LZW_COMPRESSION */
17 
18 /**************/
19 
20 /*--------------------------------------------------------------------------*/
21 
22 void G_fpcompress_printBinary(char *c, int numBits)
23 {
24  unsigned char bit;
25  int i;
26 
27  bit = 1 << (numBits - 1);
28 
29  for (i = 0; i < numBits; i++) {
30  printf("%d", (*((unsigned char *)c) & bit) != 0);
31  bit >>= 1;
32  }
33 }
34 
35 /*--------------------------------------------------------------------------*/
36 
37 void G_fpcompress_dissectXdrDouble(unsigned char *numPointer)
38 {
39  char sign, exponent;
40 
41  sign = *numPointer >> 7;
42  exponent = (*numPointer << 1) | (*(numPointer + 1) >> 7);
43 
44  printf("%f: sign = ", *((float *)numPointer));
45  G_fpcompress_printBinary(&sign, 1);
46  printf(" exp = ");
47  G_fpcompress_printBinary(&exponent, 8);
48  printf(" mantissa = ");
49  G_fpcompress_printBinary((char *)(numPointer + 1), 7);
50  G_fpcompress_printBinary((char *)(numPointer + 2), 8);
51  G_fpcompress_printBinary((char *)(numPointer + 3), 8);
52  printf("\n");
53 }
54 
55 /*--------------------------------------------------------------------------*/
56 
57 static unsigned char clearMask[9] =
58  { 255, 128, 192, 224, 240, 248, 252, 254, 255 };
59 
60 /*--------------------------------------------------------------------------*/
61 
62 #define ALL_NULL_CODE 2
63 #define ZERO_NULL_CODE 1
64 #define SOME_NULL_CODE 0
65 
66 /*--------------------------------------------------------------------------*/
67 
68 static void
69 G_fpcompress_rearrangeEncodeFloats(unsigned char *src, int size,
70  int precision, unsigned char *dst,
71  int *length, int *offsetMantissa)
72 {
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;
78  float *f;
79 
80  srcStop = src + size * XDR_FLOAT_LENGTH;
81 
82  if ((precision >= 23) || (precision == -1)) {
83  cp3 = dst;
84  cp2 = cp3 + size;
85  cp1 = cp2 + size;
86  cp0 = cp1 + size;
87  while (srcStop != src) {
88  *cp3++ = *src++; /* sign + 7 exponent bits */
89  *cp2++ = *src++; /* 1 exponent bit + 7 ms mantissa bits */
90  *cp1++ = *src++; /* 8 mantissa bits */
91  *cp0++ = *src++; /* 8 ls mantissa bits */
92  }
93 
94  *length = size * XDR_FLOAT_LENGTH;
95  *offsetMantissa = size;
96 
97  return;
98  }
99 
100  f = (float *)src;
101  nofNull = 0;
102  while (srcStop != (unsigned char *)f)
103  nofNull += G3d_isXdrNullFloat(f++);
104 
105  if (nofNull == size) {
106  *dst = (unsigned char)ALL_NULL_CODE;
107 
108  *length = 1;
109  *offsetMantissa = 1;
110 
111  return;
112  }
113 
114  precision += 1; /* treat the ls exponent bit like an addl mantissa bit */
115 
116  *dst = (unsigned char)(nofNull == 0 ? ZERO_NULL_CODE : SOME_NULL_CODE);
117 
118  gt16 = precision > 16;
119  gt8 = precision > 8;
120  srcIncrement = 1 + (!gt8) + (!gt16);
121 
122  precision %= 8;
123 
124  nullBits = dst + 1;
125  if (nofNull)
126  cp0 = nullBits + size / 8 + ((size % 8) != 0);
127  else
128  cp0 = nullBits;
129  cp3 = cp0 + size - nofNull;
130  cp2 = cp3 + size - nofNull;
131  cp1 = cp3 + (gt8 + gt16) * (size - nofNull);
132 
133  mask = clearMask[precision];
134  nBits = nNullBits = 0;
135 
136  while (srcStop != src) {
137  if (nofNull) {
138  isNull = G3d_isXdrNullFloat((float *)src);
139 
140  if (nNullBits) {
141  *nullBits |= ((unsigned char)isNull << nNullBits++);
142  if (nNullBits == 8) {
143  nullBits++;
144  nNullBits = 0;
145  }
146  }
147  else {
148  *nullBits = (unsigned char)isNull;
149  nNullBits++;
150  }
151 
152  if (isNull) {
153  src += XDR_FLOAT_LENGTH;
154  continue;
155  }
156  }
157 
158  /* printf ("write src cp0 %d %d (%d %d) %d\n", *src, *cp0, src, cp0, nullBits); */
159 
160  *cp0++ = *src++;
161  if (gt8)
162  *cp3++ = *src++;
163  if (gt16)
164  *cp2++ = *src++;
165 
166  if (nBits && precision) {
167  *cp1 |= (unsigned char)((unsigned char)(*src & mask) >> nBits);
168 
169  /*printf ("%d\n", ((*src & mask) >> nBits) << nBits); */
170 
171  if (8 - nBits < precision) {
172  cp1++;
173 
174  /*printf ("%d %d\n", *cp1, (*src & mask) << (8 - nBits)); */
175 
176  *cp1 =
177  (unsigned char)((unsigned char)((*src & mask)) <<
178  (8 - nBits));
179  nBits += precision - 8;
180  }
181  else {
182  nBits = (nBits + precision) % 8;
183  if (!nBits)
184  cp1++;
185  }
186  }
187  else {
188  *cp1 = (unsigned char)(*src & mask);
189  /* printf ("%d %d %d\n", *cp1, *src, nBits); */
190  nBits = (nBits + precision) % 8;
191  if (!nBits)
192  cp1++;
193  }
194 
195  src += srcIncrement;
196  }
197 
198  *length = 1; /* null-bit-vector indicator-byte */
199 
200  if (nofNull) /* length of null-bit-vector */
201  *length += size / 8 + ((size % 8) != 0);
202 
203  /* length of data */
204  *length += (gt8 + gt16 + (precision == 0) + 1) * (size - nofNull) +
205  ((precision * (size - nofNull)) / 8) +
206  (((precision * (size - nofNull)) % 8) != 0);
207 
208  *offsetMantissa = size - nofNull;
209 }
210 
211 /*--------------------------------------------------------------------------*/
212 
213 static void
214 G_fpcompress_rearrangeEncodeDoubles(unsigned char *src, int size,
215  int precision, unsigned char *dst,
216  int *length, int *offsetMantissa)
217 {
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;
223  unsigned char mask;
224  int gt8, gt16, gt24, gt32, gt40, gt48, srcIncrement, nofNull;
225  double *d;
226 
227  srcStop = src + size * XDR_DOUBLE_LENGTH;
228 
229  if ((precision >= 52) || (precision == -1)) {
230  cp7 = dst;
231  cp6 = cp7 + size;
232  cp5 = cp6 + size;
233  cp4 = cp5 + size;
234  cp3 = cp4 + size;
235  cp2 = cp3 + size;
236  cp1 = cp2 + size;
237  cp0 = cp1 + size;
238 
239  while (srcStop != src) {
240  *cp7++ = *src++; /* sign + 7 ms exponent bits */
241  *cp6++ = *src++; /* 4 exponent bits + 4 ms mantissa bits */
242  *cp5++ = *src++; /* 8 mantissa bits */
243  *cp4++ = *src++;
244  *cp3++ = *src++;
245  *cp2++ = *src++;
246  *cp1++ = *src++;
247  *cp0++ = *src++; /* 8 ls mantissa bits */
248  }
249 
250  *length = size * XDR_DOUBLE_LENGTH;
251  *offsetMantissa = size;
252 
253  return;
254  }
255 
256  precision += 4; /* treat the 4 ls exponent bits like addl mantissa bits */
257 
258  d = (double *)src;
259  nofNull = 0;
260  while (srcStop != (unsigned char *)d)
261  nofNull += G3d_isXdrNullDouble(d++);
262 
263  if (nofNull == size) {
264  *dst = (unsigned char)ALL_NULL_CODE;
265 
266  *length = 1;
267  *offsetMantissa = 1;
268 
269  return;
270  }
271 
272  *dst = (unsigned char)(nofNull == 0 ? ZERO_NULL_CODE : SOME_NULL_CODE);
273 
274  gt48 = precision > 48;
275  gt40 = precision > 40;
276  gt32 = precision > 32;
277  gt24 = precision > 24;
278  gt16 = precision > 16;
279  gt8 = precision > 8;
280  srcIncrement =
281  1 + (!gt8) + (!gt16) + (!gt24) + (!gt32) + (!gt40) + (!gt48);
282 
283  precision %= 8;
284 
285  nullBits = dst + 1;
286  if (nofNull)
287  cp0 = nullBits + size / 8 + ((size % 8) != 0);
288  else
289  cp0 = nullBits;
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);
297 
298  mask = clearMask[precision];
299  nBits = nNullBits = 0;
300 
301  while (srcStop != src) {
302  if (nofNull) {
303  isNull = G3d_isXdrNullDouble((double *)src);
304 
305  if (nNullBits) {
306  *nullBits |= ((unsigned char)isNull << nNullBits++);
307  if (nNullBits == 8) {
308  nullBits++;
309  nNullBits = 0;
310  }
311  }
312  else {
313  *nullBits = (unsigned char)isNull;
314  nNullBits++;
315  }
316 
317  if (isNull) {
318  src += XDR_DOUBLE_LENGTH;
319  continue;
320  }
321  }
322 
323  *cp0++ = *src++;
324  if (gt32) {
325  *cp7++ = *src++;
326  *cp6++ = *src++;
327  *cp5++ = *src++;
328 
329  if (gt32)
330  *cp4++ = *src++;
331  if (gt40)
332  *cp3++ = *src++;
333  if (gt48)
334  *cp2++ = *src++;
335  }
336  else {
337  if (gt8)
338  *cp7++ = *src++;
339  if (gt16)
340  *cp6++ = *src++;
341  if (gt24)
342  *cp5++ = *src++;
343  }
344 
345  if (nBits && precision) {
346  *cp1 |= (unsigned char)((unsigned char)(*src & mask) >> nBits);
347  if (8 - nBits < precision) {
348  cp1++;
349  *cp1 =
350  (unsigned char)(((unsigned char)(*src & mask)) <<
351  (8 - nBits));
352  nBits += precision - 8;
353  }
354  else {
355  nBits = (nBits + precision) % 8;
356  if (!nBits)
357  cp1++;
358  }
359  }
360  else {
361  *cp1 = (unsigned char)(*src & mask);
362  nBits = (nBits + precision) % 8;
363  if (!nBits)
364  cp1++;
365  }
366 
367  src += srcIncrement;
368  }
369 
370  *length = 1;
371 
372  if (nofNull)
373  *length += size / 8 + ((size % 8) != 0);
374 
375  *length +=
376  (1 + gt8 + gt16 + gt24 + gt32 + gt40 + gt48 +
377  (precision ==
378  0)) * (size - nofNull) + ((precision * (size - nofNull)) / 8) +
379  (((precision * (size - nofNull)) % 8) != 0);
380 
381  if (gt8)
382  *offsetMantissa = 2 * (size - nofNull);
383  else
384  *offsetMantissa = *length;
385 }
386 
387 /*--------------------------------------------------------------------------*/
388 
389 static void
390 G_fpcompress_rearrangeDecodeFloats(unsigned char *src, int size,
391  int precision, unsigned char *dst)
392 {
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;
398  float *f, *fStop;
399 
400  if ((precision != -1) && (precision <= 15)) { /* 23 - 8 */
401  cp3 = dst + 3;
402  dstStop = dst + XDR_FLOAT_LENGTH * size + 3;
403  while (dstStop != cp3) {
404  *cp3 = 0;
405  cp3 += XDR_FLOAT_LENGTH;
406  }
407 
408  if (precision <= 7) {
409  cp3 = dst + 2;
410  dstStop = dst + XDR_FLOAT_LENGTH * size + 2;
411  while (dstStop != cp3) {
412  *cp3 = 0;
413  cp3 += XDR_FLOAT_LENGTH;
414  }
415  }
416  }
417 
418  dstStop = dst + size * XDR_FLOAT_LENGTH;
419 
420  if ((precision >= 23) || (precision == -1)) {
421  cp3 = src;
422  cp2 = cp3 + size;
423  cp1 = cp2 + size;
424  cp0 = cp1 + size;
425  while (dstStop != dst) {
426  *dst++ = *cp3++;
427  *dst++ = *cp2++;
428  *dst++ = *cp1++;
429  *dst++ = *cp0++;
430  }
431 
432  return;
433  }
434 
435  if (*src == (unsigned char)ALL_NULL_CODE) {
436  f = (float *)dst;
437  while (dstStop != (unsigned char *)f)
438  G3d_setXdrNullFloat(f++);
439 
440  return;
441  }
442 
443  precision += 1; /* treat the ls exponent bit like an addl mantissa bit */
444 
445  gt16 = precision > 16;
446  gt8 = precision > 8;
447  dstIncrement = 1 + (!gt8) + (!gt16);
448 
449  precision %= 8;
450 
451  nofNull = 0;
452  nullBits = src + 1;
453  nNullBits = 0;
454  if (*src == (unsigned char)SOME_NULL_CODE) {
455  f = (float *)src;
456  fStop = (float *)(src + size * XDR_FLOAT_LENGTH);
457  while (fStop != f++) {
458  nofNull += ((*nullBits & ((unsigned char)1 << nNullBits++)) != 0);
459  if (nNullBits == 8) {
460  nullBits++;
461  nNullBits = 0;
462  }
463  }
464  }
465 
466  nullBits = src + 1;
467  if (nofNull)
468  cp0 = nullBits + size / 8 + ((size % 8) != 0);
469  else
470  cp0 = nullBits;
471  cp3 = cp0 + size - nofNull;
472  cp2 = cp3 + size - nofNull;
473  cp1 = cp3 + (gt8 + gt16) * (size - nofNull);
474 
475  mask = clearMask[precision];
476  nBits = nNullBits = 0;
477 
478  while (dstStop != dst) {
479  if (nofNull) {
480  isNull = *nullBits & ((unsigned char)1 << nNullBits++);
481 
482  if (nNullBits == 8) {
483  nullBits++;
484  nNullBits = 0;
485  }
486 
487  if (isNull) {
488  G3d_setXdrNullFloat((float *)dst);
489  dst += XDR_FLOAT_LENGTH;
490  continue;
491  }
492  }
493 
494  *dst++ = *cp0++;
495  if (gt8)
496  *dst++ = *cp3++;
497  if (gt16)
498  *dst++ = *cp2++;
499 
500  if (nBits && precision) {
501  *dst = (unsigned char)((*cp1 << nBits) & mask);
502 
503  if (8 - nBits < precision) {
504  cp1++;
505  *dst |= (unsigned char)((*cp1 >> (8 - nBits)) & mask);
506  nBits += precision - 8;
507  }
508  else {
509  nBits = (nBits + precision) % 8;
510  if (!nBits)
511  cp1++;
512  }
513  }
514  else {
515  *dst = (unsigned char)(*cp1 & mask);
516  nBits = (nBits + precision) % 8;
517  if (!nBits)
518  cp1++;
519  }
520 
521  dst += dstIncrement;
522  }
523 }
524 
525 /*--------------------------------------------------------------------------*/
526 
527 static void
528 G_fpcompress_rearrangeDecodeDoubles(unsigned char *src, int size,
529  int precision, unsigned char *dst)
530 {
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;
537  double *d, *dStop;
538 
539  if ((precision != -1) && (precision <= 44)) {
540  for (offs = 7; offs >= (precision + 19) / 8; offs--) {
541  cp7 = dst + offs;
542  dstStop = dst + XDR_DOUBLE_LENGTH * size + offs;
543  while (dstStop != cp7) {
544  *cp7 = 0;
545  cp7 += XDR_DOUBLE_LENGTH;
546  }
547  }
548  }
549 
550  dstStop = dst + size * XDR_DOUBLE_LENGTH;
551 
552  if ((precision >= 52) || (precision == -1)) {
553  cp7 = src;
554  cp6 = cp7 + size;
555  cp5 = cp6 + size;
556  cp4 = cp5 + size;
557  cp3 = cp4 + size;
558  cp2 = cp3 + size;
559  cp1 = cp2 + size;
560  cp0 = cp1 + size;
561 
562  while (dstStop != dst) {
563  *dst++ = *cp7++;
564  *dst++ = *cp6++;
565  *dst++ = *cp5++;
566  *dst++ = *cp4++;
567  *dst++ = *cp3++;
568  *dst++ = *cp2++;
569  *dst++ = *cp1++;
570  *dst++ = *cp0++;
571  }
572 
573  return;
574  }
575 
576  if (*src == (unsigned char)ALL_NULL_CODE) {
577  /*printf ("all null\n"); */
578  d = (double *)dst;
579  while (dstStop != (unsigned char *)d)
581 
582  return;
583  }
584 
585  precision += 4; /* treat the 4 ls exponent bits like addl mantissa bits */
586 
587  gt48 = precision > 48;
588  gt40 = precision > 40;
589  gt32 = precision > 32;
590  gt24 = precision > 24;
591  gt16 = precision > 16;
592  gt8 = precision > 8;
593 
594  dstIncrement =
595  1 + (!gt8) + (!gt16) + (!gt24) + (!gt32) + (!gt40) + (!gt48);
596 
597  precision %= 8;
598 
599  nofNull = 0;
600  nullBits = src + 1;
601  nNullBits = 0;
602  if (*src == (unsigned char)SOME_NULL_CODE) {
603  d = (double *)src;
604  dStop = (double *)(src + size * XDR_DOUBLE_LENGTH);
605  while (dStop != d++) {
606  nofNull += ((*nullBits & ((unsigned char)1 << nNullBits++)) != 0);
607  if (nNullBits == 8) {
608  nullBits++;
609  nNullBits = 0;
610  }
611  }
612  }
613 
614  nullBits = src + 1;
615  if (nofNull)
616  cp0 = nullBits + size / 8 + ((size % 8) != 0);
617  else
618  cp0 = nullBits;
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);
626 
627  mask = clearMask[precision];
628  nBits = nNullBits = 0;
629 
630  while (dstStop != dst) {
631  if (nofNull) {
632  isNull = *nullBits & ((unsigned char)1 << nNullBits++);
633 
634  if (nNullBits == 8) {
635  nullBits++;
636  nNullBits = 0;
637  }
638 
639  if (isNull) {
640  G3d_setXdrNullDouble((double *)dst);
641  dst += XDR_DOUBLE_LENGTH;
642  continue;
643  }
644  }
645 
646  *dst++ = *cp0++;
647  if (gt32) {
648  *dst++ = *cp7++;
649  *dst++ = *cp6++;
650  *dst++ = *cp5++;
651 
652  if (gt32)
653  *dst++ = *cp4++;
654  if (gt40)
655  *dst++ = *cp3++;
656  if (gt48)
657  *dst++ = *cp2++;
658  }
659  else {
660  if (gt8)
661  *dst++ = *cp7++;
662  if (gt16)
663  *dst++ = *cp6++;
664  if (gt24)
665  *dst++ = *cp5++;
666  }
667 
668  if (nBits && precision) {
669  *dst = (unsigned char)((*cp1 << nBits) & mask);
670 
671  if (8 - nBits < precision) {
672  cp1++;
673  *dst |= (unsigned char)((*cp1 >> (8 - nBits)) & mask);
674  nBits += precision - 8;
675  }
676  else {
677  nBits = (nBits + precision) % 8;
678  if (!nBits)
679  cp1++;
680  }
681  }
682  else {
683  *dst = (unsigned char)(*cp1 & mask);
684  nBits = (nBits + precision) % 8;
685  if (!nBits)
686  cp1++;
687  }
688 
689  dst += dstIncrement;
690  }
691 }
692 
693 /*--------------------------------------------------------------------------*/
694 
695 /* IMPORTANT!!! this function changes the "src". */
696 
697 int
698 G_fpcompress_writeXdrNums(int fd, char *src, int nofNum, int precision,
699  char *compressBuf, int isFloat, int useRle,
700  int useLzw)
701 {
702  /* this table is used to determine the number of bits that should be used */
703  /* with G_lzw_write (), since a too large number of bits may reduce the */
704  /* compression. the values in the table are either based on experience for */
705  /* the small values, or guesses for the larger values. */
706 
707  int status, rleLength, nBytes, offsetMantissa;
708  char *dst, *srcStop;
709 
710  if (isFloat)
711  G_fpcompress_rearrangeEncodeFloats(src, nofNum, precision,
712  compressBuf + 1,
713  &nBytes, &offsetMantissa);
714  else
715  G_fpcompress_rearrangeEncodeDoubles(src, nofNum, precision,
716  compressBuf + 1,
717  &nBytes, &offsetMantissa);
718 
719 #ifdef USE_LZW_COMPRESSION
720  G_lzw_set_bits(9);
721 #endif
722 
723  if (useRle == G3D_USE_RLE)
724  rleLength = G_rle_count_only(compressBuf + 1, offsetMantissa, 1);
725 
726  if ((useRle == G3D_USE_RLE) && (rleLength < offsetMantissa)) {
727 
728  G_rle_encode(compressBuf + 1, src, offsetMantissa, 1);
729  srcStop = src + rleLength;
730  dst = compressBuf + 1 + offsetMantissa - rleLength;
731  while (src != srcStop)
732  *dst++ = *src++;
733 
734  *(compressBuf + offsetMantissa - rleLength) = 1;
735 
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);
740 #else
741  status = G_zlib_write(fd,
742  (unsigned char *)(compressBuf +
743  offsetMantissa -
744  rleLength),
745  nBytes - offsetMantissa + rleLength + 1);
746 #endif
747  else
748 #ifdef USE_LZW_COMPRESSION
749  status =
750  G_lzw_write_noCompress(fd,
751  compressBuf + offsetMantissa -
752  rleLength,
753  nBytes - offsetMantissa + rleLength +
754  1);
755 #else
756  status = G_zlib_write_noCompress(fd,
757  (unsigned char *)(compressBuf +
758  offsetMantissa
759  - rleLength),
760  nBytes - offsetMantissa +
761  rleLength + 1);
762 #endif
763  }
764  else {
765 
766  *compressBuf = 0;
767  if (useLzw == G3D_USE_LZW)
768 #ifdef USE_LZW_COMPRESSION
769  status = G_lzw_write(fd, compressBuf, nBytes + 1);
770 #else
771  status =
772  G_zlib_write(fd, (unsigned char *)compressBuf, nBytes + 1);
773 #endif
774  else
775 #ifdef USE_LZW_COMPRESSION
776  status = G_lzw_write_noCompress(fd, compressBuf, nBytes + 1);
777 #else
778  status =
779  G_zlib_write_noCompress(fd, (unsigned char *)compressBuf,
780  nBytes + 1);
781 #endif
782  }
783 
784  if (status < 0) {
785  G3d_error("G_fpcompress_writeXdrNums: write error");
786  return 0;
787  }
788 
789  return 1;
790 }
791 
792 /*--------------------------------------------------------------------------*/
793 
794 int
795 G_fpcompress_writeXdrFloats(int fd, char *src, int nofNum, int precision,
796  char *compressBuf, int useRle, int useLzw)
797 {
798  if (!G_fpcompress_writeXdrNums(fd, src, nofNum, precision, compressBuf, 1,
799  useRle, useLzw)) {
800  G3d_error
801  ("G_fpcompress_writeXdrFloats: error in G_fpcompress_writeXdrNums");
802  return 0;
803  }
804 
805  return 1;
806 }
807 
808 /*--------------------------------------------------------------------------*/
809 
810 int
811 G_fpcompress_writeXdrDouble(int fd, char *src, int nofNum, int precision,
812  char *compressBuf, int useRle, int useLzw)
813 {
814  if (!G_fpcompress_writeXdrNums(fd, src, nofNum, precision, compressBuf, 0,
815  useRle, useLzw)) {
816  G3d_error
817  ("G_fpcompress_writeXdrDouble: error in G_fpcompress_writeXdrNums");
818  return 0;
819  }
820 
821  return 1;
822 }
823 
824 /*--------------------------------------------------------------------------*/
825 
826 int
827 G_fpcompress_readXdrNums(int fd, char *dst, int nofNum, int fileBytes,
828  int precision, char *compressBuf, int isFloat)
829 {
830  int status, lengthEncode, lengthDecode;
831  int nBytes;
832  char *src, *dest, *srcStop;
833 
834  nBytes = (isFloat ? XDR_FLOAT_LENGTH : XDR_DOUBLE_LENGTH);
835 
836 #ifdef USE_LZW_COMPRESSION
837  status = G_lzw_read2(fd, compressBuf, nofNum * nBytes + 1, fileBytes);
838 #else
839  status = G_zlib_read(fd, fileBytes, (unsigned char *)compressBuf,
840  nofNum * nBytes + 1);
841 #endif
842  if (status < 0) {
843  G3d_error("G_fpcompress_readXdrNums: read error");
844  return 0;
845  }
846 
847  if (*compressBuf++ == 1) {
848  status--;
849  G_rle_decode(compressBuf, dst, nofNum * nBytes, 1,
850  &lengthEncode, &lengthDecode);
851  if (*dst == ALL_NULL_CODE)
852  G3d_fatalError("G_fpcompress_readXdrNums: wrong code");
853 
854  if (status == nofNum * nBytes)
855  status -= lengthDecode - lengthEncode;
856 
857  src = compressBuf + status - 1;
858  srcStop = compressBuf + lengthEncode - 1;
859  dest = compressBuf + (status - lengthEncode) + lengthDecode - 1;
860  while (src != srcStop)
861  *dest-- = *src--;
862 
863  src = dst;
864  srcStop = src + lengthDecode;
865  dest = compressBuf;
866  while (src != srcStop)
867  *dest++ = *src++;
868  }
869 
870  if (isFloat)
871  G_fpcompress_rearrangeDecodeFloats(compressBuf, nofNum, precision,
872  dst);
873  else
874  G_fpcompress_rearrangeDecodeDoubles(compressBuf, nofNum, precision,
875  dst);
876 
877  return 1;
878 }
879 
880 /*--------------------------------------------------------------------------*/
881 
882 int
883 G_fpcompress_readXdrFloats(int fd, char *dst, int nofNum, int fileBytes,
884  int precision, char *compressBuf)
885 {
886  if (!G_fpcompress_readXdrNums(fd, dst, nofNum, fileBytes, precision,
887  compressBuf, 1)) {
888  G3d_error
889  ("G_fpcompress_readXdrFloats: error in G_fpcompress_readXdrNums");
890  return 0;
891  }
892 
893  return 1;
894 }
895 
896 /*--------------------------------------------------------------------------*/
897 
898 int
899 G_fpcompress_readXdrDoubles(int fd, char *dst, int nofNum, int fileBytes,
900  int precision, char *compressBuf)
901 {
902  if (!G_fpcompress_readXdrNums(fd, dst, nofNum, fileBytes, precision,
903  compressBuf, 0)) {
904  G3d_error
905  ("G_fpcompress_readXdrDouble: error in G_fpcompress_readXdrNums");
906  return 0;
907  }
908 
909  return 1;
910 }
int G_fpcompress_readXdrDoubles(int fd, char *dst, int nofNum, int fileBytes, int precision, char *compressBuf)
Definition: fpcompress.c:899
int G_fpcompress_readXdrNums(int fd, char *dst, int nofNum, int fileBytes, int precision, char *compressBuf, int isFloat)
Definition: fpcompress.c:827
#define SOME_NULL_CODE
Definition: fpcompress.c:64
#define XDR_DOUBLE_LENGTH
Definition: fpcompress.c:9
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
int G_rle_count_only(char *src, int nofElts, int eltLength)
Definition: rle.c:123
FILE * fd
Definition: g3dcolor.c:368
#define ZERO_NULL_CODE
Definition: fpcompress.c:63
#define XDR_FLOAT_LENGTH
Definition: fpcompress.c:11
void G_fpcompress_printBinary(char *c, int numBits)
Definition: fpcompress.c:22
void G3d_setXdrNullDouble(double *d)
Definition: g3dfpxdr.c:48
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int G_fpcompress_writeXdrFloats(int fd, char *src, int nofNum, int precision, char *compressBuf, int useRle, int useLzw)
Definition: fpcompress.c:795
int G_fpcompress_readXdrFloats(int fd, char *dst, int nofNum, int fileBytes, int precision, char *compressBuf)
Definition: fpcompress.c:883
void G_rle_encode(char *src, char *dst, int nofElts, int eltLength)
Definition: rle.c:166
int G3d_isXdrNullFloat(const float *f)
Definition: g3dfpxdr.c:23
int G_fpcompress_writeXdrDouble(int fd, char *src, int nofNum, int precision, char *compressBuf, int useRle, int useLzw)
Definition: fpcompress.c:811
int G3d_isXdrNullDouble(const double *d)
Definition: g3dfpxdr.c:30
void G_rle_decode(char *src, char *dst, int nofElts, int eltLength, int *lengthEncode, int *lengthDecode)
Definition: rle.c:223
int G_fpcompress_writeXdrNums(int fd, char *src, int nofNum, int precision, char *compressBuf, int isFloat, int useRle, int useLzw)
Definition: fpcompress.c:698
void G_fpcompress_dissectXdrDouble(unsigned char *numPointer)
Definition: fpcompress.c:37
dglInt32_t sign(dglInt32_t x)
Definition: flow.c:25
void G3d_setXdrNullFloat(float *f)
Definition: g3dfpxdr.c:55
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
Definition: g3derror.c:58
#define ALL_NULL_CODE
Definition: fpcompress.c:62