📄 faxcom.txt
字号:
If you wish to use this, or parts of this, program in a commercial product then the authors permission is required.
**********************************************************************/
/* This file contains variable and macro definitions common to the
faxcoder and faxdecoder */
#define NUM_LINES 2376 /* Number of lines in the fax */
#define BYTES_PER_LINE 216 /* Bytes per line in fax */
#define BITS_PER_LINE (8 * BYTES_PER_LINE)
/* Bits on a fax line */
#define FAX_SIZE (NUM_LINES * BYTES_PER_LINE)
/* Size (in bytes) of a fax file */
#define MODEL_ORDER 16 /* Number of bits of context */
#define NUM_CONTEXTS (1 << MODEL_ORDER)
#define HALVE_LIMIT 16383 /* Halve counts when sum reaches this */
#define CONTEXT_MASK 0x3ff /* This masks out unwanted portions of the
context from the bit immediately above */
#define CONTEXT_SIDE_BITS 3 /* Number bits to side of fax page to allow
for context spilling over sides */
#define CONTEXT_WINDOW_BPL (BITS_PER_LINE + 2 * CONTEXT_SIDE_BITS)
/* Bits per line for context bits array */
int cmpbytes = 0, /* Number of compressed bytes outputted */
line, /* Line currently on in fax file */
saved[CONTEXT_WINDOW_BPL];
/* Array that holds saved pieces of context */
struct context_type /* This structure holds the counts for zeros and the */
{ /* sum of zeros and ones for a context */
int zero_count, sum;
} contexts[NUM_CONTEXTS]; /* The array that holds context information */
/* Get a bit from the input file */
#define getabit() \
if ((mask>>=1) ==0) \
{ \
byte=getchar(); \
mask=0x80; \
}
/* Write a zero bit out to the output file */
#define write_decoded0 \
{ \
byte <<= 1 ; \
bitsleft-- ; \
if (bitsleft==0) \
{ \
putc(byte, stdout) ; \
bitsleft = 8 ; \
} \
}
/* Write a one bit out to the output file */
#define write_decoded1 \
{ \
byte = (byte << 1) | 0x1; \
bitsleft-- ; \
if (bitsleft==0) \
{ \
putc(byte, stdout) ; \
bitsleft = 8 ; \
} \
}
#ifdef UNIX
float start_time,total_time;
/*
* get_time()
*
* return amount of CPU time in seconds used since starting
*/
float get_time()
{
struct rusage rusage;
getrusage(RUSAGE_SELF,&rusage);
return(rusage.ru_utime.tv_sec +
rusage.ru_utime.tv_usec/1000000.0 +
rusage.ru_stime.tv_sec +
rusage.ru_stime.tv_usec/1000000.0) ;
}
#endif
*****************************************************
**** ****
**** Arithdec.i ****
**** ****
*****************************************************
/*************************************************************************
This program is shareware. If you use it and find it useful please
send ($25 is suggested) or whatever you think it is worth to :
Raymond P. Wilson
38 Paremata St.
Atawhai
Nelson
New Zealand
If you wish to use this program or parts of this program in a
commercial the authors permission is required.
Copyright Raymond P. Wilson 1989
**************************************************************************/
/* arithmetic coding routines */
/* Most of the functions here have been converted to macro definitions
to speed them up and have been customised for a binary alphabet */
#define codevaluebits 16
typedef long codevalue ;
#define topvalue (((long)1<<codevaluebits)-1)
#define firstqtr (topvalue/4+1)
#define half (2*firstqtr)
#define thirdqtr (3*firstqtr)
codevalue S_value=0 ;
int S_buffer=0;
/* Macro definition for call to get number in range when decoding files */
#define arithmetic_decode_target(totl) \
( (((long)(S_value-S_low)+1)*(totl)-1)/ ((long)(S_high -S_low)+1) )
/*==================================*/
#define addnextinputbit(v) \
{ S_bitstogo--; \
if (S_bitstogo<0) \
{ S_buffer = getc(stdin) ;\
S_bitstogo = 7 ; \
} \
v += (S_buffer & 1) ; \
S_buffer >>= 1 ; \
}
/*==================================*/
#define arithmetic_decode_zero(hbnd, totl) \
{ \
S_high = S_low + (((long)(S_high-S_low)+1) * hbnd)/totl - 1 ; \
\
for (;;) \
{ if (S_high<H) \
{ /* nothing */ \
} \
else \
if (S_low>=H) \
{ S_value -= H ; \
S_low -= H ; \
S_high -= H ; \
} \
else \
if (S_low>=firstqtr && S_high<thirdqtr) \
{ S_value -= firstqtr ; \
S_low -= firstqtr ; \
S_high -= firstqtr ; \
} \
else break ; \
S_low <<= 1; \
S_high = (S_high << 1) | 1; \
S_value <<= 1 ; \
addnextinputbit(S_value) ; \
} \
}
/*======================================================================*/
#define arithmetic_decode_one(lbnd, totl) \
{ \
S_low += (((long)(S_high-S_low)+1)*lbnd)/totl ; \
for (;;) \
{ if (S_high<H) \
{ /* nothing */ \
} \
else \
if (S_low>=H) \
{ S_value -= H ; \
S_low -= H ; \
S_high -= H ; \
} \
else \
if (S_low>=firstqtr && S_high<thirdqtr) \
{ S_value -= firstqtr ; \
S_low -= firstqtr ; \
S_high -= firstqtr ; \
} \
else break ; \
S_low <<= 1; \
S_high = (S_high << 1) | 1; \
S_value <<= 1; \
addnextinputbit(S_value) ; \
} \
}
/*==================================*/
#define startdecoding() \
{ register int i ; \
S_value = 0 ; \
for (i=1; i<=codevaluebits; i++) \
{ S_value += S_value ; \
addnextinputbit(S_value) ; \
} \
S_low = 0 ; \
S_high = topvalue ; \
} \
#define startinputingbits() \
S_bitstogo = 0 ;
*****************************************************
**** ****
**** ArithCod.i ****
**** ****
*****************************************************
/*************************************************************************
This program is shareware. If you use it and find it useful please
send ($25 is suggested) or whatever you think it is worth to :
Raymond P. Wilson
38 Paremata St.
Atawhai
Nelson
New Zealand
If you wish to use this program or parts of this program in a
commercial the authors permission is required.
Copyright Raymond P. Wilson 1989
**************************************************************************/
/* arithmetic coding routines */
/* Most of the functions here have been converted to macro definitions
to speed them up and customised bor a binary alphabet */
#define codevaluebits 16
typedef long codevalue ;
#define topvalue (((long)1<<codevaluebits)-1)
#define firstqtr (topvalue/4+1)
#define half (2*firstqtr)
#define thirdqtr (3*firstqtr)
long S_bitstofollow=0 ;
int S_buffer=0;
/*==================================*/
#define bitplusfollow0 \
{ outputbit0 ; \
while (S_bitstofollow>0) \
{ outputbit1 ; \
S_bitstofollow--; \
} \
}
#define bitplusfollow1 \
{ outputbit1 ; \
while (S_bitstofollow>0) \
{ outputbit0 ; \
S_bitstofollow--; \
} \
}
#define outputbit0 \
{ S_buffer >>= 1; \
S_bitstogo--; \
if (S_bitstogo==0) \
{ putc(S_buffer, stdout) ;\
S_bitstogo = 8 ; \
cmpbytes++ ; \
} \
}
#define outputbit1 \
{ S_buffer = (S_buffer >> 1) | 0x80; \
S_bitstogo--; \
if (S_bitstogo==0) \
{ putc(S_buffer, stdout) ;\
S_bitstogo = 8 ; \
cmpbytes++ ; \
} \
}
/*==================================*/
#define arithmetic_encode_zero(hbnd, totl) \
{ \
S_high = S_low + (((long)(S_high-S_low)+1) * hbnd)/totl - 1 ; \
\
for (;;) \
{ if (S_high<H) \
{ bitplusfollow0 ; \
} \
else \
if (S_low>=H) \
{ bitplusfollow1 ; \
S_low -= H ; \
S_high -= H ; \
} \
else \
if (S_low>=firstqtr && S_high<thirdqtr) \
{ S_bitstofollow++ ; \
S_low -= firstqtr ; \
S_high -= firstqtr ; \
} \
else break ; \
S_low <<= 1; \
S_high = (S_high << 1) | 1; \
} \
}
/*=====================================================================*/
#define arithmetic_encode_one(lbnd, totl) \
{ \
S_low += ((((long)(S_high-S_low)+1) * lbnd)/totl) ; \
\
for (;;) \
{ if (S_high<H) \
{ bitplusfollow0 ; \
} \
else \
if (S_low>=H) \
{ bitplusfollow1 ; \
S_low -= H ; \
S_high -= H ; \
} \
else \
if (S_low>=firstqtr && S_high<thirdqtr) \
{ S_bitstofollow++ ; \
S_low -= firstqtr ; \
S_high -= firstqtr ; \
} \
else break ; \
S_low <<= 1; ; \
S_high = (S_high << 1) | 1; \
} \
}
/*==================================*/
#define startencoding() \
S_high = topvalue ;
#define doneencoding() \
{ \
S_bitstofollow++; \
if (S_low<firstqtr) \
{ bitplusfollow0;} \
else \
{ bitplusfollow1; } \
}
#define startoutputingbits() \
S_bitstogo = 8 ;
#define doneoutputingbits() \
{ putc(S_buffer>>S_bitstogo, stdout) ;\
cmpbytes++; \
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -