📄 wpc1coding.c
字号:
/* Copyright (c) Colorado School of Mines, 2006.*//* All rights reserved. *//* WPC1CODING: $Revision: 1.3 $ ; $Date: 1997/07/30 15:22:59 $ *//*********************** self documentation **********************//************************************************************************WPC1CODING - routines for encoding the integer symbols in 1D WPC wpc1Encoder - encoderwpc1Decoder - decoder************************************************************************Function Prototypes:void wpc1Encoder(void *qstate, int nblock, void *wpc1)int wpc1Decoder(void *qstate, int nblock, void *wpc1)************************************************************************wpc1Encoder:Input:quant quantization statusnblock # of blocksOutput:wpc1 encoded datawpc1Decoder:Input:nblock # of blockswpc1 compressed data Output:quant quantization status Return: consistency flag, 1 if wpc1Comressed data, 0 otherwise************************************************************************Author: CWP: Tong Chen 1995************************************************************************//**************** end self doc ********************************/#include "wpc1.h"#include "wpc1lib.h"#define FMAXINTUS 65535.#define FMAXINTSN 32767.#define BITMAXNBIT 4#define MAXNBIT 16/* structure used internally *//* one code */typedef struct entCodeStruct{ unsigned int value; int bits;} entCODE;/* code book */typedef struct codeBookStruct{ int maxrun; int bitmaxrun; int booksize; entCODE *code; int *pref2bit;} codeBOOK;/* code dictionary */ typedef struct codeDictStruct{ int dictsize; codeBOOK *book;} codeDICT;/* table used internally *//* this table enables efficient compression for a wide range ofaccuracy, namely, 1-16 bit, or with relative RMS error 10^(-5) to 1. */static int amptbl[MAXNBIT] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000};/* functions used internally */void entCoder(void *qstate, int nblock, void *outbuff);int entDecoder(void *qstate, int nblock, void *outbuff);void bookCoder(void *bitbuff, void *book, void *qx, int numsmp);int bookDecoder(void *bitbuff, void *book, void *qx, int numsmp);static codeDICT *builddict(void);void wpc1Encoder(void *qstate, int nblock, void *wpc1)/*************************************************************************Encode the quantized data **************************************************************************Input:quant quantization statusnblock # of blocksOutput:wpc1 encoded data**************************************************************************Author: Tong Chen, 08/04/94*************************************************************************/{ wpc1QUANT **quant = (wpc1QUANT **) qstate; wpc1PTR *w1 = (wpc1PTR *) wpc1; wpcBUFF *buff; float *step, *ave; float atmp, maxstep, maxave, dstep, dave; int *nstep, *nave; unsigned char c1, c2; int i; /* allocate spaces */ step = (float *) malloc(nblock*sizeof(float)); ave = (float *) malloc(nblock*sizeof(float)); nstep = (int *) malloc(nblock*sizeof(int)); nave = (int *) malloc(nblock*sizeof(int)); /* allocate the buffer */ buff = (wpcBUFF *) malloc(sizeof(wpcBUFF)); /* init the buffer */ buff->code = w1->code; buff->mbound = w1->nsize; buff->pos = 0; /* obtain the steps and averages */ maxstep = -1.; maxave = -1.; for(i=0; i<nblock; i++){ step[i] = quant[i]->step; if(maxstep < step[i]) maxstep = step[i]; ave[i] = quant[i]->ave; atmp = ABS(ave[i]); if(maxave < atmp) maxave = atmp; } /* save the step and average */ w1->overallstep = maxstep; w1->overallave = maxave; /* if DC trace */ if(maxstep <= FLT_MIN){ /* if not dead trace */ if(maxave > FLT_MIN){ /* quantize the averages */ dave = FMAXINTSN/maxave; for(i=0; i<nblock; i++) nave[i] = NINT(ave[i]*dave); /* output the averages as signed short, to the buffer */ for(i=0; i<nblock; i++){ /* 2's complement */ if(nave[i] < 0) nave[i] = 0x10000 + nave[i]; c1 = (nave[i]) & 0xff; c2 = (nave[i] >> 8) & 0xff; buffPutc(buff, c1); buffPutc(buff, c2); } } } /* else, normal trace */ else{ /* quantize the steps */ dstep = FMAXINTUS/maxstep; for(i=0; i<nblock; i++) nstep[i] = NINT(step[i]*dstep); /* output the steps as unsigned short, to the buffer */ for(i=0; i<nblock; i++){ c1 = (nstep[i]) & 0xff; c2 = (nstep[i] >> 8) & 0xff; buffPutc(buff, c1); buffPutc(buff, c2); } /* if DC not zero */ if(maxave > FLT_MIN){ /* quantize the steps */ dave = FMAXINTSN/maxave; for(i=0; i<nblock; i++) nave[i] = NINT(ave[i]*dave); /* output the average as signed short, to the buffer */ for(i=0; i<nblock; i++){ /* 2's complement */ if(nave[i] < 0) nave[i] = 0x10000 + nave[i]; c1 = (nave[i]) & 0xff; c2 = (nave[i] >> 8) & 0xff; buffPutc(buff, c1); buffPutc(buff, c2); } } /* entropy encoding */ entCoder(quant, nblock, buff); } /* adjust the size */ w1->nsize = buff->pos; /* free the spaces */ free((void *) step); free((void *) ave); free((void *) nstep); free((void *) nave); free((void *) buff);}int wpc1Decoder(void *qstate, int nblock, void *wpc1)/*************************************************************************Decode the compressed data **************************************************************************Input:nblock # of blockswpc1 compressed data Output:quant quantization status Return: consistency flag, 1 if wpc1Comressed data, 0 otherwise**************************************************************************Author: Tong Chen, 08/08/94*************************************************************************/{ wpc1QUANT **quant = (wpc1QUANT **) qstate; wpc1PTR *w1 = (wpc1PTR *) wpc1; wpcBUFF *buff; float maxstep, maxave, dstep, dave; int *nstep, *nave; unsigned char c1=0, c2=0; int i, retval; /* allocate spaces */ nstep = (int *) malloc(nblock*sizeof(int)); nave = (int *) malloc(nblock*sizeof(int)); /* allocate the buffer */ buff = (wpcBUFF *) malloc(sizeof(wpcBUFF)); /* init the buffer */ buff->code = w1->code; buff->mbound = w1->nsize; buff->pos = 0; /* max of the step and average */ maxstep = w1->overallstep; maxave = w1->overallave; /* if DC trace */ if(maxstep <= FLT_MIN){ /* if not dead trace */ if(maxave > FLT_MIN){ /* input the average as signed short */ for(i=0; i<nblock; i++){ buffGetc(buff, c1); buffGetc(buff, c2); nave[i] = c1 + (c2 << 8); if(c2 & 0x80) nave[i] = nave[i] - 0x10000; } /* dequantize the averages */ dave = maxave/FMAXINTSN; for(i=0; i<nblock; i++) quant[i]->ave = nave[i]*dave; } /* set the steps */ for(i=0; i<nblock; i++) quant[i]->step = FLT_MIN; retval = 1; } /* else normal trace */ else{ /* input the step as unsigned short */ for(i=0; i<nblock; i++){ buffGetc(buff, c1); buffGetc(buff, c2); nstep[i] = c1 + (c2 << 8); } /* dequantize the steps */ dstep = maxstep/FMAXINTUS; for(i=0; i<nblock; i++) quant[i]->step = nstep[i]*dstep; /* if DC not zero */ if(maxave > FLT_MIN){ /* input the average as signed short */ for(i=0; i<nblock; i++){ buffGetc(buff, c1); buffGetc(buff, c2); nave[i] = c1 + (c2 << 8); if(c2 & 0x80) nave[i] = nave[i] - 0x10000; } /* dequantize the averages */ dave = maxave/FMAXINTSN; for(i=0; i<nblock; i++) quant[i]->ave = nave[i]*dave; } /* entropy decoding */ retval = entDecoder(quant, nblock, buff); } /* free the spaces */ free((void *) nstep); free((void *) nave); free((void *) buff); return (retval);}void entCoder(void *qstate, int nblock, void *outbuff)/**********************************************************************entropy encoder using some codebook ***********************************************************************Input:qstate quantization statusnblock # of blocksOutput:outbuff output memory buffer**********************************************************************/{ wpc1QUANT **quant = (wpc1QUANT **) qstate; wpcBUFF *buff = (wpcBUFF *) outbuff; wpcBITBUFF *bitbuff; codeBOOK codebook; static codeDICT *codedict; int iblock, flag; int numsmp, nbit; int *qx; /* init the bitwise buffer */ bitInitbuff(bitbuff, buff); /* write # of bits to the buffer */ for(iblock=0; iblock<nblock; iblock++){ nbit = quant[iblock]->nbit; bitOutputbits(bitbuff, nbit, BITMAXNBIT, flag); } /* build the code dictionary */ codedict = builddict(); /* entropy coding each block */ for(iblock=0; iblock<nblock; iblock++){ nbit = quant[iblock]->nbit; numsmp = quant[iblock]->numsmp; qx = quant[iblock]->qx; /* obtain the code book for this nbit */ codebook = codedict->book[nbit]; /* encode according to the code book */ bookCoder(bitbuff, &codebook, qx, numsmp); } nbit += 0*flag; /* dummy */ /* wind up the buffer */ bitWindbuff(bitbuff, flag); /* close the buffer */ bitFreebuff(bitbuff);}int entDecoder(void *qstate, int nblock, void *inbuff)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -