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

  1. /****************************************************************
  2.  
  3. COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
  4.  
  5. ***************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include "huff_word.h"
  10.  
  11. extern int nbyte_in;
  12.  
  13. encode(x, size, b1, b2, book, ncv, table, iband, nband, thresh, ofile)
  14. int size, b1, b2, ncv, iband, nband;
  15. float *x, *book, thresh;
  16. char *ofile;
  17. struct huff_word *table;
  18. {
  19.     float*xblk;
  20.     int *indices, k, nvec, nbyte, huff_encode();
  21.     static unsigned char *bits;
  22.  
  23.     threshold(x, size*size, thresh);
  24.  
  25.     k = b1*b2;
  26.     nvec = size*size/k;
  27.  
  28.     xblk = (float*)malloc(size*size*sizeof(float));
  29.     blk(x, xblk, b1, b2, size, size);
  30.  
  31.     indices = (int*)malloc(nvec*sizeof(int));
  32.     encode_kd(xblk, indices, nvec, book, k, ncv);
  33.     rle_encode(&indices, &nvec, ncv);
  34.     nbyte = huff_encode(indices, nvec, table, &bits, iband, nband);
  35.  
  36.     if(iband == nband-1) {
  37.         printf("Finished\n");
  38.         fflush(stdout);
  39.  
  40.         printf("Writing Output File . . . ");
  41.         fflush(stdout);
  42.         cwrite(ofile, bits, nbyte, 3*sizeof(int));
  43.         printf("Finished\n\n\n");
  44.         fflush(stdout);
  45.  
  46.         nbyte += 3*sizeof(int);
  47.         printf("Input file:   %d bytes\n", nbyte_in);
  48.         printf("Output file:  %d bytes\n", nbyte);
  49.         printf("Compression %.2f : 1\n", (float)nbyte_in/(float)nbyte);
  50.         exit(-1);
  51.     }
  52. }
  53.  
  54. /****************************************************************/
  55.  
  56. blk(inpix, outpix, bwidth, bheight, iwidth, iheight)
  57. int bwidth, bheight, iwidth;
  58. float *inpix, *outpix;
  59. {
  60.     int islice, nslice, iblk, irow;
  61.     float *x1, *x2, *max1, *max2;
  62.  
  63.     nslice = iheight/bheight;
  64.  
  65.     for(islice = 0; islice < nslice; islice++) {
  66.         x2 = outpix;
  67.         max2 = x2 + bheight*iwidth;
  68.         while(x2 < max2)
  69.             for(iblk = 0; iblk < iwidth/bwidth; iblk++)
  70.                 for(irow = 0; irow < bheight; irow++) {
  71.                     x1 = inpix + irow*iwidth + iblk*bwidth;
  72.                     max1 = x1 + bwidth;
  73.                     while(x1 < max1)
  74.                         *x2++ = *x1++;
  75.         }
  76.         inpix += bheight*iwidth;
  77.         outpix += bheight*iwidth;
  78.     }
  79. }
  80.  
  81. /****************************************************************/
  82.  
  83. #define PGSIZE 65536
  84.  
  85. int huff_encode(indices, npt, table, bits, iband, nband)
  86. int *indices, npt, iband, nband;
  87. unsigned char **bits;
  88. struct huff_word *table;
  89. {
  90.     int i, nbit, ibit, *word;
  91.     static int offset = 0, npage = 1;
  92.     static unsigned char *ptr, *max;
  93.  
  94.     if(iband == 0) {
  95.         *bits = ptr = (unsigned char*)malloc(PGSIZE);
  96.         zero(ptr, PGSIZE);
  97.         max = ptr + PGSIZE;
  98.     }
  99.  
  100.     for(i = 0; i < npt; i++) {
  101.         nbit = table[indices[i]].nbit;
  102.         word = table[indices[i]].word;
  103.  
  104.         for(ibit = 0; ibit < nbit; ibit++) {
  105.             if(word[ibit] == 1)
  106.                 *ptr |= 001;
  107.  
  108.             if(++offset == 8) {
  109.                 offset = 0;
  110.                 if( (iband == nband-1) && (i == npt-1) && (ibit == nbit - 1) )
  111.                     break;
  112.                 ptr++;
  113.                 if(ptr > max) {
  114.                     *bits = (unsigned char*)realloc(*bits, (npage+1)*PGSIZE);
  115.                     ptr = *bits + npage++ * PGSIZE;
  116.                     max = *bits + npage*PGSIZE;
  117.         }
  118.         }
  119.             else
  120.                 *ptr = *ptr << 1;
  121.         }
  122.     }
  123.  
  124.     if( (iband == nband-1) && (offset != 0) )
  125.         *ptr = *ptr << (7 - offset);
  126.  
  127.     return(ptr - *bits + 1);
  128.  
  129. }
  130.  
  131. /****************************************************************/
  132.  
  133. zero(x, n)
  134. int n;
  135. unsigned char *x;
  136. {
  137.     unsigned char *max;
  138.  
  139.     max = x + n;
  140.     while(x < max)
  141.         *x++ = 0;
  142. }
  143.  
  144. /****************************************************************/
  145.  
  146. encode_bf(tv, cbook, ndim, nvec, ncv, otpix)
  147. float *tv, *cbook;
  148. int ndim, nvec, ncv, *otpix;
  149. {
  150.     int i, indx, full_search();
  151.  
  152.     for(i = 0; i < nvec; i++) {
  153.         indx = full_search(tv, cbook, ncv, ndim);
  154.         tv += ndim;
  155.         otpix[i] = indx;
  156.     }
  157. }
  158.  
  159. int full_search(vec, cbook, ncv, k)
  160. float *vec, *cbook;
  161. int ncv, k;
  162. {
  163.     int j, jmx = 0;
  164.     float min_dist = 1.e30, dist, euc_dist();
  165.  
  166.     for(j = 0; j < ncv; j++) {
  167.         dist = euc_dist(vec, cbook, k);
  168.         cbook += k;
  169.         if(dist < min_dist) {
  170.             min_dist = dist;
  171.             jmx = j;
  172.         }
  173.     }
  174.     return(jmx);
  175. }
  176.  
  177. float euc_dist(x, y, n)
  178. float *x, *y;
  179. int n;
  180. {
  181.     float *max, dist = 0., xx;
  182.  
  183.     max = x + n;
  184.     while(x < max) {
  185.         xx = *x++ - *y++;
  186.         dist += xx*xx;
  187.     }
  188.     return(dist);
  189. }
  190.  
  191. /****************************************************************/
  192.  
  193. threshold(x, n, thresh)
  194. int n;
  195. register float *x, thresh;
  196. {
  197.     register float *max;
  198.  
  199.     max = x + n;
  200.     while(x < max) {
  201.         if(fabs(*x) < thresh)
  202.             *x++ = 0.;
  203.         else
  204.             x++;
  205.     }
  206. }
  207.