📄 shiftb.cpp
字号:
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/** **/
/** File : shiftbits.c **/
/** Date : April 16, 1996 **/
/** **/
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/** **/
/** Header Files **/
/** **/
/***************************************************************************/
/***************************************************************************/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "const.h"
#include "coder.h"
#include "stats.h"
/***************************************************************************/
/***************************************************************************/
/** **/
/** Constant Definitions **/
/** **/
/***************************************************************************/
/***************************************************************************/
#define NUM_HIST NUM_CONTEXT
#define NUM_BHIST NUM_BCONTEXT
/***************************************************************************/
/***************************************************************************/
/** **/
/** Variable Definitions **/
/** **/
/***************************************************************************/
/***************************************************************************/
extern int width;
extern int tolerance;
extern int mask;
int bits_shifted = 0;
/* variable that keeps count of number of times we encode in a histogram */
int tcount[] =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0
};
/* variable that keeps sum of symbols encoded in histogram */
double times_tail[] =
{
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0
};
/* number of bits currently being shifted out while coding in a histogram */
int aresolution[] =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0
};
int ecount = 0;
int pcount = 0;
context *histograms[NUM_HIST];
context *bhistograms[NUM_BHIST];
context *shistograms[6];
int hist_size[] =
{
18, 26, 34, 50, 66, 82, 114, 256,
18, 26, 34, 50, 66, 82, 114, 256,
50, 50
};
int extra_symbols = 0;
/***************************************************************************/
/***************************************************************************/
/** **/
/** Misc. operations **/
/** **/
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/** **/
/** Function : init_bcoder **/
/** **/
/***************************************************************************/
void init_bcoder ()
{
int i, n;
for (n = 0; n < NUM_BHIST; n++)
{
/* create context structure */
bhistograms[n] = create_context (3, STATIC);
for (i = 0; i < 3; i++) /* initialise all symbols as having a frequency of 1 */
install_symbol (bhistograms[n], i);
}
}
/***************************************************************************/
/** **/
/** Function : init_scoder **/
/** **/
/***************************************************************************/
/* this is new for encoding shifted bits */
/* u need to call this from main, both in encode and decode */
void init_scoder ()
{
int i, n, size;
for (size = 2, n = 0; n < 6; n++, size *=2)
{
/* create context structure */
shistograms[n] = create_context (size, STATIC);
for (i = 0; i < size; i++) /* initialise all symbols as having a frequency of 1 */
install_symbol (shistograms[n], i);
}
}
/***************************************************************************/
/** **/
/** Function : init_mycoder **/
/** **/
/***************************************************************************/
void init_mycoder ()
{
int i, n;
for (n = 0; n < NUM_HIST; n++)
{
int t=(hist_size[n]-2)/2;
t=(t+tolerance)/mask;
hist_size[n]=2*t+2;
/*
hist_size[n]=(hist_size[n]+tolerance)/mask;
if(hist_size[n]<2)
hist_size[n]=2;
*/
}
for (n = 0; n < NUM_HIST; n++)
{
/* create context structure */
histograms[n] = create_context (hist_size[n], STATIC);
for (i = 0; i < hist_size[n]; i++) /* initialise all symbols as having a frequency of 1 */
install_symbol (histograms[n], i);
}
start_encode ();
startoutputtingbits ();
}
/***************************************************************************/
/** **/
/** Function : stop_mycoder **/
/** **/
/***************************************************************************/
void stop_mycoder ()
{
finish_encode ();
doneoutputtingbits ();
}
/***************************************************************************/
/** **/
/** Function : encode_symbol **/
/** **/
/***************************************************************************/
void encode_symbol (int hnum, int symbol)
{
int cnt, shift, snum;
double avg;
/* check after every 256 symbols coded within histogram */
if (tcount[hnum] && ((tcount[hnum] % 256) == 0))
{
/* reset number if bits being shifted to 0 */
aresolution[hnum] = 0;
/* compute average symbol size in histogram */
avg = times_tail[hnum] / (tcount[hnum]);
cnt = 0;
/* set new number of bits to be shifted, max 6 */
while((cnt < 4) && (avg > (0.70*hist_size[hnum])))
{
cnt++;
avg /= 2.0;
aresolution[hnum]++;
}
/* rest count and sum */
times_tail[hnum] = 0;
tcount[hnum] = 0;
}
/* accumulate count and sum */
times_tail[hnum] += symbol;
tcount[hnum]++;
/* if currently bits being shifted in this histogram */
if (shift = aresolution[hnum])
{
bits_shifted += shift;
if (shift == 1)
{
snum = symbol & 0x00000001;
}
else if (shift == 2)
{
snum = symbol & 0x00000003;
}
else if (shift == 3)
{
snum = symbol & 0x00000007;
}
else if (shift == 4)
{
snum = symbol & 0x0000000F;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -