home *** CD-ROM | disk | FTP | other *** search
/ Los Alamos National Laboratory / LANL_CD.ISO / software / compres / src / compress.c next >
Encoding:
C/C++ Source or Header  |  1992-02-11  |  7.0 KB  |  293 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. float **books, *thresh;
  12. int *b1, *b2, *ncv, nband;
  13. struct huff_word **table;
  14. int nbyte_in;
  15.  
  16. main(argc, argv) char **argv; {
  17.     int nlev, size, opt, npt_h, npt_g;
  18.     float *x, *h, *g;
  19.     char *ifile, *ofile, *xfile;
  20.  
  21.     if(argc != 4) {
  22.         printf("Usage: %s input_file output_file X-file\n", argv[0]);
  23.         exit(-1);
  24.     }
  25.  
  26.     printf("\n***** Copyright (c) 1991 University of California *****\n\n");
  27.     fflush(stdout);
  28.  
  29.     ifile = argv[1];
  30.     ofile = argv[2];
  31.     xfile = argv[3];
  32.  
  33.     read_xfile(xfile, &nlev);
  34.  
  35.     opt = 1;
  36.     init(opt, ifile, &size, &x, &h, &g, &npt_h, &npt_g);
  37.     cwrite(ofile, &size, sizeof(int), 0);
  38.  
  39.     printf("Beginning Wavelet Transform / VQ Encode . . . ");
  40.     fflush(stdout);
  41.     fwt_anl(ofile, x, h, g, size, nlev, npt_h, npt_g);
  42. }
  43.  
  44. /****************************************************************/
  45.  
  46. static int iband = 0;
  47.  
  48. fwt_anl(ofile, x, h, g, size, nlev, npt_h, npt_g)
  49. int size, nlev, npt_h, npt_g;
  50. float *x, *h, *g;
  51. char *ofile;
  52. {
  53.     int band;
  54.     float *x1, *x2;
  55.  
  56.     if(nlev-- == 0)
  57.         return;
  58.  
  59.     for(band = 0; band < 4; band++) {
  60.  
  61.         switch(band) {
  62.             case 0: {
  63.                 conv_ds(x, &x1, h, npt_h, size, size, 0);
  64.                 conv_ds(x1, &x2, h, npt_h, size/2, size, 0);
  65.                 break;
  66.         }
  67.             case 1: {
  68.                 conv_ds(x1, &x2, g, npt_g, size/2, size, 1);
  69.                 free(x1);
  70.                 break;
  71.         }
  72.             case 2: {
  73.                 conv_ds(x, &x1, g, npt_g, size, size, 1);
  74.                 free(x);
  75.                 conv_ds(x1, &x2, h, npt_h, size/2, size, 0);
  76.                 break;
  77.         }
  78.             case 3: {
  79.                 conv_ds(x1, &x2, g, npt_g, size/2, size, 1);
  80.                 free(x1);
  81.                 break;
  82.         }
  83.     }
  84.  
  85.         if( (band != 0) || (nlev == 0) ) {
  86.             encode(x2, size/2, b1[iband], b2[iband], books[iband], ncv[iband],
  87.                 table[iband], iband, nband, thresh[iband], ofile);
  88.             iband++;
  89.     }
  90.  
  91.         if(!band)
  92.             fwt_anl(ofile, x2, h, g, size/2, nlev, npt_h, npt_g);
  93.         free(x2);
  94.     }
  95.  }
  96.  
  97. /****************************************************************/
  98.  
  99. init(opt, ifile, size, x, h, g, npt_h, npt_g)
  100. int opt, *npt_h, *npt_g, *size;
  101. float **x, **h, **g;
  102. char *ifile;
  103. {
  104.     int i;
  105.     double *hdoub, *gdoub;
  106.     unsigned char *x_uc;
  107.  
  108.     printf("Reading Data File . . . ");
  109.     fflush(stdout);
  110.     nbyte_in = cread2(ifile, &x_uc);
  111.     printf("Finished\n");
  112.     fflush(stdout);
  113.  
  114.     *size = (int)(sqrt((float)(nbyte_in)));
  115.     *x = (float*)malloc(*size * *size * sizeof(float));
  116.     expand(x_uc, *x, nbyte_in);
  117.     free(x_uc);
  118.  
  119.     if(opt == 1) {
  120.         *npt_h = 9;
  121.         *npt_g = 7;
  122.         hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
  123.         gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
  124.         *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
  125.         *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
  126.         b4_4bl9(hdoub);
  127.         b4_4bh7(gdoub);
  128.     }
  129.  
  130.     else if(opt == 2) {
  131.         *npt_h = 7;
  132.         *npt_g = 9;
  133.         hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
  134.         gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
  135.         *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
  136.         *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
  137.         b4_4al7(hdoub);
  138.         b4_4ah9(gdoub);
  139.     }
  140.  
  141.     else if(opt == 3) {
  142.         *npt_h = 11;
  143.         *npt_g = 9;
  144.         hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
  145.         gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
  146.         *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
  147.         *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
  148.         b5_6al11(hdoub);
  149.         b5_6ah9(gdoub);
  150.     }
  151.  
  152.     else if(opt == 4) {
  153.         *npt_h = 9;
  154.         *npt_g = 11;
  155.         hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
  156.         gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
  157.         *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
  158.         *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
  159.         b5_6bl9(hdoub);
  160.         b5_6bh11(gdoub);
  161.     }
  162.  
  163.     else {
  164.         printf("Bad opt\n");
  165.         exit(-1);
  166.     }
  167.  
  168.     for(i = 0; i < *npt_h/2+1; i++)
  169.         (*h)[i] = (float)hdoub[*npt_h/2 - i];
  170.  
  171.     for(i = 0; i < *npt_g/2+1; i++)
  172.         (*g)[i] = (float)gdoub[*npt_g/2 - i];
  173.  
  174.     free(hdoub);
  175.     free(gdoub);
  176. }
  177.  
  178. /****************************************************************/
  179.  
  180. char *buff;
  181.  
  182. read_xfile(xfile, nlev)
  183. int *nlev;
  184. char *xfile;
  185. {
  186.     int i, nbyte, icv, nbit, ibit, npt, ncvp;
  187.     char *buffsave;
  188.  
  189.     printf("Reading Xfile . . . ");
  190.     fflush(stdout);
  191.  
  192.     nbyte = cread2(xfile, &buff);
  193.     buffsave = buff;
  194.  
  195.     printf("Finished\n");
  196.     fflush(stdout);
  197.  
  198.     *nlev = *((int*)buff);
  199.     buff += sizeof(int);
  200.     nband = *((int*)buff);
  201.     buff += sizeof(int);
  202.  
  203.     b1 = (int*)malloc(nband*sizeof(int));
  204.     b2 = (int*)malloc(nband*sizeof(int));
  205.     thresh = (float*)malloc(nband*sizeof(float));
  206.     ncv = (int*)malloc(nband*sizeof(int));
  207.  
  208.     for(i = 0; i < nband; i++) {
  209.         b1[i] = (int)(*((int*)buff));
  210.         buff += sizeof(int);
  211.     }
  212.     for(i = 0; i < nband; i++) {
  213.         b2[i] = (int)(*((int*)buff));
  214.         buff += sizeof(int);
  215.     }
  216.  
  217.     for(i = 0; i < nband; i++) {
  218.         thresh[i] = (float)(*((float*)buff));
  219.         buff += sizeof(float);
  220.     }
  221.  
  222.     printf("Building Trees . . . ");
  223.     fflush(stdout);
  224.  
  225.     table = (struct huff_word**)malloc(nband*sizeof(struct huff_word*));
  226.     for(i = 0; i < nband; i++) {
  227.         ncvp = (int)(*((int*)buff));
  228.         ncv[i] = (i == 0) ? ncvp : ncvp - 34;
  229.         buff += sizeof(int);
  230.         table[i] = (struct huff_word*)malloc(ncvp*sizeof(struct huff_word));
  231.         for(icv = 0; icv < ncvp; icv++) {
  232.             nbit = (int)(*((int*)buff));
  233.             buff += sizeof(int);
  234.             (table[i])[icv].nbit = nbit;
  235.             (table[i])[icv].word = (int*)malloc(nbit*sizeof(int));
  236.             for(ibit = 0; ibit < nbit; ibit++) {
  237.                 (table[i])[icv].word[ibit] = (int)(*((int*)buff));
  238.                 buff += sizeof(int);
  239.         }
  240.     }
  241.     }
  242.  
  243.     books = (float**)malloc(nband*sizeof(float*));
  244.     for(i = 0; i < nband; i++) {
  245.         nbyte = (npt = ncv[i]*b1[i]*b2[i])*sizeof(float);
  246.         books[i] = (float*)malloc(nbyte);
  247.         fcopy((float*)buff, books[i], npt);
  248.         buff += nbyte;
  249.     }
  250.  
  251.     free(buffsave);
  252.     printf("Finished\n");
  253.     fflush(stdout);
  254. }
  255.  
  256. /****************************************************************/
  257.  
  258. expand(x1, x2, n)
  259. unsigned char *x1;
  260. float *x2;
  261. int n;
  262. {
  263.     float *max;
  264.  
  265.     max = x2 + n;
  266.     while(x2 < max)
  267.         *x2++ = (float)*x1++;
  268. }
  269.  
  270. /****************************************************************/
  271.  
  272. fcopy(xbuff, x, n)
  273. float *xbuff, *x;
  274. int n;
  275. {
  276.     float *max;
  277.  
  278.     max = x + n;
  279.     while(x < max)
  280.         *x++ = *xbuff++;
  281. }
  282.  
  283. icopy(xbuff, x, n)
  284. int *xbuff, *x;
  285. int n;
  286. {
  287.     int *max;
  288.  
  289.     max = x + n;
  290.     while(x < max)
  291.         *x++ = *xbuff++;
  292. }
  293.