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

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