home *** CD-ROM | disk | FTP | other *** search
/ Los Alamos National Laboratory / LANL_CD.ISO / software / compres / src / rle_enco.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-11  |  1.4 KB  |  65 lines

  1. /****************************************************************
  2.  
  3. COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
  4.  
  5. ***************************************************************/
  6.  
  7. #define N 4
  8. #define NMAX 32
  9. rle_encode(indices, nindex, ncv)
  10. int **indices, *nindex, ncv;
  11. {
  12.     int *indices2, *ptr1, *ptr2, *max, cnt, nn[N], ii;
  13.  
  14.     nn[0] = NMAX;
  15.     nn[1] = 2*NMAX;
  16.     nn[2] = 4*NMAX;
  17.     nn[3] = 8*NMAX;
  18.  
  19.     ptr1 = *indices;
  20.     ptr2 = indices2 = (int*)malloc(*nindex*sizeof(int));
  21.     max = ptr1 + *nindex;
  22.  
  23.     while(ptr1 < max) {
  24.  
  25.         if(*ptr1 != 0)
  26.             *ptr2++ = *ptr1++;
  27.  
  28.         else {
  29.  
  30.             cnt = 1;
  31.             while(*++ptr1 == 0) {
  32.                 cnt++;
  33.                 if(ptr1 == max - 1) {
  34.                     ptr1 = max;
  35.                     break;
  36.         }
  37.         }
  38.  
  39.             if(cnt == 1)
  40.                 *ptr2++ = 0;
  41.  
  42.             else {
  43.                 for(ii = N - 1; ii >= 0; ii--) {
  44.                     if( cnt >= nn[ii] ) {
  45.                         while(cnt >= nn[ii]) {
  46.                             *ptr2++ = ncv - 2 + NMAX + ii;
  47.                             cnt -= nn[ii];
  48.             }
  49.             }
  50.         }
  51.  
  52.                 if(cnt == 1)
  53.                     *ptr2++ = 0;
  54.                 else if(cnt != 0)
  55.                     *ptr2++ = ncv - 2 + cnt;
  56.         }
  57.     }
  58.     }
  59.  
  60.     *nindex = ptr2 - indices2;
  61.     indices2 = (int*)realloc(indices2, *nindex*sizeof(int));
  62.     free(*indices);
  63.     *indices = indices2;
  64. }
  65.