home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / gifbla20.zip / SOURCE / ARITHMOD.C < prev    next >
C/C++ Source or Header  |  1992-12-15  |  2KB  |  136 lines

  1.  
  2. /* arithmod.c - Modelling routines used in arithmetic coding. */
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include "arithmod.h"
  8.  
  9. void
  10. join_big_freqs(freqs,ncodes)
  11. int *freqs; int ncodes;
  12. {
  13.     int i;
  14.  
  15.     while (ncodes > 1) {
  16.         for (i=0; i<ncodes/2; i++)
  17.             freqs[ncodes+i] = freqs[2*i]+freqs[2*i+1];
  18.         freqs += ncodes;
  19.         ncodes /= 2;
  20.     }
  21. }
  22.  
  23. void
  24. add_to_big_freqs(c,inc,freqs,ncodes)
  25. int c; int inc; int *freqs; int ncodes;
  26. {
  27.     while (ncodes > 0) {
  28.         (freqs[c]) += inc;
  29.         freqs += ncodes;
  30.         ncodes /= 2;
  31.         c /= 2;
  32.     }
  33. }
  34.  
  35. int
  36. find_code_in_big_freqs(c,freqs,ncodes)
  37. int c; int *freqs; int ncodes;
  38. {
  39.     int start;
  40.  
  41.     start = 0;
  42.     while (c != 0) {
  43.         if ((c&1) != 0)
  44.             start += freqs[(c&~1)];
  45.         freqs += ncodes;
  46.         ncodes /= 2;
  47.         c /= 2;
  48.     }
  49.     return start;
  50. }
  51.  
  52. int
  53. find_freq_in_big_freqs(f,freqs,prstart,ncodes)
  54. int f; int *freqs; int *prstart; int ncodes;
  55. {
  56.     int c,n,pos;
  57.  
  58.     (*prstart) = 0;
  59.     c = 0;
  60.     n = 2;
  61.     pos = 2*ncodes-4;
  62.     while (pos >= 0) {
  63.         c <<= 1;
  64.         if (f >= freqs[pos+c]) {
  65.             f -= freqs[pos+c];
  66.             (*prstart) += freqs[pos+c];
  67.             c++;
  68.         }
  69.         n <<= 1;
  70.         pos -= n;
  71.     }
  72.     return c;
  73. }
  74.  
  75. void
  76. del_from_small_freqs(c,hits,hitfreqs,pnhits)
  77. int c; unsigned char *hits; char *hitfreqs; int *pnhits;
  78. {
  79.     unsigned char *p;
  80.     char *q;
  81.  
  82.     p = memchr(hits,c,*pnhits);
  83.     q = hitfreqs + (p-hits);
  84.     if (*q > 1)
  85.         (*q)--;
  86.     else {
  87.         (*p) = hits[--(*pnhits)];
  88.         (*q) = hitfreqs[*pnhits];
  89.     }
  90. }
  91.  
  92. void
  93. add_to_small_freqs(c,hits,hitfreqs,pnhits)
  94. int c; unsigned char *hits; char *hitfreqs; int *pnhits;
  95. {
  96.     unsigned char *p;
  97.  
  98.     if ((*pnhits)==0 || (p=memchr(hits,c,*pnhits))==NULL) {
  99.         hits[*pnhits] = c;
  100.         hitfreqs[(*pnhits)++] = 1;
  101.     } else
  102.         (hitfreqs[p-hits])++;
  103. }
  104.  
  105. int
  106. find_code_in_small_freqs(c,hits,hitfreqs,nhits,prstart)
  107. int c; unsigned char *hits; char *hitfreqs; int nhits; int *prstart;
  108. {
  109.     unsigned char *p;
  110.     char *q;
  111.     int f;
  112.  
  113.     if (nhits==0 || (p=memchr(hits,c,nhits))==NULL)
  114.         return 0;
  115.     q = hitfreqs + (p-hits);
  116.     f = (*q);
  117.     (*prstart) = 0;
  118.     while (q != hitfreqs)
  119.         (*prstart) += *--q;
  120.     return f;
  121. }
  122.  
  123. int
  124. find_freq_in_small_freqs(f,hits,hitfreqs,prstart,prend)
  125. int f; unsigned char *hits; char *hitfreqs; int *prstart; int *prend;
  126. {
  127.     char *q;
  128.  
  129.     (*prstart) = 0;
  130.     q = hitfreqs;
  131.     while ((*prstart)+(*q) <= f)
  132.         (*prstart) += *(q++);
  133.     (*prend) = (*prstart)+(*q);
  134.     return hits[q-hitfreqs];
  135. }
  136.