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

  1. /****************************************************************
  2.  
  3. COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
  4.  
  5. ***************************************************************/
  6.  
  7. #include "node2.h"
  8.  
  9. decode(x_unblk, bits, size, b1, b2, book, tree, ncv, iband)
  10. int size, b1, b2, ncv, iband;
  11. float *x_unblk, *book;
  12. unsigned char *bits;
  13. struct node *tree;
  14. {
  15.     int *indices, k, nvec;
  16.     float *x;
  17.  
  18.     k = b1*b2;
  19.     nvec = size*size/k;
  20.  
  21.     indices = (int*)malloc(nvec*sizeof(int));
  22.     huff_rle_decode(indices, nvec, tree, bits, ncv, iband);
  23.     x = (float*)malloc(size*size*sizeof(float));
  24.     vq_decode(x, indices, book, k, nvec);
  25.     free(indices);
  26.  
  27.     unblk(x, x_unblk, b1, b2, size, size);
  28.     free(x);
  29. }
  30.  
  31. /****************************************************************/
  32.  
  33. static int offset;
  34. static unsigned char *ptr;
  35.  
  36. huff_rle_decode(indices, n, tree, bits, ncv, iband)
  37. int *indices, n, ncv, iband;
  38. unsigned char *bits;
  39. struct node *tree;
  40. {
  41.     int index, *max;
  42.  
  43.     if(!iband) {
  44.         ptr = bits;
  45.         offset = 0;
  46.     }
  47.  
  48.     max = indices + n;
  49.     while(indices < max) {
  50.         index = index_gen(tree);
  51.         rle_decode(&indices, index, ncv);
  52.     }
  53. }
  54.  
  55. /****************************************************************/
  56.  
  57. int index_gen(tree)
  58. struct node *tree;
  59. {
  60.     int index;
  61.     struct node *next_node;
  62.  
  63.     next_node = (*ptr < 128) ? tree->node0 : tree->node1;
  64.  
  65.     if(++offset == 8) {
  66.         offset = 0;
  67.         ptr++;
  68.     }
  69.     else
  70.         *ptr = *ptr << 1;
  71.  
  72.     if(next_node->type >= 0)
  73.         return(next_node->type);
  74.  
  75.     index = index_gen(next_node);
  76.     return(index);
  77. }
  78.  
  79. /****************************************************************/
  80.  
  81. rle_decode(indices, val, ncv)
  82. int **indices, val, ncv;
  83. {
  84.     int *iptr, *max, run_length;
  85.  
  86.     iptr = *indices;
  87.  
  88.     if(val < ncv)
  89.         *iptr++ = val;
  90.  
  91.     else {
  92.         if(val <= ncv + 31)
  93.             run_length = val - ncv + 2;
  94.         if(val == ncv+31)
  95.             run_length = 64;
  96.         if(val == ncv+32)
  97.             run_length = 128;
  98.         if(val == ncv+33)
  99.             run_length = 256;
  100.  
  101.         max = iptr + run_length;
  102.         while(iptr < max)
  103.             *iptr++ = 0;
  104.     }
  105.     *indices = iptr;
  106. }
  107.  
  108. /****************************************************************/
  109.  
  110. vq_decode(image, ip, cdbook, k, nindex)
  111. int nindex;
  112. register int *ip, k;
  113. register float *image, *cdbook;
  114. {
  115.     register float *cbp, *cbpmax;
  116.     float *max;
  117.  
  118.     max = image + nindex*k;
  119.     while(image < max) {
  120.         cbp = cdbook + *ip++*k;
  121.         cbpmax = cbp + k;
  122.         while(cbp < cbpmax)
  123.             *image++ = *cbp++;
  124.     }
  125. }
  126.  
  127. /****************************************************************/
  128.  
  129. unblk(inpix, outpix, bwidth, bheight, iwidth, iheight)
  130. int bwidth, bheight, iwidth;
  131. float *inpix, *outpix;
  132. {
  133.     int nslice, islice, iblk, irow;
  134.     float *x1, *x2, *max1, *max2;
  135.  
  136.     nslice = iheight/bheight;
  137.     for(islice = 0; islice < nslice; islice++) {
  138.         x2 = inpix;
  139.         max2 = x2 + bheight*iwidth;
  140.         while(x2 < max2)
  141.             for(iblk = 0; iblk < iwidth/bwidth; iblk++)
  142.                 for(irow = 0; irow < bheight; irow++) {
  143.                     x1 = outpix + irow*iwidth + iblk*bwidth;
  144.                     max1 = x1 + bwidth;
  145.                     while(x1 < max1)
  146.                         *x1++ = *x2++;
  147.         }
  148.         inpix += bheight*iwidth;
  149.         outpix += bheight*iwidth;
  150.     }
  151. }
  152.