⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shiftb.cpp

📁 一种无损图象压缩算法,可用于珍贵图象的保存.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	}
	else if (shift == 5)
	{
	    snum = symbol & 0x0000001F;
	}
	else if (shift == 6)
	{
	    snum = symbol & 0x0000003F;
	}
	else
	{
	    fprintf (stderr, "Error, shift value > 6!!\n");
	    exit    (1);
	}

	/*
	 *  snum contains shifted bits, encode it using context
	 *  0,1,2,3,4, or 5 for shift bits 1,2,3,4,5 or 6
	 */

	if (encode (shistograms[shift-1], snum) == NOT_KNOWN)
	{
	    fprintf (stderr, "Problem, symbol not in histogram\n");
	    exit    (1);
	}

	symbol = symbol >> shift;
    }

    /* now back to old stuff, once bits are shifted */

    do
    {
        if (symbol >= hist_size[hnum] - 1)
	{
	    if (encode (histograms[hnum], hist_size[hnum]-1) == NOT_KNOWN)
	    {
	        fprintf (stderr, "Problem, symbol not in histogram\n");
		exit    (1);
	    }

	    symbol -= (hist_size[hnum]-1);

	    extra_symbols += 1;

	    if (hnum < NUM_HIST-1) hnum++;
	}
	else break;
    }
    while (1);

    if (encode (histograms[hnum], symbol) == NOT_KNOWN)
    {
        fprintf (stderr, "Problem, symbol not in histogram\n");
	exit    (1);
    }

    ecount++;
}


/***************************************************************************/
/**                                                                       **/
/**  Function : bencode_symbol                                            **/
/**                                                                       **/
/***************************************************************************/
 
void bencode_symbol (int hnum, int symbol)
{
    if (encode (bhistograms[hnum], symbol) == NOT_KNOWN)
    {
        fprintf (stderr, "Problem, symbol not in bhistogram\n");
	exit    (1);
    }

    ecount++;
}

/***************************************************************************/
/**                                                                       **/
/**  Function : init_mydecoder                                            **/
/**                                                                       **/
/***************************************************************************/
 
void init_mydecoder()
{
    
	for (int 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;
		*/
	}
	
	//int		n;

	for ( n = 0; n < NUM_HIST; n++)
    {
        /* create context structure */
        histograms[n] = create_context (hist_size[n], STATIC);
		for (int i = 0; i < hist_size[n]; i++) /* initialise all symbols as having a frequency of 1 */ 
			install_symbol (histograms[n], i);
    }

    start_decode       ();
    startinputtingbits ();
}


/***************************************************************************/
/**                                                                       **/
/**  Function : init_sdecoder                                             **/
/**                                                                       **/
/***************************************************************************/

void init_sdecoder ()
{
    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_bdecoder                                             **/
/**                                                                       **/
/***************************************************************************/

void init_bdecoder ()
{

  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 : stop_mydecoder                                            **/
/**                                                                       **/
/***************************************************************************/
 
void stop_mydecoder()
{
    finish_decode     ();
    doneinputtingbits ();
}


/***************************************************************************/
/**                                                                       **/
/**  Function : decode_symbol                                             **/
/**                                                                       **/
/***************************************************************************/

int decode_symbol (int hnum)
{
    int value, symbol = 0;
    int  cnt, shift, snum, orig_hnum;
    double avg;
 
    orig_hnum = hnum;

    if (tcount[hnum] && ((tcount[hnum] % 256) == 0))
    {
        aresolution[hnum] = 0;

	avg = times_tail[hnum] / (tcount[hnum]);
	cnt = 0;

	while ((cnt < 4) && (avg >  (0.70*hist_size[hnum])))
	{
	    cnt++;
	    avg /= 2.0;

	    aresolution[hnum]++;
	}

	times_tail[hnum] = 0;
	tcount[hnum]     = 0;
    }
 
    if (shift = aresolution[hnum])
    {
        bits_shifted += shift;

	if ((snum = decode (shistograms[shift-1])) == NOT_KNOWN)
	{
	    fprintf (stderr, "Problem, symbol not in histogram\n");
	    exit    (1);
        }
    }

    do
    {
        if ((value = decode (histograms[hnum])) == NOT_KNOWN)
	{
	    fprintf (stderr, "Problem, symbol not in histogram\n");
	    exit    (1);
	}

	symbol += value;

	if (value == hist_size[hnum] - 1)
        {
	    extra_symbols += 1;

	    if (hnum < NUM_HIST - 1) hnum++;
	}
	else break;
    }
    while(1);

    if (shift) symbol = (symbol << shift) | snum;

    pcount++;

    times_tail[orig_hnum] += symbol;
    tcount[orig_hnum]++;

    return (symbol);
}


/***************************************************************************/
/**                                                                       **/
/**  Function : bdecode_symbol                                            **/
/**                                                                       **/
/***************************************************************************/
 
int bdecode_symbol (int hnum)
{
    int symbol;

    if ((symbol = decode (bhistograms[hnum])) == NOT_KNOWN)
    {
        fprintf (stderr, "Problem, symbol not in histogram\n");
	exit    (1);
    }

    pcount++;

    return (symbol);
}

/***************************************************************************/
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -