home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 274.lha / SimGen_Src / DUNPACK.c < prev    next >
C/C++ Source or Header  |  1989-07-26  |  1KB  |  52 lines

  1. /* DUnpack.c --- Fibonacci Delta decompression by Steve Hayes */
  2.  
  3. #include <exec/types.h>
  4.  
  5. /* Fibonacci delta encoding for sound data */
  6. BYTE codeToDelta[16] = {-34,-21,-13,-8,-5,-3,-2,-1,0,1,2,3,5,8,13,21};
  7.  
  8. /* Unpack Fibonacci-delta encoded data from n byte source
  9.  * buffer into 2*n byte dest buffer, given initial data
  10.  * value x.  It returns the lats data value x so you can
  11.  * call it several times to incrementally decompress the data.
  12.  */
  13.  
  14. BYTE D1Unpack(source,n,dest,x)
  15.     BYTE    *source;
  16.     ULONG    n;
  17.     BYTE    *dest;
  18.     BYTE    x;
  19. {
  20.     UBYTE    d;
  21.     ULONG    i, lim;
  22.  
  23.     lim = n << 1;
  24.     for (i=0; i < lim; ++i) {
  25.         /* Decode a data nibble, high nibble then low nibble */
  26.         d = source[i >> 1];    /* get a pair of nibbles */
  27.         if (i & 1) {        /* select low or high nibble */
  28.             d &= 0xf;    /* mask to get the low nibble */
  29.         } else {
  30.             d >>= 4;    /* shift to get the high nibble */
  31.         }
  32.         x += codeToDelta[d];    /* add in the decoded delta */
  33.         dest[i] = x;        /* store a 1 byte sample */
  34.     }
  35.     return(x);
  36. }
  37.  
  38. /* Unpack Fibonacci-delta encoded data from n byte
  39.  * source buffer into 2*(n-2) byte dest buffer.
  40.  * Source buffer has a pad byte, an 8-bit initial
  41.  * value, followed by n-2 bytes comprising 2*(n-2)
  42.  * 4-bit encoded samples.
  43.  */
  44.  
  45. void DUnpack(source, n, dest)
  46.     BYTE    *source;
  47.     ULONG    n;
  48.     BYTE    *dest;
  49. {
  50.     D1Unpack(source+2, n-2, dest, source[1]);
  51. }
  52.