home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Los Alamos National Laboratory
/
LANL_CD.ISO
/
software
/
compres
/
src
/
encode.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-11
|
5KB
|
207 lines
/****************************************************************
COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
***************************************************************/
#include <stdio.h>
#include <math.h>
#include "huff_word.h"
extern int nbyte_in;
encode(x, size, b1, b2, book, ncv, table, iband, nband, thresh, ofile)
int size, b1, b2, ncv, iband, nband;
float *x, *book, thresh;
char *ofile;
struct huff_word *table;
{
float*xblk;
int *indices, k, nvec, nbyte, huff_encode();
static unsigned char *bits;
threshold(x, size*size, thresh);
k = b1*b2;
nvec = size*size/k;
xblk = (float*)malloc(size*size*sizeof(float));
blk(x, xblk, b1, b2, size, size);
indices = (int*)malloc(nvec*sizeof(int));
encode_kd(xblk, indices, nvec, book, k, ncv);
rle_encode(&indices, &nvec, ncv);
nbyte = huff_encode(indices, nvec, table, &bits, iband, nband);
if(iband == nband-1) {
printf("Finished\n");
fflush(stdout);
printf("Writing Output File . . . ");
fflush(stdout);
cwrite(ofile, bits, nbyte, 3*sizeof(int));
printf("Finished\n\n\n");
fflush(stdout);
nbyte += 3*sizeof(int);
printf("Input file: %d bytes\n", nbyte_in);
printf("Output file: %d bytes\n", nbyte);
printf("Compression %.2f : 1\n", (float)nbyte_in/(float)nbyte);
exit(-1);
}
}
/****************************************************************/
blk(inpix, outpix, bwidth, bheight, iwidth, iheight)
int bwidth, bheight, iwidth;
float *inpix, *outpix;
{
int islice, nslice, iblk, irow;
float *x1, *x2, *max1, *max2;
nslice = iheight/bheight;
for(islice = 0; islice < nslice; islice++) {
x2 = outpix;
max2 = x2 + bheight*iwidth;
while(x2 < max2)
for(iblk = 0; iblk < iwidth/bwidth; iblk++)
for(irow = 0; irow < bheight; irow++) {
x1 = inpix + irow*iwidth + iblk*bwidth;
max1 = x1 + bwidth;
while(x1 < max1)
*x2++ = *x1++;
}
inpix += bheight*iwidth;
outpix += bheight*iwidth;
}
}
/****************************************************************/
#define PGSIZE 65536
int huff_encode(indices, npt, table, bits, iband, nband)
int *indices, npt, iband, nband;
unsigned char **bits;
struct huff_word *table;
{
int i, nbit, ibit, *word;
static int offset = 0, npage = 1;
static unsigned char *ptr, *max;
if(iband == 0) {
*bits = ptr = (unsigned char*)malloc(PGSIZE);
zero(ptr, PGSIZE);
max = ptr + PGSIZE;
}
for(i = 0; i < npt; i++) {
nbit = table[indices[i]].nbit;
word = table[indices[i]].word;
for(ibit = 0; ibit < nbit; ibit++) {
if(word[ibit] == 1)
*ptr |= 001;
if(++offset == 8) {
offset = 0;
if( (iband == nband-1) && (i == npt-1) && (ibit == nbit - 1) )
break;
ptr++;
if(ptr > max) {
*bits = (unsigned char*)realloc(*bits, (npage+1)*PGSIZE);
ptr = *bits + npage++ * PGSIZE;
max = *bits + npage*PGSIZE;
}
}
else
*ptr = *ptr << 1;
}
}
if( (iband == nband-1) && (offset != 0) )
*ptr = *ptr << (7 - offset);
return(ptr - *bits + 1);
}
/****************************************************************/
zero(x, n)
int n;
unsigned char *x;
{
unsigned char *max;
max = x + n;
while(x < max)
*x++ = 0;
}
/****************************************************************/
encode_bf(tv, cbook, ndim, nvec, ncv, otpix)
float *tv, *cbook;
int ndim, nvec, ncv, *otpix;
{
int i, indx, full_search();
for(i = 0; i < nvec; i++) {
indx = full_search(tv, cbook, ncv, ndim);
tv += ndim;
otpix[i] = indx;
}
}
int full_search(vec, cbook, ncv, k)
float *vec, *cbook;
int ncv, k;
{
int j, jmx = 0;
float min_dist = 1.e30, dist, euc_dist();
for(j = 0; j < ncv; j++) {
dist = euc_dist(vec, cbook, k);
cbook += k;
if(dist < min_dist) {
min_dist = dist;
jmx = j;
}
}
return(jmx);
}
float euc_dist(x, y, n)
float *x, *y;
int n;
{
float *max, dist = 0., xx;
max = x + n;
while(x < max) {
xx = *x++ - *y++;
dist += xx*xx;
}
return(dist);
}
/****************************************************************/
threshold(x, n, thresh)
int n;
register float *x, thresh;
{
register float *max;
max = x + n;
while(x < max) {
if(fabs(*x) < thresh)
*x++ = 0.;
else
x++;
}
}