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

  1. /****************************************************************
  2.  
  3. COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
  4.  
  5. ***************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include "node2.h"
  9.  
  10. float **books, *thresh;
  11. int *b1, *b2, *ncv, nband;
  12. char **bands;
  13. struct node **tree;
  14. unsigned char *bits;
  15.  
  16. main(argc, argv) char **argv; {
  17.     int nlev, size, npt_h, npt_g, opt;
  18.     float *x, *h, *g;
  19.     char *ifile, *ofile, *xfile;
  20.     unsigned char *x_uc;
  21.  
  22.     if(argc != 4) {
  23.         printf("Usage: %s iput_file output_file X-file\n", argv[0]);
  24.         exit(-1);
  25.     }
  26.  
  27.     printf("\n***** Copyright (c) 1991 University of California *****\n\n");
  28.     fflush(stdout);
  29.  
  30.     ifile = argv[1];
  31.     ofile = argv[2];
  32.     xfile = argv[3];
  33.  
  34.     cread(ifile, &size, sizeof(int), 0);
  35.  
  36.     read_xfile(xfile, &nlev);
  37.  
  38.     opt = 1;
  39.     init(opt, ifile, &h, &g, &npt_h, &npt_g);
  40.  
  41.     printf("Beginning VQ Decode / Inverse Wavelet Transform . . . ");
  42.     fflush(stdout);
  43.     fwt_syn(&x, h, g, size, nlev, npt_h, npt_g);
  44.     printf("Finished\n");
  45.     fflush(stdout);
  46.  
  47.     tpose(&x, size, size);
  48.     x_uc = (unsigned char*)malloc(size*size);
  49.     crunch(x, x_uc, size*size);
  50.     printf("Writing Output File . . . ");
  51.     fflush(stdout);
  52.     cwrite(ofile, x_uc, size*size, 0);
  53.     printf("Finished\n");
  54.     fflush(stdout);
  55. }
  56.  
  57. /****************************************************************/
  58.  
  59. static int iband = 0;
  60.  
  61. fwt_syn(x, h, g, size, nlev, npt_h, npt_g)
  62. int size, nlev, npt_h, npt_g;
  63. float **x, *h, *g;
  64. {
  65.     int size1, band;
  66.     float *x1, *x2, *y1, *y2, *ptr1;
  67.  
  68.     if(nlev-- == 0) {
  69.         *x = (float*)malloc(size*size*sizeof(float));
  70.         decode(*x, bits, size, b1[iband], b2[iband], books[iband],
  71.                tree[iband], ncv[iband], iband);
  72.         iband++;
  73.         tpose(x, size, size);
  74.         return;
  75.     }
  76.  
  77.     size1 = size/2;
  78.  
  79.     for(band = 0; band < 4; band++) {
  80.  
  81.         if(!band) {
  82.             fwt_syn(&x1, h, g, size1, nlev, npt_h, npt_g);
  83.             x1 = (float*)realloc(x1, size*size1*sizeof(float));
  84.             us_conv(&x1, h, npt_h, size1, size1, 0);
  85.     }
  86.  
  87.         else {
  88.             ptr1 = (float*)malloc(size1*size1*sizeof(float));
  89.             if(iband < nband) {
  90.                 decode(ptr1, bits, size1, b1[iband], b2[iband], books[iband],
  91.                        tree[iband], ncv[iband], iband);
  92.                 tpose(&ptr1, size1, size1);
  93.         }
  94.             else
  95.                 zero(ptr1, size1*size1);
  96.             iband++;
  97.     }
  98.  
  99.         switch(band) {
  100.  
  101.             case 1: {
  102.                 x2 = (float*)realloc(ptr1, size*size1*sizeof(float));
  103.                 us_conv(&x2, g, npt_g, size1, size1, 1);
  104.                 add(x1, x2, size*size1);
  105.                 free(x2);
  106.                 y1 = x1 = (float*)realloc(x1, size*size*sizeof(float));
  107.                 us_conv(&y1, h, npt_h, size, size1, 0);
  108.                 break;
  109.         }
  110.  
  111.             case 2: {
  112.                 x1 = (float*)realloc(ptr1, size*size1*sizeof(float));
  113.                 us_conv(&x1, h, npt_h, size1, size1, 0);
  114.                 break;
  115.         }
  116.  
  117.             case 3: {
  118.                 x2 = (float*)realloc(ptr1, size*size1*sizeof(float));
  119.                 us_conv(&x2, g, npt_g, size1, size1, 1);
  120.                 add(x1, x2, size*size1);
  121.                 free(x2);
  122.                 y2 = x1 = (float*)realloc(x1, size*size*sizeof(float));
  123.                 us_conv(&y2, g, npt_g, size, size1, 1);
  124.                 break;
  125.         }
  126.     }
  127.     }
  128.  
  129.     add(*x = y1, y2, size*size);
  130.     free(y2);
  131.  }
  132.  
  133. /****************************************************************/
  134.  
  135. add(x1, x2, n)
  136. int n;
  137. register float *x1, *x2;
  138. {
  139.     register float *max;
  140.  
  141.     max = x1 + n;
  142.     while(x1 < max)
  143.          *x1++ += *x2++;
  144. }
  145.  
  146. /****************************************************************/
  147.  
  148. init(opt, ifile, h, g, npt_h, npt_g)
  149. int opt, *npt_h, *npt_g;
  150. float **h, **g;
  151. char *ifile;
  152. {
  153.     int i;
  154.     double *hdoub, *gdoub;
  155.  
  156.     printf("Reading Compressed Data File . . . ");
  157.     fflush(stdout);
  158.     cread2(ifile, &bits);
  159.     printf("Finished\n");
  160.     fflush(stdout);
  161.     bits += 3*sizeof(int);
  162.  
  163.     if(opt == 1) {
  164.         *npt_h = 7;
  165.         *npt_g = 9;
  166.         hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
  167.         gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
  168.         *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
  169.         *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
  170.         b4_4al7(hdoub);
  171.         b4_4ah9(gdoub);
  172.     }
  173.  
  174.     else if(opt == 2) {
  175.         *npt_h = 9;
  176.         *npt_g = 7;
  177.         hdoub = (double*)malloc((*npt_h)*sizeof(double));
  178.         gdoub = (double*)malloc((*npt_g)*sizeof(double));
  179.         *h = (float*)malloc((*npt_h)*sizeof(float));
  180.         *g = (float*)malloc((*npt_g)*sizeof(float));
  181.         b4_4bl9(hdoub);
  182.         b4_4bh7(gdoub);
  183.     }
  184.  
  185.     else if(opt == 3) {
  186.         *npt_h = 9;
  187.         *npt_g = 11;
  188.         hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
  189.         gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
  190.         *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
  191.         *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
  192.         b5_6bl9(hdoub);
  193.         b5_6bh11(gdoub);
  194.       }
  195.  
  196.     else if(opt == 4) {
  197.         *npt_h = 11;
  198.         *npt_g = 9;
  199.         hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
  200.         gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
  201.         *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
  202.         *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
  203.         b5_6al11(hdoub);
  204.         b5_6ah9(gdoub);
  205.     }
  206.  
  207.     else {
  208.         printf("Bad opt\n");
  209.         exit(-1);
  210.     }
  211.  
  212.     for(i = 0; i < *npt_h/2+1; i++)
  213.         (*h)[i] = (float)hdoub[*npt_h/2 - i];
  214.  
  215.     for(i = 0; i < *npt_g/2+1; i++)
  216.         (*g)[i] = (float)gdoub[*npt_g/2 - i];
  217.  
  218.     free(hdoub);
  219.     free(gdoub);
  220. }
  221.  
  222. /****************************************************************/
  223.  
  224. #include "huff_word.h"
  225.  
  226. read_xfile(xfile, nlev)
  227. int *nlev;
  228. char *xfile;
  229. {
  230.     int i, npt, nbyte, icv, nbit, ibit, ncvp;
  231.     struct node *tree_gen();
  232.     struct huff_word *table;
  233.     char *buff, *buffsave;
  234.  
  235.     printf("Reading Xfile . . . ");
  236.     fflush(stdout);
  237.  
  238.     nbyte = cread2(xfile, &buff);
  239.     buffsave = buff;
  240.  
  241.     printf("Finished\n");
  242.     fflush(stdout);
  243.  
  244.     *nlev = (int)(*((int*)buff));
  245.     buff += sizeof(int);
  246.     nband = (int)(*((int*)buff));
  247.     buff += sizeof(int);
  248.  
  249.     bands = (char**)malloc(nband*sizeof(char*));
  250.     for(i = 0; i < nband; i++)
  251.         bands[i] = (char*)malloc(10*sizeof(char));
  252.     b1 = (int*)malloc(nband*sizeof(int));
  253.     b2 = (int*)malloc(nband*sizeof(int));
  254.     thresh = (float*)malloc(nband*sizeof(float));
  255.     ncv = (int*)malloc(nband*sizeof(int));
  256.  
  257.     for(i = 0; i < nband; i++) {
  258.         b1[i] = (int)(*((int*)buff));
  259.         buff += sizeof(int);
  260.     }
  261.     for(i = 0; i < nband; i++) {
  262.         b2[i] = (int)(*((int*)buff));
  263.         buff += sizeof(int);
  264.     }
  265.  
  266.     for(i = 0; i < nband; i++) {
  267.         thresh[i] = (float)(*((float*)buff));
  268.         buff += sizeof(float);
  269.     }
  270.     free(thresh);
  271.  
  272.     printf("Building Trees . . . ");
  273.     fflush(stdout);
  274.  
  275.     tree = (struct node**)malloc(nband*sizeof(struct node*));
  276.     for(i = 0; i < nband; i++) {
  277.         ncvp = (int)(*((int*)buff));
  278.         ncv[i] = (i == 0) ? ncvp : ncvp - 34;
  279.         buff += sizeof(int);
  280.         table = (struct huff_word*)malloc(ncvp*sizeof(struct huff_word));
  281.         for(icv = 0; icv < ncvp; icv++) {
  282.             nbit = (int)(*((int*)buff));
  283.             buff += sizeof(int);
  284.             table[icv].nbit = nbit;
  285.             table[icv].word = (int*)malloc(nbit*sizeof(int));
  286.             for(ibit = 0; ibit < nbit; ibit++) {
  287.                 table[icv].word[ibit] = (int)(*((int*)buff));
  288.                 buff += sizeof(int);
  289.         }
  290.     }
  291.         tree[i] = tree_gen(table, ncvp);
  292.         free(table);
  293.     }
  294.  
  295.  
  296.     books = (float**)malloc(nband*sizeof(float*));
  297.     for(i = 0; i < nband; i++) {
  298.         nbyte = (npt = ncv[i]*b1[i]*b2[i])*sizeof(float);
  299.         books[i] = (float*)malloc(nbyte);
  300.         fcopy((float*)buff, books[i], npt);
  301.         buff += nbyte;
  302.     }
  303.  
  304.     free(buffsave);
  305.     printf("Finished\n");
  306.     fflush(stdout);
  307. }
  308.  
  309. /****************************************************************/
  310.  
  311. struct node *tree_gen(table, nsym)
  312. struct huff_word *table;
  313. {
  314.     int isym, nbit;
  315.     int *word;
  316.     struct node *treex;
  317.  
  318.     treex = (struct node*)malloc(sizeof(struct node));
  319.  
  320.     treex->node0 = NULL;
  321.     treex->node1 = NULL;
  322.  
  323.     for(isym = 0; isym < nsym; isym++) {
  324.         nbit = table[isym].nbit;
  325.         word = table[isym].word;
  326.         create_node(treex, word, 0, nbit, isym);
  327.     }
  328.  
  329.     return(treex);
  330.  
  331. }
  332.  
  333. create_node(treex, word, ibit, nbit, isym)
  334. int ibit, nbit, isym, *word;
  335. struct node *treex;
  336. {
  337.     struct node **next_node;
  338.  
  339.     if(ibit == nbit) {
  340.         treex->type = isym;
  341.         return;
  342.     }
  343.  
  344.     treex->type = -1;
  345.  
  346.     next_node = (!word[ibit]) ? &treex->node0 : &treex->node1;
  347.     if(*next_node == NULL) {
  348.         *next_node = (struct node*)malloc(sizeof(struct node));
  349.         (*next_node)->node0 = (*next_node)->node1 = NULL;
  350.     }
  351.  
  352.     create_node(*next_node, word, ibit+1, nbit, isym);
  353. }
  354.  
  355. /****************************************************************/
  356.  
  357. crunch(x1, x2, n)
  358. float *x1;
  359. unsigned char *x2;
  360. int n;
  361. {
  362.     float *max;
  363.  
  364.     max = x1 + n;
  365.     while(x1 < max) {
  366.         if(*x1 < 0.) {
  367.             *x2++ = 0;
  368.             x1++;
  369.     }
  370.  
  371.         if(*x1 > 255.) {
  372.             *x2++ = 255;
  373.             x1++;
  374.     }
  375.  
  376.         else
  377.             *x2++ = (unsigned char)(*x1++);
  378.     }
  379. }
  380.  
  381. /****************************************************************/
  382.  
  383. fcopy(xbuff, x, n)
  384. float *xbuff, *x;
  385. int n;
  386. {
  387.     float *max;
  388.  
  389.     max = x + n;
  390.     while(x < max)
  391.         *x++ = *xbuff++;
  392. }
  393.  
  394. /****************************************************************/
  395.  
  396. zero(x, n)
  397. int n;
  398. register float *x;
  399. {
  400.     register float *max;
  401.  
  402.     max = x + n;
  403.     while(x < max)
  404.         *x++ = 0.;
  405. }
  406.  
  407. /****************************************************************/
  408.  
  409. unnorm(x, xmin, xx, n)
  410. int n;
  411. register float *x, xmin, xx;
  412. {
  413.     register float *max;
  414.  
  415.     max = x + n;
  416.     while(x < max) {
  417.         *x = *x * xx + xmin;
  418.         x++;
  419.     }
  420. }
  421.