home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************
-
- COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
-
- ***************************************************************/
-
- #include <stdio.h>
- #include "node2.h"
-
- float **books, *thresh;
- int *b1, *b2, *ncv, nband;
- char **bands;
- struct node **tree;
- unsigned char *bits;
-
- main(argc, argv) char **argv; {
- int nlev, size, npt_h, npt_g, opt;
- float *x, *h, *g;
- char *ifile, *ofile, *xfile;
- unsigned char *x_uc;
-
- if(argc != 4) {
- printf("Usage: %s iput_file output_file X-file\n", argv[0]);
- exit(-1);
- }
-
- printf("\n***** Copyright (c) 1991 University of California *****\n\n");
- fflush(stdout);
-
- ifile = argv[1];
- ofile = argv[2];
- xfile = argv[3];
-
- cread(ifile, &size, sizeof(int), 0);
-
- read_xfile(xfile, &nlev);
-
- opt = 1;
- init(opt, ifile, &h, &g, &npt_h, &npt_g);
-
- printf("Beginning VQ Decode / Inverse Wavelet Transform . . . ");
- fflush(stdout);
- fwt_syn(&x, h, g, size, nlev, npt_h, npt_g);
- printf("Finished\n");
- fflush(stdout);
-
- tpose(&x, size, size);
- x_uc = (unsigned char*)malloc(size*size);
- crunch(x, x_uc, size*size);
- printf("Writing Output File . . . ");
- fflush(stdout);
- cwrite(ofile, x_uc, size*size, 0);
- printf("Finished\n");
- fflush(stdout);
- }
-
- /****************************************************************/
-
- static int iband = 0;
-
- fwt_syn(x, h, g, size, nlev, npt_h, npt_g)
- int size, nlev, npt_h, npt_g;
- float **x, *h, *g;
- {
- int size1, band;
- float *x1, *x2, *y1, *y2, *ptr1;
-
- if(nlev-- == 0) {
- *x = (float*)malloc(size*size*sizeof(float));
- decode(*x, bits, size, b1[iband], b2[iband], books[iband],
- tree[iband], ncv[iband], iband);
- iband++;
- tpose(x, size, size);
- return;
- }
-
- size1 = size/2;
-
- for(band = 0; band < 4; band++) {
-
- if(!band) {
- fwt_syn(&x1, h, g, size1, nlev, npt_h, npt_g);
- x1 = (float*)realloc(x1, size*size1*sizeof(float));
- us_conv(&x1, h, npt_h, size1, size1, 0);
- }
-
- else {
- ptr1 = (float*)malloc(size1*size1*sizeof(float));
- if(iband < nband) {
- decode(ptr1, bits, size1, b1[iband], b2[iband], books[iband],
- tree[iband], ncv[iband], iband);
- tpose(&ptr1, size1, size1);
- }
- else
- zero(ptr1, size1*size1);
- iband++;
- }
-
- switch(band) {
-
- case 1: {
- x2 = (float*)realloc(ptr1, size*size1*sizeof(float));
- us_conv(&x2, g, npt_g, size1, size1, 1);
- add(x1, x2, size*size1);
- free(x2);
- y1 = x1 = (float*)realloc(x1, size*size*sizeof(float));
- us_conv(&y1, h, npt_h, size, size1, 0);
- break;
- }
-
- case 2: {
- x1 = (float*)realloc(ptr1, size*size1*sizeof(float));
- us_conv(&x1, h, npt_h, size1, size1, 0);
- break;
- }
-
- case 3: {
- x2 = (float*)realloc(ptr1, size*size1*sizeof(float));
- us_conv(&x2, g, npt_g, size1, size1, 1);
- add(x1, x2, size*size1);
- free(x2);
- y2 = x1 = (float*)realloc(x1, size*size*sizeof(float));
- us_conv(&y2, g, npt_g, size, size1, 1);
- break;
- }
- }
- }
-
- add(*x = y1, y2, size*size);
- free(y2);
- }
-
- /****************************************************************/
-
- add(x1, x2, n)
- int n;
- register float *x1, *x2;
- {
- register float *max;
-
- max = x1 + n;
- while(x1 < max)
- *x1++ += *x2++;
- }
-
- /****************************************************************/
-
- init(opt, ifile, h, g, npt_h, npt_g)
- int opt, *npt_h, *npt_g;
- float **h, **g;
- char *ifile;
- {
- int i;
- double *hdoub, *gdoub;
-
- printf("Reading Compressed Data File . . . ");
- fflush(stdout);
- cread2(ifile, &bits);
- printf("Finished\n");
- fflush(stdout);
- bits += 3*sizeof(int);
-
- if(opt == 1) {
- *npt_h = 7;
- *npt_g = 9;
- hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
- gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
- *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
- *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
- b4_4al7(hdoub);
- b4_4ah9(gdoub);
- }
-
- else if(opt == 2) {
- *npt_h = 9;
- *npt_g = 7;
- hdoub = (double*)malloc((*npt_h)*sizeof(double));
- gdoub = (double*)malloc((*npt_g)*sizeof(double));
- *h = (float*)malloc((*npt_h)*sizeof(float));
- *g = (float*)malloc((*npt_g)*sizeof(float));
- b4_4bl9(hdoub);
- b4_4bh7(gdoub);
- }
-
- else if(opt == 3) {
- *npt_h = 9;
- *npt_g = 11;
- hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
- gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
- *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
- *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
- b5_6bl9(hdoub);
- b5_6bh11(gdoub);
- }
-
- else if(opt == 4) {
- *npt_h = 11;
- *npt_g = 9;
- hdoub = (double*)malloc((*npt_h/2+1)*sizeof(double));
- gdoub = (double*)malloc((*npt_g/2+1)*sizeof(double));
- *h = (float*)malloc((*npt_h/2+1)*sizeof(double));
- *g = (float*)malloc((*npt_g/2+1)*sizeof(double));
- b5_6al11(hdoub);
- b5_6ah9(gdoub);
- }
-
- else {
- printf("Bad opt\n");
- exit(-1);
- }
-
- for(i = 0; i < *npt_h/2+1; i++)
- (*h)[i] = (float)hdoub[*npt_h/2 - i];
-
- for(i = 0; i < *npt_g/2+1; i++)
- (*g)[i] = (float)gdoub[*npt_g/2 - i];
-
- free(hdoub);
- free(gdoub);
- }
-
- /****************************************************************/
-
- #include "huff_word.h"
-
- read_xfile(xfile, nlev)
- int *nlev;
- char *xfile;
- {
- int i, npt, nbyte, icv, nbit, ibit, ncvp;
- struct node *tree_gen();
- struct huff_word *table;
- char *buff, *buffsave;
-
- printf("Reading Xfile . . . ");
- fflush(stdout);
-
- nbyte = cread2(xfile, &buff);
- buffsave = buff;
-
- printf("Finished\n");
- fflush(stdout);
-
- *nlev = (int)(*((int*)buff));
- buff += sizeof(int);
- nband = (int)(*((int*)buff));
- buff += sizeof(int);
-
- bands = (char**)malloc(nband*sizeof(char*));
- for(i = 0; i < nband; i++)
- bands[i] = (char*)malloc(10*sizeof(char));
- b1 = (int*)malloc(nband*sizeof(int));
- b2 = (int*)malloc(nband*sizeof(int));
- thresh = (float*)malloc(nband*sizeof(float));
- ncv = (int*)malloc(nband*sizeof(int));
-
- for(i = 0; i < nband; i++) {
- b1[i] = (int)(*((int*)buff));
- buff += sizeof(int);
- }
- for(i = 0; i < nband; i++) {
- b2[i] = (int)(*((int*)buff));
- buff += sizeof(int);
- }
-
- for(i = 0; i < nband; i++) {
- thresh[i] = (float)(*((float*)buff));
- buff += sizeof(float);
- }
- free(thresh);
-
- printf("Building Trees . . . ");
- fflush(stdout);
-
- tree = (struct node**)malloc(nband*sizeof(struct node*));
- for(i = 0; i < nband; i++) {
- ncvp = (int)(*((int*)buff));
- ncv[i] = (i == 0) ? ncvp : ncvp - 34;
- buff += sizeof(int);
- table = (struct huff_word*)malloc(ncvp*sizeof(struct huff_word));
- for(icv = 0; icv < ncvp; icv++) {
- nbit = (int)(*((int*)buff));
- buff += sizeof(int);
- table[icv].nbit = nbit;
- table[icv].word = (int*)malloc(nbit*sizeof(int));
- for(ibit = 0; ibit < nbit; ibit++) {
- table[icv].word[ibit] = (int)(*((int*)buff));
- buff += sizeof(int);
- }
- }
- tree[i] = tree_gen(table, ncvp);
- free(table);
- }
-
-
- books = (float**)malloc(nband*sizeof(float*));
- for(i = 0; i < nband; i++) {
- nbyte = (npt = ncv[i]*b1[i]*b2[i])*sizeof(float);
- books[i] = (float*)malloc(nbyte);
- fcopy((float*)buff, books[i], npt);
- buff += nbyte;
- }
-
- free(buffsave);
- printf("Finished\n");
- fflush(stdout);
- }
-
- /****************************************************************/
-
- struct node *tree_gen(table, nsym)
- struct huff_word *table;
- {
- int isym, nbit;
- int *word;
- struct node *treex;
-
- treex = (struct node*)malloc(sizeof(struct node));
-
- treex->node0 = NULL;
- treex->node1 = NULL;
-
- for(isym = 0; isym < nsym; isym++) {
- nbit = table[isym].nbit;
- word = table[isym].word;
- create_node(treex, word, 0, nbit, isym);
- }
-
- return(treex);
-
- }
-
- create_node(treex, word, ibit, nbit, isym)
- int ibit, nbit, isym, *word;
- struct node *treex;
- {
- struct node **next_node;
-
- if(ibit == nbit) {
- treex->type = isym;
- return;
- }
-
- treex->type = -1;
-
- next_node = (!word[ibit]) ? &treex->node0 : &treex->node1;
- if(*next_node == NULL) {
- *next_node = (struct node*)malloc(sizeof(struct node));
- (*next_node)->node0 = (*next_node)->node1 = NULL;
- }
-
- create_node(*next_node, word, ibit+1, nbit, isym);
- }
-
- /****************************************************************/
-
- crunch(x1, x2, n)
- float *x1;
- unsigned char *x2;
- int n;
- {
- float *max;
-
- max = x1 + n;
- while(x1 < max) {
- if(*x1 < 0.) {
- *x2++ = 0;
- x1++;
- }
-
- if(*x1 > 255.) {
- *x2++ = 255;
- x1++;
- }
-
- else
- *x2++ = (unsigned char)(*x1++);
- }
- }
-
- /****************************************************************/
-
- fcopy(xbuff, x, n)
- float *xbuff, *x;
- int n;
- {
- float *max;
-
- max = x + n;
- while(x < max)
- *x++ = *xbuff++;
- }
-
- /****************************************************************/
-
- zero(x, n)
- int n;
- register float *x;
- {
- register float *max;
-
- max = x + n;
- while(x < max)
- *x++ = 0.;
- }
-
- /****************************************************************/
-
- unnorm(x, xmin, xx, n)
- int n;
- register float *x, xmin, xx;
- {
- register float *max;
-
- max = x + n;
- while(x < max) {
- *x = *x * xx + xmin;
- x++;
- }
- }
-