home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************
-
- COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
-
- ***************************************************************/
-
- #include <stdio.h>
- #include <math.h>
- #include "huff_word.h"
-
- float **books, *thresh;
- int *b1, *b2, *ncv, nband;
- struct huff_word **table;
- int nbyte_in;
-
- main(argc, argv) char **argv; {
- int nlev, size, opt, npt_h, npt_g;
- float *x, *h, *g;
- char *ifile, *ofile, *xfile;
-
- if(argc != 4) {
- printf("Usage: %s input_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];
-
- read_xfile(xfile, &nlev);
-
- opt = 1;
- init(opt, ifile, &size, &x, &h, &g, &npt_h, &npt_g);
- cwrite(ofile, &size, sizeof(int), 0);
-
- printf("Beginning Wavelet Transform / VQ Encode . . . ");
- fflush(stdout);
- fwt_anl(ofile, x, h, g, size, nlev, npt_h, npt_g);
- }
-
- /****************************************************************/
-
- static int iband = 0;
-
- fwt_anl(ofile, x, h, g, size, nlev, npt_h, npt_g)
- int size, nlev, npt_h, npt_g;
- float *x, *h, *g;
- char *ofile;
- {
- int band;
- float *x1, *x2;
-
- if(nlev-- == 0)
- return;
-
- for(band = 0; band < 4; band++) {
-
- switch(band) {
- case 0: {
- conv_ds(x, &x1, h, npt_h, size, size, 0);
- conv_ds(x1, &x2, h, npt_h, size/2, size, 0);
- break;
- }
- case 1: {
- conv_ds(x1, &x2, g, npt_g, size/2, size, 1);
- free(x1);
- break;
- }
- case 2: {
- conv_ds(x, &x1, g, npt_g, size, size, 1);
- free(x);
- conv_ds(x1, &x2, h, npt_h, size/2, size, 0);
- break;
- }
- case 3: {
- conv_ds(x1, &x2, g, npt_g, size/2, size, 1);
- free(x1);
- break;
- }
- }
-
- if( (band != 0) || (nlev == 0) ) {
- encode(x2, size/2, b1[iband], b2[iband], books[iband], ncv[iband],
- table[iband], iband, nband, thresh[iband], ofile);
- iband++;
- }
-
- if(!band)
- fwt_anl(ofile, x2, h, g, size/2, nlev, npt_h, npt_g);
- free(x2);
- }
- }
-
- /****************************************************************/
-
- init(opt, ifile, size, x, h, g, npt_h, npt_g)
- int opt, *npt_h, *npt_g, *size;
- float **x, **h, **g;
- char *ifile;
- {
- int i;
- double *hdoub, *gdoub;
- unsigned char *x_uc;
-
- printf("Reading Data File . . . ");
- fflush(stdout);
- nbyte_in = cread2(ifile, &x_uc);
- printf("Finished\n");
- fflush(stdout);
-
- *size = (int)(sqrt((float)(nbyte_in)));
- *x = (float*)malloc(*size * *size * sizeof(float));
- expand(x_uc, *x, nbyte_in);
- free(x_uc);
-
- if(opt == 1) {
- *npt_h = 9;
- *npt_g = 7;
- 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_4bl9(hdoub);
- b4_4bh7(gdoub);
- }
-
- else if(opt == 2) {
- *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 == 3) {
- *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 if(opt == 4) {
- *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 {
- 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);
- }
-
- /****************************************************************/
-
- char *buff;
-
- read_xfile(xfile, nlev)
- int *nlev;
- char *xfile;
- {
- int i, nbyte, icv, nbit, ibit, npt, ncvp;
- char *buffsave;
-
- printf("Reading Xfile . . . ");
- fflush(stdout);
-
- nbyte = cread2(xfile, &buff);
- buffsave = buff;
-
- printf("Finished\n");
- fflush(stdout);
-
- *nlev = *((int*)buff);
- buff += sizeof(int);
- nband = *((int*)buff);
- buff += sizeof(int);
-
- 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);
- }
-
- printf("Building Trees . . . ");
- fflush(stdout);
-
- table = (struct huff_word**)malloc(nband*sizeof(struct huff_word*));
- for(i = 0; i < nband; i++) {
- ncvp = (int)(*((int*)buff));
- ncv[i] = (i == 0) ? ncvp : ncvp - 34;
- buff += sizeof(int);
- table[i] = (struct huff_word*)malloc(ncvp*sizeof(struct huff_word));
- for(icv = 0; icv < ncvp; icv++) {
- nbit = (int)(*((int*)buff));
- buff += sizeof(int);
- (table[i])[icv].nbit = nbit;
- (table[i])[icv].word = (int*)malloc(nbit*sizeof(int));
- for(ibit = 0; ibit < nbit; ibit++) {
- (table[i])[icv].word[ibit] = (int)(*((int*)buff));
- buff += sizeof(int);
- }
- }
- }
-
- 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);
- }
-
- /****************************************************************/
-
- expand(x1, x2, n)
- unsigned char *x1;
- float *x2;
- int n;
- {
- float *max;
-
- max = x2 + n;
- while(x2 < max)
- *x2++ = (float)*x1++;
- }
-
- /****************************************************************/
-
- fcopy(xbuff, x, n)
- float *xbuff, *x;
- int n;
- {
- float *max;
-
- max = x + n;
- while(x < max)
- *x++ = *xbuff++;
- }
-
- icopy(xbuff, x, n)
- int *xbuff, *x;
- int n;
- {
- int *max;
-
- max = x + n;
- while(x < max)
- *x++ = *xbuff++;
- }
-