home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Los Alamos National Laboratory
/
LANL_CD.ISO
/
software
/
compres
/
src
/
decode.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-11
|
3KB
|
152 lines
/****************************************************************
COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
***************************************************************/
#include "node2.h"
decode(x_unblk, bits, size, b1, b2, book, tree, ncv, iband)
int size, b1, b2, ncv, iband;
float *x_unblk, *book;
unsigned char *bits;
struct node *tree;
{
int *indices, k, nvec;
float *x;
k = b1*b2;
nvec = size*size/k;
indices = (int*)malloc(nvec*sizeof(int));
huff_rle_decode(indices, nvec, tree, bits, ncv, iband);
x = (float*)malloc(size*size*sizeof(float));
vq_decode(x, indices, book, k, nvec);
free(indices);
unblk(x, x_unblk, b1, b2, size, size);
free(x);
}
/****************************************************************/
static int offset;
static unsigned char *ptr;
huff_rle_decode(indices, n, tree, bits, ncv, iband)
int *indices, n, ncv, iband;
unsigned char *bits;
struct node *tree;
{
int index, *max;
if(!iband) {
ptr = bits;
offset = 0;
}
max = indices + n;
while(indices < max) {
index = index_gen(tree);
rle_decode(&indices, index, ncv);
}
}
/****************************************************************/
int index_gen(tree)
struct node *tree;
{
int index;
struct node *next_node;
next_node = (*ptr < 128) ? tree->node0 : tree->node1;
if(++offset == 8) {
offset = 0;
ptr++;
}
else
*ptr = *ptr << 1;
if(next_node->type >= 0)
return(next_node->type);
index = index_gen(next_node);
return(index);
}
/****************************************************************/
rle_decode(indices, val, ncv)
int **indices, val, ncv;
{
int *iptr, *max, run_length;
iptr = *indices;
if(val < ncv)
*iptr++ = val;
else {
if(val <= ncv + 31)
run_length = val - ncv + 2;
if(val == ncv+31)
run_length = 64;
if(val == ncv+32)
run_length = 128;
if(val == ncv+33)
run_length = 256;
max = iptr + run_length;
while(iptr < max)
*iptr++ = 0;
}
*indices = iptr;
}
/****************************************************************/
vq_decode(image, ip, cdbook, k, nindex)
int nindex;
register int *ip, k;
register float *image, *cdbook;
{
register float *cbp, *cbpmax;
float *max;
max = image + nindex*k;
while(image < max) {
cbp = cdbook + *ip++*k;
cbpmax = cbp + k;
while(cbp < cbpmax)
*image++ = *cbp++;
}
}
/****************************************************************/
unblk(inpix, outpix, bwidth, bheight, iwidth, iheight)
int bwidth, bheight, iwidth;
float *inpix, *outpix;
{
int nslice, islice, iblk, irow;
float *x1, *x2, *max1, *max2;
nslice = iheight/bheight;
for(islice = 0; islice < nslice; islice++) {
x2 = inpix;
max2 = x2 + bheight*iwidth;
while(x2 < max2)
for(iblk = 0; iblk < iwidth/bwidth; iblk++)
for(irow = 0; irow < bheight; irow++) {
x1 = outpix + irow*iwidth + iblk*bwidth;
max1 = x1 + bwidth;
while(x1 < max1)
*x1++ = *x2++;
}
inpix += bheight*iwidth;
outpix += bheight*iwidth;
}
}